/
query_engine.go
195 lines (170 loc) · 5.32 KB
/
query_engine.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
// 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 (
"github.com/klaytn/klaytn/blockchain/types"
)
type MakeQueryRequest struct {
block *types.Block
blockBase uint64
txIndexBase int
txs []*types.Transaction
receipts []*types.Receipt
inc int
result chan *MakeQueryResult
}
type BulkInsertRequest struct {
bulkInserts []*BulkInsertQuery
inc int
result chan *BulkInsertResult
}
type BulkInsertResult struct {
err error
}
type MakeQueryResult struct {
block *types.Block
cols string
val []interface{}
scols string
sval []interface{}
count int
tcols string
tval []interface{}
tcount int
err error
}
// QueryEngine is a helper structure to concurrently making insert query
type QueryEngine struct {
ds *DBSyncer
taskQueue int
insertQueue int
tasks chan *MakeQueryRequest
inserts chan *BulkInsertRequest
}
func newQueryEngine(ds *DBSyncer, taskQueue int, insertQueue int) *QueryEngine {
queryEngine := &QueryEngine{
ds: ds,
tasks: make(chan *MakeQueryRequest, taskQueue),
inserts: make(chan *BulkInsertRequest, insertQueue),
taskQueue: taskQueue,
insertQueue: insertQueue,
}
for i := 0; i < taskQueue; i++ {
go queryEngine.processing()
}
for i := 0; i < insertQueue; i++ {
go queryEngine.executing()
}
return queryEngine
}
func (qe *QueryEngine) make(block *types.Block, txKey uint64, tx *types.Transaction, receipt *types.Receipt, result chan *MakeQueryResult) {
cols, val, txMapArg, summaryArg, err := MakeTxDBRow(block, txKey, tx, receipt)
scols, sval, count, serr := MakeSummaryDBRow(summaryArg)
tcols, tval, tcount, terr := MakeTxMappingRow(txMapArg)
defer func() {
// recover from panic caused by writing to a closed channel
if r := recover(); r != nil {
logger.Error("channel closed", "err", r)
return
}
}()
if err == nil && serr == nil && terr == nil {
result <- &MakeQueryResult{block, cols, val, scols, sval, count, tcols, tval, tcount, nil}
} else {
if err != nil {
logger.Error("fail to make row (tx)", "err", err)
result <- &MakeQueryResult{block, cols, val, scols, sval, count, tcols, tval, tcount, err}
}
if serr != nil {
logger.Error("fail to make row (summary)", "err", serr)
result <- &MakeQueryResult{block, cols, val, scols, sval, count, tcols, tval, tcount, serr}
}
if terr != nil {
logger.Error("fail to make row (senderHash)", "err", terr)
result <- &MakeQueryResult{block, cols, val, scols, sval, count, tcols, tval, tcount, terr}
}
}
}
func (qe *QueryEngine) execute(insertQuery *BulkInsertQuery, result chan *BulkInsertResult) {
defer func() {
// recover from panic caused by writing to a closed channel
if r := recover(); r != nil {
logger.Error("channel closed", "err", r)
return
}
}()
if err := qe.ds.bulkInsert(insertQuery.parameters, insertQuery.vals, insertQuery.blockNumber, insertQuery.insertCount); err != nil {
logger.Error("fail to bulkinsert (tx/summary/senderHash)", "err", err)
result <- &BulkInsertResult{err}
}
result <- &BulkInsertResult{nil}
}
func (qe *QueryEngine) processing() {
for task := range qe.tasks {
for i := 0; i < len(task.txs); i += task.inc {
txKey := task.blockBase + uint64(task.txIndexBase+i)
qe.make(task.block, txKey, task.txs[i], task.receipts[i], task.result)
}
}
}
func (qe *QueryEngine) executing() {
for insert := range qe.inserts {
for i := 0; i < len(insert.bulkInserts); i += insert.inc {
qe.execute(insert.bulkInserts[i], insert.result)
}
}
}
func (qe *QueryEngine) insertTransactions(block *types.Block, txs types.Transactions, receipts types.Receipts, result chan *MakeQueryResult) {
// If there's nothing to recover, abort
if len(txs) == 0 {
return
}
blockBase := block.NumberU64() * TX_KEY_FACTOR
// Ensure we have meaningful task sizes and schedule the recoveries
tasks := qe.taskQueue
if len(txs) < tasks*4 {
tasks = (len(txs) + 3) / 4
}
for i := 0; i < tasks; i++ {
qe.tasks <- &MakeQueryRequest{
block: block,
blockBase: blockBase,
txIndexBase: i,
txs: txs[i:],
receipts: receipts[i:],
inc: tasks,
result: result,
}
}
}
func (qe *QueryEngine) executeBulkInsert(bulkInserts []*BulkInsertQuery, result chan *BulkInsertResult) {
// If there's nothing to recover, abort
if len(bulkInserts) == 0 {
return
}
// Ensure we have meaningful task sizes and schedule the recoveries
tasks := qe.insertQueue
if len(bulkInserts) < tasks {
tasks = len(bulkInserts)
}
for i := 0; i < tasks; i++ {
qe.inserts <- &BulkInsertRequest{
bulkInserts: bulkInserts[i:],
inc: tasks,
result: result,
}
}
}