-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxiaoyao.go
132 lines (109 loc) · 3.38 KB
/
xiaoyao.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"github.com/AlexanderChiuluvB/xiaoyaoFS/master"
fuse2 "github.com/AlexanderChiuluvB/xiaoyaoFS/mount"
"github.com/AlexanderChiuluvB/xiaoyaoFS/storage"
b "github.com/AlexanderChiuluvB/xiaoyaoFS/utils/benchmark"
"github.com/AlexanderChiuluvB/xiaoyaoFS/utils/config"
"github.com/AlexanderChiuluvB/xiaoyaoFS/utils/parser"
"github.com/seaweedfs/fuse"
"github.com/seaweedfs/fuse/fs"
"gopkg.in/alecthomas/kingpin.v2"
"os"
"os/signal"
"syscall"
)
var (
app = kingpin.New("whalefs", "A simple filesystem for small file.")
configFile = app.Flag("config", "config file(toml)").Required().String()
masterServer = app.Command("master", "master server")
storageServer = app.Command("storage", "storage server")
mount = app.Command("mount", "mount the xiaoyaoFs to a directory")
benchmark = app.Command("benchmark", "benchmark")
bmMasterHost = benchmark.Flag("masterHost", "host of master server").Default("localhost").String()
bmMasterPort = benchmark.Flag("masterPort", "post of master server").Default("8888").Int()
bmConcurrent = benchmark.Flag("concurrent", "concurrent").Default("16").Int()
bmNum = benchmark.Flag("num", "number of file write/read").Default("1").Int()
bmSize = parser.Size(benchmark.Flag("size", "size of file write/read").Default("1024B"))
)
func main() {
command := kingpin.MustParse(app.Parse(os.Args[1:]))
switch command {
case masterServer.FullCommand():
startMaster(*configFile)
case storageServer.FullCommand():
startStorageServer(*configFile)
case mount.FullCommand():
startMount(*configFile)
case benchmark.FullCommand():
b.Benchmark(*bmMasterHost, *bmMasterPort, *bmConcurrent, *bmNum, int(*bmSize))
}
}
func startMount(configFile string) {
c, err := config.NewConfig(configFile)
if err != nil {
panic(fmt.Errorf("NewConfig(\"%s\") error(%v)", configFile, err))
}
options := []fuse.MountOption {
fuse.VolumeName("xiaoyaoFS"),
fuse.LocalVolume(),
}
conn, err := fuse.Mount(c.MountDir, options...)
if err != nil {
panic(fmt.Errorf("mount %s error %v", c.MountDir, err))
}
defer fuse.Unmount(c.MountDir)
xiaoyaoFileSystem := fuse2.NewXiaoyaoFs(c)
err = fs.Serve(conn, xiaoyaoFileSystem)
<-conn.Ready
if err := conn.MountError; err != nil {
panic(fmt.Errorf("mount process: %v", err))
}
}
func startStorageServer(configFile string) {
c, err := config.NewConfig(configFile)
if err != nil {
panic(fmt.Errorf("NewConfig(\"%s\") error(%v)", configFile, err))
}
ss, err := storage.NewStore(c)
if err != nil {
panic(fmt.Errorf("NewStore(\"%s\") error(%v)", configFile, err))
}
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signals
ss.Close()
switch sig {
case syscall.SIGINT:
os.Exit(int(syscall.SIGINT))
case syscall.SIGTERM:
os.Exit(int(syscall.SIGTERM))
}
}()
ss.Start()
}
func startMaster(configFile string) {
c, err := config.NewConfig(configFile)
if err != nil {
panic(fmt.Errorf("NewConfig(\"%s\") error(%v)", configFile, err))
}
m, err := master.NewMaster(c)
if err != nil {
panic(fmt.Errorf("NewMaster(\"%s\") error(%v)", configFile, err))
}
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signals
m.Close()
switch sig {
case syscall.SIGINT:
os.Exit(int(syscall.SIGINT))
case syscall.SIGTERM:
os.Exit(int(syscall.SIGTERM))
}
}()
m.Start()
}