This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
import_manager.go
240 lines (215 loc) · 5.24 KB
/
import_manager.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
package pilosa
import (
"fmt"
"io"
"sort"
"time"
"github.com/pkg/errors"
)
type recordImportManager struct {
client *Client
}
func newRecordImportManager(client *Client) *recordImportManager {
return &recordImportManager{
client: client,
}
}
type importWorkerChannels struct {
records <-chan []Record
errs chan<- error
status chan<- ImportStatusUpdate
}
func (rim recordImportManager) run(field *Field, iterator RecordIterator, options ImportOptions) error {
shardWidth := field.index.shardWidth
threadCount := uint64(options.threadCount)
recordChans := make([]chan []Record, threadCount)
recordBufs := make([][]Record, threadCount)
errChan := make(chan error)
recordErrChan := make(chan error, 1)
statusChan := options.statusChan
if options.importRecordsFunction == nil {
return errors.New("importRecords function is required")
}
for i := range recordChans {
recordChans[i] = make(chan []Record, 16)
recordBufs[i] = make([]Record, 0, 16)
chans := importWorkerChannels{
records: recordChans[i],
errs: errChan,
status: statusChan,
}
go recordImportWorker(i, rim.client, field, chans, &options)
}
var importErr error
done := uint64(0)
go func(it RecordIterator) {
var record Record
var err error
for {
record, err = it.NextRecord()
if err != nil {
if err == io.EOF {
err = nil
}
break
}
shard := record.Shard(shardWidth)
idx := shard % threadCount
recordBufs[idx] = append(recordBufs[idx], record)
if len(recordBufs[idx]) == cap(recordBufs[idx]) {
recordChans[idx] <- recordBufs[idx]
recordBufs[idx] = make([]Record, 0, 16)
}
}
// send any trailing data
for idx, buf := range recordBufs {
if len(buf) > 0 {
recordChans[idx] <- buf
recordBufs[idx] = nil
}
}
recordErrChan <- err
}(iterator)
sendRecords:
for done < threadCount {
select {
case workerErr := <-errChan:
done++
if workerErr != nil {
importErr = workerErr
break sendRecords
}
case recordErr := <-recordErrChan:
for _, q := range recordChans {
close(q)
}
if recordErr != nil {
importErr = recordErr
break sendRecords
}
}
}
return importErr
}
func recordImportWorker(id int, client *Client, field *Field, chans importWorkerChannels, options *ImportOptions) {
var err error
batchForShard := map[uint64][]Record{}
statusChan := chans.status
recordChan := chans.records
errChan := chans.errs
shardNodes := map[uint64][]fragmentNode{}
defer func() {
if r := recover(); r != nil {
if err == nil {
err = fmt.Errorf("worker %d panic: %v", id, r)
}
}
errChan <- err
}()
state := &importState{}
recordCount := 0
batchSize := options.batchSize
shardWidth := field.index.shardWidth
readRecords:
for recordBatch := range recordChan {
// It's fine to overrun our allowed batch size slightly, and
// we don't want to generate separate batches for part of a
// 16-item batch.
for _, record := range recordBatch {
recordCount++
shard := record.Shard(shardWidth)
if batchForShard[shard] == nil {
batchForShard[shard] = make([]Record, 0, batchSize)
}
batchForShard[shard] = append(batchForShard[shard], record)
}
if recordCount >= batchSize {
for shard, records := range batchForShard {
if len(records) == 0 {
continue
}
err = importRecords(id, client, field, shardNodes, shard, records, options, statusChan, state)
if err != nil {
break readRecords
}
delete(batchForShard, shard)
}
recordCount = 0
}
}
if err != nil {
return
}
// import remaining records
for shard, records := range batchForShard {
if len(records) == 0 {
continue
}
err = importRecords(id, client, field, shardNodes, shard, records, options, statusChan, state)
if err != nil {
break
}
}
}
func importRecords(id int, client *Client, field *Field,
shardNodes map[uint64][]fragmentNode,
shard uint64,
records []Record,
options *ImportOptions,
statusChan chan<- ImportStatusUpdate,
state *importState) error {
var nodes []fragmentNode
var ok bool
var err error
importFun := options.importRecordsFunction
if nodes, ok = shardNodes[shard]; !ok {
// if the data has row or column keys, send the data only to the coordinator
if field.index.options.keys || field.options.keys {
node, err := client.fetchCoordinatorNode()
if err != nil {
return err
}
nodes = []fragmentNode{node}
} else {
nodes, err = client.fetchFragmentNodes(field.index.name, shard)
if err != nil {
return err
}
}
}
tic := time.Now()
if !options.skipSort {
sort.Sort(recordSort(records))
}
err = importFun(field, shard, records, nodes, options, state)
if err != nil {
return err
}
took := time.Since(tic)
if statusChan != nil {
statusChan <- ImportStatusUpdate{
ThreadID: id,
Shard: shard,
ImportedCount: len(records),
Time: took,
}
}
return nil
}
// ImportStatusUpdate contains the import progress information.
type ImportStatusUpdate struct {
ThreadID int
Shard uint64
ImportedCount int
Time time.Duration
}
type recordSort []Record
func (rc recordSort) Len() int {
return len(rc)
}
func (rc recordSort) Swap(i, j int) {
rc[i], rc[j] = rc[j], rc[i]
}
func (rc recordSort) Less(i, j int) bool {
return rc[i].Less(rc[j])
}