github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/storage/lease_verifier.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package storage 22 23 import ( 24 "fmt" 25 26 "github.com/m3db/m3/src/dbnode/storage/block" 27 "github.com/m3db/m3/src/x/ident" 28 xtime "github.com/m3db/m3/src/x/time" 29 ) 30 31 type flushStateRetriever interface { 32 FlushState(namespace ident.ID, shardID uint32, blockStart xtime.UnixNano) (fileOpState, error) 33 } 34 35 type leaseVerifier struct { 36 flushStateRetriever flushStateRetriever 37 } 38 39 // NewLeaseVerifier creates a new LeaseVerifier. 40 func NewLeaseVerifier(retriever flushStateRetriever) *leaseVerifier { 41 return &leaseVerifier{ 42 flushStateRetriever: retriever, 43 } 44 } 45 46 func (v *leaseVerifier) VerifyLease( 47 descriptor block.LeaseDescriptor, 48 state block.LeaseState, 49 ) error { 50 flushState, err := v.flushStateRetriever.FlushState( 51 descriptor.Namespace, uint32(descriptor.Shard), descriptor.BlockStart) 52 if err != nil { 53 return fmt.Errorf( 54 "err retrieving flushState for lease verification, ns: %s, shard: %d, blockStart: %s, err: %v", 55 descriptor.Namespace.String(), descriptor.Shard, descriptor.BlockStart.String(), err) 56 } 57 58 if state.Volume != flushState.ColdVersionFlushed { 59 // The cold flush version and volume correspond 1:1 so a lease should only 60 // be permitted if the requested volume is equal to the highest flushed 61 // volume (the current cold flush version). 62 // 63 // This logic also holds in situations where the cold flush feature is not 64 // enabled because even in that case the volume number for the first warm 65 // flush should be 0 and the cold version should also be 0. 66 return fmt.Errorf( 67 "cannot permit lease for ns: %s, shard: %d, blockStart: %s, volume: %d when latest volume is %d", 68 descriptor.Namespace.String(), descriptor.Shard, descriptor.BlockStart.String(), state.Volume, flushState.ColdVersionFlushed) 69 } 70 71 return nil 72 } 73 74 func (v *leaseVerifier) LatestState(descriptor block.LeaseDescriptor) (block.LeaseState, error) { 75 flushState, err := v.flushStateRetriever.FlushState( 76 descriptor.Namespace, descriptor.Shard, descriptor.BlockStart) 77 if err != nil { 78 return block.LeaseState{}, fmt.Errorf( 79 "err retrieving flushState for LatestState, ns: %s, shard: %d, blockStart: %s, err: %v", 80 descriptor.Namespace.String(), descriptor.Shard, descriptor.BlockStart.String(), err) 81 } 82 83 // LeaseVerifier should return ColdVersionFlushed not ColdVersionRetrievable since ColdVersionFlushed 84 // represents the latest version that is available on disk while ColdVersion only represents 85 // the latest version that is retrievable from the block retriever and SeekerManager. 86 return block.LeaseState{Volume: flushState.ColdVersionFlushed}, nil 87 }