forked from apache/cassandra-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ShardingInfo field to DefaultNode and getShardingInfo() to Node interface. Adds NodeShardingInfo interface as public API. With this change, having a token and having determined the Node it belongs to, users can further use NodeShardingInfo to determine the shard it belongs to. Fixes #232.
- Loading branch information
Showing
6 changed files
with
140 additions
and
3 deletions.
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
19 changes: 19 additions & 0 deletions
19
core/src/main/java/com/datastax/oss/driver/api/core/metadata/NodeShardingInfo.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,19 @@ | ||
package com.datastax.oss.driver.api.core.metadata; | ||
|
||
import com.datastax.oss.driver.api.core.metadata.token.Token; | ||
|
||
/** Holds sharding information for a particular Node. */ | ||
public interface NodeShardingInfo { | ||
|
||
public int getShardsCount(); | ||
|
||
/** | ||
* Returns a shardId for a given Token. | ||
* | ||
* <p>Accepts all types of Tokens but if the Token is not an instance of {@link | ||
* com.datastax.oss.driver.internal.core.metadata.token.TokenLong64} then the return value could | ||
* be not meaningful (e.g. random shard). This method does not verify if the given Token belongs | ||
* to the Node. | ||
*/ | ||
public int shardId(Token t); | ||
} |
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
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
89 changes: 89 additions & 0 deletions
89
examples/src/main/java/com/datastax/oss/driver/examples/basic/TokenMapAndShardIdLookup.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,89 @@ | ||
package com.datastax.oss.driver.examples.basic; | ||
|
||
import com.datastax.oss.driver.api.core.CqlIdentifier; | ||
import com.datastax.oss.driver.api.core.CqlSession; | ||
import com.datastax.oss.driver.api.core.DefaultProtocolVersion; | ||
import com.datastax.oss.driver.api.core.cql.ResultSet; | ||
import com.datastax.oss.driver.api.core.cql.SimpleStatement; | ||
import com.datastax.oss.driver.api.core.cql.TraceEvent; | ||
import com.datastax.oss.driver.api.core.metadata.Metadata; | ||
import com.datastax.oss.driver.api.core.metadata.Node; | ||
import com.datastax.oss.driver.api.core.metadata.TokenMap; | ||
import com.datastax.oss.driver.api.core.metadata.token.Token; | ||
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs; | ||
import java.nio.ByteBuffer; | ||
import java.util.Set; | ||
|
||
/** | ||
* Demonstrates usage of TokenMap and NodeShardingInfo Needs a Scylla cluster to be running locally | ||
* or adjustment of session builder. | ||
*/ | ||
public class TokenMapAndShardIdLookup { | ||
|
||
private static String CREATE_KEYSPACE = | ||
"CREATE KEYSPACE IF NOT EXISTS tokenmap_example_ks " | ||
+ "WITH replication = {" | ||
+ "'class': 'SimpleStrategy', " | ||
+ "'replication_factor': 1" | ||
+ "}"; | ||
|
||
private static String CREATE_TABLE = | ||
"" | ||
+ "CREATE TABLE IF NOT EXISTS tokenmap_example_ks.example_tab (" | ||
+ "my_column bigint," | ||
+ "PRIMARY KEY (my_column)" | ||
+ ")"; | ||
|
||
private static String INSERT_COLUMN = | ||
"INSERT INTO tokenmap_example_ks.example_tab (my_column) VALUES (2)"; | ||
|
||
private static String SELECT_COLUMN = | ||
"SELECT * FROM tokenmap_example_ks.example_tab WHERE my_column = 2"; | ||
|
||
private static ByteBuffer PARTITION_KEY = TypeCodecs.BIGINT.encode(2L, DefaultProtocolVersion.V3); | ||
|
||
public static void main(String[] args) { | ||
|
||
try (CqlSession session = CqlSession.builder().build()) { | ||
|
||
System.out.printf("Connected session: %s%n", session.getName()); | ||
|
||
session.execute(CREATE_KEYSPACE); | ||
session.execute(CREATE_TABLE); | ||
session.execute(INSERT_COLUMN); | ||
|
||
Metadata metadata = session.refreshSchema(); | ||
|
||
System.out.println("Prepared example data"); | ||
|
||
TokenMap tokenMap = metadata.getTokenMap().get(); | ||
|
||
Set<Node> nodes = | ||
tokenMap.getReplicas(CqlIdentifier.fromCql("tokenmap_example_ks"), PARTITION_KEY); | ||
System.out.println("Replica set size: " + nodes.size()); | ||
|
||
Token token = tokenMap.newToken(PARTITION_KEY); | ||
assert nodes.size() > 0; | ||
Node node = nodes.iterator().next(); | ||
|
||
assert node.getShardingInfo() != null; | ||
int shardId = node.getShardingInfo().shardId(token); | ||
|
||
System.out.println( | ||
"Hardcoded partition key should belong to shard number " | ||
+ shardId | ||
+ " (on Node: " | ||
+ node | ||
+ ")"); | ||
|
||
System.out.println("You can compare it with SELECT query trace:"); | ||
// If there is only 1 node, then the SELECT has to hit the one we did shardId calculation for. | ||
SimpleStatement statement = SimpleStatement.builder(SELECT_COLUMN).setTracing(true).build(); | ||
ResultSet rs = session.execute(statement); | ||
|
||
for (TraceEvent event : rs.getExecutionInfo().getQueryTrace().getEvents()) { | ||
System.out.println(event); | ||
} | ||
} | ||
} | ||
} |