This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
469 lines (386 loc) · 10.2 KB
/
context.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package edge
import (
"fmt"
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/errors"
"github.com/ronaksoft/rony/log"
"github.com/ronaksoft/rony/store"
"github.com/ronaksoft/rony/tools"
"google.golang.org/protobuf/proto"
"reflect"
"sync"
"time"
)
/*
Creation Time: 2019 - Jun - 07
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type MessageKind byte
const (
_ MessageKind = iota
GatewayMessage
TunnelMessage
)
var (
messageKindNames = map[MessageKind]string{
GatewayMessage: "GatewayMessage",
TunnelMessage: "TunnelMessage",
}
)
func (c MessageKind) String() string {
return messageKindNames[c]
}
// DispatchCtx holds the context of the dispatcher's request. Each DispatchCtx could
// hold one or many RequestCtx. DispatchCtx lives until the last of its RequestCtx children.
type DispatchCtx struct {
edge *Server
streamID int64
serverID []byte
conn rony.Conn
req *rony.MessageEnvelope
reqFilled bool
kind MessageKind
buf *tools.LinkedList
// KeyValue Store Parameters
mtx sync.RWMutex
kv map[string]interface{}
}
func newDispatchCtx(edge *Server) *DispatchCtx {
return &DispatchCtx{
edge: edge,
req: &rony.MessageEnvelope{},
kv: make(map[string]interface{}, 3),
buf: tools.NewLinkedList(),
}
}
func (ctx *DispatchCtx) reset() {
ctx.reqFilled = false
for k := range ctx.kv {
delete(ctx.kv, k)
}
ctx.buf.Reset()
}
func (ctx *DispatchCtx) ServerID() string {
return string(ctx.serverID)
}
func (ctx *DispatchCtx) Debug() {
fmt.Println("###")
t := reflect.Indirect(reflect.ValueOf(ctx))
for i := 0; i < t.NumField(); i++ {
fmt.Println(t.Type().Field(i).Name, t.Type().Field(i).Offset, t.Type().Field(i).Type.Size())
}
}
func (ctx *DispatchCtx) Conn() rony.Conn {
return ctx.conn
}
func (ctx *DispatchCtx) StreamID() int64 {
return ctx.streamID
}
func (ctx *DispatchCtx) FillEnvelope(e *rony.MessageEnvelope) {
if ctx.reqFilled {
panic("BUG!!! request has been already filled")
}
ctx.reqFilled = true
e.DeepCopy(ctx.req)
}
func (ctx *DispatchCtx) Fill(
requestID uint64, constructor uint64, p proto.Message, kv ...*rony.KeyValue,
) {
if ctx.reqFilled {
panic("BUG!!! request has been already filled")
}
ctx.reqFilled = true
ctx.req.Fill(requestID, constructor, p, kv...)
}
func (ctx *DispatchCtx) FillBytes(data []byte) error {
if ctx.reqFilled {
panic("BUG!!! request has been already filled")
}
ctx.reqFilled = true
return proto.Unmarshal(data, ctx.req)
}
func (ctx *DispatchCtx) Set(key string, v interface{}) {
ctx.mtx.Lock()
ctx.kv[key] = v
ctx.mtx.Unlock()
}
func (ctx *DispatchCtx) Get(key string) interface{} {
ctx.mtx.RLock()
v := ctx.kv[key]
ctx.mtx.RUnlock()
return v
}
func (ctx *DispatchCtx) GetBytes(key string, defaultValue []byte) []byte {
v, ok := ctx.Get(key).([]byte)
if ok {
return v
}
return defaultValue
}
func (ctx *DispatchCtx) GetString(key string, defaultValue string) string {
v := ctx.Get(key)
switch x := v.(type) {
case []byte:
return tools.ByteToStr(x)
case string:
return x
default:
return defaultValue
}
}
func (ctx *DispatchCtx) GetInt64(key string, defaultValue int64) int64 {
v, ok := ctx.Get(key).(int64)
if ok {
return v
}
return defaultValue
}
func (ctx *DispatchCtx) GetBool(key string) bool {
v, ok := ctx.Get(key).(bool)
if ok {
return v
}
return false
}
// Kind identifies that this dispatch context is generated from Tunnel or Gateway. This helps
// developer to apply different strategies based on the source of the incoming message
func (ctx *DispatchCtx) Kind() MessageKind {
return ctx.kind
}
func (ctx *DispatchCtx) BufferPush(m *rony.MessageEnvelope) {
ctx.buf.Append(m)
}
func (ctx *DispatchCtx) BufferPop(f func(envelope *rony.MessageEnvelope)) bool {
me, _ := ctx.buf.PickHeadData().(*rony.MessageEnvelope)
if me == nil {
return false
}
f(me)
return true
}
func (ctx *DispatchCtx) BufferPopAll(f func(envelope *rony.MessageEnvelope)) {
for ctx.BufferPop(f) {
}
}
func (ctx *DispatchCtx) BufferSize() int32 {
return ctx.buf.Size()
}
var dispatchCtxPool = sync.Pool{}
func acquireDispatchCtx(
edge *Server, conn rony.Conn,
streamID int64, serverID []byte, kind MessageKind,
) *DispatchCtx {
var ctx *DispatchCtx
if v := dispatchCtxPool.Get(); v == nil {
ctx = newDispatchCtx(edge)
} else {
ctx = v.(*DispatchCtx)
}
ctx.conn = conn
ctx.kind = kind
ctx.streamID = streamID
ctx.serverID = append(ctx.serverID[:0], serverID...)
return ctx
}
func releaseDispatchCtx(ctx *DispatchCtx) {
// Reset the Key-Value store
ctx.reset()
// Put back the context into the pool
dispatchCtxPool.Put(ctx)
}
// RequestCtx holds the context of an RPC handler
type RequestCtx struct {
dispatchCtx *DispatchCtx
edge *Server
reqID uint64
nextChan chan struct{}
quickReturn bool
stop bool
}
func newRequestCtx() *RequestCtx {
return &RequestCtx{
nextChan: make(chan struct{}, 1),
}
}
// NextCanGo is useful only if the main request is rony.MessageContainer with SyncRun flag set to TRUE.
// Then by calling this function it lets the next request (if any) executed concurrently.
func (ctx *RequestCtx) NextCanGo() {
if ctx.quickReturn {
ctx.nextChan <- struct{}{}
}
}
func (ctx *RequestCtx) ConnID() uint64 {
if ctx.dispatchCtx.Conn() != nil {
return ctx.dispatchCtx.Conn().ConnID()
}
return 0
}
func (ctx *RequestCtx) Conn() rony.Conn {
return ctx.dispatchCtx.Conn()
}
func (ctx *RequestCtx) ReqID() uint64 {
return ctx.reqID
}
func (ctx *RequestCtx) ServerID() string {
return string(ctx.dispatchCtx.serverID)
}
func (ctx *RequestCtx) Kind() MessageKind {
return ctx.dispatchCtx.kind
}
func (ctx *RequestCtx) StopExecution() {
ctx.stop = true
}
func (ctx *RequestCtx) Stopped() bool {
return ctx.stop
}
func (ctx *RequestCtx) Store() *store.Store {
return ctx.edge.store
}
func (ctx *RequestCtx) Set(key string, v interface{}) {
ctx.dispatchCtx.mtx.Lock()
ctx.dispatchCtx.kv[key] = v
ctx.dispatchCtx.mtx.Unlock()
}
func (ctx *RequestCtx) Get(key string) interface{} {
return ctx.dispatchCtx.Get(key)
}
func (ctx *RequestCtx) GetBytes(key string, defaultValue []byte) []byte {
return ctx.dispatchCtx.GetBytes(key, defaultValue)
}
func (ctx *RequestCtx) GetString(key string, defaultValue string) string {
return ctx.dispatchCtx.GetString(key, defaultValue)
}
func (ctx *RequestCtx) GetInt64(key string, defaultValue int64) int64 {
return ctx.dispatchCtx.GetInt64(key, defaultValue)
}
func (ctx *RequestCtx) GetBool(key string) bool {
return ctx.dispatchCtx.GetBool(key)
}
func (ctx *RequestCtx) pushRedirect(reason rony.RedirectReason, replicaSet uint64) {
r := rony.PoolRedirect.Get()
r.Reason = reason
r.WaitInSec = 0
members := ctx.Cluster().MembersByReplicaSet(replicaSet)
for _, m := range members {
ni := m.Proto(rony.PoolEdge.Get())
r.Edges = append(r.Edges, ni)
}
ctx.PushMessage(rony.C_Redirect, r)
rony.PoolRedirect.Put(r)
ctx.StopExecution()
}
func (ctx *RequestCtx) PushRedirectSession(replicaSet uint64) {
ctx.pushRedirect(rony.RedirectReason_ReplicaSetSession, replicaSet)
}
func (ctx *RequestCtx) PushRedirectRequest(replicaSet uint64) {
ctx.pushRedirect(rony.RedirectReason_ReplicaSetRequest, replicaSet)
}
func (ctx *RequestCtx) PushMessage(constructor uint64, proto proto.Message) {
ctx.PushCustomMessage(ctx.ReqID(), constructor, proto)
}
func (ctx *RequestCtx) PushCustomMessage(
requestID uint64, constructor uint64, message proto.Message, kvs ...*rony.KeyValue,
) {
envelope := rony.PoolMessageEnvelope.Get()
envelope.Fill(requestID, constructor, message, kvs...)
switch ctx.Kind() {
case TunnelMessage:
ctx.dispatchCtx.BufferPush(envelope.Clone())
case GatewayMessage:
if ctx.Conn().Persistent() {
ctx.edge.dispatcher.OnMessage(ctx.dispatchCtx, envelope)
} else {
ctx.dispatchCtx.BufferPush(envelope.Clone())
}
}
rony.PoolMessageEnvelope.Put(envelope)
}
func (ctx *RequestCtx) PushError(err *rony.Error) {
ctx.PushMessage(rony.C_Error, err)
ctx.stop = true
}
func (ctx *RequestCtx) PushCustomError(code, item string, desc string) {
ctx.PushMessage(rony.C_Error, &rony.Error{
Code: code,
Items: item,
Description: desc,
})
ctx.stop = true
}
func (ctx *RequestCtx) Cluster() rony.Cluster {
return ctx.edge.cluster
}
func (ctx *RequestCtx) ClusterEdges(replicaSet uint64, edges *rony.Edges) (*rony.Edges, error) {
req := rony.PoolMessageEnvelope.Get()
defer rony.PoolMessageEnvelope.Put(req)
res := rony.PoolMessageEnvelope.Get()
defer rony.PoolMessageEnvelope.Put(res)
req.Fill(tools.RandomUint64(0), rony.C_GetAllNodes, &rony.GetAllNodes{})
err := ctx.TunnelRequest(replicaSet, req, res)
if err != nil {
return nil, err
}
switch res.Constructor {
case rony.C_Edges:
if edges == nil {
edges = &rony.Edges{}
}
_ = edges.Unmarshal(res.GetMessage())
return edges, nil
case rony.C_Error:
x := &rony.Error{}
_ = x.Unmarshal(res.GetMessage())
return nil, x
default:
return nil, errors.ErrUnexpectedTunnelResponse
}
}
func (ctx *RequestCtx) TryTunnelRequest(
attempts int, retryWait time.Duration, replicaSet uint64,
req, res *rony.MessageEnvelope,
) error {
return ctx.edge.TryTunnelRequest(attempts, retryWait, replicaSet, req, res)
}
func (ctx *RequestCtx) TunnelRequest(replicaSet uint64, req, res *rony.MessageEnvelope) error {
return ctx.edge.TryTunnelRequest(1, 0, replicaSet, req, res)
}
func (ctx *RequestCtx) Log() log.Logger {
return ctx.edge.logger
}
func (ctx *RequestCtx) ReplicaSet() uint64 {
if ctx.edge.cluster == nil {
return 0
}
return ctx.edge.cluster.ReplicaSet()
}
func (ctx *RequestCtx) Router() rony.Router {
return ctx.edge.router
}
var requestCtxPool = sync.Pool{}
func acquireRequestCtx(dispatchCtx *DispatchCtx, quickReturn bool) *RequestCtx {
var ctx *RequestCtx
if v := requestCtxPool.Get(); v == nil {
ctx = newRequestCtx()
} else {
ctx = v.(*RequestCtx)
}
ctx.stop = false
ctx.quickReturn = quickReturn
ctx.dispatchCtx = dispatchCtx
ctx.edge = dispatchCtx.edge
return ctx
}
func releaseRequestCtx(ctx *RequestCtx) {
// Just to make sure channel is empty, or empty it if not
select {
case <-ctx.nextChan:
default:
}
ctx.reqID = 0
// Put back into the pool
requestCtxPool.Put(ctx)
}