Search in sources :

Example 6 with RangeMetadata

use of org.apache.bookkeeper.stream.proto.RangeMetadata in project bookkeeper by apache.

the class TestMetaRangeImpl method readRangeMetadataAndVerify.

private void readRangeMetadataAndVerify(long streamId, long rangeId, long expectedStartKey, long expectedEndKey, long expectedRid, long expectedCTime, long expectedFenceTime, RangeState expectedRangeState) throws Exception {
    byte[] rangeKey = MetaRangeImpl.getStreamRangeKey(streamId, rangeId);
    byte[] rangeMetadataBytes = FutureUtils.result(store.get(rangeKey));
    RangeMetadata rangeMetadata = RangeMetadata.parseFrom(rangeMetadataBytes);
    verifyRangeMetadata(rangeMetadata, expectedStartKey, expectedEndKey, expectedRid, expectedCTime, expectedFenceTime, expectedRangeState);
}
Also used : RangeMetadata(org.apache.bookkeeper.stream.proto.RangeMetadata)

Example 7 with RangeMetadata

use of org.apache.bookkeeper.stream.proto.RangeMetadata 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

RangeMetadata (org.apache.bookkeeper.stream.proto.RangeMetadata)7 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)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 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 Supplier (java.util.function.Supplier)1 GuardedBy (javax.annotation.concurrent.GuardedBy)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CompareResult (org.apache.bookkeeper.api.kv.op.CompareResult)1 Op (org.apache.bookkeeper.api.kv.op.Op)1 TxnOp (org.apache.bookkeeper.api.kv.op.TxnOp)1 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)1