-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
init.go
101 lines (86 loc) · 2.7 KB
/
init.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
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Adam Gee (adamgee@gmail.com)
package server
import (
"errors"
"net"
"google.golang.org/grpc"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/netutil"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"golang.org/x/net/context"
)
// initListener wraps a net.Listener and turns its Close() method into
// a no-op. This is used so that the initServer's grpc.Server can be
// stopped without closing the listener (which it shares with the main
// grpc.Server).
type initListener struct {
net.Listener
}
func (initListener) Close() error {
return nil
}
// initServer manages the temporary init server used during
// bootstrapping.
type initServer struct {
server *Server
grpc *grpc.Server
bootstrapped chan struct{}
mu struct {
syncutil.Mutex
awaitDone bool
}
}
func newInitServer(s *Server) *initServer {
return &initServer{server: s, bootstrapped: make(chan struct{})}
}
func (s *initServer) serve(ctx context.Context, ln net.Listener) {
s.grpc = rpc.NewServer(s.server.rpcContext)
serverpb.RegisterInitServer(s.grpc, s)
s.server.stopper.RunWorker(ctx, func(context.Context) {
netutil.FatalIfUnexpected(s.grpc.Serve(initListener{ln}))
})
}
func (s *initServer) awaitBootstrap() error {
select {
case <-s.server.node.storeCfg.Gossip.Connected:
case <-s.bootstrapped:
case <-s.server.stopper.ShouldStop():
return errors.New("Stop called while waiting to bootstrap")
}
s.mu.Lock()
s.mu.awaitDone = false
s.mu.Unlock()
s.grpc.GracefulStop()
return nil
}
func (s *initServer) Bootstrap(
ctx context.Context, request *serverpb.BootstrapRequest,
) (response *serverpb.BootstrapResponse, err error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.mu.awaitDone {
return nil, errors.New("Bootstrap called after initialization complete")
}
if err := s.server.node.bootstrap(ctx, s.server.engines); err != nil {
log.Error(ctx, "node bootstrap failed: ", err)
return nil, err
}
close(s.bootstrapped)
return &serverpb.BootstrapResponse{}, nil
}