|
| 1 | +/* |
| 2 | + * Copyright ScyllaDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.driver.core; |
| 17 | + |
| 18 | +import static com.datastax.driver.core.Assertions.assertThat; |
| 19 | +import static org.testng.Assert.assertTrue; |
| 20 | + |
| 21 | +import com.datastax.driver.core.QueryTrace.Event; |
| 22 | +import com.datastax.driver.core.utils.ScyllaOnly; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | +import org.testng.annotations.DataProvider; |
| 26 | +import org.testng.annotations.Factory; |
| 27 | +import org.testng.annotations.Test; |
| 28 | + |
| 29 | +@ScyllaOnly |
| 30 | +@CCMConfig( |
| 31 | + numberOfNodes = 3, |
| 32 | + jvmArgs = { |
| 33 | + /* 2-shard Scylla: */ |
| 34 | + "--smp", |
| 35 | + "2" |
| 36 | + }) |
| 37 | +public class ShardAwarenessTest extends CCMTestsSupport { |
| 38 | + private static final Logger logger = LoggerFactory.getLogger(ShardAwarenessTest.class); |
| 39 | + private final boolean useAdvancedShardAwareness; |
| 40 | + |
| 41 | + @Factory(dataProvider = "dataProvider") |
| 42 | + public ShardAwarenessTest(boolean useAdvancedShardAwareness) { |
| 43 | + this.useAdvancedShardAwareness = useAdvancedShardAwareness; |
| 44 | + } |
| 45 | + |
| 46 | + @DataProvider |
| 47 | + public static Object[][] dataProvider() { |
| 48 | + // Run the tests with advanced shard awareness |
| 49 | + // and without it. |
| 50 | + return new Object[][] {{false}, {true}}; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Cluster.Builder createClusterBuilder() { |
| 55 | + Cluster.Builder builder = super.createClusterBuilder(); |
| 56 | + if (!useAdvancedShardAwareness) { |
| 57 | + builder = builder.withoutAdvancedShardAwareness(); |
| 58 | + } |
| 59 | + return builder; |
| 60 | + } |
| 61 | + |
| 62 | + private void verifyCorrectShardSingleRow(String pk, String ck, String v, String shard) { |
| 63 | + PreparedStatement prepared = |
| 64 | + session().prepare("SELECT pk, ck, v FROM shardawaretest.t WHERE pk=? AND ck=?"); |
| 65 | + ResultSet result = session().execute(prepared.bind(pk, ck).enableTracing()); |
| 66 | + |
| 67 | + Row row = result.one(); |
| 68 | + assertTrue(result.isExhausted()); |
| 69 | + assertThat(row).isNotNull(); |
| 70 | + assertThat(row.getString("pk")).isEqualTo(pk); |
| 71 | + assertThat(row.getString("ck")).isEqualTo(ck); |
| 72 | + assertThat(row.getString("v")).isEqualTo(v); |
| 73 | + |
| 74 | + ExecutionInfo executionInfo = result.getExecutionInfo(); |
| 75 | + |
| 76 | + QueryTrace trace = executionInfo.getQueryTrace(); |
| 77 | + boolean anyLocal = false; |
| 78 | + for (Event event : trace.getEvents()) { |
| 79 | + logger.info( |
| 80 | + " {} - {} - [{}] - {}", |
| 81 | + event.getSourceElapsedMicros(), |
| 82 | + event.getSource(), |
| 83 | + event.getThreadName(), |
| 84 | + event.getDescription()); |
| 85 | + assertThat(event.getThreadName()).isEqualTo(shard); |
| 86 | + if (event.getDescription().contains("querying locally")) { |
| 87 | + anyLocal = true; |
| 88 | + } |
| 89 | + } |
| 90 | + assertThat(anyLocal); |
| 91 | + } |
| 92 | + |
| 93 | + @Test(groups = "short") |
| 94 | + public void correctShardInTracingTest() { |
| 95 | + session().execute("DROP KEYSPACE IF EXISTS shardawaretest"); |
| 96 | + session() |
| 97 | + .execute( |
| 98 | + "CREATE KEYSPACE shardawaretest WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}"); |
| 99 | + session() |
| 100 | + .execute("CREATE TABLE shardawaretest.t (pk text, ck text, v text, PRIMARY KEY (pk, ck))"); |
| 101 | + |
| 102 | + PreparedStatement populateStatement = |
| 103 | + session().prepare("INSERT INTO shardawaretest.t (pk, ck, v) VALUES (?, ?, ?)"); |
| 104 | + session().execute(populateStatement.bind("a", "b", "c")); |
| 105 | + session().execute(populateStatement.bind("e", "f", "g")); |
| 106 | + session().execute(populateStatement.bind("100002", "f", "g")); |
| 107 | + |
| 108 | + verifyCorrectShardSingleRow("a", "b", "c", "shard 0"); |
| 109 | + verifyCorrectShardSingleRow("e", "f", "g", "shard 0"); |
| 110 | + verifyCorrectShardSingleRow("100002", "f", "g", "shard 1"); |
| 111 | + } |
| 112 | +} |
0 commit comments