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());
}
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);
}
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());
}
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);
}
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());
}
Aggregations