-
Notifications
You must be signed in to change notification settings - Fork 73
/
server_embed_etcd.go
251 lines (210 loc) · 5.98 KB
/
server_embed_etcd.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2016 DeepFabric, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package pdserver
import (
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/embed"
"github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/pkg/types"
"github.com/fagongzi/log"
"github.com/deepfabric/elasticell/pkg/pdapi"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// unixToHTTP replace unix scheme with http.
var unixToHTTP = strings.NewReplacer("unix://", "http://", "unixs://", "http://")
var (
maxCheckEtcdRunningCount = 60 * 10
checkEtcdRunningDelay = 1 * time.Second
)
func (s *Server) startEmbedEtcd() {
log.Info("bootstrap: start embed etcd server")
cfg, err := s.cfg.getEmbedEtcdConfig()
if err != nil {
log.Fatalf("bootstrap: start embed etcd server failure, errors:\n %+v",
err)
return
}
cfg.UserHandlers = map[string]http.Handler{
fmt.Sprintf("%s/", pdapi.APIPrefix): pdapi.NewAPIHandler(s),
}
s.etcd, err = embed.StartEtcd(cfg)
if err != nil {
log.Fatalf("bootstrap: start embed etcd server failure, errors:\n %+v",
err)
return
}
select {
case <-s.etcd.Server.ReadyNotify():
log.Info("bootstrap: embed etcd server is ready")
s.doAfterEmbedEtcdServerReady(cfg)
case <-time.After(time.Minute):
s.doStop()
}
}
func (s *Server) doAfterEmbedEtcdServerReady(cfg *embed.Config) {
s.checkEtcdCluster()
s.id = uint64(s.etcd.Server.ID())
log.Infof("bootstrap: embed server ids, id=<%d>, leader=<%d>",
s.id,
s.etcd.Server.Leader())
s.initStore(cfg)
s.updateAdvertisePeerUrls()
if err := s.waitEtcdStart(cfg); err != nil {
// See https://github.com/coreos/etcd/issues/6067
// Here may return "not capable" error because we don't start
// all etcds in initial_cluster at same time, so here just log
// an error.
// Note that pd can not work correctly if we don't start all etcds.
log.Errorf("bootstrap: etcd start failed, err %v", err)
}
}
func (s *Server) waitEtcdStart(cfg *embed.Config) error {
var err error
for i := 0; i < maxCheckEtcdRunningCount; i++ {
// etcd may not start ok, we should wait and check again
_, err = s.endpointStatus(cfg)
if err == nil {
return nil
}
time.Sleep(checkEtcdRunningDelay)
continue
}
return err
}
// endpointStatus checks whether current etcd is running.
func (s *Server) endpointStatus(cfg *embed.Config) (*clientv3.StatusResponse, error) {
c := s.store.RawClient()
endpoint := []string{cfg.LCUrls[0].String()}[0]
m := clientv3.NewMaintenance(c)
start := time.Now()
ctx, cancel := context.WithTimeout(c.Ctx(), DefaultRequestTimeout)
resp, err := m.Status(ctx, endpoint)
cancel()
if cost := time.Since(start); cost > DefaultSlowRequestTime {
log.Warnf("bootstrap: check etcd status failed, endpoint=<%s> resp=<%+v> cost<%s> errors:\n %+v",
endpoint,
resp,
cost,
err)
}
return resp, errors.Wrapf(err, "")
}
func (s *Server) initStore(cfg *embed.Config) {
store, err := NewStore(cfg)
if err != nil {
log.Fatalf("bootstrap: init store failure, cfg=<%v>, errors:\n %+v",
cfg,
err)
return
}
s.store = store
}
func (s *Server) updateAdvertisePeerUrls() {
members, err := s.store.GetCurrentClusterMembers()
if err != nil {
log.Fatalf("bootstrap: update current members of etcd cluster")
return
}
for _, m := range members.Members {
if s.id == m.ID {
etcdPeerURLs := strings.Join(m.PeerURLs, ",")
if s.cfg.URLsAdvertisePeer != etcdPeerURLs {
log.Infof("bootstrap: update advertise peer urls succ, old=<%s>, new=<%s>",
s.cfg.URLsAdvertisePeer,
etcdPeerURLs)
s.cfg.URLsAdvertisePeer = etcdPeerURLs
}
}
}
}
func (s *Server) checkEtcdCluster() {
um, err := types.NewURLsMap(s.cfg.InitialCluster)
if err != nil {
s.closeEmbedEtcd()
log.Fatalf("bootstrap: check embed etcd server failure, errors:\n %+v",
err)
return
}
err = checkClusterID(s.etcd.Server.Cluster().ID(), um)
if err != nil {
log.Fatalf("bootstrap: check embed etcd server failure, errors:\n %+v",
err)
s.closeEmbedEtcd()
return
}
}
func (s *Server) closeEmbedEtcd() {
if s.etcd == nil {
return
}
if s.store != nil {
s.store.Close()
log.Info("stop: etcd v3 client is closed")
}
s.etcd.Close()
log.Info("stop: embed etcd server is stopped")
}
func checkClusterID(localClusterID types.ID, um types.URLsMap) error {
if len(um) == 0 {
return nil
}
var peerURLs []string
for _, urls := range um {
peerURLs = append(peerURLs, urls.StringSlice()...)
}
for i, u := range peerURLs {
u, gerr := url.Parse(u)
if gerr != nil {
return errors.Wrap(gerr, "check embed etcd")
}
trp := newHTTPTransport(u.Scheme)
// For tests, change scheme to http.
// etcdserver/api/v3rpc does not recognize unix protocol.
if u.Scheme == "unix" || u.Scheme == "unixs" {
peerURLs[i] = unixToHTTP.Replace(peerURLs[i])
}
remoteCluster, gerr := etcdserver.GetClusterFromRemotePeers([]string{peerURLs[i]}, trp)
trp.CloseIdleConnections()
if gerr != nil {
// Do not return error, because other members may be not ready.
log.Warnf("bootstrap: check etcd embed, may be member is not ready, member=<%s>",
u)
continue
}
remoteClusterID := remoteCluster.ID()
if remoteClusterID != localClusterID {
return errors.Wrapf(errEmbedEtcdClusterIDNotMatch,
"expect=<%d>, got=<%d>",
localClusterID,
remoteClusterID)
}
}
return nil
}
func newHTTPTransport(scheme string) *http.Transport {
tr := &http.Transport{}
if scheme == "unix" || scheme == "unixs" {
tr.Dial = unixDial
}
return tr
}
func unixDial(_, addr string) (net.Conn, error) {
return net.Dial("unix", addr)
}