go.temporal.io/server@v1.23.0/common/persistence/cassandra/version_checker.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  	"github.com/gocql/gocql"
    29  	"go.temporal.io/server/common/config"
    30  	"go.temporal.io/server/common/log"
    31  	"go.temporal.io/server/common/metrics"
    32  	commongocql "go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/gocql"
    33  	"go.temporal.io/server/common/persistence/schema"
    34  	"go.temporal.io/server/common/resolver"
    35  	cassandraschema "go.temporal.io/server/schema/cassandra"
    36  )
    37  
    38  // VerifyCompatibleVersion ensures that the installed version of temporal and visibility keyspaces
    39  // is greater than or equal to the expected version.
    40  // In most cases, the versions should match. However if after a schema upgrade there is a code
    41  // rollback, the code version (expected version) would fall lower than the actual version in
    42  // cassandra.
    43  func VerifyCompatibleVersion(
    44  	cfg config.Persistence,
    45  	r resolver.ServiceResolver,
    46  ) error {
    47  
    48  	if err := checkMainKeyspace(cfg, r); err != nil {
    49  		return err
    50  	}
    51  	if cfg.StandardVisibilityConfigExist() {
    52  		return checkVisibilityKeyspace(cfg, r)
    53  	}
    54  	return nil
    55  }
    56  
    57  func checkMainKeyspace(
    58  	cfg config.Persistence,
    59  	r resolver.ServiceResolver,
    60  ) error {
    61  	ds, ok := cfg.DataStores[cfg.DefaultStore]
    62  	if ok && ds.Cassandra != nil {
    63  		return CheckCompatibleVersion(*ds.Cassandra, r, cassandraschema.Version)
    64  	}
    65  	return nil
    66  }
    67  
    68  func checkVisibilityKeyspace(
    69  	cfg config.Persistence,
    70  	r resolver.ServiceResolver,
    71  ) error {
    72  	ds, ok := cfg.DataStores[cfg.VisibilityStore]
    73  	if ok && ds.Cassandra != nil {
    74  		return CheckCompatibleVersion(*ds.Cassandra, r, cassandraschema.VisibilityVersion)
    75  	}
    76  	return nil
    77  }
    78  
    79  // CheckCompatibleVersion check the version compatibility
    80  func CheckCompatibleVersion(
    81  	cfg config.Cassandra,
    82  	r resolver.ServiceResolver,
    83  	expectedVersion string,
    84  ) error {
    85  
    86  	session, err := commongocql.NewSession(
    87  		func() (*gocql.ClusterConfig, error) {
    88  			return commongocql.NewCassandraCluster(cfg, r)
    89  		},
    90  		log.NewNoopLogger(),
    91  		metrics.NoopMetricsHandler,
    92  	)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	defer session.Close()
    97  
    98  	schemaVersionReader := NewSchemaVersionReader(session)
    99  
   100  	return schema.VerifyCompatibleVersion(schemaVersionReader, cfg.Keyspace, expectedVersion)
   101  }