-
Notifications
You must be signed in to change notification settings - Fork 787
/
client_bulk.go
207 lines (184 loc) · 5.94 KB
/
client_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// The MIT License (MIT)
// Copyright (c) 2017-2020 Uber Technologies Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package v6
import (
"context"
"time"
"github.com/olivere/elastic"
"github.com/uber/cadence/common/elasticsearch/bulk"
"github.com/uber/cadence/common/log"
)
// bulkProcessorParametersV6 holds all required and optional parameters for executing bulk service
type bulkProcessorParametersV6 struct {
Name string
NumOfWorkers int
BulkActions int
BulkSize int
FlushInterval time.Duration
Backoff elastic.Backoff
BeforeFunc elastic.BulkBeforeFunc
AfterFunc elastic.BulkAfterFunc
}
type v6BulkProcessor struct {
processor *elastic.BulkProcessor
logger log.Logger
}
func (v *v6BulkProcessor) Start(ctx context.Context) error {
return v.processor.Start(ctx)
}
func (v *v6BulkProcessor) Stop() error {
return v.processor.Stop()
}
func (v *v6BulkProcessor) Close() error {
return v.processor.Close()
}
func (v *v6BulkProcessor) Add(request *bulk.GenericBulkableAddRequest) {
var req elastic.BulkableRequest
switch request.RequestType {
case bulk.BulkableDeleteRequest:
req = elastic.NewBulkDeleteRequest().
Index(request.Index).
Type(request.Type).
Id(request.ID).
VersionType(request.VersionType).
Version(request.Version)
case bulk.BulkableIndexRequest:
req = elastic.NewBulkIndexRequest().
Index(request.Index).
Type(request.Type).
Id(request.ID).
VersionType(request.VersionType).
Version(request.Version).
Doc(request.Doc)
case bulk.BulkableCreateRequest:
//for bulk create request still calls the bulk index method
//with providing operation type
req = elastic.NewBulkIndexRequest().
OpType("create").
Index(request.Index).
Type(request.Type).
Id(request.ID).
VersionType("internal").
Doc(request.Doc)
}
v.processor.Add(req)
}
func (v *v6BulkProcessor) Flush() error {
return v.processor.Flush()
}
func (c *ElasticV6) runBulkProcessor(ctx context.Context, p *bulkProcessorParametersV6) (*v6BulkProcessor, error) {
processor, err := c.client.BulkProcessor().
Name(p.Name).
Workers(p.NumOfWorkers).
BulkActions(p.BulkActions).
BulkSize(p.BulkSize).
FlushInterval(p.FlushInterval).
Backoff(p.Backoff).
Before(p.BeforeFunc).
After(p.AfterFunc).
Do(ctx)
if err != nil {
return nil, err
}
return &v6BulkProcessor{
processor: processor,
}, nil
}
func (c *ElasticV6) RunBulkProcessor(ctx context.Context, parameters *bulk.BulkProcessorParameters) (bulk.GenericBulkProcessor, error) {
beforeFunc := func(executionId int64, requests []elastic.BulkableRequest) {
parameters.BeforeFunc(executionId, fromV6ToGenericBulkableRequests(requests))
}
afterFunc := func(executionId int64, requests []elastic.BulkableRequest, response *elastic.BulkResponse, err error) {
gerr := convertV6ErrorToGenericError(err)
parameters.AfterFunc(
executionId,
fromV6ToGenericBulkableRequests(requests),
fromV6toGenericBulkResponse(response),
gerr)
}
return c.runBulkProcessor(ctx, &bulkProcessorParametersV6{
Name: parameters.Name,
NumOfWorkers: parameters.NumOfWorkers,
BulkActions: parameters.BulkActions,
BulkSize: parameters.BulkSize,
FlushInterval: parameters.FlushInterval,
Backoff: parameters.Backoff,
BeforeFunc: beforeFunc,
AfterFunc: afterFunc,
})
}
func convertV6ErrorToGenericError(err error) *bulk.GenericError {
if err == nil {
return nil
}
status := bulk.UnknownStatusCode
switch e := err.(type) {
case *elastic.Error:
status = e.Status
}
return &bulk.GenericError{
Status: status,
Details: err,
}
}
func fromV6toGenericBulkResponse(response *elastic.BulkResponse) *bulk.GenericBulkResponse {
if response == nil {
return &bulk.GenericBulkResponse{}
}
return &bulk.GenericBulkResponse{
Took: response.Took,
Errors: response.Errors,
Items: fromV6ToGenericBulkResponseItemMaps(response.Items),
}
}
func fromV6ToGenericBulkResponseItemMaps(items []map[string]*elastic.BulkResponseItem) []map[string]*bulk.GenericBulkResponseItem {
var gitems []map[string]*bulk.GenericBulkResponseItem
for _, it := range items {
gitems = append(gitems, fromV6ToGenericBulkResponseItemMap(it))
}
return gitems
}
func fromV6ToGenericBulkResponseItemMap(m map[string]*elastic.BulkResponseItem) map[string]*bulk.GenericBulkResponseItem {
if m == nil {
return nil
}
gm := make(map[string]*bulk.GenericBulkResponseItem, len(m))
for k, v := range m {
gm[k] = fromV6ToGenericBulkResponseItem(v)
}
return gm
}
func fromV6ToGenericBulkResponseItem(v *elastic.BulkResponseItem) *bulk.GenericBulkResponseItem {
return &bulk.GenericBulkResponseItem{
Index: v.Index,
Type: v.Type,
ID: v.Id,
Version: v.Version,
Result: v.Result,
Status: v.Status,
}
}
func fromV6ToGenericBulkableRequests(requests []elastic.BulkableRequest) []bulk.GenericBulkableRequest {
var v6Reqs []bulk.GenericBulkableRequest
for _, req := range requests {
v6Reqs = append(v6Reqs, req)
}
return v6Reqs
}