Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ncode committed Feb 13, 2024
1 parent 054c8e3 commit 7ee4197
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ Consul based leader election with tagging support and hooks

Consul lacks a built-in feature for leader election among registered services. This tool is designed to fill that gap. It functions by designating a leader among multiple services, marking the chosen leader with a specified tag. Additionally, it allows for the execution of a script whenever a leader election occurs.

### How do I test it?
### How does it work?

Ballot uses Consul's session API to create a session for each service. The session is then used to create a lock on a key. The service that successfully creates the lock is elected as the leader. The leader is then tagged with a specified tag. The leader election is monitored and the leader is updated if the current leader is no longer healthy.
More info about the sessions here [https://developer.hashicorp.com/consul/tutorials/developer-configuration/application-leader-elections](https://developer.hashicorp.com/consul/tutorials/developer-configuration/application-leader-elections).

### How do I use it?

1. Install Ballot
```bash
Expand Down Expand Up @@ -53,9 +58,33 @@ $PORT # Port of the service
$SESSIONID # Current SessionID of the elected master
```

### Configuration

The configuration file is a yaml file with the following structure:

```yaml
consul:
token: # Consul token
election:
enabled:
- my-service-name # Name of the service enabled for election
services:
my-service-name: # Name of the service
id: my-service-name # ID of the service
key: my-service-name # Key to be used for the lock in Consul, this should be the same across all nodes
token: # Token to be used for the session in Consul
serviceChecks: # List of checks to be used to determine the health of the service
- ping # Name of the check
primaryTag: primary # Tag to be used to mark the leader
execOnPromote: '/bin/echo primary' # Command to be executed when the service is elected as leader
execOnDemote: '/bin/echo secondary' # Command to be executed when the service is demoted as leader
ttl: 10s # TTL for the session
lockDelay: 5s # Lock delay for the session
```
### TODO:
- Write tests
- Write more tests
- Add more examples
- Re-enable the hooks on state change
- Allow to pre-define the preferred leader
Expand Down
24 changes: 24 additions & 0 deletions internal/ballot/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,27 @@ func TestIsLeader(t *testing.T) {
assert.False(t, b.IsLeader())
})
}

type MockConsulClient struct {
mock.Mock
}

func (m *MockConsulClient) Agent() *api.Agent {
args := m.Called()
return args.Get(0).(*api.Agent)
}

func (m *MockConsulClient) Catalog() *api.Catalog {
args := m.Called()
return args.Get(0).(*api.Catalog)
}

func (m *MockConsulClient) KV() *api.KV {
args := m.Called()
return args.Get(0).(*api.KV)
}

func (m *MockConsulClient) Session() *api.Session {
args := m.Called()
return args.Get(0).(*api.Session)
}

0 comments on commit 7ee4197

Please sign in to comment.