Search in sources :

Example 1 with Ref

use of org.apache.cassandra.utils.concurrent.Ref in project cassandra by apache.

the class StreamTransferTaskTest method testFailSessionDuringTransferShouldNotReleaseReferences.

@Test
public void testFailSessionDuringTransferShouldNotReleaseReferences() throws Exception {
    InetAddress peer = FBUtilities.getBroadcastAddress();
    StreamCoordinator streamCoordinator = new StreamCoordinator(1, true, false, null, false, null);
    StreamResultFuture future = StreamResultFuture.init(UUID.randomUUID(), "", Collections.<StreamEventHandler>emptyList(), streamCoordinator);
    StreamSession session = new StreamSession(peer, peer, null, 0, true, false, null);
    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.addTransferFile(ref, 1, sstable.getPositionsForRanges(ranges), 0);
    }
    assertEquals(2, 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<OutgoingFileMessage> files = new LinkedList<>(task.files.values());
    //simulate start transfer
    for (OutgoingFileMessage file : files) {
        file.startTransfer();
    }
    //fail stream session mid-transfer
    session.onError(new Exception("Fake exception"));
    //make sure reference was not released
    for (Ref<SSTableReader> ref : refs) {
        assertEquals(1, ref.globalCount());
    }
    //simulate finish transfer
    for (OutgoingFileMessage file : files) {
        file.finishTransfer();
    }
    //now reference should be released
    for (Ref<SSTableReader> ref : refs) {
        assertEquals(0, ref.globalCount());
    }
}
Also used : 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) OutgoingFileMessage(org.apache.cassandra.streaming.messages.OutgoingFileMessage) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 CancellationException (java.util.concurrent.CancellationException)1 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)1 Range (org.apache.cassandra.dht.Range)1 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)1 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)1 OutgoingFileMessage (org.apache.cassandra.streaming.messages.OutgoingFileMessage)1 Ref (org.apache.cassandra.utils.concurrent.Ref)1 Test (org.junit.Test)1