-
Notifications
You must be signed in to change notification settings - Fork 189
/
dbsync_multi.go
194 lines (151 loc) · 5.88 KB
/
dbsync_multi.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
// Copyright 2019 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package dbsyncer
import (
"errors"
"github.com/klaytn/klaytn/blockchain/types"
"strings"
"time"
)
func (ds *DBSyncer) HandleChainEventParallel(block *types.Block) error {
logger.Info("dbsyncer(multi) HandleChainEvent", "number", block.Number(), "txs", block.Transactions().Len())
bulkInsertQuerys := []*BulkInsertQuery{}
startblock := time.Now()
bulkInsertQuerys, err := ds.parallelSyncBlockHeader(block)
if err != nil {
logger.Error("fail to sync block", "block", block.Number(), "err", err)
return err
}
blocktime := time.Since(startblock)
starttx := time.Now()
receipttime := time.Duration(0)
if block.Transactions().Len() > 0 {
startrceipt := time.Now()
receipts := ds.blockchain.GetReceiptsByBlockHash(block.Hash())
receipttime = time.Since(startrceipt)
bulkInsertQuerys, err = ds.parallelSyncTransactions(block, receipts, bulkInsertQuerys)
if err != nil {
logger.Error("fail to sync transaction", "block", block.Number(), "err", err)
return err
}
}
txtime := time.Since(starttx)
startInsert := time.Now()
queryResult := make(chan *BulkInsertResult, len(bulkInsertQuerys))
ds.queryEngine.executeBulkInsert(bulkInsertQuerys, queryResult)
totalResult := 0
defer close(queryResult)
RESULT:
for result := range queryResult {
if result.err != nil {
return result.err
}
totalResult++
if totalResult == len(bulkInsertQuerys) {
break RESULT
}
}
insertTime := time.Since(startInsert)
totalTime := time.Since(startblock)
if ds.logMode {
logger.Info("dbsyncer(multi) time", "number", block.Number(), "block", blocktime, "txs", txtime, "receipts", receipttime, "insert", insertTime, "total", totalTime)
}
return nil
}
func (ds *DBSyncer) parallelSyncBlockHeader(block *types.Block) ([]*BulkInsertQuery, error) {
blockStr := ds.blockInsertQuery + ","
vals := []interface{}{}
bulkInsertQuerys := []*BulkInsertQuery{}
proposerAddr, committeeAddrs, err := getProposerAndValidatorsFromBlock(block)
if err != nil {
return nil, err
}
totalTx := block.Transactions().Len()
committee := strings.ToLower(committeeAddrs)
gasUsed := block.Header().GasUsed
gasPrice := ds.blockchain.Config().UnitPrice
hash := block.Header().Hash().Hex()
number := block.Header().Number.Uint64()
parentHash := block.Header().ParentHash.Hex()
proposer := strings.ToLower(proposerAddr)
reward := block.Header().Rewardbase.Hex()
size := block.Size()
timestamp := block.Header().Time.String()
timestampFos := block.Header().TimeFoS
vals = append(vals, totalTx, committee, gasUsed, gasPrice, hash,
number, parentHash, proposer, reward, size, timestamp, timestampFos)
bulkInsertQuerys = append(bulkInsertQuerys, &BulkInsertQuery{blockStr, vals, block.NumberU64(), 1})
return bulkInsertQuerys, nil
}
func (ds *DBSyncer) parallelSyncTransactions(block *types.Block, receipts types.Receipts, bulkInsertQuerys []*BulkInsertQuery) ([]*BulkInsertQuery, error) {
txStr, vals, insertCount := ds.resetTxParameter()
summaryStr, summaryVals, summaryInsertCount := ds.resetSummaryParameter()
txMapStr, txMapVals, txMapInsertCount := ds.resetTxMapParameter()
txLen := block.Transactions().Len()
result := make(chan *MakeQueryResult, txLen)
if txLen != receipts.Len() {
logger.Error("transactions is not matched receipts", "txs", txLen, "receipts", receipts.Len())
return nil, errors.New("transaction count is not matched receipts")
}
ds.queryEngine.insertTransactions(block, block.Transactions(), receipts, result)
totalTxs := 0
defer close(result)
QUERY:
for record := range result {
if record.err != nil {
return nil, record.err
}
txStr += record.cols + ","
vals = append(vals, record.val...)
insertCount++
if insertCount >= ds.bulkInsertSize {
bulkInsertQuerys = append(bulkInsertQuerys, &BulkInsertQuery{txStr, vals, block.NumberU64(), insertCount})
txStr, vals, insertCount = ds.resetTxParameter()
}
if record.count == 1 {
summaryStr += record.scols + ","
summaryVals = append(summaryVals, record.sval...)
summaryInsertCount++
}
if summaryInsertCount >= ds.bulkInsertSize {
bulkInsertQuerys = append(bulkInsertQuerys, &BulkInsertQuery{summaryStr, summaryVals, block.NumberU64(), summaryInsertCount})
summaryStr, summaryVals, summaryInsertCount = ds.resetTxParameter()
}
if record.tcount == 1 {
txMapStr += record.tcols + ","
txMapVals = append(txMapVals, record.tval...)
txMapInsertCount++
}
if txMapInsertCount >= ds.bulkInsertSize {
bulkInsertQuerys = append(bulkInsertQuerys, &BulkInsertQuery{txMapStr, txMapVals, block.NumberU64(), txMapInsertCount})
txMapStr, txMapVals, txMapInsertCount = ds.resetTxMapParameter()
}
totalTxs++
if totalTxs == block.Transactions().Len() {
break QUERY
}
}
if insertCount > 0 {
bulkInsertQuerys = append(bulkInsertQuerys, &BulkInsertQuery{txStr, vals, block.NumberU64(), insertCount})
}
if summaryInsertCount > 0 {
bulkInsertQuerys = append(bulkInsertQuerys, &BulkInsertQuery{summaryStr, summaryVals, block.NumberU64(), summaryInsertCount})
}
if txMapInsertCount > 0 {
bulkInsertQuerys = append(bulkInsertQuerys, &BulkInsertQuery{txMapStr, txMapVals, block.NumberU64(), txMapInsertCount})
}
return bulkInsertQuerys, nil
}