use of com.pingcap.tikv.BytePairWrapper in project tispark by pingcap.
the class RowIDAllocator method getMetaToUpdate.
private Optional<BytePairWrapper> getMetaToUpdate(ByteString key, byte[] oldVal, Snapshot snapshot) {
// 1. encode hash meta key
// 2. load meta via hash meta key from TiKV
// 3. update meta's filed count and set it back to TiKV
CodecDataOutput cdo = new CodecDataOutput();
ByteString metaKey = MetaCodec.encodeHashMetaKey(cdo, key.toByteArray());
long fieldCount = 0;
ByteString metaVal = snapshot.get(metaKey);
// big endian the 8 bytes
if (!metaVal.isEmpty()) {
try {
fieldCount = IntegerCodec.readULong(new CodecDataInput(metaVal.toByteArray()));
} catch (Exception ignored) {
LOG.warn("metaDecode failed, field is ignored." + KeyUtils.formatBytesUTF8(metaVal));
}
}
// update meta field count only oldVal is null
if (oldVal == null || oldVal.length == 0) {
fieldCount++;
cdo.reset();
cdo.writeLong(fieldCount);
return Optional.of(new BytePairWrapper(metaKey.toByteArray(), cdo.toBytes()));
}
return Optional.empty();
}
use of com.pingcap.tikv.BytePairWrapper in project tispark by pingcap.
the class RowIDAllocator method updateHash.
private long updateHash(ByteString key, ByteString field, Function<byte[], byte[]> calculateNewVal, Snapshot snapshot) {
// 1. encode hash data key
// 2. get value in byte from get operation
// 3. calculate new value via calculateNewVal
// 4. check old value equals to new value or not
// 5. set the new value back to TiKV via 2pc
// 6. encode a hash meta key
// 7. update a hash meta field count if needed
CodecDataOutput cdo = new CodecDataOutput();
MetaCodec.encodeHashDataKey(cdo, key.toByteArray(), field.toByteArray());
ByteString dataKey = cdo.toByteString();
byte[] oldVal = snapshot.get(dataKey.toByteArray());
byte[] newVal = calculateNewVal.apply(oldVal);
if (Arrays.equals(newVal, oldVal)) {
// not need to update
return 0L;
}
List<BytePairWrapper> pairs = new ArrayList<>(2);
pairs.add(new BytePairWrapper(dataKey.toByteArray(), newVal));
getMetaToUpdate(key, oldVal, snapshot).ifPresent(pairs::add);
set(pairs, snapshot.getTimestamp());
return Long.parseLong(new String(newVal));
}
use of com.pingcap.tikv.BytePairWrapper in project tispark by pingcap.
the class RowIDAllocator method set.
// set key value pairs to tikv via two phase committer protocol.
private void set(@Nonnull List<BytePairWrapper> pairs, @Nonnull TiTimestamp timestamp) {
Iterator<BytePairWrapper> iterator = pairs.iterator();
if (!iterator.hasNext()) {
return;
}
TiSession session = TiSession.getInstance(conf);
TwoPhaseCommitter twoPhaseCommitter = new TwoPhaseCommitter(conf, timestamp.getVersion());
BytePairWrapper primaryPair = iterator.next();
twoPhaseCommitter.prewritePrimaryKey(ConcreteBackOffer.newCustomBackOff(BackOffer.PREWRITE_MAX_BACKOFF), primaryPair.getKey(), primaryPair.getValue());
if (iterator.hasNext()) {
twoPhaseCommitter.prewriteSecondaryKeys(primaryPair.getKey(), iterator, BackOffer.PREWRITE_MAX_BACKOFF);
}
twoPhaseCommitter.commitPrimaryKey(ConcreteBackOffer.newCustomBackOff(BackOffer.BATCH_COMMIT_BACKOFF), primaryPair.getKey(), session.getTimestamp().getVersion());
try {
twoPhaseCommitter.close();
} catch (Throwable ignored) {
}
}
Aggregations