Search in sources :

Example 1 with NamespaceProperties

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

the class CreateNamespaceCommand method run.

@Override
protected void run(String namespace, StorageAdminClient admin) throws Exception {
    checkNotNull(namespaceName, "Namespace name is not provided");
    System.out.println("Creating namespace '" + namespaceName + "' ...");
    NamespaceProperties nsProps = result(admin.createNamespace(namespaceName, NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build()));
    System.out.println("Successfully created namespace '" + namespaceName + "':");
    System.out.println(nsProps);
}
Also used : NamespaceProperties(org.apache.bookkeeper.stream.proto.NamespaceProperties)

Example 2 with NamespaceProperties

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

the class StorageAdminClientTest method testStreamAPI.

@Test
public void testStreamAPI() throws Exception {
    // Create a namespace
    String nsName = testName.getMethodName() + "_ns";
    NamespaceConfiguration colConf = NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build();
    NamespaceProperties colProps = FutureUtils.result(adminClient.createNamespace(nsName, colConf));
    assertEquals(nsName, colProps.getNamespaceName());
    assertEquals(colConf.getDefaultStreamConf(), colProps.getDefaultStreamConf());
    // Create a stream
    String streamName = testName.getMethodName() + "_stream";
    StreamConfiguration streamConf = StreamConfiguration.newBuilder(DEFAULT_STREAM_CONF).build();
    StreamProperties streamProps = FutureUtils.result(adminClient.createStream(nsName, streamName, streamConf));
    assertEquals(streamName, streamProps.getStreamName());
    assertEquals(StreamConfiguration.newBuilder(streamConf).setBackendServiceUrl(defaultBackendUri.toString()).build(), streamProps.getStreamConf());
    // create a duplicated stream
    try {
        FutureUtils.result(adminClient.createStream(nsName, streamName, streamConf));
        fail("Should fail on creation if stream " + streamName + " already exists");
    } catch (StreamExistsException cee) {
    // expected
    } catch (ClientException ce) {
        // TODO: currently it throws InternalServerError for stream exists case
        assertTrue(ce.getMessage().endsWith("code = " + StatusCode.INTERNAL_SERVER_ERROR));
    }
    String notFoundStreamName = testName.getMethodName() + "_notfound";
    // get a not-found stream
    try {
        FutureUtils.result(adminClient.getStream(nsName, notFoundStreamName));
        fail("Should fail on get if stream " + notFoundStreamName + " doesn't exist");
    } catch (StreamNotFoundException cnfe) {
    // expected
    }
    // delete a not-found stream
    try {
        FutureUtils.result(adminClient.deleteStream(nsName, notFoundStreamName));
        fail("Should fail on delete if stream " + notFoundStreamName + " doesn't exist");
    } catch (StreamNotFoundException cnfe) {
    // expected
    }
    // get an existing stream
    StreamProperties getStreamProps = FutureUtils.result(adminClient.getStream(nsName, streamName));
    assertEquals(streamProps, getStreamProps);
    // delete an existing stream
    Boolean deleted = FutureUtils.result(adminClient.deleteStream(nsName, streamName));
    assertTrue(deleted);
    // the stream should not exist after deleted.
    try {
        FutureUtils.result(adminClient.getStream(nsName, streamName));
        fail("Should fail on get if stream " + nsName + " doesn't exist");
    } catch (StreamNotFoundException cnfe) {
    // expected
    }
}
Also used : NamespaceProperties(org.apache.bookkeeper.stream.proto.NamespaceProperties) StreamConfiguration(org.apache.bookkeeper.stream.proto.StreamConfiguration) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) StreamNotFoundException(org.apache.bookkeeper.clients.exceptions.StreamNotFoundException) NamespaceConfiguration(org.apache.bookkeeper.stream.proto.NamespaceConfiguration) StreamExistsException(org.apache.bookkeeper.clients.exceptions.StreamExistsException) ClientException(org.apache.bookkeeper.clients.exceptions.ClientException) Test(org.junit.Test)

