This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
PegasusClient.java
562 lines (501 loc) · 17.5 KB
/
PegasusClient.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.
package com.xiaomi.infra.pegasus.client;
import com.xiaomi.infra.pegasus.rpc.*;
import com.xiaomi.infra.pegasus.tools.Tools;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author qinzuoyan
* <p>Implementation of {@link PegasusClientInterface}.
*/
public class PegasusClient implements PegasusClientInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(PegasusClient.class);
public static final String PEGASUS_ENABLE_WRITE_LIMIT = "enable_write_limit";
public static final String PEGASUS_ENABLE_WRITE_LIMIT_DEF = "true";
private boolean enableWriteLimit;
private final Properties config;
private final ConcurrentHashMap<String, PegasusTable> tableMap;
private final Object tableMapLock;
private final String[] metaList;
private Cluster cluster;
private static class PegasusHasher implements KeyHasher {
@Override
public long hash(byte[] key) {
Validate.isTrue(key != null && key.length >= 2);
ByteBuffer buf = ByteBuffer.wrap(key);
int hashKeyLen = 0xFFFF & buf.getShort();
Validate.isTrue(hashKeyLen != 0xFFFF && (2 + hashKeyLen <= key.length));
return hashKeyLen == 0
? Tools.dsn_crc64(key, 2, key.length - 2)
: Tools.dsn_crc64(key, 2, hashKeyLen);
}
}
private PegasusTable getTable(String tableName) throws PException {
return getTable(tableName, 0);
}
private PegasusTable getTable(String tableName, int backupRequestDelayMs) throws PException {
PegasusTable table = tableMap.get(tableName);
if (table == null) {
synchronized (tableMapLock) {
table = tableMap.get(tableName);
if (table == null) {
try {
TableOptions options = new TableOptions(new PegasusHasher(), backupRequestDelayMs);
Table internalTable = cluster.openTable(tableName, options);
table = new PegasusTable(this, internalTable);
} catch (Throwable e) {
throw new PException(e);
}
tableMap.put(tableName, table);
}
}
}
return table;
}
// pegasus client configuration keys
public static final String[] PEGASUS_CLIENT_CONFIG_KEYS =
ArrayUtils.add(ClusterOptions.allKeys(), PEGASUS_ENABLE_WRITE_LIMIT);
// configPath could be:
// - zk path: zk://host1:port1,host2:port2,host3:port3/path/to/config
// - local file path: file:///path/to/config
// - resource path: resource:///path/to/config
public PegasusClient(String configPath) throws PException {
this(PConfigUtil.loadConfiguration(configPath));
}
public PegasusClient(Properties config) throws PException {
this.config = config;
this.cluster = Cluster.createCluster(config);
this.tableMap = new ConcurrentHashMap<String, PegasusTable>();
this.tableMapLock = new Object();
this.metaList = cluster.getMetaList();
this.enableWriteLimit =
Boolean.parseBoolean(
config.getProperty(PEGASUS_ENABLE_WRITE_LIMIT, PEGASUS_ENABLE_WRITE_LIMIT_DEF));
LOGGER.info(getConfigurationString());
}
public boolean isWriteLimitEnabled() {
return enableWriteLimit;
}
String getMetaList() {
return Arrays.toString(metaList);
}
@Override
public void finalize() {
close();
}
// generate rocksdb key.
public static byte[] generateKey(byte[] hashKey, byte[] sortKey) {
int hashKeyLen = (hashKey == null ? 0 : hashKey.length);
Validate.isTrue(hashKeyLen < 0xFFFF, "length of hash key must be less than UINT16_MAX");
int sortKeyLen = (sortKey == null ? 0 : sortKey.length);
// default byte order of ByteBuffer is BIG_ENDIAN
ByteBuffer buf = ByteBuffer.allocate(2 + hashKeyLen + sortKeyLen);
buf.putShort((short) hashKeyLen);
if (hashKeyLen > 0) {
buf.put(hashKey);
}
if (sortKeyLen > 0) {
buf.put(sortKey);
}
return buf.array();
}
// generate the adjacent next rocksdb key according to hash key.
public static byte[] generateNextBytes(byte[] hashKey) {
int hashKeyLen = hashKey == null ? 0 : hashKey.length;
Validate.isTrue(hashKeyLen < 0xFFFF, "length of hash key must be less than UINT16_MAX");
ByteBuffer buf = ByteBuffer.allocate(2 + hashKeyLen);
buf.putShort((short) hashKeyLen);
if (hashKeyLen > 0) {
buf.put(hashKey);
}
byte[] array = buf.array();
int i = array.length - 1;
for (; i >= 0; i--) {
// 0xFF will look like -1
if (array[i] != -1) {
array[i]++;
break;
}
}
return Arrays.copyOf(array, i + 1);
}
// generate the adjacent next rocksdb key according to hash key and sort key.
public static byte[] generateNextBytes(byte[] hashKey, byte[] sortKey) {
byte[] array = generateKey(hashKey, sortKey);
int i = array.length - 1;
for (; i >= 0; i--) {
// 0xFF will look like -1
if (array[i] != -1) {
array[i]++;
break;
}
}
return Arrays.copyOf(array, i + 1);
}
public static Pair<byte[], byte[]> restoreKey(byte[] key) {
Validate.isTrue(key != null && key.length >= 2);
ByteBuffer buf = ByteBuffer.wrap(key);
int hashKeyLen = 0xFFFF & buf.getShort();
Validate.isTrue(hashKeyLen != 0xFFFF && (2 + hashKeyLen <= key.length));
return new ImmutablePair<byte[], byte[]>(
Arrays.copyOfRange(key, 2, 2 + hashKeyLen),
Arrays.copyOfRange(key, 2 + hashKeyLen, key.length));
}
public static int bytesCompare(byte[] left, byte[] right) {
int len = Math.min(left.length, right.length);
for (int i = 0; i < len; i++) {
int ret = (0xFF & left[i]) - (0xFF & right[i]);
if (ret != 0) return ret;
}
return left.length - right.length;
}
public String getConfigurationString() {
String configString = "PegasusClient Configuration:\n";
if (this.config == null) {
return configString;
}
for (int i = 0; i < PEGASUS_CLIENT_CONFIG_KEYS.length; ++i) {
configString +=
(PEGASUS_CLIENT_CONFIG_KEYS[i]
+ "="
+ this.config.getProperty(PEGASUS_CLIENT_CONFIG_KEYS[i], "")
+ "\n");
}
return configString;
}
@Override
public void close() {
synchronized (this) {
if (cluster != null) {
String metaList = StringUtils.join(cluster.getMetaList(), ",");
LOGGER.info("start to close pegasus client for [{}]", metaList);
cluster.close();
cluster = null;
LOGGER.info("finish to close pegasus client for [{}]", metaList);
}
}
}
@Override
public PegasusTableInterface openTable(String tableName) throws PException {
return getTable(tableName);
}
@Override
public PegasusTableInterface openTable(String tableName, int backupRequestDelayMs)
throws PException {
return getTable(tableName, backupRequestDelayMs);
}
@Override
public Properties getConfiguration() {
return config;
}
@Override
public boolean exist(String tableName, byte[] hashKey, byte[] sortKey) throws PException {
PegasusTable tb = getTable(tableName);
return tb.exist(hashKey, sortKey, 0);
}
@Override
public long sortKeyCount(String tableName, byte[] hashKey) throws PException {
PegasusTable tb = getTable(tableName);
return tb.sortKeyCount(hashKey, 0);
}
@Override
public byte[] get(String tableName, byte[] hashKey, byte[] sortKey) throws PException {
PegasusTable tb = getTable(tableName);
return tb.get(hashKey, sortKey, 0);
}
@Override
public void batchGet(String tableName, List<Pair<byte[], byte[]>> keys, List<byte[]> values)
throws PException {
PegasusTable tb = getTable(tableName);
tb.batchGet(keys, values, 0);
}
@Override
public int batchGet2(
String tableName, List<Pair<byte[], byte[]>> keys, List<Pair<PException, byte[]>> values)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.batchGet2(keys, values, 0);
}
@Override
public boolean multiGet(
String tableName,
byte[] hashKey,
List<byte[]> sortKeys,
int maxFetchCount,
int maxFetchSize,
List<Pair<byte[], byte[]>> values)
throws PException {
if (values == null) {
throw new PException("Invalid parameter: values should not be null");
}
PegasusTable tb = getTable(tableName);
PegasusTableInterface.MultiGetResult res =
tb.multiGet(hashKey, sortKeys, maxFetchCount, maxFetchSize, 0);
for (Pair<byte[], byte[]> kv : res.values) {
values.add(kv);
}
return res.allFetched;
}
@Override
public boolean multiGet(
String tableName, byte[] hashKey, List<byte[]> sortKeys, List<Pair<byte[], byte[]>> values)
throws PException {
return multiGet(tableName, hashKey, sortKeys, 100, 1000000, values);
}
@Override
public boolean multiGet(
String tableName,
byte[] hashKey,
byte[] startSortKey,
byte[] stopSortKey,
MultiGetOptions options,
int maxFetchCount,
int maxFetchSize,
List<Pair<byte[], byte[]>> values)
throws PException {
if (values == null) {
throw new PException("Invalid parameter: values should not be null");
}
PegasusTable tb = getTable(tableName);
PegasusTableInterface.MultiGetResult res =
tb.multiGet(hashKey, startSortKey, stopSortKey, options, maxFetchCount, maxFetchSize, 0);
for (Pair<byte[], byte[]> kv : res.values) {
values.add(kv);
}
return res.allFetched;
}
@Override
public boolean multiGet(
String tableName,
byte[] hashKey,
byte[] startSortKey,
byte[] stopSortKey,
MultiGetOptions options,
List<Pair<byte[], byte[]>> values)
throws PException {
return multiGet(tableName, hashKey, startSortKey, stopSortKey, options, 100, 1000000, values);
}
@Override
public void batchMultiGet(
String tableName, List<Pair<byte[], List<byte[]>>> keys, List<HashKeyData> values)
throws PException {
PegasusTable tb = getTable(tableName);
tb.batchMultiGet(keys, values, 0);
}
@Override
public int batchMultiGet2(
String tableName,
List<Pair<byte[], List<byte[]>>> keys,
List<Pair<PException, HashKeyData>> results)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.batchMultiGet2(keys, results, 0);
}
@Override
public boolean multiGetSortKeys(
String tableName, byte[] hashKey, int maxFetchCount, int maxFetchSize, List<byte[]> sortKeys)
throws PException {
if (sortKeys == null) {
throw new PException("Invalid parameter: sortKeys should not be null");
}
PegasusTable table = getTable(tableName);
PegasusTableInterface.MultiGetSortKeysResult result =
table.multiGetSortKeys(hashKey, maxFetchCount, maxFetchSize, 0);
for (byte[] key : result.keys) {
sortKeys.add(key);
}
return result.allFetched;
}
@Override
public boolean multiGetSortKeys(String tableName, byte[] hashKey, List<byte[]> sortKeys)
throws PException {
return multiGetSortKeys(tableName, hashKey, 100, 1000000, sortKeys);
}
@Override
public void set(String tableName, byte[] hashKey, byte[] sortKey, byte[] value, int ttlSeconds)
throws PException {
PegasusTable tb = getTable(tableName);
tb.set(hashKey, sortKey, value, ttlSeconds, 0);
}
@Override
public void set(String tableName, byte[] hashKey, byte[] sortKey, byte[] value)
throws PException {
set(tableName, hashKey, sortKey, value, 0);
}
@Override
public void batchSet(String tableName, List<SetItem> items) throws PException {
PegasusTable tb = getTable(tableName);
tb.batchSet(items, 0);
}
@Override
public int batchSet2(String tableName, List<SetItem> items, List<PException> results)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.batchSet2(items, results, 0);
}
@Override
public void multiSet(
String tableName, byte[] hashKey, List<Pair<byte[], byte[]>> values, int ttlSeconds)
throws PException {
PegasusTable tb = getTable(tableName);
tb.multiSet(hashKey, values, ttlSeconds, 0);
}
@Override
public void multiSet(String tableName, byte[] hashKey, List<Pair<byte[], byte[]>> values)
throws PException {
multiSet(tableName, hashKey, values, 0);
}
@Override
public void batchMultiSet(String tableName, List<HashKeyData> items, int ttlSeconds)
throws PException {
PegasusTable tb = getTable(tableName);
tb.batchMultiSet(items, ttlSeconds, 0);
}
@Override
public void batchMultiSet(String tableName, List<HashKeyData> items) throws PException {
batchMultiSet(tableName, items, 0);
}
@Override
public int batchMultiSet2(
String tableName, List<HashKeyData> items, int ttlSeconds, List<PException> results)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.batchMultiSet2(items, ttlSeconds, results, 0);
}
@Override
public int batchMultiSet2(String tableName, List<HashKeyData> items, List<PException> results)
throws PException {
return batchMultiSet2(tableName, items, 0, results);
}
@Override
public void del(String tableName, byte[] hashKey, byte[] sortKey) throws PException {
PegasusTable tb = getTable(tableName);
tb.del(hashKey, sortKey, 0);
}
@Override
public void batchDel(String tableName, List<Pair<byte[], byte[]>> keys) throws PException {
PegasusTable tb = getTable(tableName);
tb.batchDel(keys, 0);
}
@Override
public int batchDel2(String tableName, List<Pair<byte[], byte[]>> keys, List<PException> results)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.batchDel2(keys, results, 0);
}
@Override
public void multiDel(String tableName, byte[] hashKey, List<byte[]> sortKeys) throws PException {
PegasusTable tb = getTable(tableName);
tb.multiDel(hashKey, sortKeys, 0);
}
@Override
public void delRange(
String tableName,
byte[] hashKey,
byte[] startSortKey,
byte[] stopSortKey,
DelRangeOptions options)
throws PException {
PegasusTable tb = getTable(tableName);
tb.delRange(hashKey, startSortKey, stopSortKey, options, 0);
}
@Override
public void batchMultiDel(String tableName, List<Pair<byte[], List<byte[]>>> keys)
throws PException {
PegasusTable tb = getTable(tableName);
tb.batchMultiDel(keys, 0);
}
@Override
public int batchMultiDel2(
String tableName, List<Pair<byte[], List<byte[]>>> keys, List<PException> results)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.batchMultiDel2(keys, results, 0);
}
@Override
public long incr(String tableName, byte[] hashKey, byte[] sortKey, long increment, int ttlSeconds)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.incr(hashKey, sortKey, increment, ttlSeconds, 0);
}
@Override
public long incr(String tableName, byte[] hashKey, byte[] sortKey, long increment)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.incr(hashKey, sortKey, increment, 0);
}
@Override
public PegasusTableInterface.CheckAndSetResult checkAndSet(
String tableName,
byte[] hashKey,
byte[] checkSortKey,
CheckType checkType,
byte[] checkOperand,
byte[] setSortKey,
byte[] setValue,
CheckAndSetOptions options)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.checkAndSet(
hashKey, checkSortKey, checkType, checkOperand, setSortKey, setValue, options, 0);
}
@Override
public PegasusTableInterface.CheckAndMutateResult checkAndMutate(
String tableName,
byte[] hashKey,
byte[] checkSortKey,
CheckType checkType,
byte[] checkOperand,
Mutations mutations,
CheckAndMutateOptions options)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.checkAndMutate(hashKey, checkSortKey, checkType, checkOperand, mutations, options, 0);
}
@Override
public PegasusTableInterface.CompareExchangeResult compareExchange(
String tableName,
byte[] hashKey,
byte[] sortKey,
byte[] expectedValue,
byte[] desiredValue,
int ttlSeconds)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.compareExchange(hashKey, sortKey, expectedValue, desiredValue, ttlSeconds, 0);
}
@Override
public int ttl(String tableName, byte[] hashKey, byte[] sortKey) throws PException {
PegasusTable tb = getTable(tableName);
return tb.ttl(hashKey, sortKey, 0);
}
@Override
public PegasusScannerInterface getScanner(
String tableName,
byte[] hashKey,
byte[] startSortKey,
byte[] stopSortKey,
ScanOptions options)
throws PException {
PegasusTable tb = getTable(tableName);
return tb.getScanner(hashKey, startSortKey, stopSortKey, options);
}
@Override
public List<PegasusScannerInterface> getUnorderedScanners(
String tableName, int maxScannerCount, ScanOptions options) throws PException {
PegasusTable tb = getTable(tableName);
return tb.getUnorderedScanners(maxScannerCount, options);
}
}