use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.
the class TestBKDistributedLogManager method testNonPartitionedWritesInternal.
private void testNonPartitionedWritesInternal(String name, DistributedLogConfiguration conf) throws Exception {
BKDistributedLogManager dlm = createNewDLM(conf, name);
long txid = 1;
for (long i = 0; i < 3; i++) {
long start = txid;
BKSyncLogWriter writer = dlm.startLogSegmentNonPartitioned();
for (long j = 1; j <= DEFAULT_SEGMENT_SIZE; j++) {
writer.write(DLMTestUtil.getLogRecordInstance(txid++));
}
BKLogSegmentWriter perStreamLogWriter = writer.getCachedLogWriter();
writer.closeAndComplete();
BKLogWriteHandler blplm = dlm.createWriteHandler(true);
assertNotNull(zkc.exists(blplm.completedLedgerZNode(start, txid - 1, perStreamLogWriter.getLogSegmentSequenceNumber()), false));
Utils.ioResult(blplm.asyncClose());
}
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();
LogReader reader = dlm.getInputStream(1);
long numTrans = 0;
LogRecord record = reader.readNext(false);
long lastTxId = -1;
while (null != record) {
DLMTestUtil.verifyLogRecord(record);
assert (lastTxId < record.getTransactionId());
lastTxId = record.getTransactionId();
numTrans++;
record = reader.readNext(false);
}
reader.close();
assertEquals((txid - 1), numTrans);
}
use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.
the class TestBKDistributedLogManager method testWriteFailsAfterMarkEndOfStream.
@Test(timeout = 60000)
public void testWriteFailsAfterMarkEndOfStream() throws Exception {
String name = "distrlog-mark-end-failure";
DistributedLogManager dlm = createNewDLM(conf, name);
long txid = 1;
txid = writeAndMarkEndOfStream(dlm, txid);
assertEquals(txid - 1, dlm.getLastTxId());
LogRecord last = dlm.getLastLogRecord();
assertEquals(txid - 1, last.getTransactionId());
DLMTestUtil.verifyLogRecord(last);
assertTrue(dlm.isEndOfStreamMarked());
LogWriter writer = null;
boolean exceptionEncountered = false;
try {
writer = dlm.startLogSegmentNonPartitioned();
for (long j = 1; j <= DEFAULT_SEGMENT_SIZE / 2; j++) {
writer.write(DLMTestUtil.getLogRecordInstance(txid++));
}
} catch (EndOfStreamException exc) {
exceptionEncountered = true;
}
writer.close();
assertTrue(exceptionEncountered);
}
use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.
the class TestBKLogReadHandler method prepareLogSegmentsNonPartitioned.
private void prepareLogSegmentsNonPartitioned(String name, int numSegments, int numEntriesPerSegment) throws Exception {
DistributedLogManager dlm = createNewDLM(conf, name);
long txid = 1;
for (int sid = 0; sid < numSegments; ++sid) {
LogWriter out = dlm.startLogSegmentNonPartitioned();
for (int eid = 0; eid < numEntriesPerSegment; ++eid) {
LogRecord record = DLMTestUtil.getLargeLogRecordInstance(txid);
out.write(record);
++txid;
}
out.close();
}
dlm.close();
}
use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.
the class TestAsyncReaderWriter method writeLogSegment.
/**
* Write <code>numRecords</code> records to the log, starting with <code>startTxId</code>.
* It flushes every <code>flushPerNumRecords</code> records.
*
* @param dlm
* distributedlog manager
* @param numRecords
* num records to write
* @param startTxId
* start tx id
* @param flushPerNumRecords
* number records to flush
* @return next tx id
* @throws IOException
*/
private static long writeLogSegment(DistributedLogManager dlm, int numRecords, long startTxId, int flushPerNumRecords, boolean emptyRecord) throws IOException {
long txid = startTxId;
LogWriter writer = dlm.startLogSegmentNonPartitioned();
for (long j = 1; j <= numRecords; j++) {
if (emptyRecord) {
writer.write(DLMTestUtil.getEmptyLogRecordInstance(txid++));
} else {
writer.write(DLMTestUtil.getLargeLogRecordInstance(txid++));
}
if (j % flushPerNumRecords == 0) {
writer.flush();
writer.commit();
}
}
writer.flush();
writer.commit();
writer.close();
return txid;
}
use of org.apache.distributedlog.api.LogWriter in project bookkeeper by apache.
the class TestBKDistributedLogNamespace method testInvalidStreamName.
@Test(timeout = 60000)
public void testInvalidStreamName() throws Exception {
assertFalse(DLUtils.isReservedStreamName("test"));
assertTrue(DLUtils.isReservedStreamName(".test"));
URI uri = createDLMURI("/" + runtime.getMethodName());
Namespace namespace = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
try {
namespace.openLog(".test1");
fail("Should fail to create invalid stream .test");
} catch (InvalidStreamNameException isne) {
// expected
}
DistributedLogManager dlm = namespace.openLog("test1");
LogWriter writer = dlm.startLogSegmentNonPartitioned();
writer.write(DLMTestUtil.getLogRecordInstance(1));
writer.close();
dlm.close();
try {
namespace.openLog(".test2");
fail("Should fail to create invalid stream .test2");
} catch (InvalidStreamNameException isne) {
// expected
}
try {
namespace.openLog("/ test2");
fail("should fail to create invalid stream / test2");
} catch (InvalidStreamNameException isne) {
// expected
}
try {
char[] chars = new char[6];
for (int i = 0; i < chars.length; i++) {
chars[i] = 'a';
}
chars[0] = 0;
String streamName = new String(chars);
namespace.openLog(streamName);
fail("should fail to create invalid stream " + streamName);
} catch (InvalidStreamNameException isne) {
// expected
}
try {
char[] chars = new char[6];
for (int i = 0; i < chars.length; i++) {
chars[i] = 'a';
}
chars[3] = '\u0010';
String streamName = new String(chars);
namespace.openLog(streamName);
fail("should fail to create invalid stream " + streamName);
} catch (InvalidStreamNameException isne) {
// expected
}
DistributedLogManager newDLM = namespace.openLog("test_2-3");
LogWriter newWriter = newDLM.startLogSegmentNonPartitioned();
newWriter.write(DLMTestUtil.getLogRecordInstance(1));
newWriter.close();
newDLM.close();
Iterator<String> streamIter = namespace.getLogs();
Set<String> streamSet = Sets.newHashSet(streamIter);
assertEquals(2, streamSet.size());
assertTrue(streamSet.contains("test1"));
assertTrue(streamSet.contains("test_2-3"));
namespace.close();
}
Aggregations