go.temporal.io/server@v1.23.0/common/persistence/client/store.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package client
    26  
    27  import (
    28  	"go.temporal.io/server/common/config"
    29  	"go.temporal.io/server/common/log"
    30  	"go.temporal.io/server/common/metrics"
    31  	p "go.temporal.io/server/common/persistence"
    32  	"go.temporal.io/server/common/persistence/cassandra"
    33  	"go.temporal.io/server/common/persistence/sql"
    34  	"go.temporal.io/server/common/resolver"
    35  )
    36  
    37  type (
    38  	// DataStoreFactory is a low level interface to be implemented by a datastore
    39  	// Examples of datastores are cassandra, mysql etc
    40  	DataStoreFactory interface {
    41  		// Close closes the factory
    42  		Close()
    43  		// NewTaskStore returns a new task store
    44  		NewTaskStore() (p.TaskStore, error)
    45  		// NewShardStore returns a new shard store
    46  		NewShardStore() (p.ShardStore, error)
    47  		// NewMetadataStore returns a new metadata store
    48  		NewMetadataStore() (p.MetadataStore, error)
    49  		// NewExecutionStore returns a new execution store
    50  		NewExecutionStore() (p.ExecutionStore, error)
    51  		NewQueue(queueType p.QueueType) (p.Queue, error)
    52  		NewQueueV2() (p.QueueV2, error)
    53  		// NewClusterMetadataStore returns a new metadata store
    54  		NewClusterMetadataStore() (p.ClusterMetadataStore, error)
    55  	}
    56  
    57  	// AbstractDataStoreFactory creates a DataStoreFactory, can be used to implement custom datastore support outside
    58  	// of the Temporal core.
    59  	AbstractDataStoreFactory interface {
    60  		NewFactory(
    61  			cfg config.CustomDatastoreConfig,
    62  			r resolver.ServiceResolver,
    63  			clusterName string,
    64  			logger log.Logger,
    65  			metricsHandler metrics.Handler,
    66  		) DataStoreFactory
    67  	}
    68  )
    69  
    70  func DataStoreFactoryProvider(
    71  	clusterName ClusterName,
    72  	r resolver.ServiceResolver,
    73  	config *config.Persistence,
    74  	abstractDataStoreFactory AbstractDataStoreFactory,
    75  	logger log.Logger,
    76  	metricsHandler metrics.Handler,
    77  ) (DataStoreFactory, *FaultInjectionDataStoreFactory) {
    78  
    79  	var dataStoreFactory DataStoreFactory
    80  	defaultCfg := config.DataStores[config.DefaultStore]
    81  	switch {
    82  	case defaultCfg.Cassandra != nil:
    83  		dataStoreFactory = cassandra.NewFactory(*defaultCfg.Cassandra, r, string(clusterName), logger, metricsHandler)
    84  	case defaultCfg.SQL != nil:
    85  		dataStoreFactory = sql.NewFactory(*defaultCfg.SQL, r, string(clusterName), logger)
    86  	case defaultCfg.CustomDataStoreConfig != nil:
    87  		dataStoreFactory = abstractDataStoreFactory.NewFactory(*defaultCfg.CustomDataStoreConfig, r, string(clusterName), logger, metricsHandler)
    88  	default:
    89  		logger.Fatal("invalid config: one of cassandra or sql params must be specified for default data store")
    90  	}
    91  
    92  	var faultInjection *FaultInjectionDataStoreFactory
    93  	if defaultCfg.FaultInjection != nil {
    94  		faultInjection = NewFaultInjectionDatastoreFactory(defaultCfg.FaultInjection, dataStoreFactory)
    95  		dataStoreFactory = faultInjection
    96  	}
    97  
    98  	return dataStoreFactory, faultInjection
    99  }