go.temporal.io/server@v1.23.0/common/persistence/cassandra/factory.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 cassandra
    26  
    27  import (
    28  	"sync"
    29  
    30  	"github.com/gocql/gocql"
    31  
    32  	"go.temporal.io/server/common/config"
    33  	"go.temporal.io/server/common/log"
    34  	"go.temporal.io/server/common/log/tag"
    35  	"go.temporal.io/server/common/metrics"
    36  	p "go.temporal.io/server/common/persistence"
    37  	commongocql "go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/gocql"
    38  	"go.temporal.io/server/common/resolver"
    39  )
    40  
    41  type (
    42  	// Factory vends datastore implementations backed by cassandra
    43  	Factory struct {
    44  		sync.RWMutex
    45  		cfg         config.Cassandra
    46  		clusterName string
    47  		logger      log.Logger
    48  		session     commongocql.Session
    49  	}
    50  )
    51  
    52  // NewFactory returns an instance of a factory object which can be used to create
    53  // data stores that are backed by cassandra
    54  func NewFactory(
    55  	cfg config.Cassandra,
    56  	r resolver.ServiceResolver,
    57  	clusterName string,
    58  	logger log.Logger,
    59  	metricsHandler metrics.Handler,
    60  ) *Factory {
    61  	session, err := commongocql.NewSession(
    62  		func() (*gocql.ClusterConfig, error) {
    63  			return commongocql.NewCassandraCluster(cfg, r)
    64  		},
    65  		logger,
    66  		metricsHandler,
    67  	)
    68  	if err != nil {
    69  		logger.Fatal("unable to initialize cassandra session", tag.Error(err))
    70  	}
    71  	return NewFactoryFromSession(cfg, clusterName, logger, session)
    72  }
    73  
    74  // NewFactoryFromSession returns an instance of a factory object from the given session.
    75  func NewFactoryFromSession(
    76  	cfg config.Cassandra,
    77  	clusterName string,
    78  	logger log.Logger,
    79  	session commongocql.Session,
    80  ) *Factory {
    81  	return &Factory{
    82  		cfg:         cfg,
    83  		clusterName: clusterName,
    84  		logger:      logger,
    85  		session:     session,
    86  	}
    87  }
    88  
    89  // NewTaskStore returns a new task store
    90  func (f *Factory) NewTaskStore() (p.TaskStore, error) {
    91  	return NewMatchingTaskStore(f.session, f.logger), nil
    92  }
    93  
    94  // NewShardStore returns a new shard store
    95  func (f *Factory) NewShardStore() (p.ShardStore, error) {
    96  	return NewShardStore(f.clusterName, f.session, f.logger), nil
    97  }
    98  
    99  // NewMetadataStore returns a metadata store
   100  func (f *Factory) NewMetadataStore() (p.MetadataStore, error) {
   101  	return NewMetadataStore(f.clusterName, f.session, f.logger)
   102  }
   103  
   104  // NewClusterMetadataStore returns a metadata store
   105  func (f *Factory) NewClusterMetadataStore() (p.ClusterMetadataStore, error) {
   106  	return NewClusterMetadataStore(f.session, f.logger)
   107  }
   108  
   109  // NewExecutionStore returns a new ExecutionStore.
   110  func (f *Factory) NewExecutionStore() (p.ExecutionStore, error) {
   111  	return NewExecutionStore(f.session, f.logger), nil
   112  }
   113  
   114  // NewQueue returns a new queue backed by cassandra
   115  func (f *Factory) NewQueue(queueType p.QueueType) (p.Queue, error) {
   116  	return NewQueueStore(queueType, f.session, f.logger)
   117  }
   118  
   119  // NewQueueV2 returns a new data-access object for queues and messages stored in Cassandra. It will never return an
   120  // error.
   121  func (f *Factory) NewQueueV2() (p.QueueV2, error) {
   122  	return NewQueueV2Store(f.session, f.logger), nil
   123  }
   124  
   125  // Close closes the factory
   126  func (f *Factory) Close() {
   127  	f.Lock()
   128  	defer f.Unlock()
   129  	f.session.Close()
   130  }