maedhroz commented on code in PR #2492: URL: https://github.com/apache/cassandra/pull/2492#discussion_r1265912282
########## test/distributed/org/apache/cassandra/distributed/test/sai/IndexStreamingFailureTest.java: ########## @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.distributed.test.sai; + +import java.io.IOException; +import java.util.function.BiConsumer; + +import net.bytebuddy.ByteBuddy; +import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; +import net.bytebuddy.implementation.MethodDelegation; +import org.junit.Test; + +import org.apache.cassandra.db.Keyspace; +import org.apache.cassandra.distributed.Cluster; +import org.apache.cassandra.distributed.api.IInvokableInstance; +import org.apache.cassandra.distributed.api.NodeToolResult; +import org.apache.cassandra.distributed.test.TestBaseImpl; +import org.apache.cassandra.index.Index; +import org.apache.cassandra.index.SecondaryIndexManager; +import org.apache.cassandra.index.sai.IndexContext; +import org.apache.cassandra.index.sai.disk.format.IndexDescriptor; +import org.apache.cassandra.index.sai.disk.v1.SAICodecUtils; +import org.apache.cassandra.index.sai.disk.v1.segment.SegmentBuilder; +import org.apache.cassandra.index.sai.disk.v1.segment.SegmentMetadata; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.store.IndexInput; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; + +import static org.apache.cassandra.distributed.api.Feature.GOSSIP; +import static org.apache.cassandra.distributed.api.Feature.NETWORK; + +public class IndexStreamingFailureTest extends TestBaseImpl +{ + public static final String TEST_ERROR_MESSAGE = "Injected failure!"; + + @Test + public void testAvailabilityAfterFailedNonEntireFileStreaming() throws Exception + { + testAvailabilityAfterStreaming(ByteBuddyHelper::installFlushError, false); + } + + @Test + public void testAvailabilityAfterFailedEntireFileStreaming() throws Exception + { + testAvailabilityAfterStreaming(ByteBuddyHelper::installValidationError, true); + } + + private void testAvailabilityAfterStreaming(BiConsumer<ClassLoader, Integer> injectedError, boolean streamEntireSSTables) throws Exception + { + try (Cluster cluster = init(Cluster.build(2) + .withConfig(c -> c.with(NETWORK, GOSSIP).set("stream_entire_sstables", streamEntireSSTables)) + .withInstanceInitializer(injectedError) + .start())) + { + cluster.disableAutoCompaction(KEYSPACE); + cluster.schemaChange(withKeyspace("CREATE TABLE %s.test (pk int PRIMARY KEY, v text)")); + cluster.schemaChange(withKeyspace("CREATE CUSTOM INDEX test_v_index ON %s.test(v) USING 'StorageAttachedIndex'")); + + IInvokableInstance first = cluster.get(1); + IInvokableInstance second = cluster.get(2); + + first.executeInternal(withKeyspace("INSERT INTO %s.test(pk, v) VALUES (?, ?)"), 1, "v1"); + first.flush(KEYSPACE); + + Object[][] rs = second.executeInternal(withKeyspace("select pk from %s.test where v = ?"), "v1"); + assertThat(rs.length).isEqualTo(0); + + // The repair job should fail when index completion fails. This should also fail the streaming transaction. + NodeToolResult result = second.nodetoolResult("repair", KEYSPACE); + result.asserts().failure().errorContains(TEST_ERROR_MESSAGE); + + // The SSTable should not be added to the table view, as the streaming transaction failed... + rs = second.executeInternal(withKeyspace("select pk from %s.test where pk = ?"), 1); + assertThat(rs.length).isEqualTo(0); + + // ...and querying the index also returns nothing, as the index for the streamed SSTable was never built. + rs = second.executeInternal(withKeyspace("select pk from %s.test where v = ?"), "v1"); + assertThat(rs.length).isEqualTo(0); + + // On restart, ensure that the index remains querable and does not include the data we attempted to stream. + second.shutdown().get(); + second.startup(); + + // On restart, the base table should be unchanged... + rs = second.executeInternal(withKeyspace("select pk from %s.test where pk = ?"), 1); + assertThat(rs.length).isEqualTo(0); + + // ...and the index should remain queryable, because from its perspective, the streaming never happened. + Boolean isQueryable = second.callOnInstance(() -> { + SecondaryIndexManager sim = Keyspace.open(KEYSPACE).getColumnFamilyStore("test").indexManager; + Index index = sim.getIndexByName("test_v_index"); + return sim.isIndexQueryable(index); + }); + + assertTrue("The index should be queryable on restart!", isQueryable); Review Comment: TODO: Given I'm querying the index immediately after this, the check for queryability is probably redundant. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

