kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | dce212d79db50127ed3b16d0d0a1da73af0f5d45966273e2587c214b91d79a19 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-11-19 16:26 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-11-20 12:04 +01:00 |
| Parents | d13c7db1c8e7 |
6 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | ------ |
| 6 | 6 | |
| 7 | 7 | - [improvement] Shade Netty dependency (JAVA-538) |
| 8 | +- [improvement] Target schema refreshes more precisely (JAVA-543) | |
| 8 | 9 | |
| 9 | 10 | |
| 10 | 11 | 2.0.8: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +12 −10
| @@ -26,11 +26,7 @@ | ||
| 26 | 26 | |
| 27 | 27 | import com.google.common.annotations.VisibleForTesting; |
| 28 | 28 | import com.google.common.base.Predicates; |
| 29 | -import com.google.common.collect.HashMultimap; | |
| 30 | -import com.google.common.collect.Iterables; | |
| 31 | -import com.google.common.collect.MapMaker; | |
| 32 | -import com.google.common.collect.SetMultimap; | |
| 33 | -import com.google.common.collect.Sets; | |
| 29 | +import com.google.common.collect.*; | |
| 34 | 30 | import com.google.common.util.concurrent.*; |
| 35 | 31 | |
| 36 | 32 | import org.slf4j.Logger; |
| @@ -2068,15 +2064,21 @@ | ||
| 2068 | 2064 | switch (scc.change) { |
| 2069 | 2065 | case CREATED: |
| 2070 | 2066 | if (scc.table.isEmpty()) |
| 2071 | - submitSchemaRefresh(null, null); | |
| 2072 | - else | |
| 2073 | 2067 | submitSchemaRefresh(scc.keyspace, null); |
| 2068 | + else | |
| 2069 | + submitSchemaRefresh(scc.keyspace, scc.table); | |
| 2074 | 2070 | break; |
| 2075 | 2071 | case DROPPED: |
| 2076 | 2072 | if (scc.table.isEmpty()) |
| 2077 | - submitSchemaRefresh(null, null); | |
| 2078 | - else | |
| 2079 | - submitSchemaRefresh(scc.keyspace, null); | |
| 2073 | + manager.metadata.removeKeyspace(scc.keyspace); | |
| 2074 | + else { | |
| 2075 | + KeyspaceMetadata keyspace = manager.metadata.getKeyspace(scc.keyspace); | |
| 2076 | + if (keyspace == null) | |
| 2077 | + logger.warn("Received a DROPPED notification for {}.{}, but this keyspace is unknown in our metadata", | |
| 2078 | + scc.keyspace, scc.table); | |
| 2079 | + else | |
| 2080 | + keyspace.removeTable(scc.table); | |
| 2081 | + } | |
| 2080 | 2082 | break; |
| 2081 | 2083 | case UPDATED: |
| 2082 | 2084 | if (scc.table.isEmpty()) |
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +10 −4
| @@ -70,9 +70,9 @@ | ||
| 70 | 70 | switch (scc.change) { |
| 71 | 71 | case CREATED: |
| 72 | 72 | if (scc.columnFamily.isEmpty()) { |
| 73 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, null, null); | |
| 74 | - } else { | |
| 75 | 73 | session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, null); |
| 74 | + } else { | |
| 75 | + session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, scc.columnFamily); | |
| 76 | 76 | } |
| 77 | 77 | break; |
| 78 | 78 | case DROPPED: |
| @@ -82,10 +82,16 @@ | ||
| 82 | 82 | // We'll add it back if CASSANDRA-5358 changes that behavior |
| 83 | 83 | //if (scc.keyspace.equals(session.poolsState.keyspace)) |
| 84 | 84 | // session.poolsState.setKeyspace(null); |
| 85 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, null, null); | |
| 85 | + session.cluster.manager.metadata.removeKeyspace(scc.keyspace); | |
| 86 | 86 | } else { |
| 87 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, null); | |
| 87 | + KeyspaceMetadata keyspace = session.cluster.manager.metadata.getKeyspace(scc.keyspace); | |
| 88 | + if (keyspace == null) | |
| 89 | + logger.warn("Received a DROPPED notification for {}.{}, but this keyspace is unknown in our metadata", | |
| 90 | + scc.keyspace, scc.columnFamily); | |
| 91 | + else | |
| 92 | + keyspace.removeTable(scc.columnFamily); | |
| 88 | 93 | } |
| 94 | + this.setResult(rs); | |
| 89 | 95 | break; |
| 90 | 96 | case UPDATED: |
| 91 | 97 | if (scc.columnFamily.isEmpty()) { |
modified driver-core/src/main/java/com/datastax/driver/core/KeyspaceMetadata.java +4 −0
| @@ -97,6 +97,10 @@ | ||
| 97 | 97 | return tables.get(Metadata.handleId(name)); |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | + void removeTable(String table) { | |
| 101 | + tables.remove(table); | |
| 102 | + } | |
| 103 | + | |
| 100 | 104 | /** |
| 101 | 105 | * Returns the tables defined in this keyspace. |
| 102 | 106 | * |
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +5 −0
| @@ -298,6 +298,11 @@ | ||
| 298 | 298 | return keyspaces.get(handleId(keyspace)); |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | + void removeKeyspace(String keyspace) { | |
| 302 | + keyspaces.remove(keyspace); | |
| 303 | + tokenMap.tokenToHosts.remove(keyspace); | |
| 304 | + } | |
| 305 | + | |
| 301 | 306 | /** |
| 302 | 307 | * Returns a list of all the defined keyspaces. |
| 303 | 308 | * |
added driver-core/src/test/java/com/datastax/driver/core/SchemaChangesTest.java +113 −0
| @@ -0,0 +1,113 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import java.util.Collection; | |
| 4 | +import java.util.List; | |
| 5 | + | |
| 6 | +import com.google.common.collect.Lists; | |
| 7 | +import org.testng.annotations.AfterClass; | |
| 8 | +import org.testng.annotations.AfterMethod; | |
| 9 | +import org.testng.annotations.BeforeClass; | |
| 10 | +import org.testng.annotations.Test; | |
| 11 | + | |
| 12 | +import com.datastax.driver.core.utils.Bytes; | |
| 13 | + | |
| 14 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 15 | + | |
| 16 | +public class SchemaChangesTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 17 | + | |
| 18 | + // Create a second cluster to check that other clients also get notified | |
| 19 | + Cluster cluster2; | |
| 20 | + // The metadatas of the two clusters should be kept in sync | |
| 21 | + List<Metadata> metadatas; | |
| 22 | + | |
| 23 | + @Override | |
| 24 | + protected Collection<String> getTableDefinitions() { | |
| 25 | + return Lists.newArrayList(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + @BeforeClass(groups = "short") | |
| 29 | + public void setup() { | |
| 30 | + cluster2 = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build(); | |
| 31 | + metadatas = Lists.newArrayList(cluster.getMetadata(), cluster2.getMetadata()); | |
| 32 | + } | |
| 33 | + | |
| 34 | + @Test(groups = "short") | |
| 35 | + public void should_notify_of_table_creation() { | |
| 36 | + session.execute("CREATE TABLE ks.table1(i int primary key)"); | |
| 37 | + | |
| 38 | + for (Metadata m : metadatas) | |
| 39 | + assertThat(m.getKeyspace("ks").getTable("table1")) | |
| 40 | + .isNotNull(); | |
| 41 | + } | |
| 42 | + | |
| 43 | + @Test(groups = "short") | |
| 44 | + public void should_notify_of_table_update() { | |
| 45 | + session.execute("CREATE TABLE ks.table1(i int primary key)"); | |
| 46 | + session.execute("ALTER TABLE ks.table1 ADD j int"); | |
| 47 | + | |
| 48 | + for (Metadata m : metadatas) | |
| 49 | + assertThat(m.getKeyspace("ks").getTable("table1").getColumn("j")) | |
| 50 | + .isNotNull(); | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Test(groups = "short") | |
| 54 | + public void should_notify_of_table_drop() { | |
| 55 | + session.execute("CREATE TABLE ks.table1(i int primary key)"); | |
| 56 | + session.execute("DROP TABLE ks.table1"); | |
| 57 | + | |
| 58 | + for (Metadata m : metadatas) | |
| 59 | + assertThat(m.getKeyspace("ks").getTable("table1")) | |
| 60 | + .isNull(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Test(groups = "short") | |
| 64 | + public void should_notify_of_keyspace_creation() { | |
| 65 | + session.execute("CREATE KEYSPACE ks2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}"); | |
| 66 | + | |
| 67 | + for (Metadata m : metadatas) | |
| 68 | + assertThat(m.getKeyspace("ks2")) | |
| 69 | + .isNotNull(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + @Test(groups = "short") | |
| 73 | + public void should_notify_of_keyspace_update() { | |
| 74 | + session.execute("CREATE KEYSPACE ks2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}"); | |
| 75 | + for (Metadata m : metadatas) | |
| 76 | + assertThat(m.getKeyspace("ks2").isDurableWrites()) | |
| 77 | + .isTrue(); | |
| 78 | + | |
| 79 | + session.execute("ALTER KEYSPACE ks2 WITH durable_writes = false"); | |
| 80 | + for (Metadata m : metadatas) | |
| 81 | + assertThat(m.getKeyspace("ks2").isDurableWrites()) | |
| 82 | + .isFalse(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + @Test(groups = "short") | |
| 86 | + public void should_notify_of_keyspace_drop() { | |
| 87 | + session.execute("CREATE KEYSPACE ks2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}"); | |
| 88 | + for (Metadata m : metadatas) | |
| 89 | + assertThat(m.getReplicas("ks2", Bytes.fromHexString("0xCAFEBABE"))) | |
| 90 | + .isNotEmpty(); | |
| 91 | + | |
| 92 | + session.execute("DROP KEYSPACE ks2"); | |
| 93 | + | |
| 94 | + for (Metadata m : metadatas) { | |
| 95 | + assertThat(m.getKeyspace("ks2")) | |
| 96 | + .isNull(); | |
| 97 | + assertThat(m.getReplicas("ks2", Bytes.fromHexString("0xCAFEBABE"))) | |
| 98 | + .isEmpty(); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + @AfterMethod(groups = "short") | |
| 103 | + public void cleanup() { | |
| 104 | + session.execute("DROP TABLE IF EXISTS ks.table1"); | |
| 105 | + session.execute("DROP KEYSPACE IF EXISTS ks2"); | |
| 106 | + } | |
| 107 | + | |
| 108 | + @AfterClass(groups = "short") | |
| 109 | + public void teardown() { | |
| 110 | + if (cluster2 != null) | |
| 111 | + cluster2.close(); | |
| 112 | + } | |
| 113 | +} |