|
| 1 | +package com.datastax.driver.core; |
| 2 | + |
| 3 | +import static com.datastax.driver.core.Assertions.assertThat; |
| 4 | +import static com.datastax.driver.core.Metadata.handleId; |
| 5 | +import static org.mockito.Matchers.anyObject; |
| 6 | +import static org.mockito.Mockito.after; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.timeout; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | + |
| 11 | +import com.datastax.driver.core.policies.RoundRobinPolicy; |
| 12 | +import com.datastax.driver.core.utils.ScyllaVersion; |
| 13 | +import com.google.common.collect.Lists; |
| 14 | +import java.util.List; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | +import org.mockito.ArgumentCaptor; |
| 17 | +import org.testng.annotations.AfterMethod; |
| 18 | +import org.testng.annotations.BeforeMethod; |
| 19 | +import org.testng.annotations.Test; |
| 20 | + |
| 21 | +@CCMConfig( |
| 22 | + createCluster = false, |
| 23 | + numberOfNodes = 2, |
| 24 | + jvmArgs = { |
| 25 | + "--experimental-features=consistent-topology-changes", |
| 26 | + "--experimental-features=tablets" |
| 27 | + }) |
| 28 | +@ScyllaVersion(minOSS = "6.0.0", minEnterprise = "2024.2", description = "Needs to support tablets") |
| 29 | +public class TabletMapListenerTest extends CCMTestsSupport { |
| 30 | + private static final int INITIAL_TABLETS = 32; |
| 31 | + private static final String KEYSPACE_NAME = "listenerTest"; |
| 32 | + private static final String TABLE_NAME = "testTable"; |
| 33 | + private static final String CREATE_TABLETS_KEYSPACE_QUERY = |
| 34 | + "CREATE KEYSPACE " |
| 35 | + + KEYSPACE_NAME |
| 36 | + + " WITH replication = {'class': " |
| 37 | + + "'NetworkTopologyStrategy', " |
| 38 | + + "'replication_factor': '1'} AND durable_writes = true AND tablets = " |
| 39 | + + "{'initial': " |
| 40 | + + INITIAL_TABLETS |
| 41 | + + "};"; |
| 42 | + |
| 43 | + private static final String CREATE_KEYSPACE = CREATE_TABLETS_KEYSPACE_QUERY; |
| 44 | + private static final String ALTER_KEYSPACE = |
| 45 | + "ALTER KEYSPACE " + KEYSPACE_NAME + " WITH durable_writes = false"; |
| 46 | + private static final String DROP_KEYSPACE = "DROP KEYSPACE " + KEYSPACE_NAME; |
| 47 | + |
| 48 | + private static final String CREATE_TABLE = |
| 49 | + "CREATE TABLE " + KEYSPACE_NAME + "." + TABLE_NAME + "(i int primary key)"; |
| 50 | + private static final String INSERT_QUERY_TEMPLATE = |
| 51 | + "INSERT INTO " + KEYSPACE_NAME + "." + TABLE_NAME + " (i) VALUES (%s)"; |
| 52 | + private static final String INSERT_ALTERED_TEMPLATE = |
| 53 | + "INSERT INTO " + KEYSPACE_NAME + "." + TABLE_NAME + " (i,j) VALUES (%s,%s)"; |
| 54 | + private static final String SELECT_PK_WHERE = |
| 55 | + "SELECT i FROM " + KEYSPACE_NAME + "." + TABLE_NAME + " WHERE i = ?"; |
| 56 | + private static final String ALTER_TABLE = |
| 57 | + "ALTER TABLE " + KEYSPACE_NAME + "." + TABLE_NAME + " ADD j int"; |
| 58 | + private static final String DROP_TABLE = "DROP TABLE " + KEYSPACE_NAME + "." + TABLE_NAME; |
| 59 | + |
| 60 | + /** The maximum time that the test will wait to check that listeners have been notified. */ |
| 61 | + private static final long NOTIF_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(1); |
| 62 | + /** Shorter timeout for less important checks that listeners did not react to specific actions */ |
| 63 | + private static final long SHORT_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(4); |
| 64 | + |
| 65 | + Cluster cluster; |
| 66 | + Session session; |
| 67 | + SchemaChangeListener listener; |
| 68 | + List<SchemaChangeListener> listeners; |
| 69 | + |
| 70 | + @BeforeMethod(groups = "short") |
| 71 | + public void setup() throws InterruptedException { |
| 72 | + cluster = |
| 73 | + createClusterBuilderNoDebouncing().withLoadBalancingPolicy(new RoundRobinPolicy()).build(); |
| 74 | + |
| 75 | + session = cluster.connect(); |
| 76 | + |
| 77 | + cluster.register(listener = mock(TabletMapListener.class)); |
| 78 | + listeners = Lists.newArrayList(listener); |
| 79 | + } |
| 80 | + |
| 81 | + // Checks for tablet removal both on table update and removal |
| 82 | + @Test(groups = "short") |
| 83 | + public void should_remove_tablets_on_table_alterations() throws InterruptedException { |
| 84 | + session.execute(CREATE_KEYSPACE); |
| 85 | + ArgumentCaptor<KeyspaceMetadata> added = null; |
| 86 | + for (SchemaChangeListener listener : listeners) { |
| 87 | + added = ArgumentCaptor.forClass(KeyspaceMetadata.class); |
| 88 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onKeyspaceAdded(added.capture()); |
| 89 | + assertThat(added.getValue()).hasName(handleId(KEYSPACE_NAME)); |
| 90 | + } |
| 91 | + assert added != null; |
| 92 | + |
| 93 | + TabletMap tabletMap; |
| 94 | + tabletMap = cluster.getMetadata().getTabletMap(); |
| 95 | + |
| 96 | + session.execute(CREATE_TABLE); |
| 97 | + assertThat(tabletMap.getMapping()) |
| 98 | + .doesNotContainKey( |
| 99 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 100 | + |
| 101 | + session.execute(String.format(INSERT_QUERY_TEMPLATE, "42")); |
| 102 | + session.execute(session.prepare(SELECT_PK_WHERE).bind(42)); |
| 103 | + session.execute(session.prepare(SELECT_PK_WHERE).bind(42)); |
| 104 | + assertThat(tabletMap.getMapping()) |
| 105 | + .containsKey( |
| 106 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 107 | + |
| 108 | + session.execute(ALTER_TABLE); |
| 109 | + for (SchemaChangeListener listener : listeners) { |
| 110 | + ArgumentCaptor<TableMetadata> current = ArgumentCaptor.forClass(TableMetadata.class); |
| 111 | + ArgumentCaptor<TableMetadata> previous = ArgumentCaptor.forClass(TableMetadata.class); |
| 112 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)) |
| 113 | + .onTableChanged(current.capture(), previous.capture()); |
| 114 | + assertThat(previous.getValue().getKeyspace()).hasName(handleId(KEYSPACE_NAME)); |
| 115 | + assertThat(previous.getValue()).hasName(handleId(TABLE_NAME)); |
| 116 | + } |
| 117 | + assertThat(tabletMap.getMapping()) |
| 118 | + .doesNotContainKey( |
| 119 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 120 | + |
| 121 | + session.execute(String.format(INSERT_ALTERED_TEMPLATE, "42", "42")); |
| 122 | + session.execute(session.prepare(SELECT_PK_WHERE).bind(42)); |
| 123 | + session.execute(session.prepare(SELECT_PK_WHERE).bind(42)); |
| 124 | + assertThat(tabletMap.getMapping()) |
| 125 | + .containsKey( |
| 126 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 127 | + |
| 128 | + session.execute(DROP_TABLE); |
| 129 | + ArgumentCaptor<TableMetadata> removed = null; |
| 130 | + for (SchemaChangeListener listener : listeners) { |
| 131 | + removed = ArgumentCaptor.forClass(TableMetadata.class); |
| 132 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onTableRemoved(removed.capture()); |
| 133 | + assertThat(removed.getValue().getKeyspace()).hasName(handleId(KEYSPACE_NAME)); |
| 134 | + assertThat(removed.getValue()).hasName(handleId(TABLE_NAME)); |
| 135 | + } |
| 136 | + assert removed != null; |
| 137 | + assertThat(tabletMap.getMapping()) |
| 138 | + .doesNotContainKey( |
| 139 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 140 | + |
| 141 | + session.execute(DROP_KEYSPACE); |
| 142 | + } |
| 143 | + |
| 144 | + @Test(groups = "short") |
| 145 | + public void should_remove_tablets_on_keyspace_alterations() { |
| 146 | + session.execute(CREATE_KEYSPACE); |
| 147 | + ArgumentCaptor<KeyspaceMetadata> added = null; |
| 148 | + for (SchemaChangeListener listener : listeners) { |
| 149 | + added = ArgumentCaptor.forClass(KeyspaceMetadata.class); |
| 150 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onKeyspaceAdded(added.capture()); |
| 151 | + assertThat(added.getValue()).hasName(handleId(KEYSPACE_NAME)); |
| 152 | + } |
| 153 | + assert added != null; |
| 154 | + |
| 155 | + TabletMap tabletMap; |
| 156 | + tabletMap = cluster.getMetadata().getTabletMap(); |
| 157 | + |
| 158 | + session.execute(CREATE_TABLE); |
| 159 | + session.execute(String.format(INSERT_QUERY_TEMPLATE, "42")); |
| 160 | + session.execute(session.prepare(SELECT_PK_WHERE).bind(42)); |
| 161 | + session.execute(session.prepare(SELECT_PK_WHERE).bind(42)); |
| 162 | + assertThat(tabletMap.getMapping()) |
| 163 | + .containsKey( |
| 164 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 165 | + |
| 166 | + assertThat(cluster.getMetadata().getKeyspace(KEYSPACE_NAME).isDurableWrites()).isTrue(); |
| 167 | + |
| 168 | + session.execute(ALTER_KEYSPACE); |
| 169 | + assertThat(cluster.getMetadata().getKeyspace(KEYSPACE_NAME)).isNotDurableWrites(); |
| 170 | + for (SchemaChangeListener listener : listeners) { |
| 171 | + ArgumentCaptor<KeyspaceMetadata> current = ArgumentCaptor.forClass(KeyspaceMetadata.class); |
| 172 | + ArgumentCaptor<KeyspaceMetadata> previous = ArgumentCaptor.forClass(KeyspaceMetadata.class); |
| 173 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)) |
| 174 | + .onKeyspaceChanged(current.capture(), previous.capture()); |
| 175 | + assertThat(previous.getValue()).hasName(handleId(KEYSPACE_NAME)); |
| 176 | + } |
| 177 | + for (SchemaChangeListener listener : listeners) { |
| 178 | + verify(listener, after((int) SHORT_TIMEOUT_MS).never()) |
| 179 | + .onTableChanged(anyObject(), anyObject()); |
| 180 | + } |
| 181 | + assertThat(tabletMap.getMapping()) |
| 182 | + .doesNotContainKey( |
| 183 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 184 | + |
| 185 | + session.execute(session.prepare(SELECT_PK_WHERE).bind(42)); |
| 186 | + session.execute(session.prepare(SELECT_PK_WHERE).bind(42)); |
| 187 | + |
| 188 | + assertThat(tabletMap.getMapping()) |
| 189 | + .containsKey( |
| 190 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 191 | + |
| 192 | + session.execute(DROP_KEYSPACE); |
| 193 | + for (SchemaChangeListener listener : listeners) { |
| 194 | + ArgumentCaptor<KeyspaceMetadata> removed = ArgumentCaptor.forClass(KeyspaceMetadata.class); |
| 195 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onKeyspaceRemoved(removed.capture()); |
| 196 | + assertThat(removed.getValue()).hasName(handleId(KEYSPACE_NAME)); |
| 197 | + } |
| 198 | + assertThat(tabletMap.getMapping()) |
| 199 | + .doesNotContainKey( |
| 200 | + new TabletMap.KeyspaceTableNamePair(handleId(KEYSPACE_NAME), handleId(TABLE_NAME))); |
| 201 | + } |
| 202 | + |
| 203 | + @AfterMethod(groups = "short", alwaysRun = true) |
| 204 | + public void teardown() { |
| 205 | + if (session != null) session.close(); |
| 206 | + if (cluster != null) cluster.close(); |
| 207 | + } |
| 208 | +} |
0 commit comments