protected void cancelTransfers ( IntSet removedSegments ) { synchronized ( transferMapsLock ) { List < Integer > segmentsToCancel = new ArrayList <> ( removedSegments ) ; while ( ! segmentsToCancel . isEmpty ( ) ) { int segmentId = segmentsToCancel . remove ( 0 ) ; List < InboundTransferTask > inboundTransfers = transfersBySegment . get ( segmentId ) ; if ( inboundTransfers != null ) { // we need to check the transfer was not already completed for ( InboundTransferTask inboundTransfer : inboundTransfers ) { IntSet cancelledSegments = IntSets . mutableCopyFrom ( removedSegments ) ; cancelledSegments . retainAll ( inboundTransfer . getSegments ( ) ) ; segmentsToCancel . removeAll ( cancelledSegments ) ; transfersBySegment . keySet ( ) . removeAll ( cancelledSegments ) ; //this will also remove it from transfersBySource if the entire task gets cancelled inboundTransfer . cancelSegments ( cancelledSegments ) ; if ( inboundTransfer . isCancelled ( ) ) { removeTransfer ( inboundTransfer ) ; } } } } } }
Cancel transfers for segments we no longer own .