apoorvmittal10 commented on code in PR #18093:
URL: https://github.com/apache/kafka/pull/18093#discussion_r1874735278


##########
core/src/main/java/kafka/server/share/SharePartition.java:
##########
@@ -371,52 +370,35 @@ public static RecordState forId(byte id) {
      */
     public CompletableFuture<Void> maybeInitialize() {
         log.debug("Maybe initialize share partition: {}-{}", groupId, 
topicIdPartition);
-        CompletableFuture<Void> future = new CompletableFuture<>();
-        AtomicReference<Optional<Throwable>> futureException = new 
AtomicReference<>(Optional.empty());
         // Check if the share partition is already initialized.
-        InitializationResult initializationResult = 
checkInitializationCompletion();
-        if (initializationResult.isComplete()) {
-            if (initializationResult.throwable() != null) {
-                future.completeExceptionally(initializationResult.throwable());
-            } else {
-                future.complete(null);
+        try {
+            if (initializedOrThrowException()) {
+                return CompletableFuture.completedFuture(null);
             }
-            return future;
+        } catch (Exception e) {
+            return CompletableFuture.failedFuture(e);
         }
 
+        // If code reaches here then the share partition is not initialized. 
Initialize the share partition.
         // All the pending requests should wait to get completed before the 
share partition is initialized.
         // Attain lock to avoid any concurrent requests to be processed.
         lock.writeLock().lock();
-        boolean shouldFutureBeCompleted = false;
         try {
             // Re-check the state to verify if previous requests has already 
initialized the share partition.
-            initializationResult = checkInitializationCompletion();
-            if (initializationResult.isComplete()) {
-                if (initializationResult.throwable() != null) {
-                    
futureException.set(Optional.of(initializationResult.throwable()));
-                }
-                shouldFutureBeCompleted = true;
-                return future;
+            if (initializedOrThrowException()) {
+                return CompletableFuture.completedFuture(null);

Review Comment:
   Your suggestion is better hence I incorporated that.
   
   However I have tested the earlier code as well and as java specification 
says that without completion of `finally` block the method will not return 
result from `try`. Unlike earlier, in the problematic code,  where we were 
completing the `future` in `try`, but now it was a `return` statement.
   
   Below is a test and output.
   ```
       public CompletableFuture<Void> testFuture() {
           lock.writeLock().lock();
           try {
               System.out.println("testFuture");
               return CompletableFuture.completedFuture(null);
           } finally {
               System.out.println("testFuture finally");
               lock.writeLock().unlock();
               System.out.println("testFuture finally unlocked");
           }
       }
   ```
   
   ```
   @RepeatedTest(100)
       public void testFuture() {
           SharePartition sharePartition = 
SharePartitionBuilder.builder().build();
           sharePartition.testFuture().whenComplete((result, exception) -> {
               System.out.println("Completed");
           });
       }
   ```
   
   ```
   testFuture
   testFuture finally
   testFuture finally unlocked
   Completed
   
   
   



-- 
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