rdhabalia commented on code in PR #3924:
URL: https://github.com/apache/bookkeeper/pull/3924#discussion_r1170369955


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/SanityTestCommand.java:
##########
@@ -81,59 +86,126 @@ public boolean apply(ServerConfiguration conf, SanityFlags 
cmdFlags) {
         }
     }
 
-    private boolean handle(ServerConfiguration conf, SanityFlags cmdFlags) 
throws Exception {
+    private static boolean handle(ServerConfiguration conf, SanityFlags 
cmdFlags) throws Exception {
+        try {
+            return handleAsync(conf, cmdFlags).get();
+        } catch (Exception e) {
+            LOG.warn("Error in bookie sanity test", e);
+            return false;
+        }
+    }
+
+    public static CompletableFuture<Boolean> handleAsync(ServerConfiguration 
conf, SanityFlags cmdFlags) {
+        CompletableFuture<Boolean> result = new CompletableFuture<Boolean>();
         ClientConfiguration clientConf = new ClientConfiguration();
         clientConf.addConfiguration(conf);
         
clientConf.setEnsemblePlacementPolicy(LocalBookieEnsemblePlacementPolicy.class);
         clientConf.setAddEntryTimeout(cmdFlags.timeout);
         clientConf.setReadEntryTimeout(cmdFlags.timeout);
 
-        BookKeeper bk = new BookKeeper(clientConf);
-        LedgerHandle lh = null;
+        BookKeeper bk;
         try {
-            lh = bk.createLedger(1, 1, BookKeeper.DigestType.MAC, new byte[0]);
-            LOG.info("Create ledger {}", lh.getId());
+            bk = new BookKeeper(clientConf);
+        } catch (BKException | IOException | InterruptedException e) {
+            LOG.warn("Failed to initialize bookkeeper client", e);
+            result.completeExceptionally(e);
+            return result;
+        }
 
+        bk.asyncCreateLedger(1, 1, BookKeeper.DigestType.MAC, new byte[0], 
(rc, lh, ctx) -> {
+            if (rc != BKException.Code.OK) {
+                LOG.warn("ledger creation failed for sanity command {}", rc);
+                result.completeExceptionally(BKException.create(rc));
+                return;
+            }
+            List<CompletableFuture<Void>> entriesFutures = new ArrayList<>();
             for (int i = 0; i < cmdFlags.entries; i++) {
                 String content = "entry-" + i;
-                lh.addEntry(content.getBytes(UTF_8));
-            }
-
-            LOG.info("Written {} entries in ledger {}", cmdFlags.entries, 
lh.getId());
-
-            // Reopen the ledger and read entries
-            lh = bk.openLedger(lh.getId(), BookKeeper.DigestType.MAC, new 
byte[0]);
-            if (lh.getLastAddConfirmed() != (cmdFlags.entries - 1)) {
-                throw new Exception("Invalid last entry found on ledger. 
expecting: " + (cmdFlags.entries - 1)
-                                        + " -- found: " + 
lh.getLastAddConfirmed());
+                CompletableFuture<Void> entryFuture = new 
CompletableFuture<>();
+                entriesFutures.add(entryFuture);
+                lh.asyncAddEntry(content.getBytes(UTF_8), (arc, alh, entryId, 
actx) -> {
+                    if (arc != BKException.Code.OK) {
+                        LOG.warn("ledger add entry failed for {}-{}", 
alh.getId(), arc);
+                        
entryFuture.completeExceptionally(BKException.create(arc));
+                        return;
+                    }
+                    entryFuture.complete(null);
+                }, null);
             }
+            CompletableFuture<LedgerHandle> lhFuture = new 
CompletableFuture<>();
+            CompletableFuture<Void> readEntryFuture = new 
CompletableFuture<>();
+
+            FutureUtils.collect(entriesFutures).thenCompose(_r -> {
+                bk.asyncOpenLedger(lh.getId(), BookKeeper.DigestType.MAC, new 
byte[0], (orc, olh, octx) -> {
+                    if (orc != BKException.Code.OK) {
+                        LOG.warn("open sanity ledger failed for {}-{}", 
lh.getId(), orc);
+                        
lhFuture.completeExceptionally(BKException.create(orc));
+                        return;
+                    }
+                    long lac = olh.getLastAddConfirmed();
+                    if (lac != (cmdFlags.entries - 1)) {
+                        lhFuture.completeExceptionally(new Exception("Invalid 
last entry found on ledger. expecting: "
+                                + (cmdFlags.entries - 1) + " -- found: " + 
lac));
+                        return;
+                    }
+                    lhFuture.complete(lh);
+                }, null);

Review Comment:
   well running sanity is executing one operation of what bookie suppose to do 
with high throughput : creating ledger/add entries/read entries/delete ledger. 
so, one operation per minute should be fine. the only thing is we should not 
have any blocking IO and that's the reason, I have changed sanityTestCommand 
implementation with async behavior.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to