forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
210 lines (184 loc) · 5.69 KB
/
run.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package zero
import (
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/dgraph-io/badger"
bopts "github.com/dgraph-io/badger/options"
"github.com/dgraph-io/dgraph/conn"
"github.com/dgraph-io/dgraph/protos/intern"
"github.com/dgraph-io/dgraph/raftwal"
"github.com/dgraph-io/dgraph/x"
"github.com/spf13/cobra"
)
type options struct {
bindall bool
myAddr string
portOffset int
nodeId uint64
numReplicas int
peer string
w string
}
var opts options
var Zero x.SubCommand
func init() {
Zero.Cmd = &cobra.Command{
Use: "zero",
Short: "Run Dgraph zero server",
Long: `
A Dgraph zero instance manages the Dgraph cluster. Typically, a single Zero
instance is sufficient for the cluster; however, one can run multiple Zero
instances to achieve high-availability.
`,
Run: func(cmd *cobra.Command, args []string) {
defer x.StartProfile(Zero.Conf).Stop()
run()
},
}
Zero.EnvPrefix = "DGRAPH_ZERO"
flag := Zero.Cmd.Flags()
flag.Bool("bindall", true,
"Use 0.0.0.0 instead of localhost to bind to all addresses on local machine.")
flag.String("my", "",
"addr:port of this server, so other Dgraph servers can talk to this.")
flag.IntP("port_offset", "o", 0,
"Value added to all listening port numbers. [Grpc=5080, HTTP=6080]")
flag.Uint64("idx", 1, "Unique node index for this server.")
flag.Int("replicas", 1, "How many replicas to run per data shard."+
" The count includes the original shard.")
flag.String("peer", "", "Address of another dgraphzero server.")
flag.StringP("wal", "w", "zw", "Directory storing WAL.")
}
func setupListener(addr string, port int, kind string) (listener net.Listener, err error) {
laddr := fmt.Sprintf("%s:%d", addr, port)
fmt.Printf("Setting up %s listener at: %v\n", kind, laddr)
return net.Listen("tcp", laddr)
}
type state struct {
node *node
rs *conn.RaftServer
zero *Server
}
func (st *state) serveGRPC(l net.Listener, wg *sync.WaitGroup) {
s := grpc.NewServer(
grpc.MaxRecvMsgSize(x.GrpcMaxSize),
grpc.MaxSendMsgSize(x.GrpcMaxSize),
grpc.MaxConcurrentStreams(1000))
rc := intern.RaftContext{Id: opts.nodeId, Addr: opts.myAddr, Group: 0}
m := conn.NewNode(&rc)
st.rs = &conn.RaftServer{Node: m}
st.node = &node{Node: m, ctx: context.Background(), stop: make(chan struct{})}
st.zero = &Server{NumReplicas: opts.numReplicas, Node: st.node}
st.zero.Init()
st.node.server = st.zero
intern.RegisterZeroServer(s, st.zero)
intern.RegisterRaftServer(s, st.rs)
go func() {
defer wg.Done()
err := s.Serve(l)
log.Printf("gRpc server stopped : %s", err.Error())
st.node.stop <- struct{}{}
// Attempt graceful stop (waits for pending RPCs), but force a stop if
// it doesn't happen in a reasonable amount of time.
done := make(chan struct{})
const timeout = 5 * time.Second
go func() {
s.GracefulStop()
close(done)
}()
select {
case <-done:
case <-time.After(timeout):
log.Printf("Stopping grpc gracefully is taking longer than %v."+
" Force stopping now. Pending RPCs will be abandoned.", timeout)
s.Stop()
}
}()
}
func run() {
opts = options{
bindall: Zero.Conf.GetBool("bindall"),
myAddr: Zero.Conf.GetString("my"),
portOffset: Zero.Conf.GetInt("port_offset"),
nodeId: uint64(Zero.Conf.GetInt("idx")),
numReplicas: Zero.Conf.GetInt("replicas"),
peer: Zero.Conf.GetString("peer"),
w: Zero.Conf.GetString("wal"),
}
grpc.EnableTracing = false
addr := "localhost"
if opts.bindall {
addr = "0.0.0.0"
}
if len(opts.myAddr) == 0 {
opts.myAddr = fmt.Sprintf("localhost:%d", x.PortZeroGrpc+opts.portOffset)
}
grpcListener, err := setupListener(addr, x.PortZeroGrpc+opts.portOffset, "grpc")
if err != nil {
log.Fatal(err)
}
httpListener, err := setupListener(addr, x.PortZeroHTTP+opts.portOffset, "http")
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(3)
// Initialize the servers.
var st state
st.serveGRPC(grpcListener, &wg)
st.serveHTTP(httpListener, &wg)
http.HandleFunc("/state", st.getState)
http.HandleFunc("/removeNode", st.removeNode)
http.HandleFunc("/moveTablet", st.moveTablet)
// Open raft write-ahead log and initialize raft node.
x.Checkf(os.MkdirAll(opts.w, 0700), "Error while creating WAL dir.")
kvOpt := badger.DefaultOptions
kvOpt.SyncWrites = true
kvOpt.Dir = opts.w
kvOpt.ValueDir = opts.w
kvOpt.TableLoadingMode = bopts.MemoryMap
kv, err := badger.OpenManaged(kvOpt)
x.Checkf(err, "Error while opening WAL store")
defer kv.Close()
wal := raftwal.Init(kv, opts.nodeId)
x.Check(st.node.initAndStartNode(wal))
sdCh := make(chan os.Signal, 1)
signal.Notify(sdCh, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
defer wg.Done()
<-sdCh
fmt.Println("Shutting down...")
// Close doesn't close already opened connections.
httpListener.Close()
grpcListener.Close()
close(st.zero.shutDownCh)
}()
fmt.Println("Running Dgraph zero...")
wg.Wait()
fmt.Println("All done.")
}