Skip to content

Commit

Permalink
Passing in root path to determine cassandra version (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
madhuravi authored Jan 19, 2018
1 parent 0b5a268 commit 7c79aaf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
12 changes: 9 additions & 3 deletions cmd/server/cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package main
import (
"log"
"os"
"path"
"strings"

"github.com/uber/cadence/common/service/config"
Expand Down Expand Up @@ -53,7 +54,12 @@ func startHandler(c *cli.Context) {
log.Printf("config=\n%v\n", cfg.String())

cassCfg := cfg.Cassandra
if err := cassandra.VerifyCompatibleVersion(cassCfg); err != nil {
dir, err := os.Getwd() // dir = /Users/madhuravi/Uber/gocode/src/github.com/uber/cadence
if err != nil {
log.Fatal("Unable to get current directory")
}
root := path.Dir(path.Dir(path.Dir(dir))) // root = /Users/madhuravi/Uber/gocode/src
if err := cassandra.VerifyCompatibleVersion(cassCfg, root); err != nil {
log.Fatalf("Incompatible versions", err)
}
for _, svc := range getServices(c) {
Expand Down Expand Up @@ -105,7 +111,7 @@ func isValidService(in string) bool {
}

func getConfigDir(c *cli.Context) string {
return path(getRootDir(c), c.GlobalString("config"))
return constructPath(getRootDir(c), c.GlobalString("config"))
}

func getRootDir(c *cli.Context) string {
Expand All @@ -120,7 +126,7 @@ func getRootDir(c *cli.Context) string {
return dirpath
}

func path(dir string, file string) string {
func constructPath(dir string, file string) string {
return dir + "/" + file
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/server/cadence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"testing"
)

type CadenceSuite struct {
Expand All @@ -50,5 +51,5 @@ func (s *CadenceSuite) TestIsValidService() {
}

func (s *CadenceSuite) TestPath() {
s.Equal("foo/bar", path("foo", "bar"))
s.Equal("foo/bar", constructPath("foo", "bar"))
}
25 changes: 4 additions & 21 deletions tools/cassandra/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
package cassandra

import (
"errors"
"fmt"
"io/ioutil"
"path"
"regexp"
"runtime"
"strconv"
"strings"

Expand Down Expand Up @@ -132,28 +130,13 @@ func getExpectedVersion(dir string) (string, error) {
// 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
// cassandra.
func VerifyCompatibleVersion(cfg config.Cassandra) error {
_, filename, _, ok := runtime.Caller(0)
if !ok {
return errors.New("unable to get the current function path")
}
return verifyCompatibleVersionWithRoot(cfg, filename)
}

func verifyCompatibleVersionWithRoot(cfg config.Cassandra, rootFile string) error {
// Traverse until project root i.e. "cadence" dir to navigate to the schema/ directory
projRoot := rootFile
for path.Base(projRoot) != "cadence" {
projRoot = path.Dir(projRoot) // According to spec, returns "." when it cannot go any further
if projRoot == "." {
return fmt.Errorf("Unable to get project root from path: %s", rootFile)
}
}
schemaPath := path.Join(projRoot, "schema/cadence/versioned")
func VerifyCompatibleVersion(cfg config.Cassandra, rootFile string) error {
projRoot := "github.com/uber/cadence/schema"
schemaPath := path.Join(rootFile, projRoot+"/cadence/versioned")
if err := checkCompatibleVersion(cfg, cfg.Keyspace, schemaPath); err != nil {
return err
}
schemaPath = path.Join(projRoot, "schema/visibility/versioned")
schemaPath = path.Join(rootFile, projRoot+"/visibility/versioned")
return checkCompatibleVersion(cfg, cfg.VisibilityKeyspace, schemaPath)
}

Expand Down
2 changes: 1 addition & 1 deletion tools/cassandra/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (s *VersionTestSuite) TestVerifyCompatibleVersion() {
Keyspace: keyspace,
VisibilityKeyspace: visKeyspace,
}
s.NoError(verifyCompatibleVersionWithRoot(cfg, filename))
s.NoError(VerifyCompatibleVersion(cfg, path.Dir(path.Dir(path.Dir(root)))))
}

func (s *VersionTestSuite) TestCheckCompatibleVersion() {
Expand Down

0 comments on commit 7c79aaf

Please sign in to comment.