Skip to content

Commit f7ea063

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 7ee8323 commit f7ea063

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
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

@@ -554,9 +553,9 @@ public Set<Host> getReplicas(
554553
if (keyspace != null && table != null) {
555554
Token token = partitioner.hash(partitionKey);
556555
assert (token instanceof Token.TokenLong64);
557-
Set<UUID> hostUuids = tabletMap.getReplicas(keyspace, table, (long) token.getValue());
558-
if (!hostUuids.isEmpty()) {
559-
return hostUuids.stream().map(this::getHost).collect(Collectors.toSet());
556+
Set<Host> replicas = tabletMap.getReplicas(keyspace, table, (long) token.getValue());
557+
if (!replicas.isEmpty()) {
558+
return replicas;
560559
}
561560
}
562561
// Fall back to tokenMap

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Map<KeyspaceTableNamePair, TabletSet> getMapping() {
6666
* @param token the token to look for
6767
* @return Set of host UUIDS that do have a tablet for given token for a given table.
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
}

0 commit comments

Comments
 (0)