-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: retry completable future on exception (#599)
Signed-off-by: Li Zhanhui <[email protected]>
- Loading branch information
Showing
5 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
metadata/src/main/java/com/automq/rocketmq/metadata/Futures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.automq.rocketmq.metadata; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.Supplier; | ||
|
||
public class Futures { | ||
/** | ||
* Executes a loop using CompletableFutures, without invoking join()/get() on any of them or exclusively hogging a thread. | ||
* | ||
* @param condition A Supplier that indicates whether to proceed with the loop or not. | ||
* @param loopBody A Supplier that returns a CompletableFuture which represents the body of the loop. This | ||
* supplier is invoked every time the loopBody needs to execute. | ||
* @param executor An Executor that is used to execute the condition and the loop support code. | ||
* @return A CompletableFuture that, when completed, indicates the loop terminated without any exception. If | ||
* either the loopBody or condition throw/return Exceptions, these will be set as the result of this returned Future. | ||
*/ | ||
public static CompletableFuture<Void> loop(Supplier<Boolean> condition, Supplier<CompletableFuture<Void>> loopBody, | ||
Executor executor) { | ||
CompletableFuture<Void> result = new CompletableFuture<>(); | ||
Loop<Void> loop = new Loop<>(condition, loopBody, result, executor); | ||
executor.execute(loop); | ||
return result; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
metadata/src/main/java/com/automq/rocketmq/metadata/Loop.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.automq.rocketmq.metadata; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.Supplier; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Loop<T> implements Runnable { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(Loop.class); | ||
|
||
final Supplier<Boolean> condition; | ||
final Supplier<CompletableFuture<T>> loopBody; | ||
|
||
/** | ||
* A CompletableFuture that will be completed, whether normally or exceptionally, when the loop completes. | ||
*/ | ||
final CompletableFuture<Void> result; | ||
|
||
|
||
final Executor executor; | ||
|
||
public Loop(Supplier<Boolean> condition, Supplier<CompletableFuture<T>> loopBody, CompletableFuture<Void> result, | ||
Executor executor) { | ||
this.condition = condition; | ||
this.loopBody = loopBody; | ||
this.result = result; | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
execute(); | ||
} | ||
|
||
void execute() { | ||
if (this.condition.get()) { | ||
this.loopBody.get() | ||
.exceptionally(e -> { | ||
LOGGER.error("Unexpected exception raised", e); | ||
return null; | ||
}).thenRunAsync(this, executor); | ||
} else { | ||
result.complete(null); | ||
LOGGER.debug("Loop completed"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters