Search in sources :

Example 16 with BookKeeper

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());
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Example 17 with BookKeeper

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());
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Example 18 with BookKeeper

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();
}
Also used : BookKeeper(org.apache.bookkeeper.client.BookKeeper) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) Vector(java.util.Vector)

Example 19 with BookKeeper

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) BookKeeper(org.apache.bookkeeper.client.BookKeeper) Test(org.junit.Test)

Example 20 with BookKeeper

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
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) BookKeeper(org.apache.bookkeeper.client.BookKeeper) BKException(org.apache.bookkeeper.client.BKException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Aggregations

BookKeeper (org.apache.bookkeeper.client.BookKeeper)76 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)48 Test (org.junit.Test)25 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)24 BKException (org.apache.bookkeeper.client.BKException)18 IOException (java.io.IOException)17 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)12 List (java.util.List)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)10 ArrayList (java.util.ArrayList)9 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 AsyncCallback (org.apache.bookkeeper.client.AsyncCallback)7 HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)7 BookkeeperCommitLog (herddb.cluster.BookkeeperCommitLog)6 TableSpaceManager (herddb.core.TableSpaceManager)6 DataScanner (herddb.model.DataScanner)6 StatementExecutionException (herddb.model.StatementExecutionException)6