Example 3 with NamespaceProperties

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

the class StreamCluster method createDefaultNamespaces.

private void createDefaultNamespaces() throws Exception {
    StorageClientSettings settings = StorageClientSettings.newBuilder().addEndpoints(getRpcEndpoints().toArray(new Endpoint[getRpcEndpoints().size()])).usePlaintext(true).build();
    log.info("RpcEndpoints are : {}", settings.endpoints());
    String namespaceName = "default";
    try (StorageAdminClient admin = StorageClientBuilder.newBuilder().withSettings(settings).buildAdmin()) {
        System.out.println("Creating namespace '" + namespaceName + "' ...");
        try {
            NamespaceProperties nsProps = result(admin.createNamespace(namespaceName, NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build()));
            System.out.println("Successfully created namespace '" + namespaceName + "':");
            System.out.println(nsProps);
        } catch (NamespaceExistsException nee) {
            System.out.println("Namespace '" + namespaceName + "' already exists.");
        }
    }
}
Also used : StorageClientSettings(org.apache.bookkeeper.clients.config.StorageClientSettings) Endpoint(org.apache.bookkeeper.stream.proto.common.Endpoint) NamespaceProperties(org.apache.bookkeeper.stream.proto.NamespaceProperties) StorageAdminClient(org.apache.bookkeeper.clients.admin.StorageAdminClient) NamespaceExistsException(org.apache.bookkeeper.clients.exceptions.NamespaceExistsException)

Example 4 with NamespaceProperties

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

the class StorageAdminClientTest method testNamespaceAPI.

@Test
public void testNamespaceAPI() throws Exception {
    // Create a namespace
    String nsName = testName.getMethodName();
    NamespaceConfiguration colConf = NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build();
    NamespaceProperties colProps = FutureUtils.result(adminClient.createNamespace(nsName, colConf));
    assertEquals(nsName, colProps.getNamespaceName());
    assertEquals(colConf.getDefaultStreamConf(), colProps.getDefaultStreamConf());
    // create a duplicated namespace
    try {
        FutureUtils.result(adminClient.createNamespace(nsName, colConf));
        fail("Should fail on creation if namespace " + nsName + " already exists");
    } catch (NamespaceExistsException cee) {
    // expected
    } catch (ClientException ce) {
        // TODO: currently range server throws InternalServerError
        assertTrue(ce.getMessage().endsWith("code = " + StatusCode.INTERNAL_SERVER_ERROR));
    }
    String notFoundColName = testName.getMethodName() + "_notfound";
    // get a not-found namespace
    try {
        FutureUtils.result(adminClient.getNamespace(notFoundColName));
        fail("Should fail on get if namespace " + notFoundColName + " doesn't exist");
    } catch (NamespaceNotFoundException cnfe) {
    // expected
    }
    // delete a not-found namespace
    try {
        FutureUtils.result(adminClient.deleteNamespace(notFoundColName));
        fail("Should fail on delete if namespace " + notFoundColName + " doesn't exist");
    } catch (NamespaceNotFoundException cnfe) {
    // expected
    }
    // get an existing namespace
    NamespaceProperties getColProps = FutureUtils.result(adminClient.getNamespace(nsName));
    assertEquals(colProps, getColProps);
    // delete an existing namespace
    Boolean deleted = FutureUtils.result(adminClient.deleteNamespace(nsName));
    assertTrue(deleted);
    // the namespace should not exist after deleted.
    try {
        FutureUtils.result(adminClient.getNamespace(nsName));
        fail("Should fail on get if namespace " + nsName + " doesn't exist");
    } catch (NamespaceNotFoundException cnfe) {
    // expected
    }
}
Also used : NamespaceProperties(org.apache.bookkeeper.stream.proto.NamespaceProperties) NamespaceConfiguration(org.apache.bookkeeper.stream.proto.NamespaceConfiguration) ClientException(org.apache.bookkeeper.clients.exceptions.ClientException) NamespaceExistsException(org.apache.bookkeeper.clients.exceptions.NamespaceExistsException) NamespaceNotFoundException(org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException) Test(org.junit.Test)

