Search in sources :

Example 6 with OutgoingStreamMessage

use of org.apache.cassandra.streaming.messages.OutgoingStreamMessage in project cassandra by apache.

the class StreamTransferTaskTest method testFailSessionDuringTransferShouldNotReleaseReferences.

@Test
public void testFailSessionDuringTransferShouldNotReleaseReferences() throws Exception {
    InetAddressAndPort peer = FBUtilities.getBroadcastAddressAndPort();
    StreamCoordinator streamCoordinator = new StreamCoordinator(StreamOperation.BOOTSTRAP, 1, new NettyStreamingConnectionFactory(), false, false, null, PreviewKind.NONE);
    StreamResultFuture future = StreamResultFuture.createInitiator(UUID.randomUUID(), StreamOperation.OTHER, Collections.<StreamEventHandler>emptyList(), streamCoordinator);
    StreamSession session = new StreamSession(StreamOperation.BOOTSTRAP, peer, FACTORY, null, current_version, false, 0, null, PreviewKind.NONE);
    session.init(future);
    ColumnFamilyStore cfs = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD);
    // create two sstables
    for (int i = 0; i < 2; i++) {
        SchemaLoader.insertData(KEYSPACE1, CF_STANDARD, i, 1);
        cfs.forceBlockingFlush();
    }
    // create streaming task that streams those two sstables
    StreamTransferTask task = new StreamTransferTask(session, cfs.metadata.id);
    List<Ref<SSTableReader>> refs = new ArrayList<>(cfs.getLiveSSTables().size());
    for (SSTableReader sstable : cfs.getLiveSSTables()) {
        List<Range<Token>> ranges = new ArrayList<>();
        ranges.add(new Range<>(sstable.first.getToken(), sstable.last.getToken()));
        Ref<SSTableReader> ref = sstable.selfRef();
        refs.add(ref);
        task.addTransferStream(new CassandraOutgoingFile(StreamOperation.BOOTSTRAP, ref, sstable.getPositionsForRanges(ranges), ranges, 1));
    }
    assertEquals(14, task.getTotalNumberOfFiles());
    // add task to stream session, so it is aborted when stream session fails
    session.transfers.put(TableId.generate(), task);
    // make a copy of outgoing file messages, since task is cleared when it's aborted
    Collection<OutgoingStreamMessage> files = new LinkedList<>(task.streams.values());
    // simulate start transfer
    for (OutgoingStreamMessage file : files) {
        file.startTransfer();
    }
    // fail stream session mid-transfer
    session.onError(new Exception("Fake exception")).get(5, TimeUnit.SECONDS);
    // make sure reference was not released
    for (Ref<SSTableReader> ref : refs) {
        assertEquals(1, ref.globalCount());
    }
    // wait for stream to abort asynchronously
    int tries = 10;
    while (ScheduledExecutors.nonPeriodicTasks.getActiveTaskCount() > 0) {
        if (tries < 1)
            throw new RuntimeException("test did not complete in time");
        Thread.sleep(10);
        tries--;
    }
    // simulate finish transfer
    for (OutgoingStreamMessage file : files) {
        file.finishTransfer();
    }
    // now reference should be released
    for (Ref<SSTableReader> ref : refs) {
        assertEquals(0, ref.globalCount());
    }
}
Also used : CassandraOutgoingFile(org.apache.cassandra.db.streaming.CassandraOutgoingFile) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) ArrayList(java.util.ArrayList) Range(org.apache.cassandra.dht.Range) LinkedList(java.util.LinkedList) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) CancellationException(java.util.concurrent.CancellationException) Ref(org.apache.cassandra.utils.concurrent.Ref) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) NettyStreamingConnectionFactory(org.apache.cassandra.streaming.async.NettyStreamingConnectionFactory) OutgoingStreamMessage(org.apache.cassandra.streaming.messages.OutgoingStreamMessage) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Aggregations

OutgoingStreamMessage (org.apache.cassandra.streaming.messages.OutgoingStreamMessage)6 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 IOException (java.io.IOException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 CancellationException (java.util.concurrent.CancellationException)1 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)1 CassandraOutgoingFile (org.apache.cassandra.db.streaming.CassandraOutgoingFile)1 Range (org.apache.cassandra.dht.Range)1 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)1 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)1 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)1 StreamingDataOutputPlus (org.apache.cassandra.streaming.StreamingDataOutputPlus)1 NettyStreamingConnectionFactory (org.apache.cassandra.streaming.async.NettyStreamingConnectionFactory)1 Ref (org.apache.cassandra.utils.concurrent.Ref)1 UncheckedInterruptedException (org.apache.cassandra.utils.concurrent.UncheckedInterruptedException)1 Test (org.junit.Test)1