forked from absolute8511/ZanRedisDB
-
Notifications
You must be signed in to change notification settings - Fork 60
/
main.go
129 lines (117 loc) · 2.92 KB
/
main.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
128
129
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"syscall"
"time"
"github.com/absolute8511/glog"
"github.com/judwhite/go-svc/svc"
"github.com/youzan/ZanRedisDB/common"
"github.com/youzan/ZanRedisDB/node"
"github.com/youzan/ZanRedisDB/server"
)
var (
flagSet = flag.NewFlagSet("zanredisdb", flag.ExitOnError)
configFilePath = flagSet.String("config", "", "the config file path to read")
showVersion = flagSet.Bool("version", false, "print version string and exit")
)
type program struct {
server *server.Server
}
func main() {
defer log.Printf("main exit")
defer glog.Flush()
prg := &program{}
if err := svc.Run(prg, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGINT); err != nil {
log.Panic(err)
}
}
func (p *program) Init(env svc.Environment) error {
if env.IsWindowsService() {
dir := filepath.Dir(os.Args[0])
return os.Chdir(dir)
}
return nil
}
func (p *program) Start() error {
glog.InitWithFlag(flagSet)
flagSet.Parse(os.Args[1:])
fmt.Println(common.VerString("ZanRedisDB"))
if *showVersion {
os.Exit(0)
}
var configFile server.ConfigFile
configDir := filepath.Dir(*configFilePath)
if *configFilePath != "" {
d, err := ioutil.ReadFile(*configFilePath)
if err != nil {
return err
}
err = json.Unmarshal(d, &configFile)
if err != nil {
return err
}
}
if configFile.ServerConf.DataDir == "" {
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("rocksdb-test-%d", time.Now().UnixNano()))
if err != nil {
return err
}
configFile.ServerConf.DataDir = tmpDir
}
serverConf := configFile.ServerConf
common.InitDefaultForGLogger(serverConf.LogDir)
loadConf, _ := json.MarshalIndent(configFile, "", " ")
fmt.Printf("loading with conf:%v\n", string(loadConf))
bip := server.GetIPv4ForInterfaceName(serverConf.BroadcastInterface)
if bip == "" || bip == "0.0.0.0" {
return errors.New("broadcast ip can not be found")
} else {
serverConf.BroadcastAddr = bip
}
fmt.Printf("broadcast ip is :%v\n", bip)
app, err := server.NewServer(serverConf)
if err != nil {
return err
}
for _, nsNodeConf := range serverConf.Namespaces {
nsFile := path.Join(configDir, nsNodeConf.Name)
d, err := ioutil.ReadFile(nsFile)
if err != nil {
return err
}
var nsConf node.NamespaceConfig
err = json.Unmarshal(d, &nsConf)
if err != nil {
return err
}
if nsConf.Name != nsNodeConf.Name {
return errors.New("namespace name not match the config file")
}
if nsConf.Replicator <= 0 {
return errors.New("namespace replicator should be set")
}
id := nsNodeConf.LocalReplicaID
clusterNodes := make(map[uint64]node.ReplicaInfo)
for _, v := range nsConf.RaftGroupConf.SeedNodes {
clusterNodes[v.ReplicaID] = v
}
app.InitKVNamespace(id, &nsConf, false)
}
app.Start()
p.server = app
return nil
}
func (p *program) Stop() error {
if p.server != nil {
p.server.Stop()
}
return nil
}