Skip to content

Commit

Permalink
bootstrap: config loader and executable for cadence server (cadence-w…
Browse files Browse the repository at this point in the history
  • Loading branch information
venkat1109 authored May 22, 2017
1 parent c8180ad commit a7da4e0
Show file tree
Hide file tree
Showing 26 changed files with 2,035 additions and 110 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ before_install:

install:
- go get -u github.com/Masterminds/glide
- go get -u github.com/golang/lint/golint
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
Expand Down
25 changes: 11 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ TEST_ARG ?= -race -v -timeout 5m
BUILD := ./build
TOOLS_CMD_ROOT=./cmd/tools
INTEG_TEST_ROOT=./host
INTEG_TEST_DIR=host

export PATH := $(GOPATH)/bin:$(PATH)

Expand Down Expand Up @@ -51,9 +52,6 @@ TOOLS_SRC += $(TOOLS_CMD_ROOT)
# all directories with *_test.go files in them
TEST_DIRS := $(sort $(dir $(filter %_test.go,$(ALL_SRC))))

# dirs that contain integration tests, these need to be treated
# differently to get correct code coverage
INTEG_TEST_DIRS := $(filter $(INTEG_TEST_ROOT)%,$(TEST_DIRS))
# all tests other than integration test fall into the pkg_test category
PKG_TEST_DIRS := $(filter-out $(INTEG_TEST_ROOT)%,$(TEST_DIRS))

Expand All @@ -73,16 +71,16 @@ vendor/glide.updated: glide.lock glide.yaml
clean_thrift:
rm -rf .gen

thriftc: clean_thrift vendor/glide.updated $(THRIFT_GEN_SRC)
thriftc: clean_thrift $(THRIFT_GEN_SRC)

copyright: cmd/tools/copyright/licensegen.go
go run ./cmd/tools/copyright/licensegen.go --verifyOnly

cadence-cassandra-tool: vendor/glide.updated $(TOOLS_SRC)
go build -i -o cadence-cassandra-tool cmd/tools/cassandra/main.go

cadence: vendor/glide.updated main.go
go build -i -o cadence main.go
cadence: vendor/glide.updated $(ALL_SRC)
go build -i -o cadence cmd/server/cadence.go cmd/server/server.go

bins_nothrift: lint copyright cadence-cassandra-tool cadence

Expand All @@ -99,12 +97,10 @@ cover_profile: clean bins_nothrift
@mkdir -p $(BUILD)
@echo "mode: atomic" > $(BUILD)/cover.out

@echo Running integration tests:
@time for dir in $(INTEG_TEST_DIRS); do \
mkdir -p $(BUILD)/"$$dir"; \
go test "$$dir" $(TEST_ARG) $(GOCOVERPKG_ARG) -coverprofile=$(BUILD)/"$$dir"/coverage.out || exit 1; \
cat $(BUILD)/"$$dir"/coverage.out | grep -v "mode: atomic" >> $(BUILD)/cover.out; \
done
@echo Running integration test
@mkdir -p $(BUILD)/$(INTEG_TEST_DIR)
@time go test $(INTEG_TEST_ROOT) $(TEST_ARG) $(GOCOVERPKG_ARG) -coverprofile=$(BUILD)/$(INTEG_TEST_DIR)/coverage.out || exit 1;
@cat $(BUILD)/$(INTEG_TEST_DIR)/coverage.out | grep -v "mode: atomic" >> $(BUILD)/cover.out

@echo Running package tests:
@for dir in $(PKG_TEST_DIRS); do \
Expand All @@ -119,9 +115,10 @@ cover: cover_profile
cover_ci: cover_profile
goveralls -coverprofile=$(BUILD)/cover.out -service=travis-ci || echo -e "\x1b[31mCoveralls failed\x1b[m"; \

lint:
lint: vendor/glide.updated
@echo $(ALL_SRC)
@lintFail=0; for file in $(ALL_SRC); do \
golint -set_exit_status "$$file"; \
golint "$$file"; \
if [ $$? -eq 1 ]; then lintFail=1; fi; \
done; \
if [ $$lintFail -eq 1 ]; then exit 1; fi;
Expand Down
174 changes: 174 additions & 0 deletions cmd/server/cadence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package main

import (
"github.com/uber/cadence/common/service/config"
"github.com/urfave/cli"
"log"
"os"
"strings"
)

// validServices is the list of all valid cadence services
var validServices = []string{historyService, matchingService, frontendService}

// main entry point for the cadence server
func main() {
app := buildCLI()
app.Run(os.Args)
}

// startHandler is the handler for the cli start command
func startHandler(c *cli.Context) {
env := getEnvironment(c)
zone := getZone(c)
configDir := getConfigDir(c)

log.Printf("Loading config; env=%v,zone=%v,configDir=%v\n", env, zone, configDir)

var cfg config.Config
config.Load(env, configDir, zone, &cfg)
log.Printf("config=\n%v\n", cfg.String())

for _, svc := range getServices(c) {
if _, ok := cfg.Services[svc]; !ok {
log.Fatalf("`%v` service missing config", svc)
}
server := newServer(svc, &cfg)
server.Start()
}

select {}
}

func getEnvironment(c *cli.Context) string {
return strings.TrimSpace(c.GlobalString("env"))
}

func getZone(c *cli.Context) string {
return strings.TrimSpace(c.GlobalString("zone"))
}

// getServices parses the services arg from cli
// and returns a list of services to start
func getServices(c *cli.Context) []string {

val := strings.TrimSpace(c.String("services"))
tokens := strings.Split(val, ",")

if len(tokens) == 0 {
log.Fatal("list of services is empty")
}

for _, t := range tokens {
if !isValidService(t) {
log.Fatalf("invalid service `%v` in service list [%v]", t, val)
}
}

return tokens
}

func isValidService(in string) bool {
for _, s := range validServices {
if s == in {
return true
}
}
return false
}

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

func getRootDir(c *cli.Context) string {
dirpath := c.GlobalString("root")
if len(dirpath) == 0 {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("os.Getwd() failed, err=%v", err)
}
return cwd
}
return dirpath
}

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

