Search in sources :

Example 1 with StateStoreRuntimeException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException in project bookkeeper by apache.

the class RocksdbKVStore method putRaw.

private void putRaw(K key, byte[] keyBytes, V value, long revision) {
    WriteBatch batch = new WriteBatch();
    if (revision > 0) {
        // last revision has been set to revision bytes
        batch.put(metaCfHandle, LAST_REVISION, lastRevisionBytes);
    }
    if (null == value) {
        // delete a key if value is null
        batch.remove(dataCfHandle, keyBytes);
    } else {
        byte[] valBytes = valCoder.encode(value);
        batch.put(dataCfHandle, keyBytes, valBytes);
    }
    try {
        db.write(writeOpts, batch);
    } catch (RocksDBException e) {
        throw new StateStoreRuntimeException("Error while updating key " + key + " to value " + value + " from store " + name, e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) WriteBatch(org.rocksdb.WriteBatch) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Example 2 with StateStoreRuntimeException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException in project bookkeeper by apache.

the class MVCCStoreImpl method processTxn.

synchronized TxnResult<K, V> processTxn(long revision, TxnOp<K, V> op) {
    checkStoreOpen();
    // 1. process the compares
    boolean success = processCompares(op);
    // 2. prepare the response list
    List<Op<K, V>> operations;
    List<Result<K, V>> results;
    if (success) {
        operations = op.successOps();
    } else {
        operations = op.failureOps();
    }
    if (operations == null) {
        operations = Collections.emptyList();
    }
    results = Lists.newArrayListWithExpectedSize(operations.size());
    // 3. process the operations
    try (WriteBatch batch = new WriteBatch()) {
        for (Op<K, V> o : operations) {
            results.add(executeOp(revision, batch, o));
        }
        executeBatch(batch);
        // 4. repare the result
        TxnResultImpl<K, V> txnResult = resultFactory.newTxnResult(revision);
        txnResult.isSuccess(success);
        txnResult.results(results);
        txnResult.code(Code.OK);
        return txnResult;
    } catch (StateStoreRuntimeException e) {
        results.forEach(Result::close);
        throw e;
    }
}
Also used : TxnOp(org.apache.bookkeeper.api.kv.op.TxnOp) CompareOp(org.apache.bookkeeper.api.kv.op.CompareOp) PutOp(org.apache.bookkeeper.api.kv.op.PutOp) Op(org.apache.bookkeeper.api.kv.op.Op) IncrementOp(org.apache.bookkeeper.api.kv.op.IncrementOp) RangeOp(org.apache.bookkeeper.api.kv.op.RangeOp) DeleteOp(org.apache.bookkeeper.api.kv.op.DeleteOp) KV(org.apache.bookkeeper.common.kv.KV) WriteBatch(org.rocksdb.WriteBatch) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException) PutResult(org.apache.bookkeeper.api.kv.result.PutResult) Result(org.apache.bookkeeper.api.kv.result.Result) IncrementResult(org.apache.bookkeeper.api.kv.result.IncrementResult) RangeResult(org.apache.bookkeeper.api.kv.result.RangeResult) TxnResult(org.apache.bookkeeper.api.kv.result.TxnResult) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult) CompareResult(org.apache.bookkeeper.api.kv.op.CompareResult)

Example 3 with StateStoreRuntimeException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException in project bookkeeper by apache.

the class MVCCStoreImpl method processIncrement.

synchronized IncrementResult<K, V> processIncrement(long revision, IncrementOp<K, V> op) {
    checkStoreOpen();
    WriteBatch batch = new WriteBatch();
    IncrementResult<K, V> result = null;
    try {
        result = increment(revision, batch, op);
        executeBatch(batch);
        return result;
    } catch (StateStoreRuntimeException e) {
        if (null != result) {
            result.close();
        }
        throw e;
    } finally {
        RocksUtils.close(batch);
    }
}
Also used : KV(org.apache.bookkeeper.common.kv.KV) WriteBatch(org.rocksdb.WriteBatch) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Example 4 with StateStoreRuntimeException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException in project bookkeeper by apache.

