/
fetch_batch.go
154 lines (135 loc) · 4.16 KB
/
fetch_batch.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
// Copyright (c) 2016 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 client
import (
"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
"github.com/m3db/m3/src/x/checked"
"github.com/m3db/m3/src/x/pool"
)
type fetchBatchOp struct {
checked.RefCount
request rpc.FetchBatchRawRequest
requestV2Elements []rpc.FetchBatchRawV2RequestElement
completionFns []completionFn
finalizer fetchBatchOpFinalizer
}
func (f *fetchBatchOp) reset() {
f.IncWrites()
f.request.RangeStart = 0
f.request.RangeEnd = 0
f.request.NameSpace = nil
for i := range f.request.Ids {
f.request.Ids[i] = nil
}
f.request.Ids = f.request.Ids[:0]
for i := range f.completionFns {
f.completionFns[i] = nil
}
f.completionFns = f.completionFns[:0]
for i := range f.requestV2Elements {
f.requestV2Elements[i].NameSpace = 0
f.requestV2Elements[i].RangeStart = 0
f.requestV2Elements[i].RangeEnd = 0
f.requestV2Elements[i].ID = nil
f.requestV2Elements[i].RangeTimeType = 0
}
f.requestV2Elements = f.requestV2Elements[:0]
f.DecWrites()
}
func (f *fetchBatchOp) append(namespace, id []byte, completionFn completionFn) {
f.IncWrites()
f.request.NameSpace = namespace
f.request.Ids = append(f.request.Ids, id)
f.requestV2Elements = append(f.requestV2Elements, rpc.FetchBatchRawV2RequestElement{
// NameSpace filled in by the host queue later.
RangeStart: f.request.RangeStart,
RangeEnd: f.request.RangeEnd,
ID: id,
RangeTimeType: f.request.RangeTimeType,
})
f.completionFns = append(f.completionFns, completionFn)
f.DecWrites()
}
func (f *fetchBatchOp) Size() int {
f.IncReads()
value := len(f.request.Ids)
if value == 0 {
value = len(f.requestV2Elements)
}
f.DecReads()
return value
}
func (f *fetchBatchOp) CompletionFn() completionFn {
return f.completeAll
}
func (f *fetchBatchOp) completeAll(result interface{}, err error) {
for idx := range f.completionFns {
f.completionFns[idx](result, err)
}
}
func (f *fetchBatchOp) complete(idx int, result interface{}, err error) {
f.IncReads()
fn := f.completionFns[idx]
f.DecReads()
fn(result, err)
}
type fetchBatchOpFinalizer struct {
ref *fetchBatchOp
pool *fetchBatchOpPool
}
func (f fetchBatchOpFinalizer) OnFinalize() {
f.pool.Put(f.ref)
}
type fetchBatchOpPool struct {
pool pool.ObjectPool
capacity int
}
func newFetchBatchOpPool(opts pool.ObjectPoolOptions, capacity int) *fetchBatchOpPool {
p := pool.NewObjectPool(opts)
return &fetchBatchOpPool{pool: p, capacity: capacity}
}
func (p *fetchBatchOpPool) Init() {
p.pool.Init(func() interface{} {
f := &fetchBatchOp{}
f.request.Ids = make([][]byte, 0, p.capacity)
f.completionFns = make([]completionFn, 0, p.capacity)
f.finalizer.ref = f
f.finalizer.pool = p
f.SetOnFinalize(&f.finalizer)
f.IncRef()
f.reset()
f.DecRef()
return f
})
}
func (p *fetchBatchOpPool) Get() *fetchBatchOp {
return p.pool.Get().(*fetchBatchOp)
}
func (p *fetchBatchOpPool) Put(f *fetchBatchOp) {
f.IncRef()
if cap(f.request.Ids) != p.capacity || cap(f.completionFns) != p.capacity {
// Grew outside capacity, do not return to pool
f.DecRef()
return
}
f.reset()
f.DecRef()
p.pool.Put(f)
}