-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a11401b
commit a633593
Showing
11 changed files
with
229 additions
and
0 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
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); | ||
} | ||
} |