github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/nbs/nbs_metrics_wrapper.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package nbs
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"sync/atomic"
    21  
    22  	"github.com/dolthub/dolt/go/store/chunks"
    23  	"github.com/dolthub/dolt/go/store/hash"
    24  )
    25  
    26  // NBSMetricWrapper is a ChunkStore implementation that wraps a ChunkStore, and collects metrics on the calls.
    27  type NBSMetricWrapper struct {
    28  	*chunks.CSMetricWrapper
    29  	nbs *NomsBlockStore
    30  }
    31  
    32  // NewCSMetricWrapper returns a new NBSMetricWrapper
    33  func NewNBSMetricWrapper(nbs *NomsBlockStore) *NBSMetricWrapper {
    34  	csMW := chunks.NewCSMetricWrapper(nbs)
    35  	return &NBSMetricWrapper{
    36  		csMW,
    37  		nbs,
    38  	}
    39  }
    40  
    41  var _ TableFileStore = &NBSMetricWrapper{}
    42  var _ chunks.ChunkStoreGarbageCollector = &NBSMetricWrapper{}
    43  
    44  // Sources retrieves the current root hash, a list of all the table files,
    45  // and a list of the appendix table files.
    46  func (nbsMW *NBSMetricWrapper) Sources(ctx context.Context) (hash.Hash, []TableFile, []TableFile, error) {
    47  	return nbsMW.nbs.Sources(ctx)
    48  }
    49  
    50  func (nbsMW *NBSMetricWrapper) Size(ctx context.Context) (uint64, error) {
    51  	return nbsMW.nbs.Size(ctx)
    52  }
    53  
    54  // WriteTableFile will read a table file from the provided reader and write it to the TableFileStore
    55  func (nbsMW *NBSMetricWrapper) WriteTableFile(ctx context.Context, fileId string, numChunks int, rd io.Reader, contentLength uint64, contentHash []byte) error {
    56  	return nbsMW.nbs.WriteTableFile(ctx, fileId, numChunks, rd, contentLength, contentHash)
    57  }
    58  
    59  // SetRootChunk changes the root chunk hash from the previous value to the new root.
    60  func (nbsMW *NBSMetricWrapper) SetRootChunk(ctx context.Context, root, previous hash.Hash) error {
    61  	return nbsMW.nbs.SetRootChunk(ctx, root, previous)
    62  }
    63  
    64  // Forwards SupportedOperations to wrapped block store.
    65  func (nbsMW *NBSMetricWrapper) SupportedOperations() TableFileStoreOps {
    66  	return nbsMW.nbs.SupportedOperations()
    67  }
    68  
    69  func (nbsMW *NBSMetricWrapper) MarkAndSweepChunks(ctx context.Context, last hash.Hash, keepChunks <-chan []hash.Hash) error {
    70  	return nbsMW.nbs.MarkAndSweepChunks(ctx, last, keepChunks)
    71  }
    72  
    73  // PruneTableFiles deletes old table files that are no longer referenced in the manifest.
    74  func (nbsMW *NBSMetricWrapper) PruneTableFiles(ctx context.Context) error {
    75  	return nbsMW.nbs.PruneTableFiles(ctx)
    76  }
    77  
    78  // GetManyCompressed gets the compressed Chunks with |hashes| from the store. On return,
    79  // |found| will have been fully sent all chunks which have been
    80  // found. Any non-present chunks will silently be ignored.
    81  func (nbsMW *NBSMetricWrapper) GetManyCompressed(ctx context.Context, hashes hash.HashSet, found func(CompressedChunk)) error {
    82  	atomic.AddInt32(&nbsMW.TotalChunkGets, int32(len(hashes)))
    83  	return nbsMW.nbs.GetManyCompressed(ctx, hashes, found)
    84  }
    85  
    86  // GetManifestStorageVersion returns the storage version of the manifest.
    87  func (nbsMW *NBSMetricWrapper) GetManifestStorageVersion(ctx context.Context) (string, error) {
    88  	return nbsMW.nbs.GetManifestStorageVersion(ctx)
    89  }