Example 5 with NamespaceProperties

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

the class TableClientSimpleTest method testTableSimpleAPI.

@Test
public void testTableSimpleAPI() throws Exception {
    // Create a namespace
    NamespaceConfiguration nsConf = NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build();
    NamespaceProperties nsProps = result(adminClient.createNamespace(namespace, nsConf));
    assertEquals(namespace, nsProps.getNamespaceName());
    assertEquals(nsConf.getDefaultStreamConf(), nsProps.getDefaultStreamConf());
    // Create a stream
    String streamName = testName.getMethodName() + "_stream";
    StreamConfiguration streamConf = StreamConfiguration.newBuilder(DEFAULT_STREAM_CONF).build();
    StreamProperties streamProps = result(adminClient.createStream(namespace, streamName, streamConf));
    assertEquals(streamName, streamProps.getStreamName());
    assertEquals(StreamConfiguration.newBuilder(streamConf).setBackendServiceUrl(defaultBackendUri.toString()).build(), streamProps.getStreamConf());
    // Open the table
    PTable<ByteBuf, ByteBuf> table = result(storageClient.openPTable(streamName));
    byte[] rKey = "routing-key".getBytes(UTF_8);
    byte[] lKey = "testing-key".getBytes(UTF_8);
    byte[] value1 = "testing-value-1".getBytes(UTF_8);
    byte[] value2 = "testing-value-2".getBytes(UTF_8);
    // put first key
    ByteBuf rKeyBuf = Unpooled.wrappedBuffer(rKey);
    ByteBuf lKeyBuf = Unpooled.wrappedBuffer(lKey);
    ByteBuf valBuf1 = Unpooled.wrappedBuffer(value1);
    ByteBuf valBuf2 = Unpooled.wrappedBuffer(value2);
    // normal put
    assertNull(result(table.put(rKeyBuf, lKeyBuf, valBuf1)));
    // putIfAbsent failure
    assertArrayEquals(value1, ByteBufUtil.getBytes(result(table.putIfAbsent(rKeyBuf, lKeyBuf, valBuf2))));
    // delete failure
    assertFalse(result(table.delete(rKeyBuf, lKeyBuf, valBuf2)));
    // delete success
    assertTrue(result(table.delete(rKeyBuf, lKeyBuf, valBuf1)));
    // get
    assertNull(result(table.get(rKeyBuf, lKeyBuf)));
    // putIfAbsent success
    assertNull(result(table.putIfAbsent(rKeyBuf, lKeyBuf, valBuf2)));
    // get returns val2
    assertArrayEquals(value2, ByteBufUtil.getBytes(result(table.get(rKeyBuf, lKeyBuf))));
    // vPut failure
    try {
        result(table.vPut(rKeyBuf, lKeyBuf, valBuf1, 9999L));
        fail("Should fail vPut if the version doesn't match");
    } catch (KvApiException e) {
        assertEquals(Code.BAD_REVISION, e.getCode());
    }
    // vPut success
    assertEquals(1L, result(table.vPut(rKeyBuf, lKeyBuf, valBuf1, 0L)).longValue());
    // vDelete failure
    try {
        result(table.vDelete(rKeyBuf, lKeyBuf, 9999L));
        fail("Should fail vDelete if the version doesn't match");
    } catch (KvApiException e) {
        assertEquals(Code.BAD_REVISION, e.getCode());
    }
    // vDelete success
    try (KeyValue<ByteBuf, ByteBuf> prevKv = result(table.vDelete(rKeyBuf, lKeyBuf, 1L))) {
        assertNotNull(prevKv);
        assertEquals(1L, prevKv.version());
        assertArrayEquals(value1, ByteBufUtil.getBytes(prevKv.value()));
    }
    // write a range of key
    int numKvs = 100;
    rKeyBuf = Unpooled.wrappedBuffer("test-key".getBytes(UTF_8));
    for (int i = 0; i < numKvs; i++) {
        lKeyBuf = getLKey(i);
        valBuf1 = getValue(i);
        result(table.put(rKeyBuf, lKeyBuf, valBuf1));
    }
    // get ranges
    ByteBuf lStartKey = getLKey(20);
    ByteBuf lEndKey = getLKey(50);
    List<KeyValue<ByteBuf, ByteBuf>> kvs = result(table.range(rKeyBuf, lStartKey, lEndKey));
    assertEquals(31, kvs.size());
    int i = 20;
    for (KeyValue<ByteBuf, ByteBuf> kvPair : kvs) {
        assertEquals(getLKey(i), kvPair.key());
        assertEquals(getValue(i), kvPair.value());
        ++i;
        kvPair.close();
    }
    assertEquals(51, i);
    // delete range
    kvs = result(table.deleteRange(rKeyBuf, lStartKey, lEndKey));
    assertEquals(31, kvs.size());
    i = 20;
    for (KeyValue<ByteBuf, ByteBuf> kvPair : kvs) {
        assertEquals(getLKey(i), kvPair.key());
        assertEquals(getValue(i), kvPair.value());
        ++i;
        kvPair.close();
    }
    assertEquals(51, i);
    // get ranges again
    kvs = result(table.range(rKeyBuf, lStartKey, lEndKey));
    assertTrue(kvs.isEmpty());
    byte[] lIncrKey = "test-incr-lkey".getBytes(UTF_8);
    ByteBuf lIncrKeyBuf = Unpooled.wrappedBuffer(lIncrKey);
    // test increment
    for (int j = 0; j < 5; j++) {
        result(table.increment(rKeyBuf, lIncrKeyBuf, 100L));
        long number = result(table.getNumber(rKeyBuf, lIncrKeyBuf));
        assertEquals(100L * (j + 1), number);
    }
    for (int j = 5; j < 10; j++) {
        long number = result(table.incrementAndGet(rKeyBuf, lIncrKeyBuf, 100L));
        assertEquals(100L * (j + 1), number);
    }
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException) ByteBuf(io.netty.buffer.ByteBuf) Endpoint(org.apache.bookkeeper.stream.proto.common.Endpoint) NamespaceProperties(org.apache.bookkeeper.stream.proto.NamespaceProperties) StreamConfiguration(org.apache.bookkeeper.stream.proto.StreamConfiguration) NamespaceConfiguration(org.apache.bookkeeper.stream.proto.NamespaceConfiguration) Test(org.junit.Test)

