Search in sources :

Example 1 with NamespaceMetadata

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

the class RootRangeStoreImpl method executeCreateNamespaceTxn.

private CompletableFuture<CreateNamespaceResponse> executeCreateNamespaceTxn(long currentNsId, long currentNsIdRev, CreateNamespaceRequest request) {
    long namespaceId = currentNsId + 1;
    String nsName = request.getName();
    NamespaceMetadata metadata = NamespaceMetadata.newBuilder().setProps(NamespaceProperties.newBuilder().setNamespaceId(namespaceId).setNamespaceName(nsName).setDefaultStreamConf(request.getColConf().getDefaultStreamConf())).build();
    byte[] nsNameKey = getNamespaceNameKey(nsName);
    byte[] nsNameVal = Bytes.toBytes(namespaceId);
    byte[] nsIdKey = getNamespaceIdKey(namespaceId);
    byte[] nsIdVal = metadata.toByteArray();
    TxnOp<byte[], byte[]> txn = store.newTxn().If(store.newCompareValue(CompareResult.EQUAL, nsNameKey, null), currentNsIdRev < 0 ? store.newCompareValue(CompareResult.EQUAL, NS_ID_KEY, null) : store.newCompareModRevision(CompareResult.EQUAL, NS_ID_KEY, currentNsIdRev)).Then(store.newPut(nsNameKey, nsNameVal), store.newPut(nsIdKey, nsIdVal), store.newPut(NS_ID_KEY, Bytes.toBytes(namespaceId))).build();
    return store.txn(txn).thenApply(txnResult -> {
        try {
            CreateNamespaceResponse.Builder respBuilder = CreateNamespaceResponse.newBuilder();
            if (txnResult.isSuccess()) {
                respBuilder.setCode(StatusCode.SUCCESS);
                respBuilder.setColProps(metadata.getProps());
            } else {
                // TODO: differentiate the error code
                respBuilder.setCode(StatusCode.INTERNAL_SERVER_ERROR);
            }
            return respBuilder.build();
        } finally {
            txnResult.close();
        }
    }).whenComplete((resp, cause) -> txn.close());
}
Also used : ProtoUtils.validateStreamName(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.validateStreamName) GetStreamRequest(org.apache.bookkeeper.stream.proto.storage.GetStreamRequest) Options(org.apache.bookkeeper.api.kv.options.Options) DeleteNamespaceRequest(org.apache.bookkeeper.stream.proto.storage.DeleteNamespaceRequest) CompletableFuture(java.util.concurrent.CompletableFuture) CreateStreamRequest(org.apache.bookkeeper.stream.proto.storage.CreateStreamRequest) GetStreamResponse(org.apache.bookkeeper.stream.proto.storage.GetStreamResponse) DeleteStreamRequest(org.apache.bookkeeper.stream.proto.storage.DeleteStreamRequest) UTF_8(com.google.common.base.Charsets.UTF_8) MIN_DATA_STREAM_ID(org.apache.bookkeeper.stream.protocol.ProtocolConstants.MIN_DATA_STREAM_ID) StreamName(org.apache.bookkeeper.stream.proto.StreamName) CreateStreamResponse(org.apache.bookkeeper.stream.proto.storage.CreateStreamResponse) DeleteStreamResponse(org.apache.bookkeeper.stream.proto.storage.DeleteStreamResponse) MVCCAsyncStore(org.apache.bookkeeper.statelib.api.mvcc.MVCCAsyncStore) TxnOp(org.apache.bookkeeper.api.kv.op.TxnOp) NamespaceProperties(org.apache.bookkeeper.stream.proto.NamespaceProperties) NamespaceMetadata(org.apache.bookkeeper.stream.proto.NamespaceMetadata) DeleteNamespaceResponse(org.apache.bookkeeper.stream.proto.storage.DeleteNamespaceResponse) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) URI(java.net.URI) StatusCode(org.apache.bookkeeper.stream.proto.storage.StatusCode) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) StorageContainerPlacementPolicy(org.apache.bookkeeper.stream.protocol.util.StorageContainerPlacementPolicy) CreateNamespaceResponse(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceResponse) RangeOp(org.apache.bookkeeper.api.kv.op.RangeOp) RootRangeStore(org.apache.bookkeeper.stream.storage.api.metadata.RootRangeStore) StreamConfiguration(org.apache.bookkeeper.stream.proto.StreamConfiguration) Bytes(org.apache.bookkeeper.common.util.Bytes) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) Slf4j(lombok.extern.slf4j.Slf4j) CreateNamespaceRequest(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) CompareResult(org.apache.bookkeeper.api.kv.op.CompareResult) ProtoUtils.validateNamespaceName(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.validateNamespaceName) GetNamespaceRequest(org.apache.bookkeeper.stream.proto.storage.GetNamespaceRequest) GetNamespaceResponse(org.apache.bookkeeper.stream.proto.storage.GetNamespaceResponse) NamespaceMetadata(org.apache.bookkeeper.stream.proto.NamespaceMetadata)

Aggregations

UTF_8 (com.google.common.base.Charsets.UTF_8)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 URI (java.net.URI)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CompareResult (org.apache.bookkeeper.api.kv.op.CompareResult)1 RangeOp (org.apache.bookkeeper.api.kv.op.RangeOp)1 TxnOp (org.apache.bookkeeper.api.kv.op.TxnOp)1 Options (org.apache.bookkeeper.api.kv.options.Options)1 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)1 FutureUtils (org.apache.bookkeeper.common.concurrent.FutureUtils)1 Bytes (org.apache.bookkeeper.common.util.Bytes)1 MVCCAsyncStore (org.apache.bookkeeper.statelib.api.mvcc.MVCCAsyncStore)1 NamespaceMetadata (org.apache.bookkeeper.stream.proto.NamespaceMetadata)1 NamespaceProperties (org.apache.bookkeeper.stream.proto.NamespaceProperties)1 StreamConfiguration (org.apache.bookkeeper.stream.proto.StreamConfiguration)1 StreamName (org.apache.bookkeeper.stream.proto.StreamName)1 StreamProperties (org.apache.bookkeeper.stream.proto.StreamProperties)1 CreateNamespaceRequest (org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest)1