use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class TestHttpService method testDeleteLedgerService.
/**
* Create ledgers, then test Delete Ledger service.
*/
@Test
public void testDeleteLedgerService() throws Exception {
baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
BookKeeper.DigestType digestType = BookKeeper.DigestType.CRC32;
int numLedgers = 4;
int numMsgs = 100;
LedgerHandle[] lh = new LedgerHandle[numLedgers];
// create ledgers
for (int i = 0; i < numLedgers; i++) {
lh[i] = bkc.createLedger(digestType, "".getBytes());
}
String content = "Apache BookKeeper is cool!";
// add entries
for (int i = 0; i < numMsgs; i++) {
for (int j = 0; j < numLedgers; j++) {
lh[j].addEntry(content.getBytes());
}
}
// close ledgers
for (int i = 0; i < numLedgers; i++) {
lh[i].close();
}
HttpEndpointService deleteLedgerService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.DELETE_LEDGER);
// 1, null parameters of GET, should return NOT_FOUND
HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
HttpServiceResponse response1 = deleteLedgerService.handle(request1);
assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response1.getStatusCode());
// 2, null parameters of DELETE, should return NOT_FOUND
HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.DELETE, null);
HttpServiceResponse response2 = deleteLedgerService.handle(request2);
assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response2.getStatusCode());
// 3, delete first ledger, should return OK, and should only get 3 ledgers after delete.
HashMap<String, String> params = Maps.newHashMap();
Long ledgerId = Long.valueOf(lh[0].getId());
params.put("ledger_id", ledgerId.toString());
HttpServiceRequest request3 = new HttpServiceRequest(null, HttpServer.Method.DELETE, params);
HttpServiceResponse response3 = deleteLedgerService.handle(request3);
assertEquals(HttpServer.StatusCode.OK.getValue(), response3.getStatusCode());
// use list Ledger to verify left 3 ledger
HttpEndpointService listLedgerService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.LIST_LEDGER);
HttpServiceRequest request4 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
HttpServiceResponse response4 = listLedgerService.handle(request4);
assertEquals(HttpServer.StatusCode.OK.getValue(), response4.getStatusCode());
// get response , expected get 3 ledgers
@SuppressWarnings("unchecked") LinkedHashMap<String, String> respBody = JsonUtil.fromJson(response4.getBody(), LinkedHashMap.class);
assertEquals(3, respBody.size());
}
use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class TestHttpService method testListDiskFilesService.
@Test
public void testListDiskFilesService() throws Exception {
baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
BookKeeper.DigestType digestType = BookKeeper.DigestType.CRC32;
int numLedgers = 4;
int numMsgs = 100;
LedgerHandle[] lh = new LedgerHandle[numLedgers];
// create ledgers
for (int i = 0; i < numLedgers; i++) {
lh[i] = bkc.createLedger(digestType, "".getBytes());
}
String content = "Apache BookKeeper is cool!";
// add entries
for (int i = 0; i < numMsgs; i++) {
for (int j = 0; j < numLedgers; j++) {
lh[j].addEntry(content.getBytes());
}
}
// close ledgers
for (int i = 0; i < numLedgers; i++) {
lh[i].close();
}
HttpEndpointService listDiskFileService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.LIST_DISK_FILE);
// 1, null parameters of GET, should return 3 kind of files: journal, entrylog and index files
HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
HttpServiceResponse response1 = listDiskFileService.handle(request1);
assertEquals(HttpServer.StatusCode.OK.getValue(), response1.getStatusCode());
@SuppressWarnings("unchecked") HashMap<String, String> respBody = JsonUtil.fromJson(response1.getBody(), HashMap.class);
assertEquals(3, respBody.size());
// 2, parameters of GET journal file, should return journal files
HashMap<String, String> params = Maps.newHashMap();
params.put("file_type", "journal");
HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.GET, params);
HttpServiceResponse response2 = listDiskFileService.handle(request2);
assertEquals(HttpServer.StatusCode.OK.getValue(), response2.getStatusCode());
@SuppressWarnings("unchecked") HashMap<String, String> respBody2 = JsonUtil.fromJson(response2.getBody(), HashMap.class);
assertEquals(1, respBody2.size());
}
use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class LedgerMetadataCreationTest method testExecution.
public void testExecution(boolean randomLedgerId) throws Exception {
Set<Long> createRequestsLedgerIds = ConcurrentHashMap.newKeySet();
ConcurrentLinkedDeque<Long> existingLedgerIds = new ConcurrentLinkedDeque<Long>();
Vector<Long> failedCreates = new Vector<Long>();
Vector<Long> failedDeletes = new Vector<Long>();
BookKeeper bookKeeper = new BookKeeper(baseClientConf);
ExecutorService executor = Executors.newFixedThreadPool(300);
Random rand = new Random();
int numberOfOperations = 20000;
for (int i = 0; i < numberOfOperations; i++) {
int iteration = i;
if (rand.nextBoolean() || existingLedgerIds.isEmpty()) {
executor.submit(() -> {
long ledgerId = -1;
try {
if (randomLedgerId) {
do {
ledgerId = Math.abs(rand.nextLong());
if (!baseClientConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)) {
/*
* since LongHierarchicalLedgerManager
* supports ledgerIds of decimal length upto
* 19 digits but other LedgerManagers only
* upto 10 decimals
*/
ledgerId %= 9999999999L;
}
} while (!createRequestsLedgerIds.add(ledgerId));
} else {
ledgerId = iteration;
}
bookKeeper.createLedgerAdv(ledgerId, 3, 2, 2, DigestType.CRC32, "passwd".getBytes(), null);
existingLedgerIds.add(ledgerId);
} catch (Exception e) {
LOG.error("Got Exception while creating Ledger with ledgerId " + ledgerId, e);
failedCreates.add(ledgerId);
}
});
} else {
executor.submit(() -> {
Long ledgerId = null;
if (rand.nextBoolean()) {
ledgerId = existingLedgerIds.pollFirst();
} else {
ledgerId = existingLedgerIds.pollLast();
}
if (ledgerId == null) {
return;
}
try {
bookKeeper.deleteLedger(ledgerId);
} catch (Exception e) {
LOG.error("Got Exception while deleting Ledger with ledgerId " + ledgerId, e);
failedDeletes.add(ledgerId);
}
});
}
}
executor.shutdown();
assertTrue("All the ledger create/delete operations should have'been completed", executor.awaitTermination(120, TimeUnit.SECONDS));
assertTrue("There should be no failed creates. But there are " + failedCreates.size() + " failedCreates", failedCreates.isEmpty());
assertTrue("There should be no failed deletes. But there are " + failedDeletes.size() + " failedDeletes", failedDeletes.isEmpty());
bookKeeper.close();
}
use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class LedgerMetadataCreationTest method testParentNodeDeletion.
@Test
public void testParentNodeDeletion() throws Exception {
/*
* run this testcase only for HierarchicalLedgerManager and
* LongHierarchicalLedgerManager, since we do recursive zNode deletes
* only for HierarchicalLedgerManager
*/
Assume.assumeTrue((baseClientConf.getLedgerManagerFactoryClass().equals(HierarchicalLedgerManagerFactory.class) || baseClientConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)));
ZooKeeper zkc = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 10000, null);
BookKeeper bookKeeper = new BookKeeper(baseClientConf);
bookKeeper.createLedgerAdv(1, 3, 2, 2, DigestType.CRC32, "passwd".getBytes(), null);
String ledgersRootPath = baseClientConf.getZkLedgersRootPath();
String parentZnodePath;
if (baseClientConf.getLedgerManagerFactoryClass().equals(HierarchicalLedgerManagerFactory.class)) {
/*
* in HierarchicalLedgerManager (ledgersRootPath)/00/0000/L0001
* would be the path of the znode for ledger - 1. So when ledger - 1
* is deleted, (ledgersRootPath)/00 should also be deleted since
* there are no other children znodes
*/
parentZnodePath = ledgersRootPath + "/00";
} else {
/*
* in LongHierarchicalLedgerManager
* (ledgersRootPath)/000/0000/0000/0000/L0001 would be the path of
* the znode for ledger - 1. So when ledger - 1 is deleted,
* (ledgersRootPath)/000 should also be deleted since there are no
* other children znodes
*/
parentZnodePath = ledgersRootPath + "/000";
}
assertTrue(parentZnodePath + " zNode should exist", null != zkc.exists(parentZnodePath, false));
bookKeeper.deleteLedger(1);
assertTrue(parentZnodePath + " zNode should not exist anymore", null == zkc.exists(parentZnodePath, false));
bookKeeper.close();
zkc.close();
}
use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class TestTLS method testKeyMismatchFailure.
/**
* Verify handshake failure with a bad cert.
*/
@Test
public void testKeyMismatchFailure() throws Exception {
// Valid test case only for PEM format keys
Assume.assumeTrue(serverKeyStoreFormat == KeyStoreType.PEM);
ClientConfiguration clientConf = new ClientConfiguration(baseClientConf);
// restart a bookie with bad cert
int restartBookieIdx = 0;
ServerConfiguration bookieConf = bsConfs.get(restartBookieIdx).setTLSCertificatePath(getResourcePath("client-cert.pem"));
killBookie(restartBookieIdx);
LOG.info("Sleeping for 1s before restarting bookie with bad cert");
Thread.sleep(1000);
bs.add(startBookie(bookieConf));
bsConfs.add(bookieConf);
// Create ledger and write entries
BookKeeper client = new BookKeeper(clientConf);
byte[] passwd = "testPassword".getBytes();
int numEntries = 2;
byte[] testEntry = "testEntry".getBytes();
try (LedgerHandle lh = client.createLedger(numBookies, numBookies, DigestType.CRC32, passwd)) {
for (int i = 0; i <= numEntries; i++) {
lh.addEntry(testEntry);
}
fail("Should have failed with not enough bookies to write");
} catch (BKException.BKNotEnoughBookiesException bke) {
// expected
}
}
Aggregations