-
Notifications
You must be signed in to change notification settings - Fork 0
/
heavy_client.go
297 lines (255 loc) · 7.64 KB
/
heavy_client.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
/*
* Copyright 2019 Insolar Technologies
*
* 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,
* 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 heavyclient
import (
"context"
"sync"
"time"
"github.com/insolar/insolar/configuration"
"github.com/insolar/insolar/core"
"github.com/insolar/insolar/core/reply"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/instrumentation/insmetrics"
"github.com/insolar/insolar/ledger/storage"
"github.com/insolar/insolar/utils/backoff"
"github.com/pkg/errors"
"go.opencensus.io/stats"
)
// Options contains heavy client configuration params.
type Options struct {
SyncMessageLimit int
PulsesDeltaLimit int
BackoffConf configuration.Backoff
}
// JetClient heavy replication client. Replicates records for one jet.
type JetClient struct {
bus core.MessageBus
pulseStorage core.PulseStorage
replicaStorage storage.ReplicaStorage
pulseTracker storage.PulseTracker
cleaner storage.Cleaner
db storage.DBContext
opts Options
// life cycle control
//
startOnce sync.Once
cancel context.CancelFunc
signal chan struct{}
// syncdone closes when syncloop is gracefully finished
syncdone chan struct{}
// state:
jetID core.RecordID
muPulses sync.Mutex
leftPulses []core.PulseNumber
syncbackoff *backoff.Backoff
}
// NewJetClient heavy replication client constructor.
//
// First argument defines what jet it serve.
func NewJetClient(
replicaStorage storage.ReplicaStorage,
mb core.MessageBus,
pulseStorage core.PulseStorage,
pulseTracker storage.PulseTracker,
cleaner storage.Cleaner,
db storage.DBContext,
jetID core.RecordID,
opts Options,
) *JetClient {
jsc := &JetClient{
bus: mb,
pulseStorage: pulseStorage,
replicaStorage: replicaStorage,
pulseTracker: pulseTracker,
cleaner: cleaner,
db: db,
jetID: jetID,
syncbackoff: backoffFromConfig(opts.BackoffConf),
signal: make(chan struct{}, 1),
syncdone: make(chan struct{}),
opts: opts,
}
return jsc
}
// should be called from protected by mutex code
func (c *JetClient) updateLeftPulsesMetrics(ctx context.Context) {
// instrumentation
var pn core.PulseNumber
if len(c.leftPulses) > 0 {
pn = c.leftPulses[0]
}
ctx = insmetrics.InsertTag(ctx, tagJet, c.jetID.DebugString())
stats.Record(ctx,
statUnsyncedPulsesCount.M(int64(len(c.leftPulses))),
statFirstUnsyncedPulse.M(int64(pn)),
)
}
// addPulses add pulse numbers for syncing.
func (c *JetClient) addPulses(ctx context.Context, pns []core.PulseNumber) {
c.muPulses.Lock()
c.leftPulses = append(c.leftPulses, pns...)
if err := c.replicaStorage.SetSyncClientJetPulses(ctx, c.jetID, c.leftPulses); err != nil {
inslogger.FromContext(ctx).Errorf(
"attempt to persist jet sync state failed: jetID=%v: %v", c.jetID, err.Error())
}
c.updateLeftPulsesMetrics(ctx)
c.muPulses.Unlock()
}
func (c *JetClient) pulsesLeft() int {
c.muPulses.Lock()
defer c.muPulses.Unlock()
return len(c.leftPulses)
}
// unshiftPulse removes and returns pulse number from head of processing queue.
func (c *JetClient) unshiftPulse(ctx context.Context) *core.PulseNumber {
c.muPulses.Lock()
defer c.muPulses.Unlock()
if len(c.leftPulses) == 0 {
return nil
}
result := c.leftPulses[0]
// shift array elements on one position to left
shifted := c.leftPulses[:len(c.leftPulses)-1]
copy(shifted, c.leftPulses[1:])
c.leftPulses = shifted
if err := c.replicaStorage.SetSyncClientJetPulses(ctx, c.jetID, c.leftPulses); err != nil {
inslogger.FromContext(ctx).Errorf(
"attempt to persist jet sync state failed: jetID=%v: %v", c.jetID, err.Error())
}
c.updateLeftPulsesMetrics(ctx)
return &result
}
func (c *JetClient) nextPulseNumber() (core.PulseNumber, bool) {
c.muPulses.Lock()
defer c.muPulses.Unlock()
if len(c.leftPulses) == 0 {
return 0, false
}
return c.leftPulses[0], true
}
func (c *JetClient) runOnce(ctx context.Context) {
// retrydelay = m.syncbackoff.ForAttempt(attempt)
c.startOnce.Do(func() {
// TODO: reset TraceID from context, or just don't use context?
// (TraceID not meaningful in async sync loop)
ctx, cancel := context.WithCancel(context.Background())
c.cancel = cancel
go c.syncloop(ctx)
})
}
func (c *JetClient) syncloop(ctx context.Context) {
inslog := inslogger.FromContext(ctx)
defer close(c.syncdone)
var (
syncPN core.PulseNumber
hasNext bool
retrydelay time.Duration
)
finishpulse := func() {
_ = c.unshiftPulse(ctx)
c.syncbackoff.Reset()
retrydelay = 0
}
for {
select {
case <-time.After(retrydelay):
// for first try delay should be zero
case <-ctx.Done():
if c.pulsesLeft() == 0 {
// got cancel signal and have nothing to do
return
}
// client in canceled state signal but has smth to do
}
for {
// if we have pulses to sync, process it
syncPN, hasNext = c.nextPulseNumber()
if hasNext {
inslog.Debugf("synchronization next sync pulse num: %v (left=%v)", syncPN, c.leftPulses)
break
}
inslog.Debug("synchronization waiting signal what new pulse happens")
_, ok := <-c.signal
if !ok {
inslog.Info("stop is called, so we are should just stop syncronization loop")
return
}
}
if isPulseNumberOutdated(ctx, c.pulseTracker, c.pulseStorage, syncPN, c.opts.PulsesDeltaLimit) {
inslog.Infof("pulse %v on jet %v is outdated, skip it", syncPN, c.jetID)
finishpulse()
continue
}
inslog.Infof("start synchronization to heavy for pulse %v", syncPN)
shouldretry := false
isretry := c.syncbackoff.Attempt() > 0
syncerr := c.HeavySync(ctx, syncPN, isretry)
if syncerr != nil {
if heavyerr, ok := syncerr.(*reply.HeavyError); ok {
shouldretry = heavyerr.IsRetryable()
}
syncerr = errors.Wrap(syncerr, "HeavySync failed")
inslog.Errorf("%v (on attempt=%v, shouldretry=%v)",
syncerr.Error(), c.syncbackoff.Attempt(), shouldretry)
if shouldretry {
retrydelay = c.syncbackoff.Duration()
continue
}
// TODO: write some info to dust - 14.Dec.2018 @nordicdyno
} else {
ctx = insmetrics.InsertTag(ctx, tagJet, c.jetID.DebugString())
stats.Record(ctx,
statSyncedPulsesCount.M(1),
)
}
finishpulse()
}
}
// Stop stops heavy client replication
func (c *JetClient) Stop(ctx context.Context) {
// cancel should be set if client has started
if c.cancel != nil {
// two signals for sync loop to stop
c.cancel()
close(c.signal)
// waits sync loop to stop
<-c.syncdone
}
}
func backoffFromConfig(bconf configuration.Backoff) *backoff.Backoff {
return &backoff.Backoff{
Jitter: bconf.Jitter,
Min: bconf.Min,
Max: bconf.Max,
Factor: bconf.Factor,
}
}
func isPulseNumberOutdated(ctx context.Context, pulseTracker storage.PulseTracker, pstore core.PulseStorage, pn core.PulseNumber, delta int) bool {
current, err := pstore.Current(ctx)
if err != nil {
panic(err)
}
currentPulse, err := pulseTracker.GetPulse(ctx, current.PulseNumber)
if err != nil {
panic(err)
}
pnPulse, err := pulseTracker.GetPulse(ctx, pn)
if err != nil {
inslogger.FromContext(ctx).Errorf("Can't get pulse by pulse number: %v", pn)
return true
}
return currentPulse.SerialNumber-delta > pnPulse.SerialNumber
}