-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makes CASSANDRA_ENSURE_SCHEMA work when schema is absent (#1128)
Our cassandra docker image comes with the schema installed, which hid a bug where we failed to properly guard state 0 (keyspace didn't exist). This fixes the bug and also backfills known upgrade scenarios. Tested on Cassandra 2.1 and 2.2. Thanks to @gena01 for reporting
- Loading branch information
1 parent
f9c2e90
commit 0608e90
Showing
2 changed files
with
100 additions
and
6 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
94 changes: 94 additions & 0 deletions
94
zipkin-storage/cassandra/src/test/java/zipkin/storage/cassandra/EnsureSchemaTest.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,94 @@ | ||
/** | ||
* Copyright 2015-2016 The OpenZipkin Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package zipkin.storage.cassandra; | ||
|
||
import com.datastax.driver.core.Cluster; | ||
import com.datastax.driver.core.KeyspaceMetadata; | ||
import com.datastax.driver.core.Session; | ||
import com.google.common.io.Closer; | ||
import java.io.IOException; | ||
import org.junit.After; | ||
import org.junit.AssumptionViolatedException; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TestName; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class EnsureSchemaTest { | ||
|
||
@Rule | ||
public TestName name = new TestName(); | ||
|
||
@BeforeClass public static void checkCassandraIsUp() { | ||
try (Cluster cluster = new SessionFactory.Default().buildCluster( | ||
CassandraStorage.builder().build()); | ||
Session session = cluster.newSession()) { | ||
session.execute("SELECT now() FROM system.local"); | ||
} catch (RuntimeException e) { | ||
throw new AssumptionViolatedException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
Closer closer = Closer.create(); | ||
String keyspace; | ||
Cluster cluster; | ||
Session session; | ||
|
||
@Before | ||
public void connectAndDropKeyspace() { | ||
keyspace = name.getMethodName().toLowerCase(); | ||
cluster = closer.register(new SessionFactory.Default() | ||
.buildCluster(CassandraStorage.builder().keyspace(keyspace).build())); | ||
session = closer.register(cluster.newSession()); | ||
session.execute("DROP KEYSPACE IF EXISTS " + keyspace); | ||
assertThat(session.getCluster().getMetadata().getKeyspace(keyspace)).isNull(); | ||
} | ||
|
||
@After | ||
public void close() throws IOException { | ||
closer.close(); | ||
} | ||
|
||
@Test public void installsKeyspaceWhenMissing() { | ||
Schema.ensureExists(keyspace, session); | ||
|
||
KeyspaceMetadata metadata = session.getCluster().getMetadata().getKeyspace(keyspace); | ||
assertThat(metadata).isNotNull(); | ||
assertThat(Schema.hasUpgrade1_defaultTtl(metadata)).isTrue(); | ||
} | ||
|
||
@Test public void installsTablesWhenMissing() { | ||
session.execute("CREATE KEYSPACE " + keyspace | ||
+ " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};"); | ||
|
||
Schema.ensureExists(keyspace, session); | ||
|
||
KeyspaceMetadata metadata = session.getCluster().getMetadata().getKeyspace(keyspace); | ||
assertThat(metadata).isNotNull(); | ||
assertThat(Schema.hasUpgrade1_defaultTtl(metadata)).isTrue(); | ||
} | ||
|
||
@Test public void upgradesOldSchema() { | ||
Schema.applyCqlFile(keyspace, session, "/cassandra-schema-cql3-original.txt"); | ||
|
||
Schema.ensureExists(keyspace, session); | ||
|
||
KeyspaceMetadata metadata = session.getCluster().getMetadata().getKeyspace(keyspace); | ||
assertThat(metadata).isNotNull(); | ||
assertThat(Schema.hasUpgrade1_defaultTtl(metadata)).isTrue(); | ||
} | ||
} |