the class MVCCRecordCoder method decode.

@Override
public MVCCRecord decode(ByteBuf data) {
    ByteBuf copy = data.slice();
    int metaLen = copy.readInt();
    ByteBuffer metaBuf = copy.slice(copy.readerIndex(), metaLen).nioBuffer();
    KeyMeta meta;
    try {
        meta = KeyMeta.parseFrom(metaBuf);
    } catch (InvalidProtocolBufferException e) {
        throw new StateStoreRuntimeException("Failed to deserialize key metadata", e);
    }
    copy.skipBytes(metaLen);
    int valLen = copy.readInt();
    ByteBuf valBuf = copy.retainedSlice(copy.readerIndex(), valLen);
    MVCCRecord record = MVCCRecord.newRecord();
    record.setCreateRev(meta.getCreateRevision());
    record.setModRev(meta.getModRevision());
    record.setVersion(meta.getVersion());
    record.setValue(valBuf, meta.getValueType());
    return record;
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) KeyMeta(org.apache.bookkeeper.stream.proto.kv.store.KeyMeta) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Example 5 with StateStoreRuntimeException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException in project bookkeeper by apache.

the class MVCCRecordCoder method encode.

@Override
public byte[] encode(MVCCRecord record) {
    KeyMeta meta = KeyMeta.newBuilder().setCreateRevision(record.getCreateRev()).setModRevision(record.getModRev()).setVersion(record.getVersion()).setValueType(record.getValueType()).build();
    int metaLen = meta.getSerializedSize();
    int valLen = record.getValue().readableBytes();
    int totalLen = // meta len
    Integer.BYTES + // meta bytes
    metaLen + // val len
    Integer.BYTES + // val bytes
    valLen;
    // NOTE: currently rocksdb jni only supports `byte[]`
    // we can improve this if rocksdb jni support ByteBuffer or ByteBuf
    byte[] data = new byte[totalLen];
    ByteBuf buf = Unpooled.wrappedBuffer(data);
    buf.writerIndex(0);
    buf.writeInt(metaLen);
    CodedOutputStream out = CodedOutputStream.newInstance(data, Integer.BYTES, metaLen);
    try {
        meta.writeTo(out);
    } catch (IOException e) {
        throw new StateStoreRuntimeException("Failed to serialize key metadata", e);
    }
    buf.writerIndex(buf.writerIndex() + metaLen);
    buf.writeInt(valLen);
    buf.writeBytes(record.getValue().slice());
    buf.release();
    return data;
}
Also used : CodedOutputStream(com.google.protobuf.CodedOutputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) KeyMeta(org.apache.bookkeeper.stream.proto.kv.store.KeyMeta) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Aggregations

StateStoreRuntimeException (org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)8 KV (org.apache.bookkeeper.common.kv.KV)5 WriteBatch (org.rocksdb.WriteBatch)5 ByteBuf (io.netty.buffer.ByteBuf)3 KeyMeta (org.apache.bookkeeper.stream.proto.kv.store.KeyMeta)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CompareOp (org.apache.bookkeeper.api.kv.op.CompareOp)1 CompareResult (org.apache.bookkeeper.api.kv.op.CompareResult)1 DeleteOp (org.apache.bookkeeper.api.kv.op.DeleteOp)1 IncrementOp (org.apache.bookkeeper.api.kv.op.IncrementOp)1 Op (org.apache.bookkeeper.api.kv.op.Op)1 PutOp (org.apache.bookkeeper.api.kv.op.PutOp)1 RangeOp (org.apache.bookkeeper.api.kv.op.RangeOp)1 TxnOp (org.apache.bookkeeper.api.kv.op.TxnOp)1 DeleteResult (org.apache.bookkeeper.api.kv.result.DeleteResult)1 IncrementResult (org.apache.bookkeeper.api.kv.result.IncrementResult)1 PutResult (org.apache.bookkeeper.api.kv.result.PutResult)1