-
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
Per host channels #1333
Draft
jkozlowski
wants to merge
5
commits into
jakubk/stick-queue
Choose a base branch
from
jakubk/per-host-stick
base: jakubk/stick-queue
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
Per host channels #1333
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
201 changes: 201 additions & 0 deletions
201
dialogue-core/src/main/java/com/palantir/dialogue/core/DefaultStickySessionFactory.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,201 @@ | ||
/* | ||
* (c) Copyright 2021 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.FutureCallback; | ||
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.EndpointChannel; | ||
import com.palantir.dialogue.EndpointChannelFactory; | ||
import com.palantir.dialogue.Request; | ||
import com.palantir.dialogue.Response; | ||
import com.palantir.dialogue.core.StickyAttachments.StickyTarget; | ||
import com.palantir.dialogue.futures.DialogueFutures; | ||
import com.palantir.logsafe.Preconditions; | ||
import java.util.function.Supplier; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.GuardedBy; | ||
import org.immutables.value.Value; | ||
|
||
@Value.Enclosing | ||
public final class DefaultStickySessionFactory implements StickySessionFactory { | ||
|
||
private final Supplier<EndpointChannelFactory> delegate; | ||
|
||
private DefaultStickySessionFactory(Supplier<EndpointChannelFactory> endpointChannelFactory) { | ||
this.delegate = Preconditions.checkNotNull(endpointChannelFactory, "endpointChannelFactory"); | ||
} | ||
|
||
@Override | ||
public StickySession get() { | ||
return new DefaultStickySession(delegate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "StickyEndpointChannels2{" + delegate + "}"; | ||
} | ||
|
||
private static final class DefaultStickySession implements StickySession { | ||
|
||
private final Supplier<EndpointChannelFactory> channelFactory; | ||
private final StickyRouter router = new StickyRouter(); | ||
|
||
private DefaultStickySession(Supplier<EndpointChannelFactory> channelFactory) { | ||
this.channelFactory = channelFactory; | ||
} | ||
|
||
@Override | ||
public Channel get() { | ||
return new Sticky(channelFactory.get(), router); | ||
} | ||
} | ||
|
||
private static final class Sticky implements EndpointChannelFactory, Channel { | ||
|
||
private final EndpointChannelFactory channelFactory; | ||
private final StickyRouter router; | ||
|
||
private Sticky(EndpointChannelFactory channelFactory, StickyRouter router) { | ||
this.router = router; | ||
this.channelFactory = channelFactory; | ||
} | ||
|
||
@Override | ||
public EndpointChannel endpoint(Endpoint endpoint) { | ||
return new StickyEndpointChannel(router, channelFactory.endpoint(endpoint)); | ||
} | ||
|
||
/** | ||
* . | ||
* @deprecated prefer {@link #endpoint}, as it allows binding work upfront | ||
*/ | ||
@Deprecated | ||
@Override | ||
public ListenableFuture<Response> execute(Endpoint endpoint, Request request) { | ||
return endpoint(endpoint).execute(request); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Sticky{" + channelFactory + '}'; | ||
} | ||
} | ||
|
||
public static DefaultStickySessionFactory create(Supplier<EndpointChannelFactory> endpointChannelFactory) { | ||
return new DefaultStickySessionFactory(endpointChannelFactory); | ||
} | ||
|
||
private static final class StickyRouter { | ||
|
||
private final InFlightCallCallback callback = new InFlightCallCallback(); | ||
|
||
@Nullable | ||
private volatile StickyTarget stickyTarget; | ||
|
||
@Nullable | ||
@GuardedBy("this") | ||
private volatile ListenableFuture<Response> callInFlight; | ||
|
||
public ListenableFuture<Response> execute(Request request, EndpointChannel endpointChannel) { | ||
if (stickyTarget != null) { | ||
return executeWithStickyTarget(stickyTarget, request, endpointChannel); | ||
} | ||
|
||
synchronized (this) { | ||
if (stickyTarget != null) { | ||
return executeWithStickyTarget(stickyTarget, request, endpointChannel); | ||
} | ||
|
||
ListenableFuture<Response> callInFlightSnapshot = callInFlight; | ||
if (callInFlightSnapshot == null) { | ||
ListenableFuture<Response> result = DialogueFutures.addDirectCallback( | ||
executeWithStickyToken(request, endpointChannel), callback); | ||
callInFlight = Futures.nonCancellationPropagating(result); | ||
return result; | ||
} else { | ||
ListenableFuture<Response> result = callInFlightSnapshot; | ||
result = DialogueFutures.transformAsync(result, _input -> execute(request, endpointChannel)); | ||
result = DialogueFutures.catchingAllAsync(result, _throwable -> execute(request, endpointChannel)); | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
private final class InFlightCallCallback implements FutureCallback<Response> { | ||
|
||
@Override | ||
public void onSuccess(Response result) { | ||
successfulCall(result); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable _throwable) { | ||
failed(); | ||
} | ||
} | ||
|
||
private synchronized void successfulCall(Response response) { | ||
callInFlight = null; | ||
if (stickyTarget == null) { | ||
StickyTarget newStickyTarget = | ||
response.attachments().getOrDefault(StickyAttachments.STICKY_TOKEN, null); | ||
if (newStickyTarget != null) { | ||
stickyTarget = newStickyTarget; | ||
} | ||
} | ||
} | ||
|
||
private synchronized void failed() { | ||
callInFlight = null; | ||
} | ||
|
||
private static ListenableFuture<Response> executeWithStickyToken( | ||
Request request, EndpointChannel endpointChannel) { | ||
request.attachments().put(StickyAttachments.REQUEST_STICKY_TOKEN, Boolean.TRUE); | ||
return endpointChannel.execute(request); | ||
} | ||
|
||
private static ListenableFuture<Response> executeWithStickyTarget( | ||
StickyTarget stickyTarget, Request request, EndpointChannel endpointChannel) { | ||
request.attachments().put(StickyAttachments.STICKY, stickyTarget); | ||
return endpointChannel.execute(request); | ||
} | ||
} | ||
|
||
private static final class StickyEndpointChannel implements EndpointChannel { | ||
private final StickyRouter stickyRouter; | ||
private final EndpointChannel delegate; | ||
|
||
StickyEndpointChannel(StickyRouter stickyRouter, EndpointChannel delegate) { | ||
this.stickyRouter = stickyRouter; | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Response> execute(Request request) { | ||
return stickyRouter.execute(request, delegate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "StickyEndpointChannel{delegate=" + delegate + '}'; | ||
} | ||
} | ||
} |
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.
I think this piece is more or less the alternative described here, except that we don't leverage it from the StickySessionFactory.
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.
Yeah: I think it's valuable to provide wholesale fairness implementation. If we're not going to ship the whole thing (i.e. you can support internal usecase for per-transaction fairness, with ability to create a new sticky channel from an existing sticky channel with new queue), I'd be happy to switch to per-host queues even for sticky channels. The reason I added it in #1334 is because I wanted to ship the whole thing.