-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain_indexes_async.go
executable file
·294 lines (253 loc) · 9.28 KB
/
blockchain_indexes_async.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 ledger
import (
"fmt"
"sync"
"github.com/openblockchain/obc-peer/openchain/db"
"github.com/openblockchain/obc-peer/protos"
"github.com/tecbot/gorocksdb"
)
var lastIndexedBlockKey = []byte{byte(0)}
type blockWrapper struct {
block *protos.Block
blockNumber uint64
blockHash []byte
stopNow bool
}
type blockchainIndexerAsync struct {
blockchain *blockchain
// Channel for transferring block from block chain for indexing
blockChan chan blockWrapper
indexerState *blockchainIndexerState
}
func newBlockchainIndexerAsync() *blockchainIndexerAsync {
return new(blockchainIndexerAsync)
}
func (indexer *blockchainIndexerAsync) isSynchronous() bool {
return false
}
func (indexer *blockchainIndexerAsync) start(blockchain *blockchain) error {
indexer.blockchain = blockchain
indexerState, err := newBlockchainIndexerState(indexer)
if err != nil {
return err
}
indexer.indexerState = indexerState
indexLogger.Debug("staring indexer, lastIndexedBlockNum = [%d]",
indexer.indexerState.getLastIndexedBlockNumber())
err = indexer.indexPendingBlocks()
if err != nil {
return err
}
indexLogger.Debug("staring indexer, lastIndexedBlockNum = [%d] after processing pending blocks",
indexer.indexerState.getLastIndexedBlockNumber())
indexer.blockChan = make(chan blockWrapper)
go func() {
for {
indexLogger.Debug("Going to wait on channel for next block to index")
blockWrapper := <-indexer.blockChan
indexLogger.Debug("Blockwrapper received on channel: block number = [%d]", blockWrapper.blockNumber)
if blockWrapper.stopNow {
indexLogger.Debug("stop command received on channel. Closing channel")
close(indexer.blockChan)
return
}
if indexer.indexerState.hasError() {
indexLogger.Debug("Not indexing block number [%d]. Because of previous error: %s.",
blockWrapper.blockNumber, indexer.indexerState.getError())
continue
}
err := indexer.createIndexesInternal(blockWrapper.block, blockWrapper.blockNumber, blockWrapper.blockHash)
if err != nil {
indexer.indexerState.setError(err)
indexLogger.Debug(
"Error occured while indexing block number [%d]. Error: %s. Further blocks will not be indexed",
blockWrapper.blockNumber, err)
} else {
indexLogger.Debug("Finished indexing block number [%d]", blockWrapper.blockNumber)
}
}
}()
return nil
}
func (indexer *blockchainIndexerAsync) createIndexesSync(
block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error {
return fmt.Errorf("Method not applicable")
}
func (indexer *blockchainIndexerAsync) createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error {
indexer.blockChan <- blockWrapper{block, blockNumber, blockHash, false}
return nil
}
// createIndexes adds entries into db for creating indexes on various atributes
func (indexer *blockchainIndexerAsync) createIndexesInternal(block *protos.Block, blockNumber uint64, blockHash []byte) error {
openchainDB := db.GetDBHandle()
writeBatch := gorocksdb.NewWriteBatch()
defer writeBatch.Destroy()
addIndexDataForPersistence(block, blockNumber, blockHash, writeBatch)
writeBatch.PutCF(openchainDB.IndexesCF, lastIndexedBlockKey, encodeBlockNumber(blockNumber))
opt := gorocksdb.NewDefaultWriteOptions()
defer opt.Destroy()
err := openchainDB.DB.Write(opt, writeBatch)
if err != nil {
return err
}
indexer.indexerState.blockIndexed(blockNumber)
return nil
}
func (indexer *blockchainIndexerAsync) fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error) {
err := indexer.indexerState.checkError()
if err != nil {
return 0, err
}
indexer.indexerState.waitForLastCommittedBlock()
return fetchBlockNumberByBlockHashFromDB(blockHash)
}
func (indexer *blockchainIndexerAsync) fetchTransactionIndexByUUID(txUUID string) (uint64, uint64, error) {
err := indexer.indexerState.checkError()
if err != nil {
return 0, 0, err
}
indexer.indexerState.waitForLastCommittedBlock()
return fetchTransactionIndexByUUIDFromDB(txUUID)
}
func (indexer *blockchainIndexerAsync) indexPendingBlocks() error {
blockchain := indexer.blockchain
if blockchain.getSize() == 0 {
// chain is empty as yet
return nil
}
lastCommittedBlockNum := blockchain.getSize() - 1
lastIndexedBlockNum := indexer.indexerState.getLastIndexedBlockNumber()
if lastCommittedBlockNum == lastIndexedBlockNum {
// all committed blocks are indexed
return nil
}
for ; lastIndexedBlockNum < lastCommittedBlockNum; lastIndexedBlockNum++ {
blockNumToIndex := lastIndexedBlockNum + 1
blockToIndex, errBlockFetch := blockchain.getBlock(blockNumToIndex)
if errBlockFetch != nil {
return errBlockFetch
}
blockHash, errBlockHash := blockToIndex.GetHash()
if errBlockHash != nil {
return errBlockHash
}
indexer.createIndexesInternal(blockToIndex, blockNumToIndex, blockHash)
}
return nil
}
func (indexer *blockchainIndexerAsync) stop() {
indexer.indexerState.waitForLastCommittedBlock()
indexer.blockChan <- blockWrapper{nil, 0, nil, true}
}
// Code related to tracking the block number that has been indexed
// and if there has been an error in indexing a block
// Since, we index blocks asynchronously, there may be a case when
// a client query arrives before a block has been indexed.
//
// Do we really need strict symantics such that an index query results
// should include upto block number (or higher) that may have been committed
// when user query arrives?
// If a delay of a couple of blocks are allowed, we can get rid of this synchronization stuff
type blockchainIndexerState struct {
indexer *blockchainIndexerAsync
zerothBlockIndexed bool
lastBlockIndexed uint64
err error
lock *sync.RWMutex
newBlockIndexed *sync.Cond
}
func newBlockchainIndexerState(indexer *blockchainIndexerAsync) (*blockchainIndexerState, error) {
var lock sync.RWMutex
zerothBlockIndexed, lastIndexedBlockNum, err := fetchLastIndexedBlockNumFromDB()
if err != nil {
return nil, err
}
return &blockchainIndexerState{indexer, zerothBlockIndexed, lastIndexedBlockNum, nil, &lock, sync.NewCond(&lock)}, nil
}
func (indexerState *blockchainIndexerState) blockIndexed(blockNumber uint64) {
indexerState.newBlockIndexed.L.Lock()
defer indexerState.newBlockIndexed.L.Unlock()
indexerState.lastBlockIndexed = blockNumber
indexerState.zerothBlockIndexed = true
indexerState.newBlockIndexed.Broadcast()
}
func (indexerState *blockchainIndexerState) getLastIndexedBlockNumber() uint64 {
indexerState.lock.RLock()
defer indexerState.lock.RUnlock()
return indexerState.lastBlockIndexed
}
func (indexerState *blockchainIndexerState) waitForLastCommittedBlock() (err error) {
chain := indexerState.indexer.blockchain
if err != nil || chain.getSize() == 0 {
return
}
lastBlockCommitted := chain.getSize() - 1
indexerState.newBlockIndexed.L.Lock()
defer indexerState.newBlockIndexed.L.Unlock()
if !indexerState.zerothBlockIndexed {
indexLogger.Debug(
"Waiting for zeroth block to be indexed. lastBlockCommitted=[%d] and lastBlockIndexed=[%d]",
lastBlockCommitted, indexerState.lastBlockIndexed)
indexerState.newBlockIndexed.Wait()
}
for indexerState.lastBlockIndexed < lastBlockCommitted {
indexLogger.Debug(
"Waiting for index to catch up with block chain. lastBlockCommitted=[%d] and lastBlockIndexed=[%d]",
lastBlockCommitted, indexerState.lastBlockIndexed)
indexerState.newBlockIndexed.Wait()
}
return
}
func (indexerState *blockchainIndexerState) setError(err error) {
indexerState.lock.Lock()
defer indexerState.lock.Unlock()
indexerState.err = err
}
func (indexerState *blockchainIndexerState) hasError() bool {
indexerState.lock.RLock()
defer indexerState.lock.RUnlock()
return indexerState.err != nil
}
func (indexerState *blockchainIndexerState) getError() error {
indexerState.lock.RLock()
defer indexerState.lock.RUnlock()
return indexerState.err
}
func (indexerState *blockchainIndexerState) checkError() error {
indexerState.lock.RLock()
defer indexerState.lock.RUnlock()
if indexerState.err != nil {
return fmt.Errorf(
"An error had occured during indexing block number [%d]. So, index is out of sync. Detail of the error = %s",
indexerState.getLastIndexedBlockNumber()+1, indexerState.err)
}
return indexerState.err
}
func fetchLastIndexedBlockNumFromDB() (zerothBlockIndexed bool, lastIndexedBlockNum uint64, err error) {
lastIndexedBlockNumberBytes, err := db.GetDBHandle().GetFromIndexesCF(lastIndexedBlockKey)
if err != nil {
return
}
if lastIndexedBlockNumberBytes == nil {
return
}
lastIndexedBlockNum = decodeBlockNumber(lastIndexedBlockNumberBytes)
zerothBlockIndexed = true
return
}