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

     1  // Package ais provides core functionality for the AIStore object storage.
     2  /*
     3   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package reb
     6  
     7  import (
     8  	"time"
     9  
    10  	"github.com/NVIDIA/aistore/cmn/atomic"
    11  	"github.com/NVIDIA/aistore/cmn/mono"
    12  	"github.com/NVIDIA/aistore/cmn/nlog"
    13  )
    14  
    15  const timedDuration = time.Minute + time.Minute/2
    16  
    17  const (
    18  	gfnT   = "timed"
    19  	hkgfnT = "gfn-timed"
    20  	gfnG   = "global"
    21  )
    22  
    23  // get-from-neighbors (GFN) state
    24  type gfnCtx struct {
    25  	exp atomic.Int64
    26  	gon atomic.Bool
    27  }
    28  
    29  var gfn = &gfnCtx{}
    30  
    31  func IsGFN() bool {
    32  	return gfn.gon.Load() || gfn.exp.Load() > mono.NanoTime()
    33  }
    34  
    35  func OnTimedGFN() {
    36  	if gfn.gon.Load() {
    37  		return
    38  	}
    39  	act := "updated"
    40  	exp := mono.NanoTime() + timedDuration.Nanoseconds()
    41  	if gfn.exp.Swap(exp) == 0 {
    42  		act = "on"
    43  	}
    44  	nlog.Infoln(gfnT, act)
    45  }
    46  
    47  func OffTimedGFN(detail string) {
    48  	gfn.exp.Store(0)
    49  	nlog.Infoln(gfnT, "off", detail)
    50  }
    51  
    52  func onGFN() (prev bool) {
    53  	if prev = gfn.gon.Swap(true); prev {
    54  		return
    55  	}
    56  	if exp := gfn.exp.Swap(0); exp > mono.NanoTime() {
    57  		nlog.Infoln(gfnG, "on", gfnT, "off")
    58  	} else {
    59  		nlog.Infoln(gfnG, "on")
    60  	}
    61  	return
    62  }
    63  
    64  func offGFN() {
    65  	gfn.gon.Store(false)
    66  	nlog.Infoln(gfnG, "off")
    67  }