Class PiecePickerImpl

java.lang.Object
com.biglybt.core.peermanager.piecepicker.impl.PiecePickerImpl
All Implemented Interfaces:
PiecePicker

public class PiecePickerImpl extends Object implements PiecePicker
  • Field Details

    • LOG_RTA

      private static final boolean LOG_RTA
      See Also:
    • EGM_IS_BLOCK_BASED

      private static final boolean EGM_IS_BLOCK_BASED
      See Also:
    • 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:
    • TIME_MIN_FILE_AVAILABILITY

      private static final long TIME_MIN_FILE_AVAILABILITY
      See Also:
    • TIME_MIN_PRIORITIES

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

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

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

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

      private static final int PRIORITY_W_FILE_BASE
      user sets file as "High"
      See Also:
    • PRIORITY_W_FILE_RANGE

      private static final int PRIORITY_W_FILE_RANGE
      See Also:
    • PRIORITY_W_COMPLETION

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

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

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

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

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

      private static final int PRIORITY_W_SAME_PIECE
      keep working on same piece
      See Also:
    • 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:
    • PRIORITY_REQUEST_HINT

      private static final int PRIORITY_REQUEST_HINT
      See Also:
    • PRIORITY_SEQUENTIAL_START

      private static final int PRIORITY_SEQUENTIAL_START
      See Also:
    • PRIORITY_REALTIME

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

      private static final int PRIORITY_FORCED
      See Also:
    • REQUESTS_MIN_MIN

      private static final int REQUESTS_MIN_MIN
      Min number of requests sent to a peer
      See Also:
    • REQUESTS_MIN_MAX

      private static final int REQUESTS_MIN_MAX
      See Also:
    • REQUESTS_MAX

      private static final int REQUESTS_MAX
      Max number of request sent to a peer
      See Also:
    • 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:
    • RTA_END_GAME_MODE_SIZE_TRIGGER

      private static final long RTA_END_GAME_MODE_SIZE_TRIGGER
      See Also:
    • END_GAME_MODE_RESERVED_TRIGGER

      private static final long END_GAME_MODE_RESERVED_TRIGGER
      See Also:
    • END_GAME_MODE_SIZE_TRIGGER

      private static final long END_GAME_MODE_SIZE_TRIGGER
      See Also:
    • RTA_END_GAME_MODE_SIZE_TRIGGER_BLOCKS

      private static final long RTA_END_GAME_MODE_SIZE_TRIGGER_BLOCKS
      See Also:
    • END_GAME_MODE_RESERVED_TRIGGER_BLOCKS

      private static final long END_GAME_MODE_RESERVED_TRIGGER_BLOCKS
      See Also:
    • END_GAME_MODE_SIZE_TRIGGER_BLOCKS

      private static final long END_GAME_MODE_SIZE_TRIGGER_BLOCKS
      See Also:
    • END_GAME_MODE_TIMEOUT

      private static final long END_GAME_MODE_TIMEOUT
      See Also:
    • NO_REQUEST_BACKOFF_MAX_MILLIS

      private static final int NO_REQUEST_BACKOFF_MAX_MILLIS
      See Also:
    • NO_REQUEST_BACKOFF_MAX_LOOPS

      private static final int NO_REQUEST_BACKOFF_MAX_LOOPS
    • random

      static final Random random
    • diskManager

      private final DiskManager diskManager
    • peerControl

      private final PEPeerControl peerControl
    • diskManagerListener

      private final PiecePickerImpl.DiskManagerListenerImpl diskManagerListener
    • peerListeners

      protected final Map peerListeners
    • peerManagerListener

      private final PEPeerManagerListener peerManagerListener
    • nbPieces

      private final int nbPieces
    • dmPieces

      private final DiskManagerPiece[] dmPieces
    • pePieces

      private final PEPiece[] pePieces
    • pieceSize

      private final int pieceSize
    • rarestStartedPieces

      private final List<PEPiece> rarestStartedPieces
    • availabilityMon

      protected final AEMonitor availabilityMon
    • endGameModeChunkLock

      private final Object endGameModeChunkLock
    • nbPiecesDone

      protected volatile int nbPiecesDone
    • availabilityAsynch

      protected volatile int[] availabilityAsynch
      asynchronously 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 LinkedList<EndGameModeChunk> endGameModeChunks
      The list of chunks needing to be downloaded (the mechanism change when entering end-game mode)
    • endGameModeChunkMap

      private Map<Long,EndGameModeChunk> endGameModeChunkMap
    • lastProviderRecalcTime

      private long lastProviderRecalcTime
    • rta_providers

      private final CopyOnWriteList rta_providers
    • provider_piece_rtas

      private long[] provider_piece_rtas
    • priority_providers

      private final CopyOnWriteList priority_providers
    • 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
    • listeners

      private final CopyOnWriteList<PiecePickerListener> listeners
    • fileAvailabilities

      private volatile float[] fileAvailabilities
    • fileAvailabilitiesCalcTime

      private volatile long fileAvailabilitiesCalcTime
    • forced_pieces

      private volatile CopyOnWriteSet<Integer> forced_pieces
    • firstPiecePriority

      protected static volatile boolean firstPiecePriority
    • firstPriorityBytes

      protected static volatile long firstPriorityBytes
    • 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
    • dispenser

      private final SpeedTokenDispenser dispenser
  • Constructor Details

  • Method Details

    • getPeerManager

      public PEPeerManager getPeerManager()
      Specified by:
      getPeerManager in interface PiecePicker
    • addHavePiece

      public final void addHavePiece(PEPeer peer, int pieceNumber)
      Specified by:
      addHavePiece in interface PiecePicker
    • updateAvailability

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

      private int[] recomputeAvailability()
    • getNumberOfPieces

      public int getNumberOfPieces()
      Specified by:
      getNumberOfPieces in interface PiecePicker
    • getAvailability

      public final int[] getAvailability()
      Specified by:
      getAvailability in interface PiecePicker
    • getAvailability

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

      public final float getMinAvailability()
      Specified by:
      getMinAvailability in interface PiecePicker
    • getMinAvailability

      public float getMinAvailability(int fileIndex)
      Specified by:
      getMinAvailability in interface PiecePicker
    • getBytesUnavailable

      public final long getBytesUnavailable()
      Specified by:
      getBytesUnavailable in interface PiecePicker
    • getAvailWentBadTime

      public final long getAvailWentBadTime()
      Specified by:
      getAvailWentBadTime in interface PiecePicker
    • getMaxAvailability

      public final int getMaxAvailability()
      Specified by:
      getMaxAvailability in interface PiecePicker
    • getAvgAvail

      public final float getAvgAvail()
      Specified by:
      getAvgAvail in interface PiecePicker
    • getNbPiecesDone

      public int getNbPiecesDone()
      Specified by:
      getNbPiecesDone 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 dynamically 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.
    • hasDownloadablePiece

      public final boolean hasDownloadablePiece()
      Specified by:
      hasDownloadablePiece in interface PiecePicker
    • 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()
    • isInEndGameMode

      public final boolean isInEndGameMode()
      Specified by:
      isInEndGameMode in interface PiecePicker
    • hasEndGameModeBeenAbandoned

      public boolean hasEndGameModeBeenAbandoned()
      Specified by:
      hasEndGameModeBeenAbandoned in interface PiecePicker
    • 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)
    • removeFromEndGameModeChunks

      public final void removeFromEndGameModeChunks(int pieceNumber, int offset)
      Specified by:
      removeFromEndGameModeChunks in interface PiecePicker
    • clearEndGameChunks

      public final void clearEndGameChunks()
      Specified by:
      clearEndGameChunks in interface PiecePicker
    • leaveEndGameMode

      protected void leaveEndGameMode()
    • abandonEndGameMode

      protected void abandonEndGameMode()
    • computeProviderPriorities

      private boolean computeProviderPriorities()
    • addRTAProvider

      public void addRTAProvider(PieceRTAProvider provider)
      Specified by:
      addRTAProvider in interface PiecePicker
    • removeRTAProvider

      public void removeRTAProvider(PieceRTAProvider provider)
      Specified by:
      removeRTAProvider in interface PiecePicker
    • getRTAProviders

      public List getRTAProviders()
      Specified by:
      getRTAProviders in interface PiecePicker
    • addPriorityProvider

      public void addPriorityProvider(PiecePriorityProvider provider)
      Specified by:
      addPriorityProvider in interface PiecePicker
    • removePriorityProvider

      public void removePriorityProvider(PiecePriorityProvider provider)
      Specified by:
      removePriorityProvider in interface PiecePicker
    • getPriorityProviders

      public List getPriorityProviders()
      Specified by:
      getPriorityProviders in interface PiecePicker
    • addListener

      public void addListener(PiecePickerListener listener)
      Specified by:
      addListener in interface PiecePicker
    • removeListener

      public void removeListener(PiecePickerListener listener)
      Specified by:
      removeListener in interface PiecePicker
    • 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)
    • setSequentialAscendingFrom

      public void setSequentialAscendingFrom(int start_piece)
      Specified by:
      setSequentialAscendingFrom in interface PiecePicker
    • setSequentialDescendingFrom

      public void setSequentialDescendingFrom(int start_piece)
      Specified by:
      setSequentialDescendingFrom in interface PiecePicker
    • clearSequential

      public void clearSequential()
      Specified by:
      clearSequential in interface PiecePicker
    • getSequentialInfo

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

      public String getEGMInfo()
      Specified by:
      getEGMInfo in interface PiecePicker
    • 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
    • getGlobalRequestHint

      public int[] getGlobalRequestHint()
      Specified by:
      getGlobalRequestHint in interface PiecePicker
    • setReverseBlockOrder

      public void setReverseBlockOrder(boolean is_reverse)
      Specified by:
      setReverseBlockOrder in interface PiecePicker
    • getReverseBlockOrder

      public boolean getReverseBlockOrder()
      Specified by:
      getReverseBlockOrder in interface PiecePicker
    • destroy

      public void destroy()
      Specified by:
      destroy in interface PiecePicker
    • getPieceString

      public String getPieceString(int piece_number)
      Specified by:
      getPieceString in interface PiecePicker
    • generateEvidence

      public void generateEvidence(IndentWriter writer)
      Specified by:
      generateEvidence in interface PiecePicker