forked from voldemort/voldemort
/
ConsistencyFix.java
543 lines (487 loc) · 22.9 KB
/
ConsistencyFix.java
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
* Copyright 2013 LinkedIn, Inc
*
* 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 voldemort.utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import voldemort.client.protocol.admin.AdminClient;
import voldemort.client.protocol.admin.AdminClientConfig;
import voldemort.client.protocol.admin.QueryKeyResult;
import voldemort.cluster.Cluster;
import voldemort.store.StoreDefinition;
import voldemort.store.routed.NodeValue;
import voldemort.store.routed.ReadRepairer;
import voldemort.versioning.VectorClock;
import voldemort.versioning.Versioned;
import com.google.common.collect.Lists;
public class ConsistencyFix {
// TODO: Move ConsistencyFixContext into its own file?
private static class ConsistencyFixContext {
private final AdminClient adminClient;
private final StoreInstance storeInstance;
public ConsistencyFixContext(String url, String storeName) throws Exception {
System.out.println("Connecting to bootstrap server: " + url);
adminClient = new AdminClient(url, new AdminClientConfig(), 0);
Cluster cluster = adminClient.getAdminClientCluster();
System.out.println("Cluster determined to be: " + cluster.getName());
System.out.println("Determining store definition for store: " + storeName);
Versioned<List<StoreDefinition>> storeDefinitions = adminClient.getRemoteStoreDefList(0);
List<StoreDefinition> storeDefs = storeDefinitions.getValue();
StoreDefinition storeDefinition = StoreDefinitionUtils.getStoreDefinitionWithName(storeDefs,
storeName);
System.out.println("Store definition determined.");
storeInstance = new StoreInstance(cluster, storeDefinition);
}
public AdminClient getAdminClient() {
return adminClient;
}
public StoreInstance getStoreInstance() {
return storeInstance;
}
public String getStoreName() {
return storeInstance.getStoreDefinition().getName();
}
public int getMasterPartitionId(String keyInHexFormat) throws DecoderException {
byte[] key = Hex.decodeHex(keyInHexFormat.toCharArray());
return storeInstance.getMasterPartitionId(key);
}
}
public static void printUsage() {
System.out.println("Required arguments: \n" + " --url <url>\n" + " --store <storeName>\n"
+ " (--keys <keysInHexFormatSeparatedByComma "
+ "| --key-file <FileNameOfInputListOfKeysToFix>)\n"
+ "| --out-file <FileNameOfOutputListOfKeysNotFixed>)\n");
}
public static void printUsage(String errMessage) {
System.err.println("Error: " + errMessage);
printUsage();
System.exit(1);
}
private static class Options {
public String url = null;
public String storeName = null;
public String outFile = null;
public List<String> keysInHexFormat = null;
public boolean verbose = false;
}
/**
* All the logic for parsing and validating options.
*
* @param args
* @return A struct containing validated options.
* @throws IOException
*/
private static ConsistencyFix.Options parseArgs(String[] args) {
OptionParser parser = new OptionParser();
parser.accepts("help", "print help information");
parser.accepts("url")
.withRequiredArg()
.describedAs("The bootstrap url.")
.ofType(String.class);
parser.accepts("store")
.withRequiredArg()
.describedAs("The store name.")
.ofType(String.class);
parser.accepts("keys")
.withRequiredArg()
.withValuesSeparatedBy(',')
.describedAs("List of keys. "
+ "Each key must be in hexadecimal format. "
+ "Each key must be separated only by a comma ',' without any white space.")
.ofType(String.class);
parser.accepts("key-file")
.withRequiredArg()
.describedAs("Name of key-file. " + "Each key must be in hexadecimal format. "
+ "Each key must be on a separate line in the file. ")
.ofType(String.class);
parser.accepts("out-file")
.withRequiredArg()
.describedAs("Name of out-file. "
+ "Success/failure of each key is dumped to out-file. ")
.ofType(String.class);
parser.accepts("verbose", "verbose");
OptionSet optionSet = parser.parse(args);
if(optionSet.hasArgument("help")) {
try {
parser.printHelpOn(System.out);
} catch(IOException e) {
e.printStackTrace();
}
printUsage();
System.exit(0);
}
if(!optionSet.hasArgument("url")) {
printUsage("Missing required 'url' argument.");
}
if(!optionSet.hasArgument("store")) {
printUsage("Missing required 'store' argument.");
}
if(!optionSet.has("keys") && !optionSet.has("key-file")) {
printUsage("Missing required key-specifying argument: 'keys' or 'key-file'.");
}
if(optionSet.has("keys") && optionSet.has("key-file")) {
printUsage("Please provide exactly one key-specifying argument: 'keys' or 'key-file'.");
}
if(!optionSet.has("out-file")) {
printUsage("Missing required 'out-file' argument.");
}
Options options = new Options();
if(optionSet.has("verbose")) {
options.verbose = true;
}
options.url = (String) optionSet.valueOf("url");
options.storeName = (String) optionSet.valueOf("store");
options.outFile = (String) optionSet.valueOf("out-file");
options.keysInHexFormat = new LinkedList<String>();
if(optionSet.has("keys")) {
@SuppressWarnings("unchecked")
List<String> valuesOf = (List<String>) optionSet.valuesOf("keys");
options.keysInHexFormat = valuesOf;
}
if(optionSet.has("key-file")) {
String keyFile = (String) optionSet.valueOf("key-file");
System.err.println("Key file: " + keyFile);
try {
BufferedReader fileReader = new BufferedReader(new FileReader(keyFile));
for(String line = fileReader.readLine(); line != null; line = fileReader.readLine()) {
if(!line.isEmpty()) {
options.keysInHexFormat.add(line);
}
}
} catch(IOException e) {
Utils.croak("Failure to open input stream: " + e.getMessage());
}
}
return options;
}
public static void main(String[] args) throws Exception {
Options options = parseArgs(args);
BufferedWriter fileWriter = null;
try {
fileWriter = new BufferedWriter(new FileWriter(options.outFile));
} catch(IOException e) {
Utils.croak("Failure to open ouput file '" + options.outFile + "': " + e.getMessage());
}
if(fileWriter == null) {
Utils.croak("Failure to create BufferedWriter for ouput file '" + options.outFile + "'");
}
ConsistencyFixContext vInstance = new ConsistencyFixContext(options.url, options.storeName);
for(String keyInHexFormat: options.keysInHexFormat) {
FixKeyResult fixKeyResult = fixKey(vInstance, keyInHexFormat, options.verbose);
if(fixKeyResult == FixKeyResult.SUCCESS) {
System.out.println("Successfully processed " + keyInHexFormat);
} else {
fileWriter.write("BADKEY," + keyInHexFormat + "," + fixKeyResult.name());
}
}
}
public enum FixKeyResult {
SUCCESS("success"),
BAD_INIT("bad initialization of fix key"),
FETCH_EXCEPTION("exception during fetch"),
REPAIR_EXCEPTION("exception during repair");
private final String name;
private FixKeyResult(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
/**
*
* @param vInstance
* @param nodeIdList
* @param keyInHexFormat
* @param verbose
* @param nodeIdToKeyValues Effectively the output of this method. Must pass
* in a non-null object to be populated by this method.
* @return FixKeyResult
*/
private static ConsistencyFix.FixKeyResult doRead(final ConsistencyFixContext vInstance,
final List<Integer> nodeIdList,
final byte[] keyInBytes,
final String keyInHexFormat,
boolean verbose,
Map<Integer, Iterator<QueryKeyResult>> nodeIdToKeyValues) {
if(nodeIdToKeyValues == null) {
if(verbose) {
System.out.println("Aborting doRead due to bad init.");
}
return FixKeyResult.BAD_INIT;
}
List<ByteArray> keys = new ArrayList<ByteArray>();
keys.add(new ByteArray(keyInBytes));
if(verbose) {
System.out.println("Reading key-values for specified key: " + keyInHexFormat);
}
for(int nodeId: nodeIdList) {
Iterator<QueryKeyResult> keyValues;
keyValues = vInstance.getAdminClient().queryKeys(nodeId,
vInstance.getStoreName(),
keys.iterator());
nodeIdToKeyValues.put(nodeId, keyValues);
// TODO: Not sure if this is appropriate. Since the keyValues
// iterator is a google abstract iterator and inside computeNext()
// still ratains a connection of a socket store. Does this iterator
// implementation call the computeNext() immediately and release the
// connection when done? -- ZWu
}
return FixKeyResult.SUCCESS;
}
/**
*
* @param nodeIdList
* @param keyInHexFormat
* @param verbose
* @param nodeValues Effectively the output of this method. Must pass in a
* non-null object to be populated by this method.
* @return
*/
private static ConsistencyFix.FixKeyResult processReadReplies(final List<Integer> nodeIdList,
final ByteArray keyAsByteArray,
final String keyInHexFormat,
boolean verbose,
final Map<Integer, Iterator<QueryKeyResult>> nodeIdToKeyValues,
List<NodeValue<ByteArray, byte[]>> nodeValues) {
if(nodeValues == null) {
if(verbose) {
System.out.println("Aborting processReadReplies due to bad init.");
}
return FixKeyResult.BAD_INIT;
}
if(verbose) {
System.out.println("Confirming all nodes (" + nodeIdList
+ ") responded with key-values for specified key: " + keyInHexFormat);
}
boolean exceptionsEncountered = false;
for(int nodeId: nodeIdList) {
if(verbose) {
System.out.println("\t Processing response from node with id:" + nodeId);
}
QueryKeyResult keyValue;
if(nodeIdToKeyValues.get(nodeId).hasNext()) {
if(verbose) {
System.out.println("\t... There was a key-value returned from node with id:"
+ nodeId);
}
keyValue = nodeIdToKeyValues.get(nodeId).next();
if(keyValue.hasException()) {
if(verbose) {
System.out.println("\t... Exception encountered while fetching key "
+ keyInHexFormat + " from node with nodeId " + nodeId
+ " : " + keyValue.getException().getMessage());
}
exceptionsEncountered = true;
} else {
if(keyValue.getValues().isEmpty()) {
if(verbose) {
System.out.println("\t... Adding null version to nodeValues");
}
Versioned<byte[]> versioned = new Versioned<byte[]>(null);
nodeValues.add(new NodeValue<ByteArray, byte[]>(nodeId,
keyValue.getKey(),
versioned));
} else {
for(Versioned<byte[]> value: keyValue.getValues()) {
if(verbose) {
System.out.println("\t... Adding following version to nodeValues: "
+ value.getVersion());
}
nodeValues.add(new NodeValue<ByteArray, byte[]>(nodeId,
keyValue.getKey(),
value));
}
}
}
} else {
if(verbose) {
System.out.println("\t... No key-value returned from node with id:" + nodeId);
System.out.println("\t... Adding null version to nodeValues");
}
Versioned<byte[]> versioned = new Versioned<byte[]>(null);
nodeValues.add(new NodeValue<ByteArray, byte[]>(nodeId, keyAsByteArray, versioned));
}
}
if(exceptionsEncountered) {
if(verbose) {
System.out.println("Aborting fixKey because exceptions were encountered when fetching key-values.");
}
return FixKeyResult.FETCH_EXCEPTION;
}
return FixKeyResult.SUCCESS;
}
/**
* Decide on the specific key-value to write everywhere.
*
* @param verbose
* @param nodeValues
* @return The subset of entries from nodeValues that need to be repaired.
*/
private static List<NodeValue<ByteArray, byte[]>> resolveReadConflicts(boolean verbose,
final List<NodeValue<ByteArray, byte[]>> nodeValues) {
// Some cut-paste-and-modify coding from
// store/routed/action/AbstractReadRepair.java and
// store/routed/ThreadPoolRoutedStore.java
if(verbose) {
System.out.println("Resolving conflicts in responses.");
}
ReadRepairer<ByteArray, byte[]> readRepairer = new ReadRepairer<ByteArray, byte[]>();
List<NodeValue<ByteArray, byte[]>> toReadRepair = Lists.newArrayList();
for(NodeValue<ByteArray, byte[]> v: readRepairer.getRepairs(nodeValues)) {
Versioned<byte[]> versioned = Versioned.value(v.getVersioned().getValue(),
((VectorClock) v.getVersion()).clone());
if(verbose) {
System.out.println("\tAdding toReadRepair: key (" + v.getKey() + "), version ("
+ versioned.getVersion() + ")");
}
toReadRepair.add(new NodeValue<ByteArray, byte[]>(v.getNodeId(), v.getKey(), versioned));
}
// TODO: As we discussed, I don't know the read repair code path very
// well. So, feel free to discard my comments if I am off target w.r.t
// to simply doing a get() to fix everything. Semantically, it then
// gives me more comfort that given enough activity to out of sync keys,
// they will eventually reconcile. Our consistency check and fix is then
// simply a way to generate activity to the right keys. -- VChandar
if(verbose) {
System.out.println("Repair work to be done:");
for(NodeValue<ByteArray, byte[]> nodeKeyValue: toReadRepair) {
System.out.println("\tRepair key " + nodeKeyValue.getKey() + "on node with id "
+ nodeKeyValue.getNodeId() + " for version "
+ nodeKeyValue.getVersion());
}
}
return toReadRepair;
}
/**
*
* @param vInstance
* @param verbose
* @param toReadRepair Effectively the output of this method. Must pass in a
* non-null object to be populated by this method.
* @return
*/
private static ConsistencyFix.FixKeyResult doWriteBack(final ConsistencyFixContext vInstance,
boolean verbose,
final List<NodeValue<ByteArray, byte[]>> toReadRepair) {
if(verbose) {
System.out.println("Performing repair work:");
}
boolean allRepairsSuccessful = true;
for(NodeValue<ByteArray, byte[]> nodeKeyValue: toReadRepair) {
if(verbose) {
System.out.println("\tDoing repair for node with id:" + nodeKeyValue.getNodeId());
}
Exception e = vInstance.getAdminClient().repairEntry(vInstance.getStoreName(),
nodeKeyValue);
if(e != null) {
if(verbose) {
System.out.println("\t... Repair of key " + nodeKeyValue.getKey()
+ "on node with id " + nodeKeyValue.getNodeId()
+ " for version " + nodeKeyValue.getVersion()
+ " failed because of exception : " + e.getMessage());
}
allRepairsSuccessful = false;
}
}
if(!allRepairsSuccessful) {
if(verbose) {
System.err.println("Aborting fixKey because exceptions were encountered when reparing key-values.");
System.out.println("Fix failed...");
}
return FixKeyResult.REPAIR_EXCEPTION;
}
return FixKeyResult.SUCCESS;
}
// TODO: Decide between design based on a key-by-key fix and a design based
// on something that looks more like stream processing. E.g., See
// AdminClient.updateEntries and
// DonorBasedRebalancePusherSlave for ideas.
// TODO: As a follow on, need to decide
// if queryKeys should offer a queryKey interface, and/or if repairEntry
// ought to offer a repairEntries interface.
public static ConsistencyFix.FixKeyResult fixKey(ConsistencyFixContext vInstance,
String keyInHexFormat,
boolean verbose) {
if(verbose) {
System.out.println("Performing consistency fix of key: " + keyInHexFormat);
}
// Initialization.
byte[] keyInBytes;
List<Integer> nodeIdList = null;
int masterPartitionId = -1;
try {
keyInBytes = ByteUtils.fromHexString(keyInHexFormat);
masterPartitionId = vInstance.getMasterPartitionId(keyInHexFormat);
nodeIdList = vInstance.getStoreInstance().getReplicationNodeList(masterPartitionId);
} catch(Exception exception) {
if(verbose) {
System.out.println("Aborting fixKey due to bad init.");
exception.printStackTrace();
}
return FixKeyResult.BAD_INIT;
}
ByteArray keyAsByteArray = new ByteArray(keyInBytes);
// Read
Map<Integer, Iterator<QueryKeyResult>> nodeIdToKeyValues = new HashMap<Integer, Iterator<QueryKeyResult>>();
FixKeyResult fixKeyResult = ConsistencyFix.doRead(vInstance,
nodeIdList,
keyInBytes,
keyInHexFormat,
verbose,
nodeIdToKeyValues);
if(fixKeyResult != FixKeyResult.SUCCESS) {
return fixKeyResult;
}
// Process read replies
List<NodeValue<ByteArray, byte[]>> nodeValues = Lists.newArrayList();
fixKeyResult = ConsistencyFix.processReadReplies(nodeIdList,
keyAsByteArray,
keyInHexFormat,
verbose,
nodeIdToKeyValues,
nodeValues);
if(fixKeyResult != FixKeyResult.SUCCESS) {
return fixKeyResult;
}
// Resolve conflicts
List<NodeValue<ByteArray, byte[]>> toReadRepair = ConsistencyFix.resolveReadConflicts(verbose,
nodeValues);
// Write back (if necessary)
fixKeyResult = ConsistencyFix.doWriteBack(vInstance, verbose, toReadRepair);
if(fixKeyResult != FixKeyResult.SUCCESS) {
return fixKeyResult;
}
// Success!
if(verbose) {
System.out.println("Fix for key " + keyInHexFormat + "completed successfully!!");
}
return FixKeyResult.SUCCESS;
}
}