github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/fs/hrw.go (about)

     1  // Package fs provides mountpath and FQN abstractions and methods to resolve/map stored content
     2  /*
     3   * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package fs
     6  
     7  import (
     8  	"github.com/NVIDIA/aistore/cmn"
     9  	"github.com/NVIDIA/aistore/cmn/cos"
    10  	"github.com/NVIDIA/aistore/cmn/xoshiro256"
    11  	"github.com/OneOfOne/xxhash"
    12  )
    13  
    14  // A variant of consistent hash based on rendezvous algorithm by Thaler and Ravishankar,
    15  // aka highest random weight (HRW)
    16  // See also: core/meta/hrw.go
    17  
    18  func Hrw(uname string) (mi *Mountpath, digest uint64, err error) {
    19  	var (
    20  		maxH  uint64
    21  		avail = GetAvail()
    22  	)
    23  	digest = xxhash.Checksum64S(cos.UnsafeB(uname), cos.MLCG32)
    24  	for _, mpathInfo := range avail {
    25  		if mpathInfo.IsAnySet(FlagWaitingDD) {
    26  			continue
    27  		}
    28  		cs := xoshiro256.Hash(mpathInfo.PathDigest ^ digest)
    29  		if cs >= maxH {
    30  			maxH = cs
    31  			mi = mpathInfo
    32  		}
    33  	}
    34  	if mi == nil {
    35  		err = cmn.ErrNoMountpaths
    36  	}
    37  	return
    38  }