Class PiecePickerImpl

  • All Implemented Interfaces:
    PiecePicker

    public class PiecePickerImpl
    extends java.lang.Object
    implements PiecePicker
    • Field Detail

      • LOGID

        private static final LogIDs LOGID
      • TIME_MIN_AVAILABILITY

        private static final long TIME_MIN_AVAILABILITY
        min ms for recalculating availability - reducing this has serious ramifications
        See Also:
        Constant Field Values
      • TIME_MIN_FILE_AVAILABILITY

        private static final long TIME_MIN_FILE_AVAILABILITY
        See Also:
        Constant Field Values
      • TIME_MIN_PRIORITIES

        private static final long TIME_MIN_PRIORITIES
        min ms for recalculating base priorities
        See Also:
        Constant Field Values
      • TIME_AVAIL_REBUILD

        private static final long TIME_AVAIL_REBUILD
        min ms for forced availability rebuild
        See Also:
        Constant Field Values
      • PRIORITY_W_FIRSTLAST

        private static final int PRIORITY_W_FIRSTLAST
        user select prioritize first/last
        See Also:
        Constant Field Values
      • FIRST_PIECE_MIN_NB

        private static final long FIRST_PIECE_MIN_NB
        min # pieces in file for first/last prioritization
        See Also:
        Constant Field Values
      • PRIORITY_W_FILE_BASE

        private static final int PRIORITY_W_FILE_BASE
        user sets file as "High"
        See Also:
        Constant Field Values
      • PRIORITY_W_COMPLETION

        private static final int PRIORITY_W_COMPLETION
        Additional boost for more completed High priority
        See Also:
        Constant Field Values
      • PRIORITY_W_AGE

        private static final int PRIORITY_W_AGE
        priority boost due to being too old
        See Also:
        Constant Field Values
      • PRIORITY_DW_AGE

        private static final int PRIORITY_DW_AGE
        ms a block is expected to complete in
        See Also:
        Constant Field Values
      • PRIORITY_DW_STALE

        private static final int PRIORITY_DW_STALE
        ms since last write
        See Also:
        Constant Field Values
      • PRIORITY_W_PIECE_DONE

        private static final int PRIORITY_W_PIECE_DONE
        finish pieces already almost done
        See Also:
        Constant Field Values
      • PRIORITY_W_SAME_PIECE

        private static final int PRIORITY_W_SAME_PIECE
        keep working on same piece
        See Also:
        Constant Field Values
      • PRIORITY_OVERRIDES_RAREST

        private static final int PRIORITY_OVERRIDES_RAREST
        currently webseeds + other explicit priorities are around 10000 or more - at this point we ignore rarity
        See Also:
        Constant Field Values
      • PRIORITY_SEQUENTIAL_START

        private static final int PRIORITY_SEQUENTIAL_START
        See Also:
        Constant Field Values
      • PRIORITY_REALTIME

        private static final int PRIORITY_REALTIME
        priority at and above which pieces require real-time scheduling
        See Also:
        Constant Field Values
      • REQUESTS_MIN_MIN

        private static final int REQUESTS_MIN_MIN
        Min number of requests sent to a peer
        See Also:
        Constant Field Values
      • REQUESTS_MAX

        private static final int REQUESTS_MAX
        Max number of request sent to a peer
        See Also:
        Constant Field Values
      • SLOPE_REQUESTS

        private static final int SLOPE_REQUESTS
        Default number of requests sent to a peer, (for each X B/s another request will be used)
        See Also:
        Constant Field Values
      • RTA_END_GAME_MODE_SIZE_TRIGGER

        private static final long RTA_END_GAME_MODE_SIZE_TRIGGER
        See Also:
        Constant Field Values
      • END_GAME_MODE_RESERVED_TRIGGER

        private static final long END_GAME_MODE_RESERVED_TRIGGER
        See Also:
        Constant Field Values
      • END_GAME_MODE_SIZE_TRIGGER

        private static final long END_GAME_MODE_SIZE_TRIGGER
        See Also:
        Constant Field Values
      • RTA_END_GAME_MODE_SIZE_TRIGGER_BLOCKS

        private static final long RTA_END_GAME_MODE_SIZE_TRIGGER_BLOCKS
        See Also:
        Constant Field Values
      • END_GAME_MODE_RESERVED_TRIGGER_BLOCKS

        private static final long END_GAME_MODE_RESERVED_TRIGGER_BLOCKS
        See Also:
        Constant Field Values
      • END_GAME_MODE_SIZE_TRIGGER_BLOCKS

        private static final long END_GAME_MODE_SIZE_TRIGGER_BLOCKS
        See Also:
        Constant Field Values
      • NO_REQUEST_BACKOFF_MAX_MILLIS

        private static final int NO_REQUEST_BACKOFF_MAX_MILLIS
        See Also:
        Constant Field Values
      • NO_REQUEST_BACKOFF_MAX_LOOPS

        private static final int NO_REQUEST_BACKOFF_MAX_LOOPS
      • random

        static final java.util.Random random
      • peerListeners

        protected final java.util.Map peerListeners
      • nbPieces

        protected final int nbPieces
      • pePieces

        protected final PEPiece[] pePieces
      • rarestStartedPieces

        private final java.util.List<PEPiece> rarestStartedPieces
      • availabilityMon

        protected final AEMonitor availabilityMon
      • endGameModeChunkLock

        private final java.lang.Object endGameModeChunkLock
      • nbPiecesDone

        protected volatile int nbPiecesDone
      • availabilityAsynch

        protected volatile int[] availabilityAsynch
        asyncronously updated availability
      • availabilityDrift

        protected volatile long availabilityDrift
        indicates availability needs to be recomputed due to detected drift
      • timeAvailRebuild

        private long timeAvailRebuild
      • availability

        protected volatile int[] availability
        periodically updated consistent view of availability for calculating
      • time_last_avail

        private long time_last_avail
      • availabilityChange

        protected volatile long availabilityChange
      • availabilityComputeChange

        private volatile long availabilityComputeChange
      • time_last_rebuild

        private long time_last_rebuild
      • timeAvailLessThanOne

        private long timeAvailLessThanOne
      • globalAvail

        private float globalAvail
      • globalAvgAvail

        private float globalAvgAvail
      • nbRarestActive

        private int nbRarestActive
      • globalMin

        private int globalMin
      • globalMax

        private int globalMax
      • bytesUnavailable

        private long bytesUnavailable
      • globalMinOthers

        private volatile int globalMinOthers
        The rarest availability level of pieces that we affirmatively want to try to request from others soonest ie; our prime targets for requesting rarest pieces
      • filePriorityChange

        protected volatile long filePriorityChange
        event # of user file priority settings changes
      • sequentialDownload

        protected volatile int sequentialDownload
      • priorityParamChange

        private volatile long priorityParamChange
        last user parameter settings event # when priority bases were calculated
      • priorityFileChange

        private volatile long priorityFileChange
        last user priority event # when priority bases were calculated
      • priorityAvailChange

        private volatile long priorityAvailChange
        last availability event # when priority bases were calculated
      • priorityRTAexists

        private boolean priorityRTAexists
      • timeLastPriorities

        private long timeLastPriorities
        time that base priorities were last computed
      • startPriorities

        private int[] startPriorities
        the priority for starting each piece/base priority for resuming
      • hasNeededUndonePiece

        protected volatile boolean hasNeededUndonePiece
      • neededUndonePieceChange

        protected volatile long neededUndonePieceChange
      • endGameMode

        private volatile boolean endGameMode
        A flag to indicate when we're in endgame mode
      • endGameModeAbandoned

        private volatile boolean endGameModeAbandoned
      • timeEndGameModeEntered

        private volatile long timeEndGameModeEntered
      • endGameModeChunks

        private java.util.LinkedList<EndGameModeChunk> endGameModeChunks
        The list of chunks needing to be downloaded (the mechanism change when entering end-game mode)
      • endGameModeChunkMap

        private java.util.Map<java.lang.Long,​EndGameModeChunk> endGameModeChunkMap
      • lastProviderRecalcTime

        private long lastProviderRecalcTime
      • provider_piece_rtas

        private long[] provider_piece_rtas
      • provider_piece_priorities

        private long[] provider_piece_priorities
      • allocate_request_loop_count

        private int allocate_request_loop_count
      • max_file_priority

        private int max_file_priority
      • min_file_priority

        private int min_file_priority
      • reverse_block_order

        private boolean reverse_block_order
      • global_request_hint

        private int[] global_request_hint
      • enable_request_hints

        private static boolean enable_request_hints
      • includeLanPeersInReqLimiting

        private static boolean includeLanPeersInReqLimiting
      • fileAvailabilities

        private volatile float[] fileAvailabilities
      • fileAvailabilitiesCalcTime

        private volatile long fileAvailabilitiesCalcTime
      • forced_pieces

        private volatile CopyOnWriteSet<java.lang.Integer> forced_pieces
      • firstPiecePriority

        protected static volatile boolean firstPiecePriority
      • firstPiecePriorityForce

        protected static volatile boolean firstPiecePriorityForce
      • completionPriority

        protected static volatile boolean completionPriority
      • paramPriorityChange

        protected static volatile long paramPriorityChange
        event # of user settings controlling priority changes
    • Constructor Detail

    • Method Detail

      • updateAvailability

        public final void updateAvailability()
        This methd will compute the pieces' overall availability (including ourself) and the _globalMinOthers & _globalAvail
        Specified by:
        updateAvailability in interface PiecePicker
      • recomputeAvailability

        private int[] recomputeAvailability()
      • getAvailability

        public final int getAvailability​(int pieceNumber)
        Specified by:
        getAvailability in interface PiecePicker
      • checkDownloadablePiece

        protected final void checkDownloadablePiece()
        Early-outs when finds a downloadable piece Either way sets hasNeededUndonePiece and neededUndonePieceChange if necessary
      • allocateRequests

        public final void allocateRequests()
        one reason requests don't stem from the individual peers is so the connections can be sorted by best uploaders, providing some ooprtunity to download the most important (ie; rarest and/or highest priority) pieces faster and more reliably
        Specified by:
        allocateRequests in interface PiecePicker
      • getNextBlockETAFromNow

        protected int getNextBlockETAFromNow​(PEPeerTransport pt)
      • calcRarestAllowed

        private int calcRarestAllowed()
        Count current global min avail pieces in progress (not counting non-rarest pieces but keep them to compensate high churn, remove completed ones, ignore idle ones)
        Returns:
        number of pieces that may be started due to the "rarest first" picking rule
      • syncFilePriorities

        private void syncFilePriorities()
      • computeBasePriorities

        private void computeBasePriorities()
        This computes the base priority for all pieces that need requesting if there's been any availability change or user priority setting changes since the last call, which will be most of the time since availability changes so dynamicaly It will change startPriorities[] (unless there was nothing to do)
      • findPieceToDownload

        protected final int findPieceToDownload​(PEPeerTransport pt,
                                                int nbWanted)
        Parameters:
        pt - the PEPeerTransport we're working on
        Returns:
        int # of blocks that were requested (0 if no requests were made)
      • findRTAPieceToDownload

        protected final boolean findRTAPieceToDownload​(PEPeerTransport pt,
                                                       boolean best_uploader,
                                                       long best_uploader_next_block_eta)
      • getRequestCandidate

        private int getRequestCandidate​(PEPeerTransport pt)
        This method is the downloading core. It decides, for a given peer, which block should be requested. Here is the overall algorithm : 0. If there a FORCED_PIECE or reserved piece, that will be started/resumed if possible 1. Scan all the active pieces and find the rarest piece (and highest priority among equally rarest) that can possibly be continued by this peer, if any 2. While scanning the active pieces, develop a list of equally highest priority pieces (and equally rarest among those) as candidates for starting a new piece 3. If it can't find any piece, this means all pieces are already downloaded/full requested 4. Returns int[] pieceNumber, blockNumber if a request to be made is found, or null if none could be found
        Parameters:
        pc - PEPeerTransport to work with
        Returns:
        int with pieceNumberto be requested or -1 if no request could be found
      • getPieceToStart

        protected final int getPieceToStart​(BitFlags startCandidates)
        Parameters:
        startCandidates - BitFlags of potential candidates to choose from
        Returns:
        int the piece number that was chosen to be started. Note it's possible for the chosen piece to have been started already (by another thread). This method considers that potential to not be relevant.
      • getNeededUndonePieceChange

        public final long getNeededUndonePieceChange()
        Specified by:
        getNeededUndonePieceChange in interface PiecePicker
        Returns:
        long value indicated serial number of current count of changes to hasNeededUndonePiece. A method interesting in tracking changes can compare this with a locally stored value to determine if the hasNeededUndonePiece status has changed since the last check.
      • checkEndGameMode

        private void checkEndGameMode()
      • computeEndGameModeChunks

        private void computeEndGameModeChunks()
      • addEndGameChunks

        public final void addEndGameChunks​(PEPiece pePiece)
        adds every block from the piece to the list of chuncks to be selected for egm requesting
        Specified by:
        addEndGameChunks in interface PiecePicker
      • findPieceInEndGameMode

        protected final int findPieceInEndGameMode​(PEPeerTransport pt,
                                                   int wants)
      • leaveEndGameMode

        protected void leaveEndGameMode()
      • abandonEndGameMode

        protected void abandonEndGameMode()
      • computeProviderPriorities

        private boolean computeProviderPriorities()
      • setForcePiece

        public void setForcePiece​(int pieceNumber,
                                  boolean forced)
        Specified by:
        setForcePiece in interface PiecePicker
      • isForcePiece

        public boolean isForcePiece​(int pieceNumber)
        Specified by:
        isForcePiece in interface PiecePicker
      • setSequentialDownload

        private void setSequentialDownload​(int val)
      • getSequentialInfo

        public int getSequentialInfo()
        Specified by:
        getSequentialInfo in interface PiecePicker
        Returns:
        0 - inactive; +ve -> ascending from (n-1) -ve -> descending from (-n+1)
      • getEGMRequestCount

        public int getEGMRequestCount​(int piece_number,
                                      int block_number)
        Specified by:
        getEGMRequestCount in interface PiecePicker
      • setGlobalRequestHint

        public void setGlobalRequestHint​(int piece_number,
                                         int start_bytes,
                                         int byte_count)
        Specified by:
        setGlobalRequestHint in interface PiecePicker
      • getPieceString

        public java.lang.String getPieceString​(int piece_number)
        Specified by:
        getPieceString in interface PiecePicker