-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Callbacks Easier to Use (#3360)
* Impl, docs, and tests * Clarify docs * Address comments
- Loading branch information
Showing
13 changed files
with
296 additions
and
16 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
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 | ||
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.