Skip to content

Commit

Permalink
Divide agent command into multiple ones by role
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda committed Nov 23, 2018
1 parent 3e324a0 commit 88bab03
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 105 deletions.
65 changes: 65 additions & 0 deletions cmd/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"github.com/bbva/qed/gossip"
"github.com/spf13/cobra"
)

type agentContext struct {
config *gossip.Config
}

func newAgentCommand() *cobra.Command {
var (
nodeName, bindAddr, advertiseAddr string
startJoin []string
)

ctx := &agentContext{}

cmd := &cobra.Command{
Use: "agent",
Short: "Start a gossip agent for the verifiable log QED",
Long: ``,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
config := gossip.DefaultConfig()
config.NodeName = nodeName
config.BindAddr = bindAddr
config.AdvertiseAddr = advertiseAddr
config.EnableCompression = true
ctx.config = config
},
TraverseChildren: true,
}

cmd.Flags().StringVarP(&nodeName, "node", "", "", "Unique name for node. If not set, fallback to hostname")
cmd.Flags().StringVarP(&bindAddr, "bind", "", "", "Bind address for TCP/UDP gossip on (host:port)")
cmd.Flags().StringVarP(&advertiseAddr, "advertise", "", "", "Address to advertise to cluster")
cmd.Flags().StringSliceVarP(&startJoin, "join", "", []string{}, "Comma-delimited list of nodes ([host]:port), through which a cluster can be joined")

cmd.MarkPersistentFlagRequired("node")
cmd.MarkPersistentFlagRequired("bind")
cmd.MarkPersistentFlagRequired("join")

cmd.AddCommand(newAgentMonitorCommand(ctx))
cmd.AddCommand(newAgentAuditorCommand(ctx))

return cmd

}
61 changes: 61 additions & 0 deletions cmd/agent_auditor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, n.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"github.com/bbva/qed/gossip"
"github.com/bbva/qed/log"
"github.com/bbva/qed/util"
"github.com/spf13/cobra"
)

func newAgentAuditorCommand(ctx *agentContext) *cobra.Command {

var qedEndpoints []string

cmd := &cobra.Command{
Use: "monitor",
Short: "Start a QED auditor",
Long: `Start a QED auditor that reacts to snapshot batches
propagated by QED servers and periodically executes membership
queries to verify the inclusion of events`,
Run: func(cmd *cobra.Command, args []string) {

log.SetLogger("QedAuditor", logLevel)

config := ctx.config
//auditorConfig := auditor.DefaultConfig()

agent, err := gossip.NewAgent(config, []gossip.Processor{gossip.DummyProcessor{}})
if err != nil {
log.Fatalf("Failed to start the QED auditor: %v", err)
}

contacted, err := agent.Join(config.StartJoin)
if err != nil {
log.Fatalf("Failed to join the cluster: %v", err)
}
log.Debugf("Number of nodes contacted: %d", contacted)

agent.Start()
defer agent.Shutdown()
util.AwaitTermSignal(agent.Leave)
},
}

cmd.Flags().StringSliceVarP(&qedEndpoints, "endpoints", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which an auditor can make queries")
cmd.MarkPersistentFlagRequired("endpoints")

return cmd
}
61 changes: 61 additions & 0 deletions cmd/agent_monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, n.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"github.com/bbva/qed/gossip"
"github.com/bbva/qed/log"
"github.com/bbva/qed/util"
"github.com/spf13/cobra"
)

func newAgentMonitorCommand(ctx *agentContext) *cobra.Command {

var qedEndpoints []string

cmd := &cobra.Command{
Use: "monitor",
Short: "Start a QED monitor",
Long: `Start a QED monitor that reacts to snapshot batches
propagated by QED servers and periodically executes incremental
queries to verify the consistency between commitments`,
Run: func(cmd *cobra.Command, args []string) {

log.SetLogger("QedMonitor", logLevel)

config := ctx.config
//monitorConfig := monitor.DefaultConfig()

agent, err := gossip.NewAgent(config, []gossip.Processor{gossip.DummyProcessor{}})
if err != nil {
log.Fatalf("Failed to start the QED monitor: %v", err)
}

contacted, err := agent.Join(config.StartJoin)
if err != nil {
log.Fatalf("Failed to join the cluster: %v", err)
}
log.Debugf("Number of nodes contacted: %d", contacted)

agent.Start()
defer agent.Shutdown()
util.AwaitTermSignal(agent.Leave)
},
}

cmd.Flags().StringSliceVarP(&qedEndpoints, "endpoints", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which a monitor can make queries")
cmd.MarkPersistentFlagRequired("endpoints")

return cmd
}
102 changes: 0 additions & 102 deletions cmd/agent_start.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewRootCommand() *cobra.Command {

cmd.AddCommand(newStartCommand())
cmd.AddCommand(newClientCommand())
cmd.AddCommand(newAgentStartCommand())
cmd.AddCommand(newAgentCommand())

return cmd
}
28 changes: 28 additions & 0 deletions gossip/auditor/auditor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, n.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package auditor

type Config struct {
}

func DefaultConfig() *Config {
return nil
}

type Auditor struct {
}

func NewAuditor(conf *Config) *Auditor {
return &Auditor{}
}
5 changes: 5 additions & 0 deletions gossip/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ type Config struct {
// the TERM signal. Defaults false. This can be changed on reload.
LeaveOnTerm bool

// StartJoin is a list of addresses to attempt to join when the
// agent starts. If the agent is unable to communicate with any of these
// addresses, then the agent will error and exit.
StartJoin []string

// EnableCompression specifies whether message compression is enabled
// by `github.com/hashicorp/memberlist` when broadcasting events.
EnableCompression bool
Expand Down
28 changes: 28 additions & 0 deletions gossip/monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, n.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package monitor

type Config struct {
}

func DefaultConfig() *Config {
return nil
}

type Monitor struct {
}

func NewMonitor(conf *Config) *Monitor {
return &Monitor{}
}
4 changes: 2 additions & 2 deletions tests/gossip/run_gossip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ sleep 1s

for i in `seq 1 $1`;
do
xterm -hold -e "go run $GOPATH/src/github.com/bbva/qed/main.go agent -k key -l debug --bind 127.0.0.1:910$i --join $master --node auditor$i --role auditor" &
xterm -hold -e "go run $GOPATH/src/github.com/bbva/qed/main.go agent auditor -k key -l debug --bind 127.0.0.1:910$i --join $master --endpoints $master --node auditor$i" &
pids+=($!)
done

for i in `seq 1 $2`;
do
xterm -hold -e "go run $GOPATH/src/github.com/bbva/qed/main.go agent -k key -l debug --bind 127.0.0.1:920$i --join $master --node monitor$i --role monitor" &
xterm -hold -e "go run $GOPATH/src/github.com/bbva/qed/main.go agent monitor -k key -l debug --bind 127.0.0.1:920$i --join $master --endpoints $master --node monitor$i" &
pids+=($!)
done

Expand Down
Loading

0 comments on commit 88bab03

Please sign in to comment.