forked from dinstein/fabric
/
ledger_snapshot.go
434 lines (365 loc) · 13.4 KB
/
ledger_snapshot.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
package ledger
import (
"fmt"
"github.com/abchain/fabric/core/db"
"github.com/abchain/fabric/core/ledger/statemgmt"
"github.com/abchain/fabric/core/ledger/statemgmt/state"
"github.com/abchain/fabric/core/util"
"github.com/abchain/fabric/protos"
)
type LedgerSnapshot struct {
stateM statemgmt.SnapshotState
*db.DBSnapshot
}
func (sledger *LedgerSnapshot) GetParitalRangeIterator() (statemgmt.PartialRangeIterator, error) {
return sledger.stateM.GetPartialRangeIterator(sledger.DBSnapshot)
}
//notice the indexs in snapshot is not ensured to be latest (e.g. in the asyncindexers)
func (sledger *LedgerSnapshot) GetBlockNumberByState(hash []byte) (uint64, error) {
return fetchBlockNumberByStateHashFromSnapshot(sledger.DBSnapshot, hash)
}
func (sledger *LedgerSnapshot) GetRawBlockByNumber(blockNumber uint64) (*protos.Block, error) {
blockBytes, err := sledger.GetFromBlockchainCFSnapshot(encodeBlockNumberDBKey(blockNumber))
if err != nil {
return nil, err
}
blk, err := bytesToBlock(blockBytes)
if err != nil {
return nil, err
}
return blk, nil
}
func (sledger *LedgerSnapshot) GetBlockByNumber(blockNumber uint64) (*protos.Block, error) {
size, err := sledger.GetBlockchainSize()
if err != nil {
return nil, err
}
if blockNumber >= size {
return nil, newLedgerError(ErrorTypeOutOfBounds, fmt.Sprintf("block number %d too large (%d)", blockNumber, size))
}
blk, err := sledger.GetRawBlockByNumber(blockNumber)
if err != nil {
return nil, err
} else if blk == nil {
return nil, nil
}
return finishFetchedBlock(blk)
}
// TODO: make the snapshot version for testBlockExistedRangeSafe
// func (sledger *LedgerSnapshot) TestExistedBlockRange(blockNumber uint64) uint64 {
// return 0
// }
//test how height the blocks we have obtained continuously
func (sledger *LedgerSnapshot) TestContinuouslBlockRange() (uint64, error) {
bytes, err := sledger.GetFromBlockchainCFSnapshot(continuousblockCountKey)
if err != nil {
return 0, err
}
return bytesToBlockNumber(bytes), nil
}
func (sledger *LedgerSnapshot) GetBlockchainSize() (uint64, error) {
bytes, err := sledger.GetFromBlockchainCFSnapshot(blockCountKey)
if err != nil {
return 0, err
}
return bytesToBlockNumber(bytes), nil
}
func (sledger *LedgerSnapshot) GetStateDelta(blockNumber uint64) (*statemgmt.StateDelta, error) {
size, err := sledger.GetBlockchainSize()
if err != nil {
return nil, err
}
if blockNumber >= size {
return nil, ErrOutOfBounds
}
stateDeltaBytes, err := sledger.GetFromStateDeltaCFSnapshot(util.EncodeUint64(blockNumber))
if err != nil {
return nil, err
}
if len(stateDeltaBytes) == 0 {
return nil, nil
}
stateDelta := statemgmt.NewStateDelta()
err = stateDelta.Unmarshal(stateDeltaBytes)
return stateDelta, err
}
func (sledger *LedgerSnapshot) GetStateSnapshot() (*state.StateSnapshot, error) {
blockHeight, err := sledger.GetBlockchainSize()
if err != nil {
return nil, err
}
if 0 == blockHeight {
return nil, fmt.Errorf("Blockchain has no blocks, cannot determine block number")
}
itr, err := sledger.stateM.GetStateSnapshotIterator(sledger.DBSnapshot)
if err != nil {
return nil, err
}
return state.NewStateSnapshot(blockHeight-1, itr, sledger.DBSnapshot), nil
}
func (ledger *Ledger) GetLedgerStatus() *protos.LedgerState {
ledger.readCache.RLock()
defer ledger.readCache.RUnlock()
ret := new(protos.LedgerState)
ret.States = ledger.snapshots.GetStableIndex()
ret.Height = ledger.blockchain.getContinuousBlockHeight() + 1
if ret.Height > 0 {
blk, _ := ledger.blockchain.getRawBlock(ret.Height - 1)
ret.HeadBlock, _ = blk.GetHash()
}
return ret
}
func (ledger *Ledger) CreateSyncingSnapshot(shash []byte) (*LedgerSnapshot, error) {
ledger.readCache.RLock()
defer ledger.readCache.RUnlock()
blkn, err := ledger.index.fetchBlockNumberByStateHash(shash)
if err != nil {
return nil, err
}
//check current first
if blkn+1 == ledger.snapshots.currentHeight {
return &LedgerSnapshot{ledger.state.GetSnapshotManager(), ledger.snapshots.GetSnapshotCurrent()}, nil
}
if sn, snblkn := ledger.snapshots.GetSnapshot(blkn); sn != nil {
if snblkn != blkn {
sn.Release()
return nil, fmt.Errorf("request state [%X] is not stable (found blocknumber is %d but not cached [closed set is %d])", shash, blkn, snblkn)
}
return &LedgerSnapshot{ledger.state.GetSnapshotManager(), sn}, nil
}
return nil, fmt.Errorf("request state [%X] is not avaliable", shash)
}
// for state syncing, proving the state hash and block number which can be requested
// for syncing in a async process. That is, a syncing request for this state will
// be raised sometime later and ledger ensure that it will be still available in
// high possibility (i.e. not flushed by the updating of current state or other reasons)
// TODO: use snapshot manager for syncing is not complete correct beacuse syncer
// may lost their available state because all nodes have trunced the target state,
// (consider a node is syncing some state A from another node B and B is off-line
// in the process, and time has passed so no other nodes will keep the state which A
// wish to sync) later we should use checkpoint for state syncing and a long hold node
// should be provided for alleviating this problem
func (ledger *Ledger) GetStableSyncingState() *protos.StateFilter {
ledger.readCache.RLock()
defer ledger.readCache.RUnlock()
return ledger.snapshots.GetStableIndex()
}
// create a snapshot wrapper for current db, the corresponding blocknumber is also returned
// for verifing
// this snapshot is not enusred to sync with state and should be only used for block syncing
// and indexing should be avoiding used
func (ledger *Ledger) CreateSnapshot() (*LedgerSnapshot, uint64) {
ledger.readCache.RLock()
defer ledger.readCache.RUnlock()
if ledger.blockchain.getSize() == 0 {
//no snapshot because we have no block yet
return nil, 0
}
return &LedgerSnapshot{ledger.state.GetSnapshotManager(), ledger.snapshots.GetSnapshotCurrent()},
ledger.blockchain.getSize() - 1
}
// ----- will be deprecated -----
// GetStateSnapshot returns a point-in-time view of the global state for the current block. This
// should be used when transferring the state from one peer to another peer. You must call
// stateSnapshot.Release() once you are done with the snapshot to free up resources.
func (ledger *Ledger) GetStateSnapshot() (*state.StateSnapshot, error) {
snapshotL, blockHeight := ledger.CreateSnapshot()
if snapshotL == nil {
return nil, fmt.Errorf("Blockchain has no blocks")
}
return ledger.state.GetSnapshot(blockHeight, snapshotL.DBSnapshot)
}
func indexState(stateHash []byte) string {
if l := len(stateHash); l == 0 {
return "#NIL"
} else if l < 20 { //the first 160bit is used
return fmt.Sprintf("#%X", stateHash)
} else {
return fmt.Sprintf("#%X", stateHash[:20])
}
}
//maintain a series of snapshot for state querying
type ledgerHistory struct {
db *db.OpenchainDB
snapshotInterval int
beginIntervalNum uint64
currentHeight uint64
currentState []byte
snsIndexed [][]byte
stableStatus *protos.StateFilter
}
//we should not keep too many snapshot or scaliablity problem will raise in rocksdb,
//see the doc for detail: https://github.com/facebook/rocksdb/wiki/Snapshot
const defaultSnapshotTotal = 8
const defaultSnapshotInterval = 16
const currentSNTag = "#current"
func initNewLedgerSnapshotManager(odb *db.OpenchainDB, s *stateWrapper, config *ledgerConfig) *ledgerHistory {
lsm := new(ledgerHistory)
//TODO: read config
lsm.snapshotInterval = defaultSnapshotInterval
lsm.snsIndexed = make([][]byte, defaultSnapshotTotal)
lsm.stableStatus = new(protos.StateFilter)
filterEff := lsm.stableStatus.InitAuto(uint(len(lsm.snsIndexed)))
if fr := filterEff * float64(100); fr > float64(1.0) {
ledgerLogger.Warningf("The false positive ratio of filter is larger than 1% (%f)", fr)
}
lsm.db = odb
if s.isSyncing() {
ledgerLogger.Info("State is syncing, not cache current snapshots")
return lsm
}
if s.cache.refHeight > 0 {
lsm.currentHeight = s.cache.refHeight
curBlk := lsm.currentHeight - 1
lsm.beginIntervalNum = curBlk / uint64(lsm.snapshotInterval)
shash := s.cache.refHash
if int(curBlk)%lsm.snapshotInterval == 0 {
ledgerLogger.Debugf("cached state at the beginning [%X]@%d", shash, curBlk)
lsm.currentState = shash
lsm.snapshotCurrent(shash)
}
if oldSN := odb.ManageSnapshot(currentSNTag); oldSN != nil {
oldSN.Release()
}
}
return lsm
}
func (lh *ledgerHistory) GetStableIndex() *protos.StateFilter {
ret := new(protos.StateFilter)
ret.Filter = lh.stableStatus.Filter
ret.HashCounts = lh.stableStatus.HashCounts
return ret
}
func (lh *ledgerHistory) GetSnapshotCurrent() *db.DBSnapshot {
sn := lh.db.GetManagedSnapshot(currentSNTag)
if sn == nil {
ledgerLogger.Warningf("Current snapshot not managed, create it instead")
return lh.db.GetSnapshot()
}
return sn
}
func (lh *ledgerHistory) GetSnapshot(blknum uint64) (sn *db.DBSnapshot, blkn uint64) {
sni, blkn := lh.historyIndex(blknum)
if sni == -1 {
return nil, blkn
} else if pickS := lh.snsIndexed[sni]; len(pickS) > 0 {
return lh.db.GetManagedSnapshot(indexState(pickS)), blkn
} else {
return nil, blkn
}
}
func (lh *ledgerHistory) historyIndex(blknum uint64) (int, uint64) {
if lh.currentHeight <= 1 {
//we have no history yet
return -1, blknum
} else if blknum+1 >= lh.currentHeight {
blknum = lh.currentHeight - 2
}
sec := blknum / uint64(lh.snapshotInterval)
if sec < lh.beginIntervalNum {
return -1, blknum
} else {
corblk := sec * uint64(lh.snapshotInterval)
return int(sec % uint64(len(lh.snsIndexed))), corblk
}
}
func (lh *ledgerHistory) snapshotCurrent(shash []byte) {
duplicatedSn := lh.db.ManageSnapshot(indexState(shash))
if duplicatedSn != nil {
ledgerLogger.Warningf("We have duplidated snapshot in state %X and release it", shash)
duplicatedSn.Release()
}
//also add current state into bloom filter
lh.stableStatus.Add(shash)
}
func (lh *ledgerHistory) UpdateR(blknum uint64, shash []byte) {
lh.Update(shash, blknum)
}
func (lh *ledgerHistory) Update(shash []byte, blknum uint64) {
//out-of-order updating is allowed only if blknum is not exceed the current bound
sec := blknum / uint64(lh.snapshotInterval)
if sec < lh.beginIntervalNum {
ledgerLogger.Errorf("Try update too small blocknumber %d (current least bound is %d), not accept", blknum, lh.beginIntervalNum*uint64(lh.snapshotInterval))
return
} else if blknum+1 == lh.currentHeight {
ledgerLogger.Errorf("duplicated blocknumber %d, not accept", blknum)
return
}
ledgerLogger.Debugf("updating state [%X]@%d (now height %d)", shash, blknum, lh.currentHeight)
if blknum < lh.currentHeight {
//to see if we can cached it, and exit
//notice: it is ok when the modulo is expected to be 0, but not other integer
//(go get minus result for a negitave integer being modulo)
if int(blknum)%lh.snapshotInterval == 0 {
indx := int(sec % uint64(len(lh.snsIndexed)))
if s := lh.snsIndexed[indx]; len(s) == 0 {
lh.snapshotCurrent(shash)
lh.snsIndexed[indx] = shash
ledgerLogger.Debugf("kept out-of-order state [%X]@%d", shash, blknum)
} //or it should has been cached, we just duplicated, TODO: sanity check?
}
return
}
//so we forwarded
cacheCurrent := int(blknum)%lh.snapshotInterval == 0
//if current blknum has "push" the history range forward, we still have some works
//to do
if lh.beginIntervalNum+uint64(len(lh.snsIndexed)) <= sec {
oldBegin := lh.beginIntervalNum
if !cacheCurrent {
lh.beginIntervalNum = sec - uint64(len(lh.snsIndexed)) + 1
} else {
//NOTICE: this is a trick: we cache the "edge" block at currentState
//and not put it into history now, so we will not "waste" a slot
//which will point to current height
lh.beginIntervalNum = sec - uint64(len(lh.snsIndexed))
}
if oldBegin < lh.beginIntervalNum {
ledgerLogger.Debugf("begin range has forward to [%d] (%d in section)", lh.beginIntervalNum*uint64(lh.snapshotInterval), lh.beginIntervalNum)
for i := oldBegin; i < lh.beginIntervalNum; i++ {
//now we must clean the current snapshot we kept in the old history
dropS := lh.snsIndexed[int(i)%len(lh.snsIndexed)]
lh.snsIndexed[int(i)%len(lh.snsIndexed)] = nil
lh.db.UnManageSnapshot(indexState(dropS))
ledgerLogger.Debugf("history snapshot [%X]@<%d> has been dropped", dropS, i)
}
//rebuild bloom filter
lh.stableStatus.ResetFilter()
for _, h := range lh.snsIndexed {
if len(h) > 0 {
lh.stableStatus.Add(h)
}
}
}
}
//now, keep the cache of previous "current" if we have one
if len(lh.currentState) > 0 {
//keep last "current" snapshot we have cached first
sec := (lh.currentHeight - 1) / uint64(lh.snapshotInterval)
//CAUTION: we may forward too fast so the cached has to be drop ...
if sec < lh.beginIntervalNum {
ledgerLogger.Infof("we have dropped a cached snapshot [%X]", lh.currentState)
lh.db.UnManageSnapshot(indexState(lh.currentState))
} else {
indx := int(sec % uint64(len(lh.snsIndexed)))
//sanity check
if len(lh.snsIndexed[indx]) > 0 {
panic("wrong code: forwarding do not clean current slot yet")
}
lh.snsIndexed[indx] = lh.currentState
ledgerLogger.Debugf("we have kept a new snapshot [%X]", lh.currentState)
}
lh.currentState = nil
}
if cacheCurrent {
lh.snapshotCurrent(shash)
lh.currentState = shash
ledgerLogger.Debugf("cache current snapshot [%X]@%d", lh.currentState, blknum)
}
//also cache another copy of current db
oldSN := lh.db.ManageSnapshot(currentSNTag)
if oldSN != nil {
oldSN.Release()
}
lh.currentHeight = blknum + 1
}