-
Notifications
You must be signed in to change notification settings - Fork 6
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
Make Callbacks Easier to Use #3360
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
52 changes: 52 additions & 0 deletions
52
atlasdb-commons/src/main/java/com/palantir/async/initializer/CallbackInitializable.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,52 @@ | ||
/* | ||
* Copyright 2018 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the BSD-3 License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/BSD-3-Clause | ||
* | ||
* 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.palantir.async.initializer; | ||
|
||
/** | ||
* An interface simplifying the creation of initialization Callbacks. If a class C requires a resource R to initialize | ||
* some of its components, but R may not be ready yet at the time of C's creation, C should implement this interface | ||
* and pass the desired Callback to R. When R is ready, it can then call {@link Callback#runWithRetry(R)} to initialize | ||
* C. | ||
*/ | ||
public interface CallbackInitializable<R> { | ||
void initialize(R resource); | ||
|
||
/** | ||
* If {@link #initialize(R)} failing can result in a state where cleanup is necessary, override this method. | ||
* | ||
* @param resource the resource used in initialization. | ||
* @param initFailure the Throwable causing the failure in initialization. | ||
*/ | ||
default void onInitializationFailureCleanup(R resource, Throwable initFailure) { | ||
} | ||
|
||
/** | ||
* Returns a Callback that runs initialize only once. On initialization failure, executes the specified cleanup and | ||
* then wraps and throws the cause. | ||
*/ | ||
default Callback<R> singleAttemptCallback() { | ||
return LambdaCallback.singleAttempt(this::initialize, this::onInitializationFailureCleanup); | ||
} | ||
|
||
/** | ||
* Returns a Callback that will retry initialization on failure, unless the cleanup task also throws, in which case | ||
* the exception thrown by the cleanup task is propagated. | ||
*/ | ||
default Callback<R> retryUnlessCleanupThrowsCallback() { | ||
return LambdaCallback.retrying(this::initialize, this::onInitializationFailureCleanup); | ||
} | ||
} |
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
132 changes: 132 additions & 0 deletions
132
atlasdb-commons/src/test/java/com/palantir/async/initializer/LambdaCallbackTest.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,132 @@ | ||
/* | ||
* Copyright 2018 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the BSD-3 License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/BSD-3-Clause | ||
* | ||
* 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.palantir.async.initializer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import org.immutables.value.Value; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class LambdaCallbackTest { | ||
private static final RuntimeException INIT_FAIL = new RuntimeException("Failed during initialization."); | ||
private static final RuntimeException CLEANUP_FAIL = new RuntimeException("Failed during cleanup."); | ||
private ModifiableInitsAndCleanups initsAndCleanups; | ||
private Callback<ModifiableInitsAndCleanups> callback; | ||
|
||
@Before | ||
public void setup() { | ||
initsAndCleanups = InitsAndCleanups.createInitialized(); | ||
} | ||
|
||
@Test | ||
public void singleAttemptCallbackCallsInitializeOnceAndSkipsCleanupOnSuccess() { | ||
callback = LambdaCallback.singleAttempt(this::markInit, this::markCleanup); | ||
|
||
callback.runWithRetry(initsAndCleanups); | ||
assertThat(initsAndCleanups.inits()).isEqualTo(1); | ||
assertThat(initsAndCleanups.cleanups()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void singleAttemptCallbackCallsInitializeAndCleanupOnceThenRethrowsOnInitFailure() { | ||
callback = LambdaCallback.singleAttempt(this::markInitAndFail, this::markCleanup); | ||
|
||
assertThatThrownBy(() -> callback.runWithRetry(initsAndCleanups)).isEqualToComparingFieldByField(INIT_FAIL); | ||
assertThat(initsAndCleanups.inits()).isEqualTo(1); | ||
assertThat(initsAndCleanups.cleanups()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void singleAttemptCallbackThrowsCleanupExceptionOnCleanupFailure() { | ||
callback = LambdaCallback.singleAttempt(this::markInitAndFail, this::markCleanupAndFail); | ||
|
||
assertThatThrownBy(() -> callback.runWithRetry(initsAndCleanups)).isEqualToComparingFieldByField(CLEANUP_FAIL); | ||
} | ||
|
||
@Test | ||
public void retryingCallbackCallsInitializeOnceAndSkipsCleanupOnSuccess() { | ||
callback = LambdaCallback.retrying(this::markInit, this::markCleanup); | ||
|
||
callback.runWithRetry(initsAndCleanups); | ||
assertThat(initsAndCleanups.inits()).isEqualTo(1); | ||
assertThat(initsAndCleanups.cleanups()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void retryingCallbackCallsInitializeAndCleanupUntilSuccess() { | ||
callback = LambdaCallback.retrying(this::markInitThenFailIfFewerThatTenInits, this::markCleanup); | ||
|
||
callback.runWithRetry(initsAndCleanups); | ||
assertThat(initsAndCleanups.inits()).isEqualTo(10); | ||
assertThat(initsAndCleanups.cleanups()).isEqualTo(9); | ||
} | ||
|
||
@Test | ||
public void retryingCallbackStopsRetryingWhenCleanupThrows() { | ||
callback = LambdaCallback.retrying(this::markInitAndFail, this::markCleanupThenFailIfMoreThatTenInits); | ||
|
||
assertThatThrownBy(() -> callback.runWithRetry(initsAndCleanups)).isEqualToComparingFieldByField(CLEANUP_FAIL); | ||
assertThat(initsAndCleanups.inits()).isEqualTo(11); | ||
assertThat(initsAndCleanups.cleanups()).isEqualTo(11); | ||
} | ||
|
||
private void markInit(ModifiableInitsAndCleanups initsAndCleanups) { | ||
initsAndCleanups.setInits(initsAndCleanups.inits() + 1); | ||
} | ||
|
||
private void markInitAndFail(ModifiableInitsAndCleanups initsAndCleanups) { | ||
markInit(initsAndCleanups); | ||
throw INIT_FAIL; | ||
} | ||
|
||
private void markInitThenFailIfFewerThatTenInits(ModifiableInitsAndCleanups initsAndCleanups) { | ||
markInit(initsAndCleanups); | ||
if (initsAndCleanups.inits() < 10) { | ||
throw INIT_FAIL; | ||
} | ||
} | ||
|
||
private void markCleanup(ModifiableInitsAndCleanups initsAndCleanups, Throwable ignore) { | ||
initsAndCleanups.setCleanups(initsAndCleanups.cleanups() + 1); | ||
} | ||
|
||
private void markCleanupAndFail(ModifiableInitsAndCleanups initsAndCleanups, Throwable ignore) { | ||
markCleanup(initsAndCleanups, ignore); | ||
throw CLEANUP_FAIL; | ||
} | ||
|
||
private void markCleanupThenFailIfMoreThatTenInits(ModifiableInitsAndCleanups initsAndCleanups, Throwable ignore) { | ||
markCleanup(initsAndCleanups, ignore); | ||
if (initsAndCleanups.inits() > 10) { | ||
throw CLEANUP_FAIL; | ||
} | ||
} | ||
|
||
@Value.Modifiable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting! Wasn't aware of this feature There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah really useful, I saw it in one of the recent PR's |
||
interface InitsAndCleanups { | ||
int inits(); | ||
int cleanups(); | ||
|
||
static ModifiableInitsAndCleanups createInitialized() { | ||
return ModifiableInitsAndCleanups.create() | ||
.setInits(0) | ||
.setCleanups(0); | ||
} | ||
} | ||
} |
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
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
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
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.
👍