forked from FeatureBaseDB/go-pilosa
/
import_manager.go
219 lines (192 loc) · 4.5 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
package pilosa
import (
"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 := options.shardWidth
threadCount := uint64(options.threadCount)
recordChans := make([]chan Record, threadCount)
errChan := make(chan error)
statusChan := options.statusChan
if options.importRecordsFunction == nil {
return errors.New("importRecords function is required")
}
for i := range recordChans {
recordChans[i] = make(chan Record, options.batchSize)
chans := importWorkerChannels{
records: recordChans[i],
errs: errChan,
status: statusChan,
}
go recordImportWorker(i, rim.client, field, chans, options)
}
var record Record
var recordIteratorError error
for {
record, recordIteratorError = iterator.NextRecord()
if recordIteratorError != nil {
if recordIteratorError == io.EOF {
recordIteratorError = nil
}
break
}
shard := record.Shard(shardWidth)
recordChans[shard%threadCount] <- record
}
for _, q := range recordChans {
close(q)
}
if recordIteratorError != nil {
return recordIteratorError
}
done := uint64(0)
for {
select {
case workerErr := <-errChan:
if workerErr != nil {
return workerErr
}
done += 1
if done == threadCount {
return nil
}
}
}
}
func recordImportWorker(id int, client *Client, field *Field, chans importWorkerChannels, options ImportOptions) {
batchForShard := map[uint64][]Record{}
importFun := options.importRecordsFunction
statusChan := chans.status
recordChan := chans.records
errChan := chans.errs
shardNodes := map[uint64][]fragmentNode{}
importRecords := func(shard uint64, records []Record) error {
var nodes []fragmentNode
var ok bool
var err error
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()
sort.Sort(recordSort(records))
err = importFun(field, shard, records, nodes)
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
}
largestShard := func() uint64 {
largest := 0
resultShard := uint64(0)
for shard, records := range batchForShard {
if len(records) > largest {
largest = len(records)
resultShard = shard
}
}
return resultShard
}
var err error
tic := time.Now()
strategy := options.strategy
recordCount := 0
timeout := options.timeout
batchSize := options.batchSize
shardWidth := options.shardWidth
for record := range recordChan {
recordCount += 1
shard := record.Shard(shardWidth)
batchForShard[shard] = append(batchForShard[shard], record)
if strategy == BatchImport && recordCount >= batchSize {
for shard, records := range batchForShard {
if len(records) == 0 {
continue
}
err = importRecords(shard, records)
if err != nil {
break
}
batchForShard[shard] = nil
}
recordCount = 0
tic = time.Now()
} else if strategy == TimeoutImport && time.Since(tic) >= timeout {
shard := largestShard()
err = importRecords(shard, batchForShard[shard])
if err != nil {
break
}
batchForShard[shard] = nil
recordCount = 0
tic = time.Now()
}
}
if err != nil {
errChan <- err
return
}
// import remaining records
for shard, records := range batchForShard {
if len(records) == 0 {
continue
}
err = importRecords(shard, records)
if err != nil {
break
}
}
errChan <- err
}
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])
}