github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/xact/xreg/uuid.go (about)

     1  // Package xreg provides registry and (renew, find) functions for AIS eXtended Actions (xactions).
     2  /*
     3   * Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package xreg
     6  
     7  import (
     8  	"time"
     9  
    10  	"github.com/NVIDIA/aistore/cmn/atomic"
    11  	"github.com/NVIDIA/aistore/cmn/cos"
    12  	"github.com/NVIDIA/aistore/core"
    13  	"github.com/OneOfOne/xxhash"
    14  )
    15  
    16  var (
    17  	PrimeTime atomic.Int64
    18  	MyTime    atomic.Int64
    19  )
    20  
    21  // see related: cmn/cos/uuid.go
    22  
    23  // "best-effort ID" - to independently and locally generate globally unique xaction ID
    24  func GenBEID(div uint64, tag string) (beid string, xctn core.Xact, err error) {
    25  	// primary's "now"
    26  	now := uint64(time.Now().UnixNano() - MyTime.Load() + PrimeTime.Load())
    27  
    28  	// compute
    29  	val := now / div
    30  	org := val
    31  	val ^= xxhash.Checksum64S(cos.UnsafeB(tag), val)
    32  	beid = cos.GenBEID(val, cos.LenShortID)
    33  
    34  	// check vs registry
    35  	xctn, err = GetXact(beid)
    36  	if err != nil {
    37  		beid = ""
    38  		return // unlikely
    39  	}
    40  	if xctn == nil {
    41  		return
    42  	}
    43  
    44  	// "idling" away, so try again but only once
    45  	val ^= org
    46  	beid = cos.GenBEID(val, cos.LenShortID)
    47  	if xctn, err = GetXact(beid); err != nil || xctn != nil {
    48  		beid = ""
    49  	}
    50  	return
    51  }