Search in sources :

Example 1 with BytePairWrapper

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();
}
Also used : BytePairWrapper(com.pingcap.tikv.BytePairWrapper) ByteString(com.google.protobuf.ByteString) CodecDataInput(com.pingcap.tikv.codec.CodecDataInput) CodecDataOutput(com.pingcap.tikv.codec.CodecDataOutput) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException) AllocateRowIDOverflowException(com.pingcap.tikv.exception.AllocateRowIDOverflowException)

Example 2 with BytePairWrapper

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));
}
Also used : BytePairWrapper(com.pingcap.tikv.BytePairWrapper) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) CodecDataOutput(com.pingcap.tikv.codec.CodecDataOutput) ByteString(com.google.protobuf.ByteString)

Example 3 with BytePairWrapper

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) {
    }
}
Also used : BytePairWrapper(com.pingcap.tikv.BytePairWrapper) TwoPhaseCommitter(com.pingcap.tikv.TwoPhaseCommitter) TiSession(com.pingcap.tikv.TiSession)

Aggregations

BytePairWrapper (com.pingcap.tikv.BytePairWrapper)3 ByteString (com.google.protobuf.ByteString)2 CodecDataOutput (com.pingcap.tikv.codec.CodecDataOutput)2 TiSession (com.pingcap.tikv.TiSession)1 TwoPhaseCommitter (com.pingcap.tikv.TwoPhaseCommitter)1 CodecDataInput (com.pingcap.tikv.codec.CodecDataInput)1 AllocateRowIDOverflowException (com.pingcap.tikv.exception.AllocateRowIDOverflowException)1 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)1 ArrayList (java.util.ArrayList)1