use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback in project bookkeeper by apache.
the class TestZkLedgerIdGenerator method testGenerateLedgerId.
@Test
public void testGenerateLedgerId() throws Exception {
// Create *nThread* threads each generate *nLedgers* ledger id,
// and then check there is no identical ledger id.
final int nThread = 2;
final int nLedgers = 2000;
final CountDownLatch countDownLatch = new CountDownLatch(nThread * nLedgers);
final AtomicInteger errCount = new AtomicInteger(0);
final ConcurrentLinkedQueue<Long> ledgerIds = new ConcurrentLinkedQueue<Long>();
final GenericCallback<Long> cb = new GenericCallback<Long>() {
@Override
public void operationComplete(int rc, Long result) {
if (Code.OK.intValue() == rc) {
ledgerIds.add(result);
} else {
errCount.incrementAndGet();
}
countDownLatch.countDown();
}
};
long start = System.currentTimeMillis();
for (int i = 0; i < nThread; i++) {
new Thread() {
@Override
public void run() {
for (int j = 0; j < nLedgers; j++) {
ledgerIdGenerator.generateLedgerId(cb);
}
}
}.start();
}
assertTrue("Wait ledger id generation threads to stop timeout : ", countDownLatch.await(30, TimeUnit.SECONDS));
LOG.info("Number of generated ledger id: {}, time used: {}", ledgerIds.size(), System.currentTimeMillis() - start);
assertEquals("Error occur during ledger id generation : ", 0, errCount.get());
Set<Long> ledgers = new HashSet<Long>();
while (!ledgerIds.isEmpty()) {
Long ledger = ledgerIds.poll();
assertNotNull("Generated ledger id is null : ", ledger);
assertFalse("Ledger id [" + ledger + "] conflict : ", ledgers.contains(ledger));
ledgers.add(ledger);
}
}
use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback in project bookkeeper by apache.
the class GcLedgersTest method testGcLedgersIfReadLedgerMetadataSaysNoSuchLedger.
/*
* In this test scenario no ledger is deleted, but LedgerManager.readLedgerMetadata says there is NoSuchLedger. So
* even in that case, GarbageCollector.gc shouldn't delete ledgers data.
*
* Consider the possible scenario - when the LedgerIterator is created that ledger is not deleted, so as per
* LedgerIterator that is live ledger. But right after the LedgerIterator creation that ledger is deleted, so
* readLedgerMetadata call would return NoSuchLedger. In this testscenario we are validating that as per Iterator if
* that ledger is alive though currently that ledger is deleted, we should not clean data of that ledger.
*
* ScanAndCompareGarbageCollector/GC should clean data of ledger only if both the LedgerManager.getLedgerRanges says
* that ledger is not existing and also ledgerManager.readLedgerMetadata fails with error
* NoSuchLedgerExistsException.
*
*/
@Test
public void testGcLedgersIfReadLedgerMetadataSaysNoSuchLedger() throws Exception {
final SortedSet<Long> createdLedgers = Collections.synchronizedSortedSet(new TreeSet<Long>());
final SortedSet<Long> cleaned = Collections.synchronizedSortedSet(new TreeSet<Long>());
// Create few ledgers
final int numLedgers = 5;
createLedgers(numLedgers, createdLedgers);
LedgerManager mockLedgerManager = new CleanupLedgerManager(getLedgerManager()) {
@Override
public void readLedgerMetadata(long ledgerId, GenericCallback<LedgerMetadata> readCb) {
readCb.operationComplete(BKException.Code.NoSuchLedgerExistsException, null);
}
};
final GarbageCollector garbageCollector = new ScanAndCompareGarbageCollector(mockLedgerManager, new MockLedgerStorage(), baseConf, NullStatsLogger.INSTANCE);
GarbageCollector.GarbageCleaner cleaner = new GarbageCollector.GarbageCleaner() {
@Override
public void clean(long ledgerId) {
LOG.info("Cleaned {}", ledgerId);
cleaned.add(ledgerId);
}
};
validateLedgerRangeIterator(createdLedgers);
garbageCollector.gc(cleaner);
assertTrue("Should have cleaned nothing", cleaned.isEmpty());
}
use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback in project bookkeeper by apache.
the class GcLedgersTest method createLedgers.
/**
* Create ledgers.
*/
private void createLedgers(int numLedgers, final Set<Long> createdLedgers) throws IOException {
final AtomicInteger expected = new AtomicInteger(numLedgers);
for (int i = 0; i < numLedgers; i++) {
getLedgerIdGenerator().generateLedgerId(new GenericCallback<Long>() {
@Override
public void operationComplete(int rc, final Long ledgerId) {
if (BKException.Code.OK != rc) {
synchronized (expected) {
int num = expected.decrementAndGet();
if (num == 0) {
expected.notify();
}
}
return;
}
getLedgerManager().createLedgerMetadata(ledgerId, new LedgerMetadata(1, 1, 1, DigestType.MAC, "".getBytes()), new GenericCallback<Void>() {
@Override
public void operationComplete(int rc, Void result) {
if (rc == BKException.Code.OK) {
activeLedgers.put(ledgerId, true);
createdLedgers.add(ledgerId);
}
synchronized (expected) {
int num = expected.decrementAndGet();
if (num == 0) {
expected.notify();
}
}
}
});
}
});
}
synchronized (expected) {
try {
while (expected.get() > 0) {
expected.wait(100);
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
}
Aggregations