-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] RTO/RPO: retry framework #14783
Draft
SzyWilliam
wants to merge
1
commit into
apache:rto_rpo
Choose a base branch
from
SzyWilliam:retry_framework
base: rto_rpo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 7 additions & 0 deletions
7
iotdb-client/session/src/main/java/org/apache/iotdb/session/util/retry/Handler.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,7 @@ | ||
package org.apache.iotdb.session.util.retry; | ||
|
||
public interface Handler<T> { | ||
RetryDecision onExecutionResult(T result); | ||
RetryDecision onException(Exception exception); | ||
T onFinalResult(T lastResult, Exception lastException) throws Exception; | ||
} |
10 changes: 10 additions & 0 deletions
10
iotdb-client/session/src/main/java/org/apache/iotdb/session/util/retry/RetriableTask.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,10 @@ | ||
package org.apache.iotdb.session.util.retry; | ||
|
||
/** | ||
* This interface defines a retriable task | ||
* @param <T> return type of this task | ||
*/ | ||
@FunctionalInterface | ||
public interface RetriableTask<T> { | ||
T execute() throws Exception; | ||
} |
43 changes: 43 additions & 0 deletions
43
iotdb-client/session/src/main/java/org/apache/iotdb/session/util/retry/Retry.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,43 @@ | ||
package org.apache.iotdb.session.util.retry; | ||
|
||
import org.apache.tsfile.utils.TimeDuration; | ||
|
||
public class Retry { | ||
final static public RetryDecision NO_RETRY = new RetryDecision() { | ||
@Override | ||
public boolean shouldRetry() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public long getSleepTimeIfRetry() { | ||
return 0; | ||
} | ||
}; | ||
public static <T> T execute(RetriableTask<T> task, Handler<T> handler) throws Exception { | ||
T result = null; | ||
RetryDecision decision; | ||
Exception lastException; | ||
|
||
while (true){ | ||
try { | ||
result = task.execute(); | ||
decision = handler.onExecutionResult(result); | ||
lastException = null; | ||
} catch (Exception e) { | ||
decision = handler.onException(e); | ||
lastException = e; | ||
} | ||
|
||
if (!decision.shouldRetry()) { | ||
return handler.onFinalResult(result, lastException); | ||
} | ||
try { | ||
Thread.sleep(decision.getSleepTimeIfRetry()); | ||
} catch (InterruptedException interruptedException) { | ||
Thread.currentThread().interrupt(); | ||
// TODO (william) what shall we do here? | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
iotdb-client/session/src/main/java/org/apache/iotdb/session/util/retry/RetryDecision.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,9 @@ | ||
package org.apache.iotdb.session.util.retry; | ||
|
||
public interface RetryDecision { | ||
boolean shouldRetry(); | ||
|
||
long getSleepTimeIfRetry(); | ||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
iotdb-client/session/src/main/java/org/apache/iotdb/session/util/retry/TaskResult.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,32 @@ | ||
package org.apache.iotdb.session.util.retry; | ||
|
||
import java.util.Optional; | ||
|
||
public class TaskResult <T, E extends Throwable> { | ||
private final T result; | ||
private final E exception; | ||
|
||
private TaskResult(T result, E exception) { | ||
this.result = result; | ||
this.exception = exception; | ||
} | ||
static <T> TaskResult<T, Throwable> from(T result) { | ||
return new TaskResult<>(result, null); | ||
} | ||
|
||
static <E extends Throwable> TaskResult<Object, E> fromException(E exception) { | ||
return new TaskResult<>(null, exception); | ||
} | ||
|
||
public boolean isPresent() { | ||
return Optional.ofNullable(result).isPresent(); | ||
} | ||
|
||
public Optional<T> getResult() { | ||
return Optional.ofNullable(result); | ||
} | ||
|
||
public Optional<E> getException() { | ||
return Optional.ofNullable(exception); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/retry/Handler.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,9 @@ | ||
package org.apache.iotdb.commons.utils.retry; | ||
|
||
import java.util.Optional; | ||
|
||
public interface Handler<T> { | ||
RetryDecision onExecutionResult(T result); | ||
RetryDecision onException(Exception exception); | ||
T onFinalResult(T lastResult, Exception lastException) throws Exception; | ||
} |
10 changes: 10 additions & 0 deletions
10
...b-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/retry/RetriableTask.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,10 @@ | ||
package org.apache.iotdb.commons.utils.retry; | ||
|
||
/** | ||
* This interface defines a retriable task | ||
* @param <T> return type of this task | ||
*/ | ||
@FunctionalInterface | ||
public interface RetriableTask<T> { | ||
T execute() throws Exception; | ||
} |
32 changes: 32 additions & 0 deletions
32
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/retry/Retry.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,32 @@ | ||
package org.apache.iotdb.commons.utils.retry; | ||
|
||
import java.util.Optional; | ||
|
||
public class Retry { | ||
public static <T> T execute(RetriableTask<T> task, Handler<T> handler) throws Exception { | ||
T result = null; | ||
RetryDecision decision; | ||
Exception lastException; | ||
|
||
while (true){ | ||
try { | ||
result = task.execute(); | ||
decision = handler.onExecutionResult(result); | ||
lastException = null; | ||
} catch (Exception e) { | ||
decision = handler.onException(e); | ||
lastException = e; | ||
} | ||
|
||
if (!decision.shouldRetry()) { | ||
return handler.onFinalResult(result, lastException); | ||
} | ||
try { | ||
Thread.sleep(decision.getSleepTimeIfRetry()); | ||
} catch (InterruptedException interruptedException) { | ||
Thread.currentThread().interrupt(); | ||
// TODO (william) what shall we do here? | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...b-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/retry/RetryDecision.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,7 @@ | ||
package org.apache.iotdb.commons.utils.retry; | ||
|
||
public interface RetryDecision { | ||
boolean shouldRetry(); | ||
|
||
long getSleepTimeIfRetry(); | ||
} |
32 changes: 32 additions & 0 deletions
32
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/retry/TaskResult.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,32 @@ | ||
package org.apache.iotdb.commons.utils.retry; | ||
|
||
import java.util.Optional; | ||
|
||
public class TaskResult <T, E extends Throwable> { | ||
private final T result; | ||
private final E exception; | ||
|
||
private TaskResult(T result, E exception) { | ||
this.result = result; | ||
this.exception = exception; | ||
} | ||
static <T> TaskResult<T, Throwable> from(T result) { | ||
return new TaskResult<>(result, null); | ||
} | ||
|
||
static <E extends Throwable> TaskResult<Object, E> fromException(E exception) { | ||
return new TaskResult<>(null, exception); | ||
} | ||
|
||
public boolean isPresent() { | ||
return Optional.ofNullable(result).isPresent(); | ||
} | ||
|
||
public Optional<T> getResult() { | ||
return Optional.ofNullable(result); | ||
} | ||
|
||
public Optional<E> getException() { | ||
return Optional.ofNullable(exception); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new class need add apache license header.