-
Notifications
You must be signed in to change notification settings - Fork 248
/
registerdiscovery.go
167 lines (146 loc) · 4.15 KB
/
registerdiscovery.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
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.,
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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.
*/
package discovery
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"sync"
"time"
"github.com/Tencent/bk-bcs/bcs-common/common/RegisterDiscover"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/common/types"
)
// Client client for service discovery
type Client struct {
zkAddrs string
zkSubPath string
svcInfo *types.ServerInfo
rd *RegisterDiscover.RegDiscover
rootCxt context.Context
cancel context.CancelFunc
services []*types.ServerInfo
isMaster bool
lock sync.Mutex
}
// New create client for service discovery
func New(zkAddrs, zkSubPath string, svcInfo *types.ServerInfo) *Client {
return &Client{
zkAddrs: zkAddrs,
zkSubPath: zkSubPath,
svcInfo: svcInfo,
rd: RegisterDiscover.NewRegDiscover(zkAddrs),
}
}
// Run run service discovery
func (c *Client) Run() error {
c.rootCxt, c.cancel = context.WithCancel(context.Background())
if err := c.rd.Start(); err != nil {
blog.Errorf("fail to connect registry, err %s", err.Error())
return err
}
if err := c.registerService(); err != nil {
blog.Errorf("fail to register service, err %s", err.Error())
return err
}
path := c.getServicePath()
discoveryEvent, err := c.rd.DiscoverService(path)
if err != nil {
blog.Errorf("failed to discover service by path %s, err %s", path, err.Error())
return err
}
go c.checkMasterStatus()
for {
select {
case e := <-discoveryEvent:
err := c.resolveServices(e.Server)
if err != nil {
blog.Warnf("resolve service %s failed, err %s", e.Server, err.Error())
}
case <-c.rootCxt.Done():
blog.Warnf("service discovery done")
return nil
}
}
}
// GetServers get server info
func (c *Client) GetServers() []*types.ServerInfo {
c.lock.Lock()
defer c.lock.Unlock()
return c.services
}
// IsMaster return if it is master
func (c *Client) IsMaster() bool {
c.lock.Lock()
defer c.lock.Unlock()
return c.isMaster
}
// Stop stop discovery
func (c *Client) Stop() error {
c.cancel()
if err := c.rd.Stop(); err != nil {
return fmt.Errorf("stop discovery failed err %s", err.Error())
}
return nil
}
// resolveServices xxx
// resolve services
func (c *Client) resolveServices(svcStrs []string) error {
blog.V(3).Infof("discovery bkcmdb-synchronizer [%+v]", svcStrs)
svcs := []*types.ServerInfo{}
for _, serverStr := range svcStrs {
newSvc := new(types.ServerInfo)
if err := json.Unmarshal([]byte(serverStr), newSvc); err != nil {
blog.Warnf("failed to unmarshal %s, err %s", serverStr, err.Error())
continue
}
svcs = append(svcs, newSvc)
}
c.lock.Lock()
defer c.lock.Unlock()
c.services = svcs
return nil
}
// checkMasterStatus xxx
// check master status
func (c *Client) checkMasterStatus() {
for {
c.lock.Lock()
if len(c.services) > 0 {
if c.services[0].IP == c.svcInfo.IP {
if !c.isMaster {
blog.Infof("[Role changed] slave->master")
}
c.isMaster = true
} else {
c.isMaster = false
}
}
c.lock.Unlock()
time.Sleep(5 * time.Second)
}
}
// registerService xxx
func (c *Client) registerService() error {
data, err := json.Marshal(c.svcInfo)
if err != nil {
blog.Errorf("failed to marshal service info, err %s", err.Error())
return err
}
path := filepath.Join(c.getServicePath(), c.svcInfo.IP)
return c.rd.RegisterAndWatchService(path, data)
}
// getServicePath xxx
func (c *Client) getServicePath() string {
return filepath.Join(types.BCS_SERV_BASEPATH, types.BCS_MODULE_BKCMDB_SYNCHRONIZER, c.zkSubPath)
}