Search in sources :

Example 1 with CreateNamespaceRequest

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

Example 2 with CreateNamespaceRequest

use of org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest in project bookkeeper by apache.

the class TestGrpcRootRangeService method testCreateNamespaceSuccess.

// 
// Test Namespace API
// 
@Test
public void testCreateNamespaceSuccess() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    CreateNamespaceResponse createResp = CreateNamespaceResponse.newBuilder().setCode(StatusCode.SUCCESS).setColProps(namespaceProps).build();
    CreateNamespaceRequest createReq = createCreateNamespaceRequest(nsName, namespaceConf);
    when(rangeService.createNamespace(createReq)).thenReturn(CompletableFuture.completedFuture(createResp));
    AtomicReference<CreateNamespaceResponse> resultHolder = new AtomicReference<>();
    AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<CreateNamespaceResponse> streamObserver = new StreamObserver<CreateNamespaceResponse>() {

        @Override
        public void onNext(CreateNamespaceResponse resp) {
            resultHolder.set(resp);
        }

        @Override
        public void onError(Throwable t) {
            exceptionHolder.set(t);
            latch.countDown();
        }

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    };
    grpcService.createNamespace(CreateNamespaceRequest.newBuilder().setName(nsName).setColConf(namespaceConf).build(), streamObserver);
    latch.await();
    assertNull(exceptionHolder.get());
    assertNotNull(resultHolder.get());
    assertTrue(createResp == resultHolder.get());
    verify(rangeService, times(1)).createNamespace(createReq);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RangeStoreImpl(org.apache.bookkeeper.stream.storage.impl.RangeStoreImpl) CreateNamespaceResponse(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceResponse) ProtoUtils.createCreateNamespaceRequest(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createCreateNamespaceRequest) CreateNamespaceRequest(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest) Test(org.junit.Test)

Example 3 with CreateNamespaceRequest

use of org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest in project bookkeeper by apache.

the class TestProtoUtils method testCreateCreateNamespaceRequest.

// 
// Namespace API
// 
@Test
public void testCreateCreateNamespaceRequest() {
    NamespaceConfiguration colConf = NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build();
    CreateNamespaceRequest request = createCreateNamespaceRequest(name.getMethodName(), colConf);
    assertEquals(name.getMethodName(), request.getName());
    assertEquals(colConf, request.getColConf());
}
Also used : NamespaceConfiguration(org.apache.bookkeeper.stream.proto.NamespaceConfiguration) CreateNamespaceRequest(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest) ProtoUtils.createCreateNamespaceRequest(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createCreateNamespaceRequest) Test(org.junit.Test)

Example 4 with CreateNamespaceRequest

use of org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest in project bookkeeper by apache.

the class TestGrpcRootRangeService method testCreateNamespaceFailure.

@Test
public void testCreateNamespaceFailure() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    CreateNamespaceResponse createResp = CreateNamespaceResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
    CreateNamespaceRequest createReq = createCreateNamespaceRequest(nsName, namespaceConf);
    when(rangeService.createNamespace(createReq)).thenReturn(FutureUtils.exception(CAUSE));
    AtomicReference<CreateNamespaceResponse> resultHolder = new AtomicReference<>();
    AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<CreateNamespaceResponse> streamObserver = new StreamObserver<CreateNamespaceResponse>() {

        @Override
        public void onNext(CreateNamespaceResponse resp) {
            resultHolder.set(resp);
        }

        @Override
        public void onError(Throwable t) {
            exceptionHolder.set(t);
            latch.countDown();
        }

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    };
    grpcService.createNamespace(CreateNamespaceRequest.newBuilder().setName(nsName).setColConf(namespaceConf).build(), streamObserver);
    latch.await();
    assertNull(exceptionHolder.get());
    assertNotNull(resultHolder.get());
    assertEquals(createResp, resultHolder.get());
    verify(rangeService, times(1)).createNamespace(createReq);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RangeStoreImpl(org.apache.bookkeeper.stream.storage.impl.RangeStoreImpl) CreateNamespaceResponse(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceResponse) ProtoUtils.createCreateNamespaceRequest(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createCreateNamespaceRequest) CreateNamespaceRequest(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest) Test(org.junit.Test)

Example 5 with CreateNamespaceRequest

use of org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest in project bookkeeper by apache.

the class TestRangeStoreImpl method testCreateNamespaceMockRootStorageContainerStore.

@Test
public void testCreateNamespaceMockRootStorageContainerStore() throws Exception {
    String colName = "test-create-namespace-mock-root-storage-container-store";
    StorageContainer scStore = mock(StorageContainer.class);
    when(scStore.stop()).thenReturn(FutureUtils.value(null));
    rangeStore.getRegistry().setStorageContainer(ROOT_STORAGE_CONTAINER_ID, scStore);
    CreateNamespaceResponse createResp = CreateNamespaceResponse.newBuilder().setCode(StatusCode.NAMESPACE_EXISTS).build();
    CreateNamespaceRequest request = createCreateNamespaceRequest(colName, namespaceConf);
    when(scStore.createNamespace(request)).thenReturn(CompletableFuture.completedFuture(createResp));
    CompletableFuture<CreateNamespaceResponse> createRespFuture = rangeStore.createNamespace(request);
    verify(scStore, times(1)).createNamespace(request);
    assertTrue(createResp == createRespFuture.get());
}
Also used : ByteString(com.google.protobuf.ByteString) StorageContainer(org.apache.bookkeeper.stream.storage.api.sc.StorageContainer) CreateNamespaceResponse(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceResponse) ProtoUtils.createCreateNamespaceRequest(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createCreateNamespaceRequest) CreateNamespaceRequest(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest) Test(org.junit.Test)

Aggregations

CreateNamespaceRequest (org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest)6 CreateNamespaceResponse (org.apache.bookkeeper.stream.proto.storage.CreateNamespaceResponse)5 Test (org.junit.Test)5 ProtoUtils.createCreateNamespaceRequest (org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createCreateNamespaceRequest)4 StreamObserver (io.grpc.stub.StreamObserver)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 UTF_8 (com.google.common.base.Charsets.UTF_8)1 ByteString (com.google.protobuf.ByteString)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