Search in sources :

Example 1 with TxnResult

use of org.apache.bookkeeper.api.kv.result.TxnResult 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 2 with TxnResult

use of org.apache.bookkeeper.api.kv.result.TxnResult in project bookkeeper by apache.

the class TableClientTest method testTableAPI.

@Test
public void testTableAPI() throws Exception {
    // Create a namespace
    NamespaceConfiguration nsConf = NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build();
    NamespaceProperties nsProps = FutureUtils.result(adminClient.createNamespace(namespace, nsConf));
    assertEquals(namespace, nsProps.getNamespaceName());
    assertEquals(nsConf.getDefaultStreamConf(), nsProps.getDefaultStreamConf());
    // Create a stream
    String streamName = testName.getMethodName() + "_stream";
    StreamConfiguration streamConf = StreamConfiguration.newBuilder(DEFAULT_STREAM_CONF).build();
    StreamProperties streamProps = FutureUtils.result(adminClient.createStream(namespace, streamName, streamConf));
    assertEquals(streamName, streamProps.getStreamName());
    assertEquals(StreamConfiguration.newBuilder(streamConf).setBackendServiceUrl(defaultBackendUri.toString()).build(), streamProps.getStreamConf());
    // Open the table
    PTable<ByteBuf, ByteBuf> table = FutureUtils.result(storageClient.openPTable(streamName));
    byte[] rKey = "routing-key".getBytes(UTF_8);
    byte[] lKey = "testing-key".getBytes(UTF_8);
    byte[] value = "testing-value".getBytes(UTF_8);
    // put first key
    ByteBuf rKeyBuf = Unpooled.wrappedBuffer(rKey);
    ByteBuf lKeyBuf = Unpooled.wrappedBuffer(lKey);
    ByteBuf valBuf = Unpooled.wrappedBuffer(value);
    try (PutOption<ByteBuf> option = Options.putAndGet()) {
        try (PutResult<ByteBuf, ByteBuf> putResult = FutureUtils.result(table.put(rKeyBuf, lKeyBuf, valBuf, option))) {
            assertNull(putResult.prevKv());
        }
    }
    // put second key
    ByteBuf valBuf2 = Unpooled.wrappedBuffer("testing-value-2".getBytes(UTF_8));
    try (PutOption<ByteBuf> option = Options.putAndGet()) {
        try (PutResult<ByteBuf, ByteBuf> putResult = FutureUtils.result(table.put(rKeyBuf, lKeyBuf, valBuf2, option))) {
            assertNotNull(putResult.prevKv());
            KeyValue<ByteBuf, ByteBuf> prevKv = putResult.prevKv();
            assertEquals("testing-key", new String(ByteBufUtil.getBytes(prevKv.key()), UTF_8));
            assertEquals("testing-value", new String(ByteBufUtil.getBytes(prevKv.value()), UTF_8));
        }
    }
    // get key
    try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) {
        try (RangeResult<ByteBuf, ByteBuf> getResult = FutureUtils.result(table.get(rKeyBuf, lKeyBuf, option))) {
            assertEquals(1, getResult.count());
            assertEquals(1, getResult.kvs().size());
            KeyValue<ByteBuf, ByteBuf> kv = getResult.kvs().get(0);
            assertEquals("testing-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8));
            assertEquals("testing-value-2", new String(ByteBufUtil.getBytes(kv.value()), UTF_8));
        }
    }
    // delete key
    try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().prevKv(true).build()) {
        try (DeleteResult<ByteBuf, ByteBuf> deleteResult = FutureUtils.result(table.delete(rKeyBuf, lKeyBuf, option))) {
            assertEquals(1, deleteResult.numDeleted());
            assertEquals(1, deleteResult.prevKvs().size());
            KeyValue<ByteBuf, ByteBuf> kv = deleteResult.prevKvs().get(0);
            assertEquals("testing-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8));
            assertEquals("testing-value-2", new String(ByteBufUtil.getBytes(kv.value()), UTF_8));
        }
    }
    // write a range of key
    int numKvs = 100;
    rKeyBuf = Unpooled.wrappedBuffer("test-key".getBytes(UTF_8));
    try (PutOption<ByteBuf> option = Options.blindPut()) {
        for (int i = 0; i < numKvs; i++) {
            lKeyBuf = getLKey(i);
            valBuf = getValue(i);
            FutureUtils.result(table.put(rKeyBuf, lKeyBuf, valBuf, option));
        }
    }
    // get ranges
    ByteBuf lStartKey = getLKey(20);
    ByteBuf lEndKey = getLKey(50);
    try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().endKey(lEndKey).build()) {
        try (RangeResult<ByteBuf, ByteBuf> rangeResult = FutureUtils.result(table.get(rKeyBuf, lStartKey, option))) {
            assertEquals(31, rangeResult.kvs().size());
            assertEquals(31, rangeResult.count());
            int i = 20;
            for (KeyValue<ByteBuf, ByteBuf> kvPair : rangeResult.kvs()) {
                assertEquals(getLKey(i), kvPair.key());
                assertEquals(getValue(i), kvPair.value());
                ++i;
            }
            assertEquals(51, i);
        }
    }
    // delete range
    try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().prevKv(true).endKey(lEndKey).build()) {
        try (DeleteResult<ByteBuf, ByteBuf> deleteRangeResult = FutureUtils.result(table.delete(rKeyBuf, lStartKey, option))) {
            assertEquals(31, deleteRangeResult.numDeleted());
            assertEquals(31, deleteRangeResult.prevKvs().size());
            int i = 20;
            for (KeyValue<ByteBuf, ByteBuf> kvPair : deleteRangeResult.prevKvs()) {
                assertEquals(getLKey(i), kvPair.key());
                assertEquals(getValue(i), kvPair.value());
                ++i;
            }
            assertEquals(51, i);
        }
    }
    // test txn
    byte[] lTxnKey = "txn-key".getBytes(UTF_8);
    ByteBuf lTxnKeyBuf = Unpooled.wrappedBuffer(lTxnKey);
    byte[] txnValue = "txn-value".getBytes(UTF_8);
    ByteBuf txnValueBuf = Unpooled.wrappedBuffer(txnValue);
    Txn<ByteBuf, ByteBuf> txn = table.txn(lTxnKeyBuf);
    CompletableFuture<TxnResult<ByteBuf, ByteBuf>> commitFuture = txn.If(table.opFactory().compareValue(CompareResult.EQUAL, lTxnKeyBuf, Unpooled.wrappedBuffer(new byte[0]))).Then(table.opFactory().newPut(lTxnKeyBuf, txnValueBuf, table.opFactory().optionFactory().newPutOption().build())).commit();
    try (TxnResult<ByteBuf, ByteBuf> txnResult = FutureUtils.result(commitFuture)) {
        assertTrue(txnResult.isSuccess());
        assertEquals(1, txnResult.results().size());
        Result<ByteBuf, ByteBuf> opResult = txnResult.results().get(0);
        assertEquals(OpType.PUT, opResult.type());
    }
    // get key
    try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) {
        try (RangeResult<ByteBuf, ByteBuf> getResult = FutureUtils.result(table.get(lTxnKeyBuf, lTxnKeyBuf, option))) {
            assertEquals(1, getResult.count());
            assertEquals(1, getResult.kvs().size());
            KeyValue<ByteBuf, ByteBuf> kv = getResult.kvs().get(0);
            assertEquals("txn-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8));
            assertEquals("txn-value", new String(ByteBufUtil.getBytes(kv.value()), UTF_8));
        }
    }
    txn = table.txn(lTxnKeyBuf);
    // txn failure
    commitFuture = txn.If(table.opFactory().compareValue(CompareResult.EQUAL, lTxnKeyBuf, Unpooled.wrappedBuffer(new byte[0]))).Then(table.opFactory().newPut(lTxnKeyBuf, valBuf, table.opFactory().optionFactory().newPutOption().build())).commit();
    try (TxnResult<ByteBuf, ByteBuf> txnResult = FutureUtils.result(commitFuture)) {
        assertFalse(txnResult.isSuccess());
        assertEquals(0, txnResult.results().size());
    }
}
Also used : TxnResult(org.apache.bookkeeper.api.kv.result.TxnResult) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) ByteBuf(io.netty.buffer.ByteBuf) Endpoint(org.apache.bookkeeper.stream.proto.common.Endpoint) NamespaceProperties(org.apache.bookkeeper.stream.proto.NamespaceProperties) StreamConfiguration(org.apache.bookkeeper.stream.proto.StreamConfiguration) NamespaceConfiguration(org.apache.bookkeeper.stream.proto.NamespaceConfiguration) Test(org.junit.Test)

Aggregations

TxnResult (org.apache.bookkeeper.api.kv.result.TxnResult)2 ByteBuf (io.netty.buffer.ByteBuf)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 RangeResult (org.apache.bookkeeper.api.kv.result.RangeResult)1 Result (org.apache.bookkeeper.api.kv.result.Result)1 KV (org.apache.bookkeeper.common.kv.KV)1 StateStoreRuntimeException (org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)1 NamespaceConfiguration (org.apache.bookkeeper.stream.proto.NamespaceConfiguration)1 NamespaceProperties (org.apache.bookkeeper.stream.proto.NamespaceProperties)1 StreamConfiguration (org.apache.bookkeeper.stream.proto.StreamConfiguration)1