-
Notifications
You must be signed in to change notification settings - Fork 211
Add mechanism for pro-active routing connection management #802
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
Open
timmartin-stripe
wants to merge
10
commits into
Netflix:master
Choose a base branch
from
timmartin-stripe:timmartin/add-mechanism-for-pro-active-connection-management
base: master
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6522656
Add more efficient mechanism for managing connections
timmartin-stripe 899f810
add some tests
timmartin-stripe 1f860b0
move to using Optional instead of checking null
timmartin-stripe 2bac27f
clean up and test fixes
timmartin-stripe e7d656e
set up config for proactive router
timmartin-stripe b606b57
Clean up for PR
timmartin-stripe 46cf57b
actually increment connection updates
timmartin-stripe c46322d
Merge branch 'master' into timmartin/add-mechanism-for-pro-active-con…
timmartin-stripe 7159abd
some more clean up
timmartin-stripe 4e8aeab
address PR comments
timmartin-stripe 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 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
171 changes: 171 additions & 0 deletions
171
...work/src/main/java/io/reactivex/mantis/network/push/ProactiveConsistentHashingRouter.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,171 @@ | ||
| /* | ||
| * Copyright 2025 Netflix, Inc. | ||
| * | ||
| * 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.reactivex.mantis.network.push; | ||
|
|
||
| import com.netflix.spectator.api.Tag; | ||
| import io.mantisrx.common.metrics.Counter; | ||
| import io.mantisrx.common.metrics.Metrics; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import rx.functions.Func1; | ||
|
|
||
| import java.util.*; | ||
| import java.util.concurrent.locks.ReadWriteLock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
|
||
| public class ProactiveConsistentHashingRouter<K, V> implements ProactiveRouter<KeyValuePair<K, V>> { | ||
| private static final Logger logger = LoggerFactory.getLogger(ProactiveConsistentHashingRouter.class); | ||
| private final int connectionRepetitionOnRing; | ||
|
|
||
| protected final Func1<KeyValuePair<K, V>, byte[]> encoder; | ||
| protected final Counter numEventsRouted; | ||
| protected final Counter numEventsProcessed; | ||
| protected final Counter numConnectionUpdates; | ||
| protected final Metrics metrics; | ||
| private final HashFunction hashFunction; | ||
| private final NavigableMap<Long, AsyncConnection<KeyValuePair<K, V>>> ring = new TreeMap<>(); | ||
| private final ReadWriteLock ringLock = new ReentrantReadWriteLock(); | ||
|
|
||
| public ProactiveConsistentHashingRouter( | ||
| String name, | ||
| Func1<KeyValuePair<K, V>, byte[]> dataEncoder, | ||
| HashFunction hashFunction) { | ||
| this(name, dataEncoder, hashFunction, 1000); | ||
| } | ||
|
|
||
| public ProactiveConsistentHashingRouter( | ||
| String name, | ||
| Func1<KeyValuePair<K, V>, byte[]> dataEncoder, | ||
| HashFunction hashFunction, | ||
| int ringRepetitionPerConnection) { | ||
| this.connectionRepetitionOnRing = ringRepetitionPerConnection; | ||
| this.encoder = dataEncoder; | ||
| metrics = new Metrics.Builder() | ||
| .id("Router_" + name, Tag.of("router_type", "proactive_consistent_hashing")) | ||
| .addCounter("numEventsRouted") | ||
| .addCounter("numEventsProcessed") | ||
| .addCounter("numConnectionUpdates") | ||
| .build(); | ||
| numEventsRouted = metrics.getCounter("numEventsRouted"); | ||
| numEventsProcessed = metrics.getCounter("numEventsProcessed"); | ||
| numConnectionUpdates = metrics.getCounter("numConnectionUpdates"); | ||
| this.hashFunction = hashFunction; | ||
| } | ||
|
|
||
| @Override | ||
| public void route(List<KeyValuePair<K, V>> chunks) { | ||
| if (chunks == null || chunks.isEmpty()) { | ||
| return; | ||
| } | ||
| numEventsProcessed.increment(chunks.size()); | ||
|
|
||
| // Read lock only for ring access | ||
| Map<AsyncConnection<KeyValuePair<K, V>>, List<byte[]>> writes; | ||
| ringLock.readLock().lock(); | ||
| try { | ||
| if (ring.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| int numConnections = ring.size() / connectionRepetitionOnRing; | ||
| int bufferCapacity = (chunks.size() / numConnections) + 1; // assume even distribution | ||
| writes = new HashMap<>(numConnections); | ||
|
|
||
| // process chunks (ring access inside lookupConnection) | ||
| for (KeyValuePair<K, V> kvp : chunks) { | ||
| long hash = kvp.getKeyBytesHashed(); | ||
| // lookup slot | ||
| Map.Entry<Long, AsyncConnection<KeyValuePair<K, V>>> connectionEntry = ring.ceilingEntry(hash); | ||
| AsyncConnection<KeyValuePair<K, V>> connection = (connectionEntry == null ? ring.firstEntry() : connectionEntry).getValue(); | ||
| // add to writes | ||
| Func1<KeyValuePair<K, V>, Boolean> predicate = connection.getPredicate(); | ||
| if (predicate == null || predicate.call(kvp)) { | ||
| List<byte[]> buffer = writes.computeIfAbsent(connection, k -> new ArrayList<>(bufferCapacity)); | ||
| buffer.add(encoder.call(kvp)); | ||
| } | ||
| } | ||
| } finally { | ||
| ringLock.readLock().unlock(); | ||
| } | ||
|
|
||
| // process writes (outside lock - no ring access) | ||
| if (!writes.isEmpty()) { | ||
| for (Map.Entry<AsyncConnection<KeyValuePair<K, V>>, List<byte[]>> entry : writes.entrySet()) { | ||
| AsyncConnection<KeyValuePair<K, V>> connection = entry.getKey(); | ||
| List<byte[]> toWrite = entry.getValue(); | ||
| connection.write(toWrite); | ||
| numEventsRouted.increment(toWrite.size()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addConnection(AsyncConnection<KeyValuePair<K, V>> connection) { | ||
| String connectionId = connection.getSlotId(); | ||
| if (connectionId == null) { | ||
| throw new IllegalStateException("Connection must specify an id for consistent hashing"); | ||
| } | ||
|
|
||
| List<String> hashCollisions = new ArrayList<>(); | ||
| ringLock.writeLock().lock(); | ||
| try { | ||
| for (int i = 0; i < connectionRepetitionOnRing; i++) { | ||
| // hash node on ring | ||
| byte[] connectionBytes = (connectionId + "-" + i).getBytes(); | ||
| long hash = hashFunction.computeHash(connectionBytes); | ||
| if (ring.containsKey(hash)) { | ||
| hashCollisions.add(connectionId + "-" + i); | ||
| } | ||
| ring.put(hash, connection); | ||
| } | ||
| } finally { | ||
| ringLock.writeLock().unlock(); | ||
| } | ||
| numConnectionUpdates.increment(); | ||
|
|
||
| // Log outside lock | ||
| if (!hashCollisions.isEmpty()) { | ||
| logger.error("Hash collisions detected when adding connection {}: {}", connectionId, hashCollisions); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void removeConnection(AsyncConnection<KeyValuePair<K, V>> connection) { | ||
| String connectionId = connection.getSlotId(); | ||
| if (connectionId == null) { | ||
| throw new IllegalStateException("Connection must specify an id for consistent hashing"); | ||
| } | ||
|
|
||
| ringLock.writeLock().lock(); | ||
| try { | ||
| for (int i = 0; i < connectionRepetitionOnRing; i++) { | ||
| // hash node on ring | ||
| byte[] connectionBytes = (connectionId + "-" + i).getBytes(); | ||
| long hash = hashFunction.computeHash(connectionBytes); | ||
| ring.remove(hash); | ||
| } | ||
| } finally { | ||
| ringLock.writeLock().unlock(); | ||
| } | ||
| numConnectionUpdates.increment(); | ||
| } | ||
|
|
||
| @Override | ||
| public Metrics getMetrics() { | ||
| return metrics; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Basically the same as
ConsistentHashingRouterexcept that does not recalculate the whole thing. It just makes the updates to add/remove a connection.