-
Notifications
You must be signed in to change notification settings - Fork 3.9k
grpclb: Cache Subchannels for 10 seconds after it is removed from the backendlist #4238
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
Merged
Merged
Changes from all commits
Commits
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
142 changes: 142 additions & 0 deletions
142
grpclb/src/main/java/io/grpc/grpclb/CachedSubchannelPool.java
This file contains hidden or 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,142 @@ | ||
| /* | ||
| * Copyright 2018, gRPC Authors 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 io.grpc.grpclb; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
| import static com.google.common.base.Preconditions.checkState; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import io.grpc.Attributes; | ||
| import io.grpc.EquivalentAddressGroup; | ||
| import io.grpc.LoadBalancer.Helper; | ||
| import io.grpc.LoadBalancer.Subchannel; | ||
| import java.util.HashMap; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.ScheduledFuture; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * A {@link SubchannelPool} that keeps returned {@link Subchannel}s for a given time before it's | ||
| * shut down by the pool. | ||
| */ | ||
| final class CachedSubchannelPool implements SubchannelPool { | ||
| private final HashMap<EquivalentAddressGroup, CacheEntry> cache = | ||
| new HashMap<EquivalentAddressGroup, CacheEntry>(); | ||
|
|
||
| private Helper helper; | ||
| private ScheduledExecutorService timerService; | ||
|
|
||
| @VisibleForTesting | ||
| static final long SHUTDOWN_TIMEOUT_MS = 10000; | ||
|
|
||
| @Override | ||
| public void init(Helper helper, ScheduledExecutorService timerService) { | ||
| this.helper = checkNotNull(helper, "helper"); | ||
| this.timerService = checkNotNull(timerService, "timerService"); | ||
| } | ||
|
|
||
| @Override | ||
| public Subchannel takeOrCreateSubchannel( | ||
| EquivalentAddressGroup eag, Attributes defaultAttributes) { | ||
| CacheEntry entry = cache.remove(eag); | ||
| Subchannel subchannel; | ||
| if (entry == null) { | ||
| subchannel = helper.createSubchannel(eag, defaultAttributes); | ||
| } else { | ||
| subchannel = entry.subchannel; | ||
| entry.shutdownTimer.cancel(false); | ||
| } | ||
| return subchannel; | ||
| } | ||
|
|
||
| @Override | ||
| public void returnSubchannel(Subchannel subchannel) { | ||
| CacheEntry prev = cache.get(subchannel.getAddresses()); | ||
| if (prev != null) { | ||
| // Returning the same Subchannel twice has no effect. | ||
| // Returning a different Subchannel for an already cached EAG will cause the | ||
| // latter Subchannel to be shutdown immediately. | ||
| if (prev.subchannel != subchannel) { | ||
| subchannel.shutdown(); | ||
| } | ||
| return; | ||
| } | ||
| final ShutdownSubchannelTask shutdownTask = new ShutdownSubchannelTask(subchannel); | ||
| ScheduledFuture<?> shutdownTimer = | ||
| timerService.schedule( | ||
| new ShutdownSubchannelScheduledTask(shutdownTask), | ||
| SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS); | ||
| shutdownTask.timer = shutdownTimer; | ||
| CacheEntry entry = new CacheEntry(subchannel, shutdownTimer); | ||
| cache.put(subchannel.getAddresses(), entry); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| for (CacheEntry entry : cache.values()) { | ||
| entry.shutdownTimer.cancel(false); | ||
| entry.subchannel.shutdown(); | ||
| } | ||
| cache.clear(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| final class ShutdownSubchannelScheduledTask implements Runnable { | ||
| private final ShutdownSubchannelTask task; | ||
|
|
||
| ShutdownSubchannelScheduledTask(ShutdownSubchannelTask task) { | ||
| this.task = checkNotNull(task, "task"); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| helper.runSerialized(task); | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| final class ShutdownSubchannelTask implements Runnable { | ||
| private final Subchannel subchannel; | ||
| private ScheduledFuture<?> timer; | ||
|
|
||
| private ShutdownSubchannelTask(Subchannel subchannel) { | ||
| this.subchannel = checkNotNull(subchannel, "subchannel"); | ||
| } | ||
|
|
||
| // This runs in channelExecutor | ||
| @Override | ||
| public void run() { | ||
| // getSubchannel() may have cancelled the timer after the timer has expired but before this | ||
| // task is actually run in the channelExecutor. | ||
| if (!timer.isCancelled()) { | ||
| CacheEntry entry = cache.remove(subchannel.getAddresses()); | ||
| checkState(entry.subchannel == subchannel, "Inconsistent state"); | ||
| subchannel.shutdown(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class CacheEntry { | ||
| final Subchannel subchannel; | ||
| final ScheduledFuture<?> shutdownTimer; | ||
|
|
||
| CacheEntry(Subchannel subchannel, ScheduledFuture<?> shutdownTimer) { | ||
| this.subchannel = checkNotNull(subchannel, "subchannel"); | ||
| this.shutdownTimer = checkNotNull(shutdownTimer, "shutdownTimer"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| /* | ||
| * Copyright 2018, gRPC Authors 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 io.grpc.grpclb; | ||
|
|
||
| import io.grpc.Attributes; | ||
| import io.grpc.EquivalentAddressGroup; | ||
| import io.grpc.LoadBalancer.Helper; | ||
| import io.grpc.LoadBalancer.Subchannel; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import javax.annotation.concurrent.NotThreadSafe; | ||
|
|
||
| /** | ||
| * Manages life-cycle of Subchannels for {@link GrpclbState}. | ||
| * | ||
| * <p>All methods are run from the ChannelExecutor that the helper uses. | ||
| */ | ||
| @NotThreadSafe | ||
| interface SubchannelPool { | ||
| /** | ||
| * Pass essential utilities. | ||
| */ | ||
| void init(Helper helper, ScheduledExecutorService timerService); | ||
|
|
||
| /** | ||
| * Takes a {@link Subchannel} from the pool for the given {@code eag} if there is one available. | ||
| * Otherwise, creates and returns a new {@code Subchannel} with the given {@code eag} and {@code | ||
| * defaultAttributes}. | ||
| */ | ||
| Subchannel takeOrCreateSubchannel(EquivalentAddressGroup eag, Attributes defaultAttributes); | ||
|
|
||
| /** | ||
| * Puts a {@link Subchannel} back to the pool. From this point the Subchannel is owned by the | ||
| * pool. | ||
| */ | ||
| void returnSubchannel(Subchannel subchannel); | ||
|
|
||
| /** | ||
| * Shuts down all subchannels in the pool immediately. | ||
| */ | ||
| void clear(); | ||
| } |
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.
Return immediately if
prev != null && prev.subchannel == subchannelThere 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.
Done.