-
Notifications
You must be signed in to change notification settings - Fork 0
/
jetkeeper.go
449 lines (364 loc) · 12.8 KB
/
jetkeeper.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
//
// Copyright 2019 Insolar Technologies GmbH
//
// 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 executor
import (
"context"
"fmt"
"sync"
"github.com/pkg/errors"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/insolar/jet"
insolarPulse "github.com/insolar/insolar/insolar/pulse"
"github.com/insolar/insolar/insolar/store"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/pulse"
)
//go:generate minimock -i github.com/insolar/insolar/ledger/heavy/executor.JetKeeper -o ./ -s _gen_mock.go -g
// JetKeeper provides a method for adding jet to storage, checking pulse completion and getting access to highest synced pulse.
type JetKeeper interface {
// AddDropConfirmation performs adding jet to storage and checks pulse completion.
AddDropConfirmation(ctx context.Context, pn insolar.PulseNumber, jet insolar.JetID, split bool) error
// AddHotConfirmation performs adding hot confirmation to storage and checks pulse completion.
AddHotConfirmation(ctx context.Context, pn insolar.PulseNumber, jet insolar.JetID, split bool) error
// AddBackupConfirmation performs adding backup confirmation to storage and checks pulse completion.
AddBackupConfirmation(ctx context.Context, pn insolar.PulseNumber) error
// TopSyncPulse provides access to highest synced (replicated) pulse.
TopSyncPulse() insolar.PulseNumber
// HasAllJetConfirms says if given pulse has drop and hot confirms. Ignore backups
HasAllJetConfirms(ctx context.Context, pn insolar.PulseNumber) bool
// Storage returns jets storage
Storage() jet.Storage
}
func NewJetKeeper(jets jet.Storage, db store.DB, pulses insolarPulse.Calculator) *DBJetKeeper {
return &DBJetKeeper{
jetTrees: jets,
db: db,
pulses: pulses,
}
}
type DBJetKeeper struct {
lock sync.RWMutex
jetTrees jet.Storage
pulses insolarPulse.Calculator
db store.DB
}
func (jk *DBJetKeeper) Storage() jet.Storage {
return jk.jetTrees
}
func (jk *DBJetKeeper) AddHotConfirmation(ctx context.Context, pn insolar.PulseNumber, id insolar.JetID, split bool) error {
jk.lock.Lock()
defer jk.lock.Unlock()
inslogger.FromContext(ctx).Debug("AddHotConfirmation. pulse: ", pn, ". ID: ", id.DebugString())
if err := jk.updateHot(ctx, pn, id, split); err != nil {
return errors.Wrapf(err, "failed to save updated jets")
}
return nil
}
// AddDropConfirmation performs adding jet to storage and checks pulse completion.
func (jk *DBJetKeeper) AddDropConfirmation(ctx context.Context, pn insolar.PulseNumber, id insolar.JetID, split bool) error {
jk.lock.Lock()
defer jk.lock.Unlock()
inslogger.FromContext(ctx).Debug("AddDropConfirmation. pulse: ", pn, ". ID: ", id.DebugString(), ", Split: ", split)
if err := jk.updateDrop(ctx, pn, id, split); err != nil {
return errors.Wrapf(err, "AddDropConfirmation. failed to save updated jets")
}
return nil
}
// AddBackupConfirmation performs adding backup confirmation to storage and checks pulse completion.
func (jk *DBJetKeeper) AddBackupConfirmation(ctx context.Context, pn insolar.PulseNumber) error {
jk.lock.Lock()
defer jk.lock.Unlock()
inslogger.FromContext(ctx).Debug("AddBackupConfirmation. pulse: ", pn)
if err := jk.updateBackup(pn); err != nil {
return errors.Wrapf(err, "AddDropConfirmation. failed to save updated jets")
}
err := jk.updateTopSyncPulse(ctx, pn)
return errors.Wrap(err, "updateTopSyncPulse returns error")
}
func (jk *DBJetKeeper) updateBackup(pulse insolar.PulseNumber) error {
jets, err := jk.get(pulse)
if err != nil && err != store.ErrNotFound {
return errors.Wrapf(err, "updateBackup. can't get pulse: %d", pulse)
}
if len(jets) == 0 {
return errors.New("Received backup confirmation before replication data")
}
for i := range jets {
jets[i].addBackup()
}
return jk.set(pulse, jets)
}
func (jk *DBJetKeeper) updateTopSyncPulse(ctx context.Context, pn insolar.PulseNumber) error {
logger := inslogger.FromContext(ctx)
if jk.checkPulseConsistency(ctx, pn, true) {
err := jk.updateSyncPulse(pn)
if err != nil {
return errors.Wrapf(err, "failed to update consistent pulse")
}
logger.Debugf("pulse completed: %d", pn)
}
return nil
}
// HasJetConfirms says if given pulse has drop and hot confirms. Ignore backups
func (jk *DBJetKeeper) HasAllJetConfirms(ctx context.Context, pulse insolar.PulseNumber) bool {
jk.lock.RLock()
defer jk.lock.RUnlock()
if jk.topSyncPulse() >= pulse {
return true
}
return jk.checkPulseConsistency(ctx, pulse, false)
}
// TopSyncPulse provides access to highest synced (replicated) pulse.
func (jk *DBJetKeeper) TopSyncPulse() insolar.PulseNumber {
jk.lock.RLock()
defer jk.lock.RUnlock()
return jk.topSyncPulse()
}
func (jk *DBJetKeeper) topSyncPulse() insolar.PulseNumber {
val, err := jk.db.Get(syncPulseKey{})
if err != nil {
return insolar.GenesisPulse.PulseNumber
}
return insolar.NewPulseNumber(val)
}
func (jk *DBJetKeeper) getForJet(ctx context.Context, pulse insolar.PulseNumber, jet insolar.JetID) (int, []JetInfo, error) {
logger := inslogger.FromContext(ctx)
jets, err := jk.get(pulse)
if err != nil && err != store.ErrNotFound {
return 0, nil, errors.Wrapf(err, "updateHot. can't get pulse: %d", pulse)
}
for i := range jets {
if jets[i].JetID.Equal(jet) {
logger.Debug("getForJet. found. jet: ", jet.DebugString(), ", pulse: ", pulse)
return i, jets, nil
}
}
newInfo := JetInfo{}
jets = append(jets, newInfo)
logger.Debug("getForJet. create new. jet: ", jet.DebugString(), ", pulse: ", pulse)
return len(jets) - 1, jets, nil
}
func (jk *DBJetKeeper) updateHot(ctx context.Context, pulse insolar.PulseNumber, id insolar.JetID, split bool) error {
parentID := id
if split {
parentID = jet.Parent(id)
}
idx, jets, err := jk.getForJet(ctx, pulse, parentID)
if err != nil {
return errors.Wrap(err, "Can't getForJet")
}
err = jets[idx].addHot(id, parentID, split)
if err != nil {
return errors.Wrap(err, "can't addHot")
}
return jk.set(pulse, jets)
}
func (jk *DBJetKeeper) updateDrop(ctx context.Context, pulse insolar.PulseNumber, id insolar.JetID, split bool) error {
idx, jets, err := jk.getForJet(ctx, pulse, id)
if err != nil {
return errors.Wrap(err, "Can't getForJet")
}
err = jets[idx].addDrop(id, split)
if err != nil {
return errors.Wrap(err, "can't addHot")
}
return jk.set(pulse, jets)
}
// infoToSet converts given jetInfo slice to set and checks confirmations
// if at least one jetInfo is not confirmed it returns false
// checkBackup is used to skip checking of backup confirmation
func infoToSet(ctx context.Context, s []JetInfo, checkBackup bool) (map[insolar.JetID]struct{}, bool) {
r := make(map[insolar.JetID]struct{}, len(s))
for _, el := range s {
if !el.isConfirmed(ctx, checkBackup) {
return nil, false
}
r[el.JetID] = struct{}{}
}
return r, len(r) != 0
}
func infoToList(s map[insolar.JetID]struct{}) []insolar.JetID {
r := make([]insolar.JetID, len(s))
var idx int
for jet := range s {
r[idx] = jet
idx++
}
return r
}
func (jk *DBJetKeeper) getTopSyncJets(ctx context.Context) ([]insolar.JetID, error) {
var result []insolar.JetID
top := jk.topSyncPulse()
if top == pulse.MinTimePulse {
return []insolar.JetID{insolar.ZeroJetID}, nil
}
jets, err := jk.get(top)
if err != nil {
return nil, errors.Wrapf(err, "can't getTopSyncJets: %d", top)
}
for _, ji := range jets {
if !ji.IsSplitSet {
inslogger.FromContext(ctx).Error("IsSplitJet must be set before calling for isConfirmed")
return nil, fmt.Errorf("IsSplitJet must be set before calling for isConfirmed. JetID:%v", ji.JetID.DebugString())
}
if ji.Split {
left, right := jet.Siblings(ji.JetID)
result = append(result, left, right)
} else {
result = append(result, ji.JetID)
}
}
return result, nil
}
func compareJets(ctx context.Context, what []insolar.JetID, actualJetsSet map[insolar.JetID]struct{}) (bool, error) {
if len(actualJetsSet) != len(what) {
if len(actualJetsSet) > len(what) {
return false, errors.New("num actual jets is more then topSyncJets." +
" TopSyncJets: " + insolar.JetIDCollection(what).DebugString() +
". Actual: " + insolar.JetIDCollection(infoToList(actualJetsSet)).DebugString())
}
inslogger.FromContext(ctx).Debug("actual and top sync pule jets are still different")
return false, nil
}
for _, expID := range what {
if _, ok := actualJetsSet[expID]; !ok {
return false, errors.New("jet sets are different. it's too bad. " +
". TopSyncJets: " + insolar.JetIDCollection(what).DebugString() +
". Actual: " + insolar.JetIDCollection(infoToList(actualJetsSet)).DebugString())
}
}
return true, nil
}
func (jk *DBJetKeeper) checkPulseConsistency(ctx context.Context, pulse insolar.PulseNumber, checkBackup bool) bool {
logger := inslogger.FromContext(ctx)
prev, err := jk.pulses.Backwards(ctx, pulse, 1)
if err != nil {
logger.Errorf("failed to get previous pulse for %d, %s", pulse, err)
return false
}
top := jk.topSyncPulse()
logger.Debug("propagateConsistency. pulse: ", pulse, ". top: ", top, ". prev.PulseNumber: ", prev.PulseNumber)
if prev.PulseNumber != top {
// We should sync pulses sequentially. We can't skip.
logger.Info("Try to checkPulseConsistency for future pulse. Skip it. prev.PulseNumber: ", prev.PulseNumber, ", top: ", top)
return false
}
topSyncJets, err := jk.getTopSyncJets(ctx)
if err != nil {
logger.Fatal("can't get jets for top sync pulse: ", err)
return false
}
actualJets := jk.all(pulse)
actualJetsSet, allConfirmed := infoToSet(ctx, actualJets, checkBackup)
if !allConfirmed {
return false
}
logger.Debug("topSyncJets: ", insolar.JetIDCollection(topSyncJets).DebugString(), " | ",
"actualJets: ", insolar.JetIDCollection(infoToList(actualJetsSet)).DebugString())
areEqual, err := compareJets(ctx, topSyncJets, actualJetsSet)
if err != nil {
logger.Error("top sync jets and actual jets are different. Pulse: ", pulse, ". Err: ", err)
return false
}
if !areEqual {
return false
}
currentJetTree := jk.jetTrees.All(ctx, pulse)
areEqual, err = compareJets(ctx, currentJetTree, actualJetsSet)
if err != nil {
logger.Error("current jet tree and actual jets are different. Pulse: ", pulse, ". Err: ", err)
return false
}
if !areEqual {
return false
}
return true
}
func (jk *DBJetKeeper) all(pulse insolar.PulseNumber) []JetInfo {
jets, err := jk.get(pulse)
if err != nil {
jets = []JetInfo{}
}
return jets
}
type jetKeeperKey insolar.PulseNumber
func (k jetKeeperKey) Scope() store.Scope {
return store.ScopeJetKeeper
}
func (k jetKeeperKey) ID() []byte {
return insolar.PulseNumber(k).Bytes()
}
func newJetKeeperKey(raw []byte) jetKeeperKey {
return jetKeeperKey(insolar.NewPulseNumber(raw))
}
type syncPulseKey struct{}
func (k syncPulseKey) Scope() store.Scope {
return store.ScopeJetKeeperSyncPulse
}
func (k syncPulseKey) ID() []byte {
return []byte{}
}
func (jk *DBJetKeeper) get(pn insolar.PulseNumber) ([]JetInfo, error) {
serializedJets, err := jk.db.Get(jetKeeperKey(pn))
if err != nil {
if err == store.ErrNotFound {
return nil, err
}
return nil, errors.Wrapf(err, "failed to get jets by pulse=%v", pn)
}
var jets JetsInfo
err = jets.Unmarshal(serializedJets)
if err != nil {
return nil, errors.Wrap(err, "failed to deserialize jets")
}
return jets.Jets, nil
}
func (jk *DBJetKeeper) set(pn insolar.PulseNumber, jets []JetInfo) error {
key := jetKeeperKey(pn)
jetsInfo := JetsInfo{Jets: jets}
serialized, err := jetsInfo.Marshal()
if err != nil {
return errors.Wrap(err, "failed to serialize jets")
}
return jk.db.Set(key, serialized)
}
func (jk *DBJetKeeper) updateSyncPulse(pn insolar.PulseNumber) error {
err := jk.db.Set(syncPulseKey{}, pn.Bytes())
return errors.Wrapf(err, "failed to set up new sync pulse")
}
func (jk *DBJetKeeper) TruncateHead(ctx context.Context, from insolar.PulseNumber) error {
jk.lock.Lock()
defer jk.lock.Unlock()
if from <= jk.topSyncPulse() {
return errors.New("try to truncate top sync pulse")
}
it := jk.db.NewIterator(jetKeeperKey(from), false)
defer it.Close()
var hasKeys bool
for it.Next() {
hasKeys = true
key := newJetKeeperKey(it.Key())
err := jk.db.Delete(&key)
if err != nil {
return errors.Wrapf(err, "can't delete key: %+v", key)
}
inslogger.FromContext(ctx).Debugf("Erased key. Pulse number: %d", key)
}
if !hasKeys {
inslogger.FromContext(ctx).Infof("No records. Nothing done. Pulse number: %s", from.String())
}
return nil
}