github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/protectedts/ptprovider/provider.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  // Package ptprovider encapsulates the concrete implementation of the
    12  // protectedts.Provider.
    13  package ptprovider
    14  
    15  import (
    16  	"context"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/kv"
    19  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
    20  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts"
    21  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptcache"
    22  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptreconcile"
    23  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptstorage"
    24  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptverifier"
    25  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    26  	"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
    27  	"github.com/cockroachdb/cockroach/pkg/util/stop"
    28  	"github.com/cockroachdb/errors"
    29  )
    30  
    31  // Config configures the Provider.
    32  type Config struct {
    33  	Settings             *cluster.Settings
    34  	DB                   *kv.DB
    35  	Stores               *kvserver.Stores
    36  	ReconcileStatusFuncs ptreconcile.StatusFuncs
    37  	InternalExecutor     sqlutil.InternalExecutor
    38  }
    39  
    40  type provider struct {
    41  	protectedts.Storage
    42  	protectedts.Verifier
    43  	protectedts.Cache
    44  }
    45  
    46  // New creates a new protectedts.Provider.
    47  func New(cfg Config) (protectedts.Provider, error) {
    48  	if err := validateConfig(cfg); err != nil {
    49  		return nil, err
    50  	}
    51  	storage := ptstorage.New(cfg.Settings, cfg.InternalExecutor)
    52  	verifier := ptverifier.New(cfg.DB, storage)
    53  	cache := ptcache.New(ptcache.Config{
    54  		DB:       cfg.DB,
    55  		Storage:  storage,
    56  		Settings: cfg.Settings,
    57  	})
    58  	return &provider{
    59  		Storage:  storage,
    60  		Cache:    cache,
    61  		Verifier: verifier,
    62  	}, nil
    63  }
    64  
    65  func validateConfig(cfg Config) error {
    66  	switch {
    67  	case cfg.Settings == nil:
    68  		return errors.Errorf("invalid nil Settings")
    69  	case cfg.DB == nil:
    70  		return errors.Errorf("invalid nil DB")
    71  	case cfg.InternalExecutor == nil:
    72  		return errors.Errorf("invalid nil InternalExecutor")
    73  	default:
    74  		return nil
    75  	}
    76  }
    77  
    78  func (p *provider) Start(ctx context.Context, stopper *stop.Stopper) error {
    79  	return p.Cache.(*ptcache.Cache).Start(ctx, stopper)
    80  }