-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk.go
183 lines (153 loc) · 4.32 KB
/
bulk.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
/*
Copyright 2016 Medcl (m AT medcl.net)
Licensed 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 main
import (
"bytes"
"encoding/json"
"github.com/cheggaaa/pb"
"strings"
"sync"
"time"
log "github.com/cihub/seelog"
)
func (c *Migrator) NewBulkWorker(docCount *int, pb *pb.ProgressBar, wg *sync.WaitGroup) {
log.Debug("start es bulk worker")
bulkItemSize := 0
mainBuf := bytes.Buffer{}
docBuf := bytes.Buffer{}
docEnc := json.NewEncoder(&docBuf)
idleDuration := 5 * time.Second
idleTimeout := time.NewTimer(idleDuration)
defer idleTimeout.Stop()
taskTimeOutDuration := 5 * time.Minute
taskTimeout := time.NewTimer(taskTimeOutDuration)
defer taskTimeout.Stop()
READ_DOCS:
for {
idleTimeout.Reset(idleDuration)
taskTimeout.Reset(taskTimeOutDuration)
select {
case docI, open := <-c.DocChan:
var err error
log.Trace("read doc from channel,", docI)
// this check is in case the document is an error with scroll stuff
if status, ok := docI["status"]; ok {
if status.(int) == 404 {
log.Error("error: ", docI["response"])
continue
}
}
// sanity check
for _, key := range []string{"_index", "_type", "_source", "_id"} {
if _, ok := docI[key]; !ok {
break READ_DOCS
}
}
var tempDestIndexName string
var tempTargetTypeName string
tempDestIndexName = docI["_index"].(string)
tempTargetTypeName = docI["_type"].(string)
if c.Config.TargetIndexName != "" {
tempDestIndexName = c.Config.TargetIndexName
}
if c.Config.OverrideTypeName != "" {
tempTargetTypeName = c.Config.OverrideTypeName
}
doc := Document{
Index: tempDestIndexName,
Type: tempTargetTypeName,
source: docI["_source"].(map[string]interface{}),
Id: docI["_id"].(string),
}
if c.Config.RegenerateID {
doc.Id = ""
}
if c.Config.RenameFields != "" {
kvs := strings.Split(c.Config.RenameFields, ",")
for _, i := range kvs {
fvs := strings.Split(i, ":")
oldField := strings.TrimSpace(fvs[0])
newField := strings.TrimSpace(fvs[1])
if oldField == "_type" {
doc.source[newField] = docI["_type"].(string)
} else {
v := doc.source[oldField]
doc.source[newField] = v
delete(doc.source, oldField)
}
}
}
// add doc "_routing" if exists
if _, ok := docI["_routing"]; ok {
str, ok := docI["_routing"].(string)
if ok && str != "" {
doc.Routing = str
}
}
// if channel is closed flush and gtfo
if !open {
goto WORKER_DONE
}
// sanity check
if len(doc.Index) == 0 || len(doc.Type) == 0 {
log.Errorf("failed decoding document: %+v", doc)
continue
}
// encode the doc and and the _source field for a bulk request
post := map[string]Document{
"index": doc,
}
if err = docEnc.Encode(post); err != nil {
log.Error(err)
}
if err = docEnc.Encode(doc.source); err != nil {
log.Error(err)
}
// append the doc to the main buffer
mainBuf.Write(docBuf.Bytes())
// reset for next document
bulkItemSize++
(*docCount)++
docBuf.Reset()
// if we approach the 100mb es limit, flush to es and reset mainBuf
if mainBuf.Len()+docBuf.Len() > (c.Config.BulkSizeInMB * 1024*1024) {
goto CLEAN_BUFFER
}
case <-idleTimeout.C:
log.Debug("5s no message input")
goto CLEAN_BUFFER
case <-taskTimeout.C:
log.Warn("5m no message input, close worker")
goto WORKER_DONE
}
goto READ_DOCS
CLEAN_BUFFER:
c.TargetESAPI.Bulk(&mainBuf)
log.Trace("clean buffer, and execute bulk insert")
pb.Add(bulkItemSize)
bulkItemSize = 0
if c.Config.SleepSecondsAfterEachBulk >0{
time.Sleep(time.Duration(c.Config.SleepSecondsAfterEachBulk) * time.Second)
}
}
WORKER_DONE:
if docBuf.Len() > 0 {
mainBuf.Write(docBuf.Bytes())
bulkItemSize++
}
c.TargetESAPI.Bulk(&mainBuf)
log.Trace("bulk insert")
pb.Add(bulkItemSize)
bulkItemSize = 0
wg.Done()
}