Skip to content

Commit 7eb594d

Browse files
committed
Change return type of TabletMap#getReplicas() to Set<Host>
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.
1 parent d838e54 commit 7eb594d

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

driver-core/src/main/java/com/datastax/driver/core/Metadata.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.concurrent.CopyOnWriteArrayList;
4545
import java.util.concurrent.locks.Lock;
4646
import java.util.concurrent.locks.ReentrantLock;
47-
import java.util.stream.Collectors;
4847
import org.slf4j.Logger;
4948
import org.slf4j.LoggerFactory;
5049

@@ -570,9 +569,9 @@ public Set<Host> getReplicas(
570569
if (keyspace != null && table != null) {
571570
Token token = partitioner.hash(partitionKey);
572571
assert (token instanceof Token.TokenLong64);
573-
Set<UUID> hostUuids = tabletMap.getReplicas(keyspace, table, (long) token.getValue());
574-
if (!hostUuids.isEmpty()) {
575-
return hostUuids.stream().map(this::getHost).collect(Collectors.toSet());
572+
Set<Host> replicas = tabletMap.getReplicas(keyspace, table, (long) token.getValue());
573+
if (!replicas.isEmpty()) {
574+
return replicas;
576575
}
577576
}
578577
// Fall back to tokenMap

driver-core/src/main/java/com/datastax/driver/core/TabletMap.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public Map<KeyspaceTableNamePair, TabletSet> getMapping() {
6464
* @param keyspace the keyspace that table is in
6565
* @param table the table name
6666
* @param token the token to look for
67-
* @return Set of host UUIDS that do have a tablet for given token for a given table.
67+
* @return Set of Host instances that do have a tablet for the given token and table combination.
6868
*/
69-
public Set<UUID> getReplicas(String keyspace, String table, long token) {
69+
public Set<Host> getReplicas(String keyspace, String table, long token) {
7070
TabletMap.KeyspaceTableNamePair key = new TabletMap.KeyspaceTableNamePair(keyspace, table);
7171

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

96-
HashSet<UUID> uuidSet = new HashSet<>();
96+
HashSet<Host> replicaSet = new HashSet<>();
9797
for (HostShardPair hostShardPair : row.replicas) {
98-
if (cluster.metadata.getHost(hostShardPair.getHost()) == null) {
98+
Host replica = cluster.metadata.getHost(hostShardPair.getHost());
99+
if (replica == null) {
99100
// We've encountered a stale host. Return an empty set to
100101
// misroute the request. If misrouted then response will
101102
// contain up to date tablet information that will be processed.
102103
return Collections.emptySet();
103104
} else {
104-
uuidSet.add(hostShardPair.getHost());
105+
replicaSet.add(replica);
105106
}
106107
}
107-
return uuidSet;
108+
return replicaSet;
108109
} finally {
109110
readLock.unlock();
110111
}

driver-core/src/main/java/com/datastax/driver/core/TabletMapListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.datastax.driver.core;
22

3+
// This class does not handle topology changes. Node removal is covered by TabletMap#getReplicas()
4+
// implementation and tablet overlap is resolved when adding new tablets.
35
public class TabletMapListener extends SchemaChangeListenerBase {
46
private final TabletMap tabletMap;
57

0 commit comments

Comments
 (0)