forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
endpointmanager.go
235 lines (220 loc) · 5.54 KB
/
endpointmanager.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
package tars
import (
"strings"
"sync"
"time"
"tars/protocol/res/endpointf"
"tars/protocol/res/queryf"
"tars/util/endpoint"
"tars/util/set"
)
// EndpointManager is a struct which contains endpoint information.
type EndpointManager struct {
objName string
directproxy bool
adapters map[endpoint.Endpoint]*AdapterProxy
index []interface{} //cache the set
pointsSet *set.Set
comm *Communicator
mlock *sync.Mutex
refreshInterval int
pos int32
depth int32
}
func (e *EndpointManager) setObjName(objName string) {
if objName == "" {
return
}
pos := strings.Index(objName, "@")
if pos > 0 {
//[direct]
e.objName = objName[0:pos]
endpoints := objName[pos+1:]
e.directproxy = true
for _, end := range strings.Split(endpoints, ":") {
e.pointsSet.Add(endpoint.Parse(end))
}
e.index = e.pointsSet.Slice()
} else {
//[proxy] TODO singleton
TLOG.Debug("proxy mode:", objName)
e.objName = objName
//comm := NewCommunicator()
//comm.SetProperty("netthread", 1)
obj, _ := e.comm.GetProperty("locator")
q := new(queryf.QueryF)
e.comm.StringToProxy(obj, q)
e.findAndSetObj(q)
go func() {
loop := time.NewTicker(time.Duration(e.refreshInterval) * time.Millisecond)
for range loop.C {
//TODO exit
e.findAndSetObj(q)
}
}()
}
}
// Init endpoint struct.
func (e *EndpointManager) Init(objName string, comm *Communicator) error {
e.comm = comm
e.mlock = new(sync.Mutex)
e.adapters = make(map[endpoint.Endpoint]*AdapterProxy)
e.pointsSet = set.NewSet()
e.directproxy = false
e.refreshInterval = comm.Client.refreshEndpointInterval
e.pos = 0
e.depth = 0
//ObjName要放到最后初始化
e.setObjName(objName)
return nil
}
// GetNextValidProxy returns polling adapter information.
func (e *EndpointManager) GetNextValidProxy() *AdapterProxy {
e.mlock.Lock()
ep := e.GetNextEndpoint()
if ep == nil {
e.mlock.Unlock()
return nil
}
if adp, ok := e.adapters[*ep]; ok {
// returns nil if recursively all nodes have not found an available node.
if adp.status {
e.mlock.Unlock()
return adp
} else if e.depth > e.pointsSet.Len() {
e.mlock.Unlock()
return nil
} else {
e.depth++
e.mlock.Unlock()
return e.GetNextValidProxy()
}
}
err := e.createProxy(*ep)
if err != nil {
TLOG.Error("create adapter fail:", *ep, err)
e.mlock.Unlock()
return nil
}
a := e.adapters[*ep]
e.mlock.Unlock()
return a
}
// GetNextEndpoint returns the endpoint basic information.
func (e *EndpointManager) GetNextEndpoint() *endpoint.Endpoint {
length := len(e.index)
if length <= 0 {
return nil
}
var ep endpoint.Endpoint
e.pos = (e.pos + 1) % int32(length)
ep = e.index[e.pos].(endpoint.Endpoint)
return &ep
}
// GetAllEndpoint returns all endpoint information as a array(support not tars service).
func (e *EndpointManager) GetAllEndpoint() []*endpoint.Endpoint {
es := make([]*endpoint.Endpoint, len(e.index))
e.mlock.Lock()
defer e.mlock.Unlock()
for i, v := range e.index {
e := v.(endpoint.Endpoint)
es[i] = &e
}
return es
}
func (e *EndpointManager) createProxy(ep endpoint.Endpoint) error {
TLOG.Debug("create adapter:", ep)
adp := new(AdapterProxy)
//TODO
end := endpoint.Endpoint2tars(ep)
err := adp.New(&end, e.comm)
if err != nil {
return err
}
e.adapters[ep] = adp
return nil
}
// GetHashProxy returns hash adapter information.
func (e *EndpointManager) GetHashProxy(hashcode int64) *AdapterProxy {
// very unsafe.
ep := e.GetHashEndpoint(hashcode)
if ep == nil {
return nil
}
if adp, ok := e.adapters[*ep]; ok {
return adp
}
err := e.createProxy(*ep)
if err != nil {
TLOG.Error("create adapter fail:", ep, err)
return nil
}
return e.adapters[*ep]
}
// GetHashEndpoint returns hash endpoint information.
func (e *EndpointManager) GetHashEndpoint(hashcode int64) *endpoint.Endpoint {
length := len(e.index)
if length <= 0 {
return nil
}
pos := hashcode % int64(length)
ep := e.index[pos].(endpoint.Endpoint)
return &ep
}
// SelectAdapterProxy returns selected adapter.
func (e *EndpointManager) SelectAdapterProxy(msg *Message) *AdapterProxy {
if msg.isHash {
return e.GetHashProxy(msg.hashCode)
}
return e.GetNextValidProxy()
}
func (e *EndpointManager) findAndSetObj(q *queryf.QueryF) {
activeEp := new([]endpointf.EndpointF)
inactiveEp := new([]endpointf.EndpointF)
var setable, ok bool
var setID string
var ret int32
var err error
if setable, ok = e.comm.GetPropertyBool("enableset"); ok {
setID, _ = e.comm.GetProperty("setdivision")
}
if setable {
ret, err = q.FindObjectByIdInSameSet(e.objName, setID, activeEp, inactiveEp)
} else {
ret, err = q.FindObjectByIdInSameGroup(e.objName, activeEp, inactiveEp)
}
if err != nil {
TLOG.Error("find obj end fail:", err.Error())
return
}
TLOG.Debug("find obj endpoint:", e.objName, ret, *activeEp, *inactiveEp)
e.mlock.Lock()
if (len(*inactiveEp)) > 0 {
for _, ep := range *inactiveEp {
end := endpoint.Tars2endpoint(ep)
e.pointsSet.Remove(end)
if a, ok := e.adapters[end]; ok {
delete(e.adapters, end)
a.Close()
}
}
}
if (len(*activeEp)) > 0 {
e.pointsSet.Clear() // clean it first,then add back .this action must lead to add lock,but if don't clean may lead to leakage.it's better to use remove.
for _, ep := range *activeEp {
end := endpoint.Tars2endpoint(ep)
e.pointsSet.Add(end)
}
e.index = e.pointsSet.Slice()
}
for end := range e.adapters {
// clean up dirty data
if !e.pointsSet.Has(end) {
if a, ok := e.adapters[end]; ok {
delete(e.adapters, end)
a.Close()
}
}
}
e.mlock.Unlock()
}