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

     1  // Package core provides core metadata and in-cluster API
     2  /*
     3   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package core
     6  
     7  import (
     8  	"github.com/NVIDIA/aistore/cmn"
     9  	"github.com/NVIDIA/aistore/cmn/debug"
    10  	"github.com/NVIDIA/aistore/fs"
    11  )
    12  
    13  // LOM In Flight (LIF)
    14  type (
    15  	LIF struct {
    16  		Uname  string
    17  		BID    uint64
    18  		digest uint64
    19  	}
    20  	lifUnlocker interface {
    21  		CacheIdx() int
    22  		getLocker() *nlc
    23  	}
    24  )
    25  
    26  // interface guard to make sure that LIF can be used to unlock LOM
    27  var _ lifUnlocker = (*LIF)(nil)
    28  
    29  // constructor
    30  func (lom *LOM) LIF() (lif LIF) {
    31  	debug.Assert(lom.md.uname != "")
    32  	debug.Assert(lom.Bprops() != nil && lom.Bprops().BID != 0)
    33  	return LIF{
    34  		Uname:  lom.md.uname,
    35  		BID:    lom.Bprops().BID,
    36  		digest: lom.digest,
    37  	}
    38  }
    39  
    40  // LIF => LOF with a check for bucket existence
    41  func (lif *LIF) LOM() (lom *LOM, err error) {
    42  	b, objName := cmn.ParseUname(lif.Uname)
    43  	lom = AllocLOM(objName)
    44  	if err = lom.InitBck(&b); err != nil {
    45  		FreeLOM(lom)
    46  		return
    47  	}
    48  	if bprops := lom.Bprops(); bprops == nil {
    49  		err = cmn.NewErrObjDefunct(lom.String(), 0, lif.BID)
    50  		FreeLOM(lom)
    51  	} else if bprops.BID != lif.BID {
    52  		err = cmn.NewErrObjDefunct(lom.String(), bprops.BID, lif.BID)
    53  		FreeLOM(lom)
    54  	}
    55  	return
    56  }
    57  
    58  // deferred unlocking
    59  
    60  func (lif *LIF) CacheIdx() int   { return fs.LcacheIdx(lif.digest) }
    61  func (lif *LIF) getLocker() *nlc { return &g.locker[lif.CacheIdx()] }
    62  
    63  func (lif *LIF) Unlock(exclusive bool) {
    64  	nlc := lif.getLocker()
    65  	nlc.Unlock(lif.Uname, exclusive)
    66  }