This repository was archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
202 lines (162 loc) · 4.76 KB
/
node.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
package rplx
import (
"context"
"go.uber.org/zap"
"google.golang.org/grpc"
"sync"
"sync/atomic"
"time"
)
const (
defaultRemoteNodeConnectionInterval = time.Second * 5 // interval for connect to remote node
defaultRemoteNodeSyncInterval = time.Second * 2 // interval for sync with remote node
defaultRemoteNodeMaxBufferSize = 1024 // by reach this limit, inits sync process
defaultRemoteNodeWaitSyncCount = 5 // count sync tasks in queue, while current sync in progress
)
var (
nodeChSize = 1024 * 1024
)
// node describe remote node
type node struct {
connected int32
syncing int32
addr string
localNodeID string
remoteNodeID string
replicatorClient ReplicatorClient
replicationChan chan *variable
bufferMx sync.RWMutex
buffer map[string]*variable
maxBufferSize int
// replicatedVersions contains data for replicated variableItems versions
// map key format: <VARIABLE_NAME>@<REMOTE_NODE_ID>
// map value: last replicated variableItem version
replicatedVersionsMx sync.RWMutex
replicatedVersions map[string]int64
syncInterval time.Duration
connectionInterval time.Duration
conn *grpc.ClientConn
syncQueue chan struct{}
stopChan chan struct{}
logger *zap.Logger
metrics *metrics
}
// RemoteNodeOption describe options for RemoteNode, returns from RemoteNodeProvider
type RemoteNodeOption struct {
Addr string
DialOpts []grpc.DialOption
SyncInterval time.Duration
MaxBufferSize int
ConnectionInterval time.Duration
WaitSyncCount int
}
// DefaultRemoteNodeOption returns default remoteNodeOption with provided address
func DefaultRemoteNodeOption(addr string) *RemoteNodeOption {
option := &RemoteNodeOption{
Addr: addr,
DialOpts: []grpc.DialOption{grpc.WithInsecure()},
SyncInterval: defaultRemoteNodeSyncInterval,
MaxBufferSize: defaultRemoteNodeMaxBufferSize,
ConnectionInterval: defaultRemoteNodeConnectionInterval,
WaitSyncCount: defaultRemoteNodeWaitSyncCount,
}
return option
}
func newNode(options *RemoteNodeOption, localNodeID string, logger *zap.Logger, metrics *metrics) *node {
n := &node{
addr: options.Addr,
localNodeID: localNodeID,
replicationChan: make(chan *variable, nodeChSize),
buffer: make(map[string]*variable),
maxBufferSize: options.MaxBufferSize,
replicatedVersions: make(map[string]int64),
syncInterval: options.SyncInterval,
connectionInterval: options.ConnectionInterval,
syncQueue: make(chan struct{}, options.WaitSyncCount),
stopChan: make(chan struct{}),
logger: logger,
metrics: metrics,
}
go n.listenSyncQueue()
go n.listenReplicationChannel()
go n.syncByTicker()
return n
}
func (n *node) Stop() {
close(n.stopChan)
time.Sleep(time.Millisecond * 1000)
close(n.syncQueue)
close(n.replicationChan)
if err := n.conn.Close(); err != nil {
n.logger.Warn("error close grpc connection", zap.Error(err), zap.String("remote node addr", n.addr))
}
}
func (n *node) dial(dialOpts []grpc.DialOption) error {
var err error
if n.replicatorClient != nil {
return nil
}
n.conn, err = grpc.Dial(n.addr, dialOpts...)
if err != nil {
return err
}
n.replicatorClient = NewReplicatorClient(n.conn)
return nil
}
func (n *node) connect(dialOpts []grpc.DialOption, rplx *Rplx) {
t := time.NewTicker(n.connectionInterval)
defer t.Stop()
for {
select {
case <-n.stopChan:
return
case <-t.C:
if err := n.dial(dialOpts); err != nil {
n.logger.Error("error dial to remote node", zap.String("addr", n.addr), zap.Error(err))
continue
}
hello, err := n.replicatorClient.Hello(context.Background(), &HelloRequest{})
if err != nil {
n.logger.Warn("error send hello request to remote node", zap.String("addr", n.addr), zap.Error(err))
continue
}
n.remoteNodeID = hello.ID
n.logger.Debug("connected to remote node", zap.String("addr", n.addr), zap.String("remote node ID", n.remoteNodeID))
// send all current variables to replication for new connected node
rplx.variablesMx.RLock()
for _, v := range rplx.variables {
n.replicationChan <- v
}
rplx.variablesMx.RUnlock()
rplx.nodesMx.Lock()
rplx.nodesIDToAddr[n.remoteNodeID] = n.addr
rplx.nodesMx.Unlock()
atomic.StoreInt32(&n.connected, 1)
go n.sync()
return
}
}
}
func (n *node) syncByTicker() {
t := time.NewTicker(n.syncInterval)
defer t.Stop()
for {
select {
case <-n.stopChan:
return
case <-t.C:
n.sync()
}
}
}
func (n *node) listenReplicationChannel() {
for v := range n.replicationChan {
n.bufferMx.Lock()
n.buffer[v.name] = v
l := len(n.buffer)
n.bufferMx.Unlock()
if l > n.maxBufferSize {
go n.sync()
}
}
}