Skip to content

Commit

Permalink
Merge branch 'hotfix/discovery_hostname'
Browse files Browse the repository at this point in the history
  • Loading branch information
mtenrero committed Jul 7, 2018
2 parents 4a11170 + 0e4adbb commit 31b5227
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 27 deletions.
16 changes: 8 additions & 8 deletions dockerMiddleware/taskOrchestration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func TaskMasterWorker(task *app.TaskPayload, persistance *persistance.Persistanc
}

// Init Worker Service
worker, err = InitService(Worker, task.Name, task.Worker, discovererNetworkID, persistance)
worker, err = InitService("WORKER", task.Name, task.Worker, discovererNetworkID, persistance)
if err != nil {
return nil, err
}

// Wait until service upstart
errWaiting := VIPSWaiter(task.Name, task.Worker.Alias, *task.Worker.Replicas, GlobalTimeoutSeconds, Worker)
errWaiting := VIPSWaiter(task.Name, task.Worker.Alias, *task.Worker.Replicas, GlobalTimeoutSeconds, Worker, persistance.DiscoveryHost)
if errWaiting != nil {
return nil, errWaiting
}
Expand All @@ -68,13 +68,13 @@ func TaskMasterWorker(task *app.TaskPayload, persistance *persistance.Persistanc
// Inject Worker Service VIPs into an Environment variable
newService, err := injectVIPsIntoService(task.Name, task.Worker.Alias, task.Master)

master, err = InitService(Master, task.Name, newService, discovererNetworkID, persistance)
master, err = InitService("MASTER", task.Name, newService, discovererNetworkID, persistance)
if err != nil {
return nil, err
}

// Wait until service upstart
errWaiting = VIPSWaiter(task.Name, task.Master.Alias, *task.Master.Replicas, GlobalTimeoutSeconds, Master)
errWaiting = VIPSWaiter(task.Name, task.Master.Alias, *task.Master.Replicas, GlobalTimeoutSeconds, Master, persistance.DiscoveryHost)
if errWaiting != nil {
return nil, errWaiting
}
Expand Down Expand Up @@ -103,9 +103,9 @@ func TaskMasterWorker(task *app.TaskPayload, persistance *persistance.Persistanc
}

// InitService initializes the service
func InitService(serviceType Service, globalAlias string, service *app.ServicePayload, networkID *string, persistance *persistance.Persistance) (*app.AtqService, error) {
func InitService(serviceType string, globalAlias string, service *app.ServicePayload, networkID *string, persistance *persistance.Persistance) (*app.AtqService, error) {

var workerBaseAlias = globalAlias + "_" + service.Alias + serviceType.Name()
var workerBaseAlias = globalAlias + "_" + service.Alias + serviceType
var volumeBindPath *string
var serviceCreateResponse *dockerTypes.ServiceCreateResponse
var err error
Expand All @@ -123,12 +123,12 @@ func InitService(serviceType Service, globalAlias string, service *app.ServicePa
}

switch serviceType {
case Master:
case "MASTER":
if networkID == nil {
return nil, errors.New("networkID must be specified when creating Master Services")
}
serviceCreateResponse, err = ComposeService(&serviceImage, globalAlias, workerBaseAlias, volumeBindPath, replicatedService(*service.Replicas), networkID)
case Worker:
case "WORKER":
serviceCreateResponse, err = ComposeService(&serviceImage, globalAlias, workerBaseAlias, volumeBindPath, replicatedService(*service.Replicas), networkID)
}

Expand Down
14 changes: 11 additions & 3 deletions dockerMiddleware/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func ServiceHostWaiter(serviceID string, replicas int, timeout int) error {

// VIPSWaiter await for a given amount of VIPS specified in the parameter.
// Timeout in seconds
func VIPSWaiter(globalAlias, serviceName string, replicas int, timeout int, service Service) error {
func VIPSWaiter(globalAlias, serviceName string, replicas int, timeout int, service Service, host string) error {

var vipsExpected = replicas

Expand All @@ -66,7 +66,7 @@ func VIPSWaiter(globalAlias, serviceName string, replicas int, timeout int, serv
for {
select {
case <-tick:
vips, _ := dnsdiscovery.Discovery("http://localhost:9090/api/", globalAlias+"_"+serviceName+service.Name())
vips, _ := dnsdiscovery.Discovery("http://"+host+":9090/api/", globalAlias+"_"+serviceName+service.Name())

vipsAmount := len(*vips)

Expand All @@ -86,7 +86,15 @@ func injectVIPsIntoService(globalAlias, serviceName string, service *app.Service
return nil, err
}

newService := *service
newService := app.ServicePayload{
Alias: service.Alias,
Args: service.Args,
Fileid: service.Fileid,
Image: service.Image,
Replicas: service.Replicas,
Tty: service.Tty,
Environment: make([]string, 0),
}

csvVips := strings.Join(*vips, ",")

Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package main

import (
"flag"
"log"
"os"

"github.com/goadesign/goa"
Expand Down Expand Up @@ -34,13 +36,20 @@ func main() {
service.Use(middleware.ErrorHandler(service, true))
service.Use(middleware.Recover())

// Get Params
discoveryHost := flag.String("discoveryHost", "localhost", "Custom Discovery Hostname")
flag.Parse()

// Initialize Persistance Datastore
Persistance, err = persistance.InitPersistance(persistancePath, config.GlusterPath)
Persistance, err = persistance.InitPersistance(persistancePath, config.GlusterPath, *discoveryHost)
if err != nil {
service.LogError("Error initializing datastore", "datastoreErr", err)
os.Exit(-200)
}

log.Print("DISCOVERY_HOSTNAME: ")
log.Println(Persistance.DiscoveryHost)

// Mount "databind" controller
c := NewDatabindController(service, Persistance)
app.MountDatabindController(service, c)
Expand Down
6 changes: 3 additions & 3 deletions persistance/databind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestStoreAndReadFile(t *testing.T) {
p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")
if err != nil {
t.Error(err)
}
Expand All @@ -21,7 +21,7 @@ func TestStoreAndReadFile(t *testing.T) {
}

func TestReadFileNotPresent(t *testing.T) {
p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")
if err != nil {
t.Error(err)
}
Expand All @@ -32,7 +32,7 @@ func TestReadFileNotPresent(t *testing.T) {
}

func TestReadAllFiles(t *testing.T) {
p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")
if err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion persistance/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestFirstRun(t *testing.T) {
os.RemoveAll("./storage")

p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")

if err != nil {
t.Error(err)
Expand Down
7 changes: 4 additions & 3 deletions persistance/persistance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// InitPersistance initializes and loads the K/V datastore
func InitPersistance(path string, glusterPath string) (*Persistance, error) {
func InitPersistance(path string, glusterPath string, discoveryHost string) (*Persistance, error) {
// Attempt to create storage directory ignoring errors
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll("./storage", os.ModePerm)
Expand All @@ -21,8 +21,9 @@ func InitPersistance(path string, glusterPath string) (*Persistance, error) {
}

persistance := Persistance{
DB: db,
GlusterPath: glusterPath,
DB: db,
GlusterPath: glusterPath,
DiscoveryHost: discoveryHost,
}

// Initialize indexes
Expand Down
2 changes: 1 addition & 1 deletion persistance/persistance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const TestingDBPath = "./storage/dev_testing.atq"

func TestBasicDatastore(t *testing.T) {
absolutePath, _ := filepath.Abs(TestingDBPath)
p, err := InitPersistance(absolutePath, "")
p, err := InitPersistance(absolutePath, "", "localhost")
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions persistance/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestStoreTask(t *testing.T) {
p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -37,7 +37,7 @@ func TestStoreTask(t *testing.T) {
}

func TestWholeTask(t *testing.T) {
p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")
if err != nil {
t.Error(err)
}
Expand Down
5 changes: 3 additions & 2 deletions persistance/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

// Persistance contains all relevant data to the Framework datastore
type Persistance struct {
DB *buntdb.DB
GlusterPath string
DB *buntdb.DB
GlusterPath string
DiscoveryHost string
}
6 changes: 3 additions & 3 deletions persistance/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestDeleteNotExists(t *testing.T) {
p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")
if err != nil {
t.Error(err)
}
Expand All @@ -20,7 +20,7 @@ func TestDeleteNotExists(t *testing.T) {
}

func TestStoreDuplicatedKey(t *testing.T) {
p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")
if err != nil {
t.Error(err)
}
Expand All @@ -35,7 +35,7 @@ func TestStoreDuplicatedKey(t *testing.T) {
}

func TestIterateStringString(t *testing.T) {
p, err := InitPersistance(TestingDBPath, ".")
p, err := InitPersistance(TestingDBPath, ".", "localhost")
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 31b5227

Please sign in to comment.