use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class CookieTest method testWriteToZooKeeper.
/**
* Test write cookie multiple times.
*/
@Test
public void testWriteToZooKeeper() throws Exception {
String[] ledgerDirs = new String[] { newDirectory(), newDirectory(), newDirectory() };
String journalDir = newDirectory();
ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
conf.setJournalDirName(journalDir).setLedgerDirNames(ledgerDirs).setBookiePort(bookiePort).setZkServers(zkUtil.getZooKeeperConnectString());
// should work fine
Bookie b = new Bookie(conf);
b.start();
b.shutdown();
Versioned<Cookie> zkCookie = Cookie.readFromRegistrationManager(rm, conf);
Version version1 = zkCookie.getVersion();
assertTrue("Invalid type expected ZkVersion type", version1 instanceof LongVersion);
LongVersion zkVersion1 = (LongVersion) version1;
Cookie cookie = zkCookie.getValue();
cookie.writeToRegistrationManager(rm, conf, version1);
zkCookie = Cookie.readFromRegistrationManager(rm, conf);
Version version2 = zkCookie.getVersion();
assertTrue("Invalid type expected ZkVersion type", version2 instanceof LongVersion);
LongVersion zkVersion2 = (LongVersion) version2;
assertEquals("Version mismatches!", zkVersion1.getLongVersion() + 1, zkVersion2.getLongVersion());
}
use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class TestZKLogStreamMetadataStore method testCreateLogMetadataWithMissingPaths.
private void testCreateLogMetadataWithMissingPaths(URI uri, String logName, String logIdentifier, List<String> pathsToDelete, boolean ownAllocator, boolean createLogFirst) throws Exception {
if (createLogFirst) {
createLog(zkc, uri, logName, logIdentifier);
}
// delete a path
for (String path : pathsToDelete) {
zkc.get().delete(path, -1);
}
LogMetadataForWriter logMetadata = Utils.ioResult(getLog(uri, logName, logIdentifier, zkc, ownAllocator, true));
final String logRootPath = getLogRootPath(uri, logName, logIdentifier);
List<Versioned<byte[]>> metadatas = Utils.ioResult(checkLogMetadataPaths(zkc.get(), logRootPath, ownAllocator));
if (ownAllocator) {
assertEquals("Should have 8 paths : ownAllocator = " + ownAllocator, 8, metadatas.size());
} else {
assertEquals("Should have 7 paths : ownAllocator = " + ownAllocator, 7, metadatas.size());
}
for (Versioned<byte[]> metadata : metadatas) {
assertTrue(pathExists(metadata));
assertTrue(((LongVersion) metadata.getVersion()).getLongVersion() >= 0L);
}
Versioned<byte[]> logSegmentsData = logMetadata.getMaxLSSNData();
assertEquals(DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO, DLUtils.deserializeLogSegmentSequenceNumber(logSegmentsData.getValue()));
Versioned<byte[]> maxTxIdData = logMetadata.getMaxTxIdData();
assertEquals(0L, DLUtils.deserializeTransactionId(maxTxIdData.getValue()));
if (ownAllocator) {
Versioned<byte[]> allocationData = logMetadata.getAllocationData();
assertEquals(0, allocationData.getValue().length);
}
}
use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class TestLedgerAllocator method testSuccessAllocatorShouldDeleteUnusedledger.
@Test(timeout = 60000)
public void testSuccessAllocatorShouldDeleteUnusedledger() throws Exception {
String allocationPath = "/allocation-delete-unused-ledger";
zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Stat stat = new Stat();
byte[] data = zkc.get().getData(allocationPath, false, stat);
Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
allocator1.allocate();
// wait until allocated
ZKTransaction txn1 = newTxn();
LedgerHandle lh1 = Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
// Second allocator kicks in
stat = new Stat();
data = zkc.get().getData(allocationPath, false, stat);
allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
allocator2.allocate();
// wait until allocated
ZKTransaction txn2 = newTxn();
LedgerHandle lh2 = Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
// should fail to commit txn1 as version is changed by second allocator
try {
Utils.ioResult(txn1.execute());
fail("Should fail commit obtaining ledger handle from first allocator" + " as allocator is modified by second allocator.");
} catch (ZKException ke) {
// as expected
}
Utils.ioResult(txn2.execute());
Utils.close(allocator1);
Utils.close(allocator2);
// ledger handle should be deleted
try {
lh1.close();
fail("LedgerHandle allocated by allocator1 should be deleted.");
} catch (BKException bke) {
// as expected
}
try {
bkc.get().openLedger(lh1.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
fail("LedgerHandle allocated by allocator1 should be deleted.");
} catch (BKException.BKNoSuchLedgerExistsException nslee) {
// as expected
}
long eid = lh2.addEntry("hello world".getBytes());
lh2.close();
LedgerHandle readLh = bkc.get().openLedger(lh2.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
int i = 0;
while (entries.hasMoreElements()) {
LedgerEntry entry = entries.nextElement();
assertEquals("hello world", new String(entry.getEntry(), UTF_8));
++i;
}
assertEquals(1, i);
}
use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class TestLedgerAllocator method testBadVersionOnTwoAllocators.
@Test(timeout = 60000)
public void testBadVersionOnTwoAllocators() throws Exception {
String allocationPath = "/allocation-bad-version";
zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Stat stat = new Stat();
byte[] data = zkc.get().getData(allocationPath, false, stat);
Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
allocator1.allocate();
// wait until allocated
ZKTransaction txn1 = newTxn();
LedgerHandle lh = Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
allocator2.allocate();
ZKTransaction txn2 = newTxn();
try {
Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
} catch (ZKException ke) {
assertEquals(KeeperException.Code.BADVERSION, ke.getKeeperExceptionCode());
}
Utils.ioResult(txn1.execute());
Utils.close(allocator1);
Utils.close(allocator2);
long eid = lh.addEntry("hello world".getBytes());
lh.close();
LedgerHandle readLh = bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
int i = 0;
while (entries.hasMoreElements()) {
LedgerEntry entry = entries.nextElement();
assertEquals("hello world", new String(entry.getEntry(), UTF_8));
++i;
}
assertEquals(1, i);
}
use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class TestZKLogSegmentMetadataStore method testStoreMaxTxnIdOnNonExistentPath.
@Test(timeout = 60000)
public void testStoreMaxTxnIdOnNonExistentPath() throws Exception {
Transaction<Object> updateTxn = lsmStore.transaction();
Versioned<Long> value = new Versioned<Long>(999L, new LongVersion(10));
final CompletableFuture<Version> result = new CompletableFuture<Version>();
String nonExistentPath = rootZkPath + "/non-existent";
LogMetadataForWriter metadata = mock(LogMetadataForWriter.class);
when(metadata.getMaxTxIdPath()).thenReturn(nonExistentPath);
lsmStore.storeMaxTxnId(updateTxn, metadata, value, new Transaction.OpListener<Version>() {
@Override
public void onCommit(Version r) {
result.complete(r);
}
@Override
public void onAbort(Throwable t) {
result.completeExceptionally(t);
}
});
try {
Utils.ioResult(updateTxn.execute());
fail("Should fail on storing log record transaction id if path doesn't exist");
} catch (ZKException zke) {
assertEquals(KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
}
try {
Utils.ioResult(result);
fail("Should fail on storing log record transaction id if path doesn't exist");
} catch (ZKException ze) {
assertEquals(KeeperException.Code.NONODE, ze.getKeeperExceptionCode());
}
}
Aggregations