github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/core/linit.go (about) 1 // Package core provides core metadata and in-cluster API 2 /* 3 * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package core 6 7 import ( 8 "fmt" 9 "strings" 10 11 "github.com/NVIDIA/aistore/cmn" 12 "github.com/NVIDIA/aistore/cmn/debug" 13 "github.com/NVIDIA/aistore/core/meta" 14 "github.com/NVIDIA/aistore/fs" 15 ) 16 17 // Local Object Metadata (LOM) - is cached. Respectively, lifecycle of any given LOM 18 // instance includes the following steps: 19 // 1) construct LOM instance and initialize its runtime state: lom = LOM{...}.Init() 20 // 2) load persistent state (aka lmeta) from one of the LOM caches or the underlying 21 // filesystem: lom.Load(); Load(false) also entails *not adding* LOM to caches 22 // (useful when deleting or moving objects 23 // 3) usage: lom.Atime(), lom.Cksum(), and other accessors 24 // It is illegal to check LOM's existence and, generally, do almost anything 25 // with it prior to loading - see previous 26 // 4) update persistent state in memory: lom.Set*() methods 27 // (requires subsequent re-caching via lom.Recache()) 28 // 5) update persistent state on disk: lom.Persist() 29 // 6) remove a given LOM instance from cache: lom.Uncache() 30 // 7) evict an entire bucket-load of LOM cache: cluster.EvictCache(bucket) 31 // 8) periodic (lazy) eviction followed by access-time synchronization: see LomCacheRunner 32 33 // NOTE: to facilitate fast path filtering-out 34 func (lom *LOM) PreInit(fqn string) (err error) { 35 var parsed fs.ParsedFQN 36 lom.HrwFQN, err = ResolveFQN(fqn, &parsed) 37 if err != nil { 38 return 39 } 40 debug.Assert(parsed.ContentType == fs.ObjectType) 41 lom.FQN = fqn 42 lom.mi = parsed.Mountpath 43 lom.digest = parsed.Digest 44 lom.ObjName = parsed.ObjName 45 lom.bck = *(*meta.Bck)(&parsed.Bck) 46 return nil 47 } 48 49 func (lom *LOM) PostInit() (err error) { 50 if err = lom.bck.InitFast(T.Bowner()); err != nil { 51 return err 52 } 53 lom.md.uname = lom.bck.MakeUname(lom.ObjName) 54 return nil 55 } 56 57 func (lom *LOM) InitFQN(fqn string, expbck *cmn.Bck) (err error) { 58 if err = lom.PreInit(fqn); err != nil { 59 return err 60 } 61 if expbck != nil && !lom.Bucket().Equal(expbck) { 62 err = fmt.Errorf("lom-init mismatch for %q: %s vs %s", fqn, lom.Bucket(), expbck) 63 debug.AssertNoErr(err) 64 return err 65 } 66 return lom.PostInit() 67 } 68 69 func (lom *LOM) InitCT(ct *CT) { 70 debug.Assert(ct.contentType == fs.ObjectType) 71 debug.Assert(ct.bck.Props != nil, ct.bck.String()+" must be initialized") 72 lom.FQN = ct.fqn 73 lom.HrwFQN = ct.hrwFQN 74 lom.mi = ct.mi 75 lom.digest = ct.digest 76 lom.ObjName = ct.objName 77 lom.bck = *ct.bck 78 lom.md.uname = ct.Uname() 79 } 80 81 func (lom *LOM) InitBck(bck *cmn.Bck) (err error) { 82 lom.bck = *(*meta.Bck)(bck) 83 if err = lom.bck.InitFast(T.Bowner()); err != nil { 84 return 85 } 86 lom.md.uname = lom.bck.MakeUname(lom.ObjName) 87 lom.mi, lom.digest, err = fs.Hrw(lom.md.uname) 88 if err != nil { 89 return 90 } 91 lom.FQN = lom.mi.MakePathFQN(lom.Bucket(), fs.ObjectType, lom.ObjName) 92 lom.HrwFQN = lom.FQN 93 return 94 } 95 96 func (lom *LOM) String() string { 97 sb := &strings.Builder{} 98 sb.WriteString("o[") 99 sb.WriteString(lom.bck.Name) 100 sb.WriteByte('/') 101 sb.WriteString(lom.ObjName) 102 if !lom.loaded() { 103 sb.WriteString("(-)") 104 } 105 sb.WriteByte(']') 106 return sb.String() 107 } 108 109 // allocates and copies metadata (in particular, atime and uname) 110 func (lom *LOM) CloneMD(fqn string) *LOM { 111 dst := AllocLOM("") 112 *dst = *lom 113 dst.md = lom.md 114 dst.md.bckID = 0 115 dst.md.copies = nil 116 dst.FQN = fqn 117 return dst 118 }