Search in sources :

Example 1 with Result

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

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

the class MVCCAsyncStore method rDelete.

@Override
default CompletableFuture<KeyValue<K, V>> rDelete(K k, long expectedRevision) {
    TxnOp<K, V> op = getOpFactory().newTxn().If(newCompareModRevision(CompareResult.EQUAL, k, expectedRevision)).Then(getOpFactory().newDelete(k, Options.deleteAndGet())).build();
    return txn(op).thenCompose(result -> {
        try {
            Code code = result.code();
            if (Code.OK == code && !result.isSuccess()) {
                code = Code.BAD_REVISION;
            }
            if (Code.OK == code) {
                List<Result<K, V>> subResults = result.results();
                DeleteResult<K, V> deleteResult = (DeleteResult<K, V>) subResults.get(0);
                List<KeyValue<K, V>> prevKvs = deleteResult.getPrevKvsAndClear();
                if (prevKvs.isEmpty()) {
                    return FutureUtils.value(null);
                } else {
                    return FutureUtils.value(prevKvs.get(0));
                }
            } else {
                return failWithCode(code, "Failed to rDelete key " + k + " (mod_rev=" + expectedRevision + ") to store " + name());
            }
        } finally {
            result.close();
        }
    });
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) Code(org.apache.bookkeeper.api.kv.result.Code) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult) Result(org.apache.bookkeeper.api.kv.result.Result) RangeResult(org.apache.bookkeeper.api.kv.result.RangeResult) CompareResult(org.apache.bookkeeper.api.kv.op.CompareResult)

Example 3 with Result

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

the class MVCCAsyncStore method vDelete.

@Override
default CompletableFuture<KeyValue<K, V>> vDelete(K k, long expectedVersion) {
    TxnOp<K, V> op = getOpFactory().newTxn().If(newCompareVersion(CompareResult.EQUAL, k, expectedVersion)).Then(getOpFactory().newDelete(k, Options.deleteAndGet())).build();
    return txn(op).thenCompose(result -> {
        try {
            Code code = result.code();
            if (Code.OK == code && !result.isSuccess()) {
                code = Code.BAD_REVISION;
            }
            if (Code.OK == code) {
                List<Result<K, V>> subResults = result.results();
                DeleteResult<K, V> deleteResult = (DeleteResult<K, V>) subResults.get(0);
                List<KeyValue<K, V>> prevKvs = deleteResult.getPrevKvsAndClear();
                if (prevKvs.isEmpty()) {
                    return FutureUtils.value(null);
                } else {
                    return FutureUtils.value(prevKvs.get(0));
                }
            } else {
                return failWithCode(code, "Failed to vDelete key " + k + " (version=" + expectedVersion + ") to store " + name());
            }
        } finally {
            result.close();
        }
    });
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) Code(org.apache.bookkeeper.api.kv.result.Code) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult) Result(org.apache.bookkeeper.api.kv.result.Result) RangeResult(org.apache.bookkeeper.api.kv.result.RangeResult) CompareResult(org.apache.bookkeeper.api.kv.op.CompareResult)

Example 4 with Result

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

the class MetaRangeImpl method unsafeCreate.

