-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk.go
134 lines (113 loc) · 3.2 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
/*
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 (
"sync"
log "github.com/cihub/seelog"
"encoding/json"
"bytes"
"gopkg.in/cheggaaa/pb.v1"
"time"
)
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)
READ_DOCS:
for {
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 {
//json,_:=json.Marshal(docI)
//log.Errorf("failed parsing document: %v", string(json))
break READ_DOCS
}
}
var tempDestIndexName string
tempDestIndexName = docI["_index"].(string)
if c.Config.TargetIndexName != "" {
tempDestIndexName = c.Config.TargetIndexName
}
doc := Document{
Index: tempDestIndexName,
Type: docI["_type"].(string),
source: docI["_source"].(map[string]interface{}),
Id: docI["_id"].(string),
}
// if channel is closed flush and gtfo
if !open {
goto WORKER_DONE
}
// sanity check
if len(doc.Index) == 0 || len(doc.Id) == 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{
"create": doc,
}
if err = docEnc.Encode(post); err != nil {
log.Error(err)
}
if err = docEnc.Encode(doc.source); err != nil {
log.Error(err)
}
// if we approach the 100mb es limit, flush to es and reset mainBuf
if mainBuf.Len() + docBuf.Len() > (c.Config.BulkSizeInMB * 1000000) {
goto CLEAN_BUFFER
}
// append the doc to the main buffer
mainBuf.Write(docBuf.Bytes())
// reset for next document
bulkItemSize++
docBuf.Reset()
(*docCount)++
case <-time.After(time.Second * 5):
log.Debug("5s no message input")
goto CLEAN_BUFFER
case <-time.After(time.Minute * 5):
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
}
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()
}