forked from talent-plan/tinykv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (90 loc) · 2.52 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
package main
import (
"flag"
"net"
_ "net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/pingcap-incubator/tinykv/kv/config"
"github.com/pingcap-incubator/tinykv/kv/server"
"github.com/pingcap-incubator/tinykv/kv/storage"
"github.com/pingcap-incubator/tinykv/kv/storage/raft_storage"
"github.com/pingcap-incubator/tinykv/kv/storage/standalone_storage"
"github.com/pingcap-incubator/tinykv/log"
"github.com/pingcap-incubator/tinykv/proto/pkg/tinykvpb"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
var (
schedulerAddr = flag.String("scheduler", "", "scheduler address")
storeAddr = flag.String("addr", "", "store address")
dbPath = flag.String("path", "", "directory path of db")
logLevel = flag.String("loglevel", "", "the level of log")
)
func main() {
flag.Parse()
conf := config.NewDefaultConfig()
if *schedulerAddr != "" {
conf.SchedulerAddr = *schedulerAddr
}
if *storeAddr != "" {
conf.StoreAddr = *storeAddr
}
if *dbPath != "" {
conf.DBPath = *dbPath
}
if *logLevel != "" {
conf.LogLevel = *logLevel
}
log.SetLevelByString(conf.LogLevel)
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
log.Infof("Server started with conf %+v", conf)
var storage storage.Storage
if conf.Raft {
storage = raft_storage.NewRaftStorage(conf)
} else {
storage = standalone_storage.NewStandAloneStorage(conf)
}
if err := storage.Start(); err != nil {
log.Fatal(err)
}
server := server.NewServer(storage)
var alivePolicy = keepalive.EnforcementPolicy{
MinTime: 2 * time.Second, // If a client pings more than once every 2 seconds, terminate the connection
PermitWithoutStream: true, // Allow pings even when there are no active streams
}
grpcServer := grpc.NewServer(
grpc.KeepaliveEnforcementPolicy(alivePolicy),
grpc.InitialWindowSize(1<<30),
grpc.InitialConnWindowSize(1<<30),
grpc.MaxRecvMsgSize(10*1024*1024),
)
tinykvpb.RegisterTinyKvServer(grpcServer, server)
listenAddr := conf.StoreAddr[strings.IndexByte(conf.StoreAddr, ':'):]
l, err := net.Listen("tcp", listenAddr)
if err != nil {
log.Fatal(err)
}
handleSignal(grpcServer)
err = grpcServer.Serve(l)
if err != nil {
log.Fatal(err)
}
log.Info("Server stopped.")
}
func handleSignal(grpcServer *grpc.Server) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
sig := <-sigCh
log.Infof("Got signal [%s] to exit.", sig)
grpcServer.Stop()
}()
}