-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Make consistentHashing to a generic tools class #17718
Open
maobaolong
wants to merge
4
commits into
Alluxio:main
Choose a base branch
from
maobaolong:consistentHashImprove
base: main
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
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
156 changes: 156 additions & 0 deletions
156
dora/core/common/src/main/java/alluxio/node/ConsistentHashingNodeProvider.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,156 @@ | ||
/* | ||
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 | ||
* (the "License"). You may not use this work except in compliance with the License, which is | ||
* available at www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied, as more fully set forth in the License. | ||
* | ||
* See the NOTICE file distributed with this work for information regarding copyright ownership. | ||
*/ | ||
|
||
package alluxio.node; | ||
|
||
import static com.google.common.hash.Hashing.murmur3_32_fixed; | ||
import static java.lang.Math.ceil; | ||
import static java.lang.String.format; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import alluxio.Constants; | ||
import alluxio.collections.Pair; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.hash.HashFunction; | ||
|
||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NavigableMap; | ||
import java.util.Set; | ||
import java.util.SortedMap; | ||
import java.util.TreeMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* The consistent hashing node provider implementation. | ||
* @param <T> the type of node | ||
*/ | ||
public class ConsistentHashingNodeProvider<T> implements NodeProvider<T> { | ||
private static final HashFunction HASH_FUNCTION = murmur3_32_fixed(); | ||
private static final long UPDATE_INTERVAL_MS = Constants.MINUTE_MS; | ||
private final Function<T, String> mIdentifierFunction; | ||
private final Function<Pair<T, T>, Boolean> mNodeUpdateFunction; | ||
private Collection<T> mLastNodes = ImmutableList.of(); | ||
private NavigableMap<Integer, T> mActiveNodesByConsistentHashing; | ||
private final int mVirtualNodes; | ||
private volatile long mLastUpdatedTimestamp = 0L; | ||
private final AtomicBoolean mNeedUpdate = new AtomicBoolean(false); | ||
|
||
/** | ||
* | ||
* @param nodes the nodes to select | ||
* @param numVirtualNodes number of virtual nodes | ||
* @param identifierFunction the function to provide identifier | ||
* @param nodeUpdateFunction the function to check whether should update node | ||
* @return the instance of the {@link ConsistentHashingNodeProvider} | ||
* @param <T> the type of node | ||
*/ | ||
public static <T> ConsistentHashingNodeProvider create(List<T> nodes, int numVirtualNodes, | ||
Function<T, String> identifierFunction, | ||
Function<Pair<List<T>, List<T>>, Boolean> nodeUpdateFunction) { | ||
ConsistentHashingNodeProvider consistentHashingNodeProvider = | ||
new ConsistentHashingNodeProvider(numVirtualNodes, identifierFunction, nodeUpdateFunction); | ||
if (nodes != null && !nodes.isEmpty()) { | ||
consistentHashingNodeProvider.refresh(nodes); | ||
} | ||
return consistentHashingNodeProvider; | ||
} | ||
|
||
private ConsistentHashingNodeProvider(int virtualNodes, Function<T, String> identifierFunction, | ||
Function<Pair<T, T>, Boolean> nodeUpdateFunction) { | ||
mVirtualNodes = virtualNodes; | ||
mIdentifierFunction = identifierFunction; | ||
mNodeUpdateFunction = nodeUpdateFunction; | ||
} | ||
|
||
@Override | ||
public List<T> get(Object identifier, int count) { | ||
if (count > mVirtualNodes) { | ||
count = mVirtualNodes; | ||
} | ||
ImmutableList.Builder<T> nodes = ImmutableList.builder(); | ||
Set<T> unique = new HashSet<>(); | ||
int hashKey = HASH_FUNCTION.hashString(format("%s", identifier), UTF_8).asInt(); | ||
Map.Entry<Integer, T> entry = mActiveNodesByConsistentHashing.ceilingEntry(hashKey); | ||
T candidate; | ||
SortedMap<Integer, T> nextEntries; | ||
if (entry != null) { | ||
candidate = entry.getValue(); | ||
nextEntries = mActiveNodesByConsistentHashing.tailMap(entry.getKey(), false); | ||
} | ||
else { | ||
candidate = mActiveNodesByConsistentHashing.firstEntry().getValue(); | ||
nextEntries = mActiveNodesByConsistentHashing.tailMap( | ||
mActiveNodesByConsistentHashing.firstKey(), false); | ||
} | ||
unique.add(candidate); | ||
nodes.add(candidate); | ||
while (unique.size() < count) { | ||
for (Map.Entry<Integer, T> next : nextEntries.entrySet()) { | ||
candidate = next.getValue(); | ||
if (!unique.contains(candidate)) { | ||
unique.add(candidate); | ||
nodes.add(candidate); | ||
if (unique.size() == count) { | ||
break; | ||
} | ||
} | ||
} | ||
nextEntries = mActiveNodesByConsistentHashing; | ||
} | ||
return nodes.build(); | ||
} | ||
|
||
@Override | ||
public void refresh(List<T> nodes) { | ||
// check if we need to update worker info | ||
if (mLastUpdatedTimestamp <= 0L | ||
|| System.currentTimeMillis() - mLastUpdatedTimestamp > UPDATE_INTERVAL_MS) { | ||
mNeedUpdate.set(true); | ||
} | ||
// update worker info if needed | ||
if (mNeedUpdate.compareAndSet(true, false)) { | ||
if (mNodeUpdateFunction.apply(new Pair(nodes, mLastNodes))) { | ||
build(nodes); | ||
} | ||
mLastUpdatedTimestamp = System.currentTimeMillis(); | ||
} | ||
} | ||
|
||
private void build(Collection<T> nodes) { | ||
NavigableMap<Integer, T> activeNodesByConsistentHashing = new TreeMap<>(); | ||
int weight = (int) ceil(1.0 * mVirtualNodes / nodes.size()); | ||
for (T node : nodes) { | ||
for (int i = 0; i < weight; i++) { | ||
activeNodesByConsistentHashing.put( | ||
HASH_FUNCTION.hashString(format("%s%d", mIdentifierFunction.apply(node), i), | ||
UTF_8).asInt(), | ||
node); | ||
} | ||
} | ||
mLastNodes = nodes; | ||
mActiveNodesByConsistentHashing = activeNodesByConsistentHashing; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("VirtualNodes", mVirtualNodes) | ||
.add("mActiveNodesSize", | ||
mActiveNodesByConsistentHashing == null ? -1 : mActiveNodesByConsistentHashing.size()) | ||
.toString(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
dora/core/common/src/main/java/alluxio/node/ModularHashingNodeProvider.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,53 @@ | ||
/* | ||
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 | ||
* (the "License"). You may not use this work except in compliance with the License, which is | ||
* available at www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied, as more fully set forth in the License. | ||
* | ||
* See the NOTICE file distributed with this work for information regarding copyright ownership. | ||
*/ | ||
|
||
package alluxio.node; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* The modular hashing node provider implementation. | ||
* @param <T> the type of node | ||
*/ | ||
public class ModularHashingNodeProvider<T> implements NodeProvider<T> { | ||
private List<T> mLastNodes; | ||
|
||
/** | ||
* Constructs a new {@link ModularHashingNodeProvider}. | ||
* | ||
* @param nodes the nodes to select | ||
*/ | ||
public ModularHashingNodeProvider(List<T> nodes) { | ||
mLastNodes = nodes; | ||
} | ||
|
||
@Override | ||
public List<T> get(Object identifier, int count) { | ||
if (mLastNodes == null || mLastNodes.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
int size = mLastNodes.size(); | ||
int mod = identifier.hashCode() % size; | ||
int position = mod < 0 ? mod + size : mod; | ||
List<T> chosenCandidates = new ArrayList<>(); | ||
for (int i = 0; i < count && i < mLastNodes.size(); i++) { | ||
chosenCandidates.add(mLastNodes.get((position + i) % size)); | ||
} | ||
return chosenCandidates; | ||
} | ||
|
||
@Override | ||
public void refresh(List<T> nodes) { | ||
mLastNodes = nodes; | ||
} | ||
} |
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.
what is the use case for this hash+mode algorithm?
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.
Modular hash is simple fast and do not care the node id, ConsistentHashing is complex and have to maintain the node list dynamically.
We use these two algorithm to supply a fake file block location, so that the application can dispatch the split to the node what we want.
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.
Modular hash does not support dynamic worker membership, so I imagine you have:
I don't think I fully understood how you fake block locations by modular operation. But I think sure you can implement this, and let us know how that works out in your environment.
In #17817 we have extracted WorkerLocationPolicy interface. Could you move your implementation under
WorkerLocationPolicy
? Thanks!