Aggregations

NamespaceProperties (org.apache.bookkeeper.stream.proto.NamespaceProperties)6 NamespaceConfiguration (org.apache.bookkeeper.stream.proto.NamespaceConfiguration)4 Test (org.junit.Test)4 StreamConfiguration (org.apache.bookkeeper.stream.proto.StreamConfiguration)3 StreamProperties (org.apache.bookkeeper.stream.proto.StreamProperties)3 Endpoint (org.apache.bookkeeper.stream.proto.common.Endpoint)3 ByteBuf (io.netty.buffer.ByteBuf)2 ClientException (org.apache.bookkeeper.clients.exceptions.ClientException)2 NamespaceExistsException (org.apache.bookkeeper.clients.exceptions.NamespaceExistsException)2 KvApiException (org.apache.bookkeeper.api.kv.exceptions.KvApiException)1 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)1 TxnResult (org.apache.bookkeeper.api.kv.result.TxnResult)1 StorageAdminClient (org.apache.bookkeeper.clients.admin.StorageAdminClient)1 StorageClientSettings (org.apache.bookkeeper.clients.config.StorageClientSettings)1 NamespaceNotFoundException (org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException)1 StreamExistsException (org.apache.bookkeeper.clients.exceptions.StreamExistsException)1 StreamNotFoundException (org.apache.bookkeeper.clients.exceptions.StreamNotFoundException)1