Skip to content
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

3.x: Make TabletMap return empty replica set when stale replica is found #385

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -570,9 +569,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
23 changes: 16 additions & 7 deletions driver-core/src/main/java/com/datastax/driver/core/TabletMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ public Map<KeyspaceTableNamePair, TabletSet> getMapping() {
}

/**
* Finds hosts that have replicas for a given table and token combination
* Finds hosts that have replicas for a given table and token combination. Meant for use in query
* planning. Can return empty collection if internal replica list information is determined not up
* to date.
*
* @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 @@ -91,12 +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)
uuidSet.add(hostShardPair.getHost());
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 {
replicaSet.add(replica);
}
}
return uuidSet;
return replicaSet;
} finally {
readLock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.datastax.driver.core;

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

Expand Down
Loading