Skip to content

Commit

Permalink
Change return type of TabletMap#getReplicas() to Set<Host>
Browse files Browse the repository at this point in the history
Having this method return set of hosts instead saves one `Map#get()` query
per host.
In theory, previously it was also possible that between `TabletMap#getReplicas()`
returning and `Metadata#getReplicas()` translating uuids to hosts, some hosts could
become invalid, making the translation represent them as nulls.
  • Loading branch information
Bouncheck committed Nov 27, 2024
1 parent 7ee8323 commit 493b19d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -554,9 +553,9 @@ public Set<Host> getReplicas(
if (keyspace != null && table != null) {
Token token = partitioner.hash(partitionKey);
assert (token instanceof Token.TokenLong64);
Set<UUID> hostUuids = tabletMap.getReplicas(keyspace, table, (long) token.getValue());
if (!hostUuids.isEmpty()) {
return hostUuids.stream().map(this::getHost).collect(Collectors.toSet());
Set<Host> replicas = tabletMap.getReplicas(keyspace, table, (long) token.getValue());
if (!replicas.isEmpty()) {
return replicas;
}
}
// Fall back to tokenMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public Map<KeyspaceTableNamePair, TabletSet> getMapping() {
* @param keyspace the keyspace that table is in
* @param table the table name
* @param token the token to look for
* @return Set of host UUIDS that do have a tablet for given token for a given table.
* @return Set of Host instances that do have a tablet for the given token and table combination.
*/
public Set<UUID> getReplicas(String keyspace, String table, long token) {
public Set<Host> getReplicas(String keyspace, String table, long token) {
TabletMap.KeyspaceTableNamePair key = new TabletMap.KeyspaceTableNamePair(keyspace, table);

if (mapping == null) {
Expand All @@ -93,18 +93,19 @@ public Set<UUID> getReplicas(String keyspace, String table, long token) {
return Collections.emptySet();
}

HashSet<UUID> uuidSet = new HashSet<>();
HashSet<Host> replicaSet = new HashSet<>();
for (HostShardPair hostShardPair : row.replicas) {
if (cluster.metadata.getHost(hostShardPair.getHost()) == null) {
Host replica = cluster.metadata.getHost(hostShardPair.getHost());
if (replica == null) {
// We've encountered a stale host. Return an empty set to
// misroute the request. If misrouted then response will
// contain up to date tablet information that will be processed.
return Collections.emptySet();
} else {
uuidSet.add(hostShardPair.getHost());
replicaSet.add(replica);
}
}
return uuidSet;
return replicaSet;
} finally {
readLock.unlock();
}
Expand Down

0 comments on commit 493b19d

Please sign in to comment.