gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/uploadheapworsthealth.go (about)

     1  package renter
     2  
     3  import "gitlab.com/SkynetLabs/skyd/skymodules"
     4  
     5  type (
     6  	// worstIgnoredHealth is a helper struct for the callBuildAndPushChunks
     7  	// function. The struct and methods on the data are split out to improve
     8  	// ease of testing.
     9  	worstIgnoredHealth struct {
    10  		health float64
    11  		remote bool
    12  
    13  		nextDirHealth float64
    14  		nextDirRemote bool
    15  
    16  		target repairTarget
    17  	}
    18  )
    19  
    20  // updateWorstIgnoredHealth takes the health of a chunk that is being skipped
    21  // and updates the worst known health to account for this chunk.
    22  func (wh *worstIgnoredHealth) updateWorstIgnoredHealth(newHealth float64, newHealthRemote bool) {
    23  	// The new health is not worse if it does not need to be repaired.
    24  	if !skymodules.NeedsRepair(newHealth) {
    25  		return
    26  	}
    27  	// The new health is not worse if it is not remote, but the worst health is
    28  	// remote.
    29  	if !newHealthRemote && wh.remote {
    30  		return
    31  	}
    32  	// The new health is worse if it is remote and the current health is not
    33  	// remote.
    34  	if newHealthRemote && !wh.remote {
    35  		wh.health = newHealth
    36  		wh.remote = newHealthRemote
    37  		return
    38  	}
    39  	// The remote values match for the new health and the current health. Update
    40  	// the current health only if the new health is worse.
    41  	if wh.health < newHealth {
    42  		wh.health = newHealth
    43  		return
    44  	}
    45  }
    46  
    47  // canSkip contains the logic for determining whether a chunk can be skipped
    48  // based on the worst health of any chunk so far skipped, and also based on the
    49  // worst health of any chunk in the next directory in the directory heap.
    50  //
    51  // We want to make sure that the upload heap has all of the absolute worst
    52  // chunks in the renter in it, so we want to skip any chunk that we know is in
    53  // better health than any chunk which is being ignored because its in another
    54  // directory.
    55  func (wh *worstIgnoredHealth) canSkip(chunkHealth float64, chunkRemote bool) bool {
    56  	// Can skip any chunk that does not need to be repaired.
    57  	if !skymodules.NeedsRepair(chunkHealth) {
    58  		return true
    59  	}
    60  	// Cannot skip any chunks if we are not targeting unstuck chunks. Assuming
    61  	// they are above the repair threshold.
    62  	if wh.target != targetUnstuckChunks {
    63  		return false
    64  	}
    65  
    66  	// If this chunk is not remote and there are skipped chunks that are
    67  	// remote, this chunk can be skipped.
    68  	if !chunkRemote && (wh.remote || wh.nextDirRemote) {
    69  		return true
    70  	}
    71  	// If this chunk is remote and nothing that has been skipped is remote,
    72  	// this chunk cannot be skipped.
    73  	if chunkRemote && !wh.remote && !wh.nextDirRemote {
    74  		return false
    75  	}
    76  	// If the chunk is not remote, neither are either of the other values (those
    77  	// possibilities checked above).
    78  	//
    79  	// This chunk can be skipped only if its health is better than those
    80  	// other chunks.
    81  	if !chunkRemote && (chunkHealth < wh.nextDirHealth || chunkHealth < wh.health) {
    82  		return true
    83  	} else if !chunkRemote {
    84  		return false
    85  	}
    86  
    87  	// By elimination, the chunk is remote, and at least one of the other values
    88  	// (maybe both) is remote. Grab the worst health of the two, and compare the
    89  	// chunk health to that.
    90  	var reqHealth float64
    91  	if wh.nextDirRemote {
    92  		reqHealth = wh.nextDirHealth
    93  	}
    94  	if wh.remote && reqHealth < wh.health {
    95  		reqHealth = wh.health
    96  	}
    97  	return chunkHealth < reqHealth
    98  }