Search in sources :

Example 1 with CreateStreamResponse

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

the class TestRootRangeStoreImpl method createStreamAndVerify.

private CreateStreamResponse createStreamAndVerify(String nsName, String streamName, long expectedStreamId) throws Exception {
    CompletableFuture<CreateStreamResponse> createFuture = rootRangeStore.createStream(createCreateStreamRequest(nsName, streamName, streamConf));
    CreateStreamResponse response = FutureUtils.result(createFuture);
    assertEquals(StatusCode.SUCCESS, response.getCode());
    assertEquals(MIN_DATA_STREAM_ID, response.getStreamProps().getStreamId());
    assertEquals(streamName, response.getStreamProps().getStreamName());
    assertEquals(streamConf, response.getStreamProps().getStreamConf());
    assertTrue(response.getStreamProps().getStorageContainerId() >= 0);
    return response;
}
Also used : CreateStreamResponse(org.apache.bookkeeper.stream.proto.storage.CreateStreamResponse)

Example 2 with CreateStreamResponse

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

the class TestRootRangeStoreImpl method testCreateStreamInvalidName.

// 
// Tests for Stream API
// 
@Test
public void testCreateStreamInvalidName() throws Exception {
    String nsName = name.getMethodName();
    String streamName = "";
    CompletableFuture<CreateStreamResponse> createFuture = rootRangeStore.createStream(createCreateStreamRequest(nsName, streamName, streamConf));
    CreateStreamResponse response = FutureUtils.result(createFuture);
    assertEquals(StatusCode.INVALID_STREAM_NAME, response.getCode());
}
Also used : CreateStreamResponse(org.apache.bookkeeper.stream.proto.storage.CreateStreamResponse) Test(org.junit.Test)

Example 3 with CreateStreamResponse

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

the class StorageContainerImplTest method testCreateStream.

@Test
public void testCreateStream() throws Exception {
    mockStorageContainer(SCID);
    CreateStreamResponse expectedResp = CreateStreamResponse.getDefaultInstance();
    when(rrStore.createStream(any(CreateStreamRequest.class))).thenReturn(FutureUtils.value(expectedResp));
    CreateStreamRequest expectedReq = CreateStreamRequest.getDefaultInstance();
    assertSame(expectedResp, FutureUtils.result(rrStore.createStream(expectedReq)));
    verify(rrStore, times(1)).createStream(same(expectedReq));
}
Also used : CreateStreamRequest(org.apache.bookkeeper.stream.proto.storage.CreateStreamRequest) CreateStreamResponse(org.apache.bookkeeper.stream.proto.storage.CreateStreamResponse) Test(org.junit.Test)

Example 4 with CreateStreamResponse

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

the class RootRangeStoreImpl method executeCreateStreamTxn.

private CompletableFuture<CreateStreamResponse> executeCreateStreamTxn(long nsId, String streamName, StreamConfiguration streamConf, long currentStreamId, long currentStreamIdRev) {
    long streamId;
    if (currentStreamId < 0) {
        streamId = MIN_DATA_STREAM_ID;
    } else {
        streamId = currentStreamId + 1;
    }
    long scId = placementPolicy.placeStreamRange(streamId, 0L);
    StreamConfiguration newStreamConf = streamConf;
    // no backend service url is provided, use the default service url
    if (isBlank(streamConf.getBackendServiceUrl())) {
        newStreamConf = StreamConfiguration.newBuilder(streamConf).setBackendServiceUrl(defaultServiceUri.toString()).build();
    }
    StreamProperties streamProps = StreamProperties.newBuilder().setStreamId(streamId).setStreamName(streamName).setStorageContainerId(scId).setStreamConf(newStreamConf).build();
    byte[] nsIdKey = getNamespaceIdKey(nsId);
    byte[] streamNameKey = getStreamNameKey(nsId, streamName);
    byte[] streamNameVal = Bytes.toBytes(streamId);
    byte[] streamIdKey = getStreamIdKey(nsId, streamId);
    byte[] streamIdVal = streamProps.toByteArray();
    TxnOp<byte[], byte[]> txn = store.newTxn().If(store.newCompareValue(CompareResult.NOT_EQUAL, nsIdKey, null), currentStreamIdRev < 0 ? store.newCompareValue(CompareResult.EQUAL, STREAM_ID_KEY, null) : store.newCompareModRevision(CompareResult.EQUAL, STREAM_ID_KEY, currentStreamIdRev), store.newCompareValue(CompareResult.EQUAL, streamNameKey, null)).Then(store.newPut(streamNameKey, streamNameVal), store.newPut(streamIdKey, streamIdVal), store.newPut(STREAM_ID_KEY, Bytes.toBytes(streamId))).build();
    return store.txn(txn).thenApply(txnResult -> {
        try {
            CreateStreamResponse.Builder respBuilder = CreateStreamResponse.newBuilder();
            if (txnResult.isSuccess()) {
                respBuilder.setCode(StatusCode.SUCCESS);
                respBuilder.setStreamProps(streamProps);
            } else {
                // TODO: differentiate the error codes
                respBuilder.setCode(StatusCode.INTERNAL_SERVER_ERROR);
            }
            return respBuilder.build();
        } finally {
            txnResult.close();
            txn.close();
        }
    }).exceptionally(cause -> {
        txn.close();
        return CreateStreamResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
    });
}
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) StreamConfiguration(org.apache.bookkeeper.stream.proto.StreamConfiguration) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties)

Example 5 with CreateStreamResponse

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

the class TestGrpcRootRangeService method testCreateStreamFailure.

@Test
public void testCreateStreamFailure() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    CreateStreamResponse createResp = CreateStreamResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
    CreateStreamRequest createReq = createCreateStreamRequest(nsName, streamName, DEFAULT_STREAM_CONF);
    when(rangeService.createStream(createReq)).thenReturn(FutureUtils.exception(CAUSE));
    AtomicReference<CreateStreamResponse> resultHolder = new AtomicReference<>();
    AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<CreateStreamResponse> streamObserver = new StreamObserver<CreateStreamResponse>() {

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

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

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

Aggregations

CreateStreamResponse (org.apache.bookkeeper.stream.proto.storage.CreateStreamResponse)9 Test (org.junit.Test)7 CreateStreamRequest (org.apache.bookkeeper.stream.proto.storage.CreateStreamRequest)5 ProtoUtils.createCreateStreamRequest (org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createCreateStreamRequest)3 StreamObserver (io.grpc.stub.StreamObserver)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CreateNamespaceResponse (org.apache.bookkeeper.stream.proto.storage.CreateNamespaceResponse)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