|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.distributed.test.fql; |
| 20 | + |
| 21 | +import com.google.common.collect.Sets; |
| 22 | +import org.junit.AfterClass; |
| 23 | +import org.junit.BeforeClass; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.rules.TemporaryFolder; |
| 27 | + |
| 28 | +import com.datastax.driver.core.BatchStatement; |
| 29 | +import com.datastax.driver.core.PreparedStatement; |
| 30 | +import com.datastax.driver.core.Session; |
| 31 | +import org.apache.cassandra.distributed.Cluster; |
| 32 | +import org.apache.cassandra.distributed.test.TestBaseImpl; |
| 33 | +import org.apache.cassandra.tools.ToolRunner; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | +import static org.junit.Assert.assertTrue; |
| 37 | +import static org.apache.cassandra.distributed.api.Feature.GOSSIP; |
| 38 | +import static org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL; |
| 39 | +import static org.apache.cassandra.distributed.api.Feature.NETWORK; |
| 40 | +import static org.apache.cassandra.distributed.shared.AssertUtils.assertRows; |
| 41 | +import static org.apache.cassandra.distributed.shared.AssertUtils.row; |
| 42 | + |
| 43 | +public class FqlTombstoneHandlingTest extends TestBaseImpl |
| 44 | +{ |
| 45 | + private static Cluster CLUSTER; |
| 46 | + |
| 47 | + @Rule |
| 48 | + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 49 | + |
| 50 | + @BeforeClass |
| 51 | + public static void beforeClass() throws Throwable |
| 52 | + { |
| 53 | + CLUSTER = init(Cluster.build(1).withConfig(updater -> updater.with(NETWORK, GOSSIP, NATIVE_PROTOCOL)).start()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testNullCellBindingInBatch() |
| 58 | + { |
| 59 | + String tableName = "null_as_tombstone_in_batch"; |
| 60 | + CLUSTER.schemaChange(withKeyspace("CREATE TABLE %s." + tableName + " (k int, c int, s set<int>, primary key (k, c))")); |
| 61 | + CLUSTER.get(1).nodetool("enablefullquerylog", "--path", temporaryFolder.getRoot().getAbsolutePath()); |
| 62 | + String insertTemplate = withKeyspace("INSERT INTO %s." + tableName + " (k, c, s) VALUES ( ?, ?, ?) USING TIMESTAMP 2"); |
| 63 | + String select = withKeyspace("SELECT * FROM %s." + tableName + " WHERE k = 0 AND c = 0"); |
| 64 | + |
| 65 | + com.datastax.driver.core.Cluster.Builder builder1 =com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1"); |
| 66 | + |
| 67 | + // Use the driver to write this initial row, since otherwise we won't hit the dispatcher |
| 68 | + try (com.datastax.driver.core.Cluster cluster1 = builder1.build(); Session session1 = cluster1.connect()) |
| 69 | + { |
| 70 | + BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED); |
| 71 | + PreparedStatement preparedWrite = session1.prepare(insertTemplate); |
| 72 | + batch.add(preparedWrite.bind(0, 0, null)); |
| 73 | + session1.execute(batch); |
| 74 | + } |
| 75 | + |
| 76 | + CLUSTER.get(1).nodetool("disablefullquerylog"); |
| 77 | + |
| 78 | + // The dump should contain a null entry for our tombstone |
| 79 | + ToolRunner.ToolResult runner = ToolRunner.invokeClass("org.apache.cassandra.fqltool.FullQueryLogTool", |
| 80 | + "dump", |
| 81 | + "--", |
| 82 | + temporaryFolder.getRoot().getAbsolutePath()); |
| 83 | + assertTrue(runner.getStdout().contains(insertTemplate)); |
| 84 | + assertEquals(0, runner.getExitCode()); |
| 85 | + |
| 86 | + Object[][] preReplayResult = CLUSTER.get(1).executeInternal(select); |
| 87 | + assertRows(preReplayResult, row(0, 0, null)); |
| 88 | + |
| 89 | + // Make sure the row no longer exists after truncate... |
| 90 | + CLUSTER.get(1).executeInternal(withKeyspace("TRUNCATE %s." + tableName)); |
| 91 | + assertRows(CLUSTER.get(1).executeInternal(select)); |
| 92 | + |
| 93 | + // ...insert a new row with an actual value for the set at an earlier timestamp... |
| 94 | + CLUSTER.get(1).executeInternal(withKeyspace("INSERT INTO %s." + tableName + " (k, c, s) VALUES ( ?, ?, ?) USING TIMESTAMP 1"), 0, 0, Sets.newHashSet(1)); |
| 95 | + assertRows(CLUSTER.get(1).executeInternal(select), row(0, 0, Sets.newHashSet(1))); |
| 96 | + |
| 97 | + runner = ToolRunner.invokeClass("org.apache.cassandra.fqltool.FullQueryLogTool", |
| 98 | + "replay", |
| 99 | + "--keyspace", KEYSPACE, |
| 100 | + "--target", "127.0.0.1", |
| 101 | + "--", temporaryFolder.getRoot().getAbsolutePath()); |
| 102 | + assertEquals(0, runner.getExitCode()); |
| 103 | + |
| 104 | + // ...then ensure the replayed row deletes the one we wrote before replay. |
| 105 | + Object[][] postReplayResult = CLUSTER.get(1).executeInternal(select); |
| 106 | + assertRows(postReplayResult, preReplayResult); |
| 107 | + } |
| 108 | + |
| 109 | + @AfterClass |
| 110 | + public static void afterClass() |
| 111 | + { |
| 112 | + if (CLUSTER != null) |
| 113 | + CLUSTER.close(); |
| 114 | + } |
| 115 | +} |
0 commit comments