Skip to content

Commit

Permalink
Improve Cassandra errors for schema check (uber#5038)
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-sidlauskas authored Dec 7, 2022
1 parent d934bf1 commit 653ab49
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"testing"
"time"

"github.com/uber/cadence/testflags"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand All @@ -39,6 +37,7 @@ import (
"github.com/uber/cadence/common/dynamicconfig"
cassandra_db "github.com/uber/cadence/common/persistence/nosql/nosqlplugin/cassandra"
"github.com/uber/cadence/environment"
"github.com/uber/cadence/testflags"
"github.com/uber/cadence/tools/cassandra"
)

Expand Down Expand Up @@ -106,7 +105,7 @@ func (s *VersionTestSuite) TestCheckCompatibleVersion() {
{"2.0", "1.0", "version mismatch", false},
{"1.0", "1.0", "", false},
{"1.0", "2.0", "", false},
{"1.0", "abc", "unable to read schema version keyspace/database", false},
{"1.0", "abc", "reading schema version: unconfigured table schema_version", false},
}
for _, flag := range flags {
s.runCheckCompatibleVersion(flag.expectedVersion, flag.actualVersion, flag.errStr, flag.expectedFail)
Expand Down
13 changes: 5 additions & 8 deletions tools/cassandra/cqlclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package cassandra

import (
"errors"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -54,8 +53,6 @@ type (
}
)

var errGetSchemaVersion = errors.New("failed to get current schema version from cassandra")

const (
DefaultTimeout = 30 // Timeout in seconds
DefaultConnectTimeout = 2 // Connect timeout in seconds
Expand Down Expand Up @@ -128,12 +125,12 @@ func (client *CqlClient) DropDatabase(name string) error {
return client.DropKeyspace(name)
}

// createKeyspace creates a cassandra Keyspace if it doesn't exist
// CreateKeyspace creates a cassandra Keyspace if it doesn't exist
func (client *CqlClient) CreateKeyspace(name string) error {
return client.ExecDDLQuery(fmt.Sprintf(createKeyspaceCQL, name, client.nReplicas))
}

// createNTSKeyspace creates a cassandra Keyspace if it doesn't exist using network topology strategy
// CreateNTSKeyspace creates a cassandra Keyspace if it doesn't exist using network topology strategy
func (client *CqlClient) CreateNTSKeyspace(name string, datacenter string) error {
return client.ExecDDLQuery(fmt.Sprintf(createNTSKeyspaceCQL, name, datacenter, client.nReplicas))
}
Expand Down Expand Up @@ -161,16 +158,16 @@ func (client *CqlClient) ReadSchemaVersion() (string, error) {
iter := query.Iter()
var version string
if !iter.Scan(&version) {
iter.Close()
return "", errGetSchemaVersion
err := iter.Close()
return "", fmt.Errorf("reading schema version: %w", err)
}
if err := iter.Close(); err != nil {
return "", err
}
return version, nil
}

// UpdateShemaVersion updates the schema version for the Keyspace
// UpdateSchemaVersion updates the schema version for the Keyspace
func (client *CqlClient) UpdateSchemaVersion(newVersion string, minCompatibleVersion string) error {
query := client.session.Query(writeSchemaVersionCQL, client.cfg.Keyspace, time.Now(), newVersion, minCompatibleVersion)
return query.Exec()
Expand Down
4 changes: 2 additions & 2 deletions tools/cassandra/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func verifyCompatibleVersion(
// However, this file will be refactor to support NoSQL soon. After the refactoring, cycle dependency issue
// should be gone and we can use constant at that time
if ds.NoSQL.PluginName != "cassandra" {
return fmt.Errorf("unknown NoSQL plugin name: %v", ds.NoSQL.PluginName)
return fmt.Errorf("unknown NoSQL plugin name: %q", ds.NoSQL.PluginName)
}

return CheckCompatibleVersion(*ds.NoSQL, expectedCassandraVersion)
Expand All @@ -100,7 +100,7 @@ func CheckCompatibleVersion(
ProtoVersion: cfg.ProtoVersion,
})
if err != nil {
return fmt.Errorf("unable to create CQL Client: %v", err.Error())
return fmt.Errorf("creating CQL client: %w", err)
}
defer client.Close()

Expand Down
2 changes: 1 addition & 1 deletion tools/common/schema/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func VerifyCompatibleVersion(

version, err := db.ReadSchemaVersion()
if err != nil {
return fmt.Errorf("unable to read schema version keyspace/database: %s error: %v", dbName, err.Error())
return fmt.Errorf("reading schema version keyspace/database: %q, error: %w", dbName, err)
}
// In most cases, the versions should match. However if after a schema upgrade there is a code
// rollback, the code version (expected version) would fall lower than the actual version in
Expand Down
2 changes: 1 addition & 1 deletion tools/sql/clitest/versionTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (s *VersionTestSuite) TestCheckCompatibleVersion() {
{"2.0", "1.0", "version mismatch", false},
{"1.0", "1.0", "", false},
{"1.0", "2.0", "", false},
{"1.0", "abc", "unable to read schema version keyspace/database", false},
{"1.0", "abc", "schema_version' doesn't exist", false},
}
for _, flag := range flags {
s.runCheckCompatibleVersion(flag.expectedVersion, flag.actualVersion, flag.errStr, flag.expectedFail)
Expand Down

0 comments on commit 653ab49

Please sign in to comment.