private void unsafeCreate(CompletableFuture<Boolean> createFuture, StreamProperties streamProps) {
    // 1. verify the state
    checkLifecycleState(LifecycleState.UNINIT);
    this.lifecycleState = LifecycleState.CREATING;
    // 2. store the props/configuration
    synchronized (this) {
        this.streamProps = streamProps;
    }
    this.streamId = streamProps.getStreamId();
    this.cTime = this.mTime = System.currentTimeMillis();
    // 3. create the ranges
    List<RangeProperties> propertiesList = split(streamId, streamProps.getStreamConf().getInitialNumRanges(), nextRangeId, placementPolicy);
    List<Op<byte[], byte[]>> successOps = Lists.newArrayListWithExpectedSize(propertiesList.size() + 1);
    for (RangeProperties props : propertiesList) {
        RangeMetadata meta = RangeMetadata.newBuilder().setProps(props).setCreateTime(cTime).setFenceTime(Long.MAX_VALUE).setState(RangeState.RANGE_ACTIVE).addAllParents(Lists.newArrayList()).build();
        ranges.put(props.getRangeId(), meta);
        currentRanges.add(props.getRangeId());
        successOps.add(store.newPut(getStreamRangeKey(streamId, props.getRangeId()), meta.toByteArray()));
    }
    nextRangeId += propertiesList.size();
    // serialize the stream metadata
    byte[] streamMetadataKey = getStreamMetadataKey(streamId);
    successOps.add(store.newPut(streamMetadataKey, toStreamMetadata(LifecycleState.CREATED).toByteArray()));
    TxnOp<byte[], byte[]> txn = store.newTxn().If(store.newCompareValue(CompareResult.EQUAL, streamMetadataKey, null)).Then(successOps.toArray(new Op[successOps.size()])).build();
    if (log.isTraceEnabled()) {
        log.trace("Execute create stream metadata range txn {}", streamProps);
    }
    store.txn(txn).thenApplyAsync(txnResult -> {
        try {
            if (log.isTraceEnabled()) {
                log.trace("Create stream metadata range txn result = {}", txnResult.isSuccess());
            }
            if (txnResult.isSuccess()) {
                List<Result<byte[], byte[]>> results = txnResult.results();
                MetaRangeImpl.this.revision = results.get(results.size() - 1).revision();
                // mark the state to CREATED
                this.lifecycleState = LifecycleState.CREATED;
                createFuture.complete(true);
            } else {
                createFuture.complete(false);
            }
            return null;
        } finally {
            txnResult.close();
        }
    }, executor).exceptionally(cause -> {
        createFuture.completeExceptionally(cause);
        return null;
    });
}
Also used : RangeMetadata(org.apache.bookkeeper.stream.proto.RangeMetadata) RangeState(org.apache.bookkeeper.stream.proto.RangeState) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ProtoUtils.split(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.split) MVCCAsyncStore(org.apache.bookkeeper.statelib.api.mvcc.MVCCAsyncStore) StreamMetadata(org.apache.bookkeeper.stream.proto.StreamMetadata) Lists(com.google.common.collect.Lists) TxnOp(org.apache.bookkeeper.api.kv.op.TxnOp) MetaRange(org.apache.bookkeeper.stream.storage.api.metadata.stream.MetaRange) ImmutableList(com.google.common.collect.ImmutableList) ProtoUtils.isStreamCreated(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.isStreamCreated) RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) LifecycleState(org.apache.bookkeeper.stream.proto.StreamMetadata.LifecycleState) KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) ExecutorService(java.util.concurrent.ExecutorService) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) StorageContainerPlacementPolicy(org.apache.bookkeeper.stream.protocol.util.StorageContainerPlacementPolicy) MIN_DATA_RANGE_ID(org.apache.bookkeeper.stream.protocol.ProtocolConstants.MIN_DATA_RANGE_ID) DataRangeNotFoundException(org.apache.bookkeeper.stream.storage.exceptions.DataRangeNotFoundException) ServingState(org.apache.bookkeeper.stream.proto.StreamMetadata.ServingState) Result(org.apache.bookkeeper.api.kv.result.Result) StreamConfiguration(org.apache.bookkeeper.stream.proto.StreamConfiguration) Bytes(org.apache.bookkeeper.common.util.Bytes) NavigableMap(java.util.NavigableMap) GuardedBy(javax.annotation.concurrent.GuardedBy) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) Maps(com.google.common.collect.Maps) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Consumer(java.util.function.Consumer) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CompareResult(org.apache.bookkeeper.api.kv.op.CompareResult) Op(org.apache.bookkeeper.api.kv.op.Op) RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) TxnOp(org.apache.bookkeeper.api.kv.op.TxnOp) Op(org.apache.bookkeeper.api.kv.op.Op) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RangeMetadata(org.apache.bookkeeper.stream.proto.RangeMetadata)

Aggregations

CompareResult (org.apache.bookkeeper.api.kv.op.CompareResult)4 Result (org.apache.bookkeeper.api.kv.result.Result)4 DeleteResult (org.apache.bookkeeper.api.kv.result.DeleteResult)3 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)3 RangeResult (org.apache.bookkeeper.api.kv.result.RangeResult)3 Op (org.apache.bookkeeper.api.kv.op.Op)2 TxnOp (org.apache.bookkeeper.api.kv.op.TxnOp)2 Code (org.apache.bookkeeper.api.kv.result.Code)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 List (java.util.List)1 NavigableMap (java.util.NavigableMap)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Consumer (java.util.function.Consumer)1