Search in sources :

Example 6 with LogWriter

use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.

the class TestBKDistributedLogNamespace method initDlogMeta.

private void initDlogMeta(String dlNamespace, String un, String streamName) throws Exception {
    URI uri = createDLMURI(dlNamespace);
    DistributedLogConfiguration newConf = new DistributedLogConfiguration();
    newConf.addConfiguration(conf);
    newConf.setCreateStreamIfNotExists(true);
    newConf.setZkAclId(un);
    Namespace namespace = NamespaceBuilder.newBuilder().conf(newConf).uri(uri).build();
    DistributedLogManager dlm = namespace.openLog(streamName);
    LogWriter writer = dlm.startLogSegmentNonPartitioned();
    for (int i = 0; i < 10; i++) {
        writer.write(DLMTestUtil.getLogRecordInstance(1L));
    }
    writer.close();
    dlm.close();
    namespace.close();
}
Also used : LogWriter(org.apache.distributedlog.api.LogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace)

Example 7 with LogWriter

use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.

the class TestBKDistributedLogNamespace method testCreateIfNotExists.

@Test(timeout = 60000)
public void testCreateIfNotExists() throws Exception {
    URI uri = createDLMURI("/" + runtime.getMethodName());
    ensureURICreated(zooKeeperClient.get(), uri);
    DistributedLogConfiguration newConf = new DistributedLogConfiguration();
    newConf.addConfiguration(conf);
    newConf.setCreateStreamIfNotExists(false);
    String streamName = "test-stream";
    Namespace namespace = NamespaceBuilder.newBuilder().conf(newConf).uri(uri).build();
    DistributedLogManager dlm = namespace.openLog(streamName);
    LogWriter writer;
    try {
        writer = dlm.startLogSegmentNonPartitioned();
        writer.write(DLMTestUtil.getLogRecordInstance(1L));
        fail("Should fail to write data if stream doesn't exist.");
    } catch (IOException ioe) {
    // expected
    }
    dlm.close();
    // create the stream
    namespace.createLog(streamName);
    DistributedLogManager newDLM = namespace.openLog(streamName);
    LogWriter newWriter = newDLM.startLogSegmentNonPartitioned();
    newWriter.write(DLMTestUtil.getLogRecordInstance(1L));
    newWriter.close();
    newDLM.close();
}
Also used : LogWriter(org.apache.distributedlog.api.LogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) IOException(java.io.IOException) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.junit.Test)

Example 8 with LogWriter

use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.

the class TestBKDistributedLogNamespace method createLogPathTest.

private void createLogPathTest(String logName) throws Exception {
    URI uri = createDLMURI("/" + runtime.getMethodName());
    ensureURICreated(zooKeeperClient.get(), uri);
    DistributedLogConfiguration newConf = new DistributedLogConfiguration();
    newConf.addConfiguration(conf);
    newConf.setCreateStreamIfNotExists(false);
    Namespace namespace = NamespaceBuilder.newBuilder().conf(newConf).uri(uri).build();
    DistributedLogManager dlm = namespace.openLog(logName);
    LogWriter writer;
    try {
        writer = dlm.startLogSegmentNonPartitioned();
        writer.write(DLMTestUtil.getLogRecordInstance(1L));
        writer.commit();
        fail("Should fail to write data if stream doesn't exist.");
    } catch (IOException ioe) {
    // expected
    }
    dlm.close();
}
Also used : LogWriter(org.apache.distributedlog.api.LogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) IOException(java.io.IOException) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace)

Example 9 with LogWriter

use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.

the class TestBKDistributedLogManager method testCheckLogExists.

@Test(timeout = 60000)
public void testCheckLogExists() throws Exception {
    String name = "distrlog-check-log-exists";
    DistributedLogManager dlm = createNewDLM(conf, name);
    long txid = 1;
    LogWriter writer = dlm.startLogSegmentNonPartitioned();
    for (long j = 1; j <= DEFAULT_SEGMENT_SIZE / 2; j++) {
        writer.write(DLMTestUtil.getLogRecordInstance(txid++));
    }
    writer.flush();
    writer.commit();
    writer.close();
    dlm.close();
    URI uri = createDLMURI("/" + name);
    Namespace namespace = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    assertTrue(namespace.logExists(name));
    assertFalse(namespace.logExists("non-existent-log"));
    URI nonExistentUri = createDLMURI("/" + "non-existent-ns");
    Namespace nonExistentNS = NamespaceBuilder.newBuilder().conf(conf).uri(nonExistentUri).build();
    assertFalse(nonExistentNS.logExists(name));
    int logCount = 0;
    Iterator<String> logIter = namespace.getLogs();
    while (logIter.hasNext()) {
        String log = logIter.next();
        logCount++;
        assertEquals(name, log);
    }
    assertEquals(1, logCount);
    namespace.close();
}
Also used : LogWriter(org.apache.distributedlog.api.LogWriter) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.junit.Test)

Aggregations

LogWriter (org.apache.distributedlog.api.LogWriter)9 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)7 URI (java.net.URI)5 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)5 Namespace (org.apache.distributedlog.api.namespace.Namespace)5 Test (org.junit.Test)4 IOException (java.io.IOException)2 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)1 LogReader (org.apache.distributedlog.api.LogReader)1 EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)1 InvalidStreamNameException (org.apache.distributedlog.exceptions.InvalidStreamNameException)1