func buildCLI() *cli.App {

app := cli.NewApp()
app.Name = "cadence"
app.Usage = "Cadence server"
app.Version = "0.0.1"

app.Flags = []cli.Flag{
cli.StringFlag{
Name: "root, r",
Value: ".",
Usage: "root directory of execution environment",
EnvVar: config.EnvKeyRoot,
},
cli.StringFlag{
Name: "config, c",
Value: "config",
Usage: "config dir path relative to root",
EnvVar: config.EnvKeyConfigDir,
},
cli.StringFlag{
Name: "env, e",
Value: "development",
Usage: "runtime environment",
EnvVar: config.EnvKeyEnvironment,
},
cli.StringFlag{
Name: "zone, az",
Value: "",
Usage: "availability zone",
EnvVar: config.EnvKeyAvailabilityZone,
},
}

app.Commands = []cli.Command{
{
Name: "start",
Aliases: []string{""},
Usage: "start cadence server",
Flags: []cli.Flag{
cli.StringFlag{
Name: "services, s",
Value: strings.Join(validServices, ","),
Usage: "list of services to start",
},
},
Action: func(c *cli.Context) {
startHandler(c)
},
},
}

return app

}
31 changes: 30 additions & 1 deletion main.go → cmd/server/cadence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@

package main

func main() {
import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"testing"
)

type CadenceSuite struct {
*require.Assertions
suite.Suite
}

func TestCadenceSuite(t *testing.T) {
suite.Run(t, new(CadenceSuite))
}

func (s *CadenceSuite) SetupTest() {
s.Assertions = require.New(s.T())
}

func (s *CadenceSuite) TestIsValidService() {
s.True(isValidService("history"))
s.True(isValidService("matching"))
s.True(isValidService("frontend"))
s.False(isValidService("cadence-history"))
s.False(isValidService("cadence-matching"))
s.False(isValidService("cadence-frontend"))
s.False(isValidService("foobar"))
}

func (s *CadenceSuite) TestPath() {
s.Equal("foo/bar", path("foo", "bar"))
}
Loading

0 comments on commit a7da4e0

Please sign in to comment.