-
Notifications
You must be signed in to change notification settings - Fork 16
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
fix #351 fix #302 fix #312 Replace QueuedChannel with a backoff based retryer #432
Changes from 14 commits
554928f
b697561
e83923f
bf0bd47
2aede1d
9d68989
e912ef3
310cfba
0081d8a
f841e93
f036e9d
ff0e692
95d73f7
628bc34
91456c6
4562397
f4f999a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Replace QueuedChannel with a backoff based retryer | ||
links: | ||
- https://github.com/palantir/dialogue/pull/432 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed 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.palantir.dialogue.core; | ||
|
||
import java.time.Duration; | ||
import java.util.function.DoubleSupplier; | ||
|
||
final class ExponentialBackoffStrategy implements BackoffStrategy { | ||
carterkozak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final Duration backoffSlotSize; | ||
private final DoubleSupplier random; | ||
|
||
ExponentialBackoffStrategy(Duration backoffSlotSize, DoubleSupplier random) { | ||
this.backoffSlotSize = backoffSlotSize; | ||
this.random = random; | ||
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. s/random/jitter/ - it's fine for this to always return 1 in testing, and be a random implementation in prod? 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. It's important to get jitter in our simulations as well, otherwise the we may cause waves of requests. In tests using a constant is fine, but ideally we don't measure wall clock time in tests and watch the request rate/etc. |
||
} | ||
|
||
@Override | ||
public Duration backoffDuration(int failures) { | ||
if (failures == 0) { | ||
return Duration.ZERO; | ||
} | ||
int upperBound = (int) Math.pow(2, failures - 1); | ||
return Duration.ofNanos(Math.round(backoffSlotSize.toNanos() * random.getAsDouble() * upperBound)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed 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.palantir.dialogue.core; | ||
|
||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.palantir.dialogue.Channel; | ||
import com.palantir.dialogue.Endpoint; | ||
import com.palantir.dialogue.Request; | ||
import com.palantir.dialogue.Response; | ||
import com.palantir.logsafe.Preconditions; | ||
import com.palantir.logsafe.exceptions.SafeRuntimeException; | ||
import java.util.function.Supplier; | ||
|
||
/** Adapter from {@link LimitedChannel} to {@link Channel} which produces a failed future when results are limited. */ | ||
final class LimitedChannelToChannelAdapter implements Channel { | ||
|
||
// Avoid method reference allocations | ||
@SuppressWarnings("UnnecessaryLambda") | ||
private static final Supplier<ListenableFuture<Response>> limitedResultSupplier = | ||
() -> Futures.immediateFailedFuture(new SafeRuntimeException("Failed to make a request")); | ||
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. I think we're going to need to give people more information in this exception message (or possibly a comment here) otherwise we're going to be fielding questions in When people hit this, we probably don't want them to do their own retrying right? Perhaps we should even put some instrumentation on this, so we know exactly how many times our client refused to send a request out the door? 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. Agreed, I like the idea of replacing the limited-channel result of |
||
|
||
private final LimitedChannel delegate; | ||
|
||
LimitedChannelToChannelAdapter(LimitedChannel delegate) { | ||
this.delegate = Preconditions.checkNotNull(delegate, "LimitedChannel"); | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Response> execute(Endpoint endpoint, Request request) { | ||
return delegate.maybeExecute(endpoint, request).orElseGet(limitedResultSupplier); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LimitedChannelToChannelAdapter{delegate=" + delegate + '}'; | ||
} | ||
} |
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.
cute little optimization :)