1u0 commented on a change in pull request #9727: [FLINK-14145] Fix 
getLatestCheckpoint(true) returns wrong checkpoint
URL: https://github.com/apache/flink/pull/9727#discussion_r326659911
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/CompletedCheckpointStore.java
 ##########
 @@ -55,31 +54,26 @@
         * added.
         */
        default CompletedCheckpoint getLatestCheckpoint(boolean 
isPreferCheckpointForRecovery) throws Exception {
-               if (getAllCheckpoints().isEmpty()) {
+               List<CompletedCheckpoint> allCheckpoints = getAllCheckpoints();
+               if (allCheckpoints.isEmpty()) {
                        return null;
                }
 
-               CompletedCheckpoint candidate = 
getAllCheckpoints().get(getAllCheckpoints().size() - 1);
-               if (isPreferCheckpointForRecovery && getAllCheckpoints().size() 
> 1) {
-                       List<CompletedCheckpoint> allCheckpoints;
-                       try {
-                               allCheckpoints = getAllCheckpoints();
-                               ListIterator<CompletedCheckpoint> listIterator 
= allCheckpoints.listIterator(allCheckpoints.size() - 1);
-                               while (listIterator.hasPrevious()) {
-                                       CompletedCheckpoint prev = 
listIterator.previous();
-                                       if 
(!prev.getProperties().isSavepoint()) {
-                                               candidate = prev;
-                                               LOG.info("Found a completed 
checkpoint before the latest savepoint, will use it to recover!");
-                                               break;
-                                       }
+               CompletedCheckpoint lastCompleted = 
allCheckpoints.get(allCheckpoints.size() - 1);
+
+               if (lastCompleted.getProperties().isSavepoint() && 
isPreferCheckpointForRecovery && allCheckpoints.size() > 1) {
+                       ListIterator<CompletedCheckpoint> listIterator = 
allCheckpoints.listIterator(allCheckpoints.size() - 1);
 
 Review comment:
   You can simplify code further: as I've mentioned before, you don't need a 
special `lastCompleted.getProperties().isSavepoint()` check, instead 
instantiate the iterator from the very last element 
`allCheckpoints.listIterator(allCheckpoints.size())`.
   
   The whole method can be structured as:
   ```
   if (allCheckpoints.isEmpty()) {
       return null;
   }
   if (isPreferCheckpointForRecovery) {
       // try to find the latest checkpoint by reverse traversal over 
allCheckpoints
       // return it if found
   }
   // return the latest (be it checkpoint or savepoint)
   return allCheckpoints.get(allCheckpoints.size() - 1);
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to