This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition_parser.go
349 lines (289 loc) · 8.95 KB
/
partition_parser.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright 2013-2017 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements WHICH ARE COMPATIBLE WITH THE APACHE LICENSE, VERSION 2.0.
*
* 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 aerospike
import (
"encoding/base64"
"fmt"
"strconv"
"sync"
. "github.com/aerospike/aerospike-client-go/logger"
. "github.com/aerospike/aerospike-client-go/types"
)
const (
_PartitionGeneration = "partition-generation"
_ReplicasMaster = "replicas-master"
_Replicas = "replicas"
_ReplicasAll = "replicas-all"
)
var partitionMapLock sync.Mutex
// Parse node's master (and optionally prole) partitions.
type partitionParser struct {
pmap partitionMap
buffer []byte
partitionCount int
generation int
length int
offset int
}
func newPartitionParser(node *Node, partitions partitionMap, partitionCount int, requestProleReplicas bool) (*partitionParser, error) {
newPartitionParser := &partitionParser{
partitionCount: partitionCount,
}
// Send format 1: partition-generation\nreplicas\n
// Send format 2: partition-generation\nreplicas-all\n
// Send format 3: partition-generation\nreplicas-master\n
command := _ReplicasMaster
if node.supportsReplicas.Get() {
command = _Replicas
} else if requestProleReplicas {
command = _ReplicasAll
}
info, err := node.requestRawInfo(_PartitionGeneration, command)
if err != nil {
return nil, err
}
newPartitionParser.buffer = info.msg.Data
newPartitionParser.length = len(info.msg.Data)
if newPartitionParser.length == 0 {
return nil, NewAerospikeError(PARSE_ERROR, fmt.Sprintf("Partition info is empty"))
}
newPartitionParser.generation, err = newPartitionParser.parseGeneration()
if err != nil {
return nil, err
}
newPartitionParser.pmap = partitions
partitionMapLock.Lock()
defer partitionMapLock.Unlock()
if node.supportsReplicas.Get() {
err = newPartitionParser.parseReplicasAll(node, command)
} else if requestProleReplicas {
err = newPartitionParser.parseReplicasAll(node, command)
} else {
err = newPartitionParser.parseReplicasMaster(node)
}
if err != nil {
return nil, err
}
return newPartitionParser, nil
}
func (pp *partitionParser) getGeneration() int {
return pp.generation
}
func (pp *partitionParser) getPartitionMap() partitionMap {
return pp.pmap
}
func (pp *partitionParser) parseGeneration() (int, error) {
if err := pp.expectName(_PartitionGeneration); err != nil {
return -1, err
}
begin := pp.offset
for pp.offset < pp.length {
if pp.buffer[pp.offset] == '\n' {
s := string(pp.buffer[begin:pp.offset])
pp.offset++
return strconv.Atoi(s)
}
pp.offset++
}
return -1, NewAerospikeError(PARSE_ERROR, fmt.Sprintf("Failed to find partition-generation value"))
}
func (pp *partitionParser) parseReplicasMaster(node *Node) error {
// Use low-level info methods and parse byte array directly for maximum performance.
// Receive format: replicas-master\t<ns1>:<base 64 encoded bitmap1>;<ns2>:<base 64 encoded bitmap2>...\n
if err := pp.expectName(_ReplicasMaster); err != nil {
return err
}
begin := pp.offset
for pp.offset < pp.length {
if pp.buffer[pp.offset] == ':' {
// Parse namespace.
namespace := string(pp.buffer[begin:pp.offset])
if len(namespace) <= 0 || len(namespace) >= 32 {
response := pp.getTruncatedResponse()
return NewAerospikeError(PARSE_ERROR, fmt.Sprintf("Invalid partition namespace `%s` response: `%s`", namespace, response))
}
pp.offset++
begin = pp.offset
// Parse partition bitmap.
for pp.offset < pp.length {
b := pp.buffer[pp.offset]
if b == ';' || b == '\n' {
break
}
pp.offset++
}
if pp.offset == begin {
response := pp.getTruncatedResponse()
return NewAerospikeError(PARSE_ERROR, fmt.Sprintf("Empty partition id for namespace `%s` response: `%s`", namespace, response))
}
partitions := pp.pmap[namespace]
if partitions == nil {
// Create new replica array.
partitions = newPartitions(pp.partitionCount, 1, false)
pp.pmap[namespace] = partitions
}
if err := pp.decodeBitmap(node, partitions, 0, 0, begin); err != nil {
return err
}
pp.offset++
begin = pp.offset
} else {
pp.offset++
}
}
return nil
}
func (pp *partitionParser) parseReplicasAll(node *Node, command string) error {
// Use low-level info methods and parse byte array directly for maximum performance.
// Receive format: replicas-all\t
// <ns1>:[regime],<count>,<base 64 encoded bitmap1>,<base 64 encoded bitmap2>...;
// <ns2>:[regime],<count>,<base 64 encoded bitmap1>,<base 64 encoded bitmap2>...;\n
if err := pp.expectName(command); err != nil {
return err
}
begin := pp.offset
regime := 0
for pp.offset < pp.length {
if pp.buffer[pp.offset] == ':' {
// Parse namespace.
namespace := string(pp.buffer[begin:pp.offset])
if len(namespace) <= 0 || len(namespace) >= 32 {
response := pp.getTruncatedResponse()
return NewAerospikeError(PARSE_ERROR, fmt.Sprintf("Invalid partition namespace `%s` response: `%s`", namespace, response))
}
pp.offset++
begin = pp.offset
// Parse regime.
if command == _Replicas {
for pp.offset < pp.length {
b := pp.buffer[pp.offset]
if b == ',' {
break
}
pp.offset++
}
var err error
regime, err = strconv.Atoi(string(pp.buffer[begin:pp.offset]))
if err != nil {
return err
}
pp.offset++
begin = pp.offset
}
// Parse replica count.
for pp.offset < pp.length {
b := pp.buffer[pp.offset]
if b == ',' {
break
}
pp.offset++
}
replicaCount, err := strconv.Atoi(string(pp.buffer[begin:pp.offset]))
if err != nil {
return err
}
partitions := pp.pmap[namespace]
if partitions == nil {
// Create new replica array.
partitions = newPartitions(pp.partitionCount, replicaCount, regime != 0)
pp.pmap[namespace] = partitions
} else if len(partitions.Replicas) != replicaCount {
// Ensure replicaArray is correct size.
Logger.Info("Namespace `%s` replication factor changed from `%d` to `%d` ", namespace, len(partitions.Replicas), replicaCount)
partitions.setReplicaCount(replicaCount) //= clonePartitions(partitions, replicaCount)
pp.pmap[namespace] = partitions
}
// Parse partition bitmaps.
for i := 0; i < replicaCount; i++ {
pp.offset++
begin = pp.offset
// Find bitmap endpoint
for pp.offset < pp.length {
b := pp.buffer[pp.offset]
if b == ',' || b == ';' {
break
}
pp.offset++
}
if pp.offset == begin {
response := pp.getTruncatedResponse()
return NewAerospikeError(PARSE_ERROR, fmt.Sprintf("Empty partition id for namespace `%s` response: `%s`", namespace, response))
}
if err := pp.decodeBitmap(node, partitions, i, regime, begin); err != nil {
return err
}
}
pp.offset++
begin = pp.offset
} else {
pp.offset++
}
}
return nil
}
func (pp *partitionParser) decodeBitmap(node *Node, partitions *Partitions, replica int, regime int, begin int) error {
restoreBuffer, err := base64.StdEncoding.DecodeString(string(pp.buffer[begin:pp.offset]))
if err != nil {
return err
}
for partition := 0; partition < pp.partitionCount; partition++ {
nodeOld := partitions.Replicas[replica][partition]
if (restoreBuffer[partition>>3] & (0x80 >> uint(partition&7))) != 0 {
// Node owns this partition.
regimeOld := partitions.regimes[partition]
if regime == 0 || regime >= regimeOld {
if regime > regimeOld {
partitions.regimes[partition] = regime
}
if nodeOld != nil && nodeOld != node {
// Force previously mapped node to refresh it's partition map on next cluster tend.
nodeOld.partitionGeneration.Set(-1)
}
partitions.Replicas[replica][partition] = node
}
} else {
// Node does not own partition.
if node == nodeOld {
// Must erase previous map.
partitions.Replicas[replica][partition] = nil
}
}
}
return nil
}
func (pp *partitionParser) expectName(name string) error {
begin := pp.offset
for pp.offset < pp.length {
if pp.buffer[pp.offset] == '\t' {
s := string(pp.buffer[begin:pp.offset])
if name == s {
pp.offset++
return nil
}
break
}
pp.offset++
}
return NewAerospikeError(PARSE_ERROR, fmt.Sprintf("Failed to find `%s`", name))
}
func (pp *partitionParser) getTruncatedResponse() string {
max := pp.length
if max > 200 {
max = 200
}
return string(pp.buffer[0:max])
}