Skip to content

Commit 4b654fc

Browse files
committed
common: moved master election types to etcd package
1 parent e0e5fa0 commit 4b654fc

File tree

13 files changed

+47
-73
lines changed

13 files changed

+47
-73
lines changed

alert/server.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232

3333
"github.com/skydive-project/skydive/api/server"
3434
"github.com/skydive-project/skydive/api/types"
35-
"github.com/skydive-project/skydive/common"
3635
"github.com/skydive-project/skydive/graffiti/api/rest"
3736
api "github.com/skydive-project/skydive/graffiti/api/server"
3837
etcd "github.com/skydive-project/skydive/graffiti/etcd/client"
@@ -191,7 +190,7 @@ func NewGremlinAlert(alert *types.Alert, g *graph.Graph, p *traversal.GremlinTra
191190
// evaluates to true
192191
type Server struct {
193192
insanelock.RWMutex
194-
common.MasterElection
193+
etcd.MasterElection
195194
Graph *graph.Graph
196195
Pool ws.StructSpeakerPool
197196
AlertHandler rest.ResourceWatcher

common/election.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

contrib/snort/snortSkydive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ import (
3434
"github.com/google/gopacket/layers"
3535

3636
"github.com/skydive-project/skydive/analyzer"
37-
"github.com/skydive-project/skydive/common"
3837
"github.com/skydive-project/skydive/config"
3938
"github.com/skydive-project/skydive/flow"
39+
etcd "github.com/skydive-project/skydive/graffiti/etcd/client"
4040
"github.com/skydive-project/skydive/graffiti/logging"
4141
es "github.com/skydive-project/skydive/graffiti/storage/elasticsearch"
4242
)
@@ -96,7 +96,7 @@ type snortMessage struct {
9696
type fakeMasterElection struct {
9797
}
9898

99-
func (f *fakeMasterElection) NewElection(name string) common.MasterElection {
99+
func (f *fakeMasterElection) NewElection(name string) etcd.MasterElection {
100100
return nil
101101
}
102102

graffiti/etcd/client/client.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
etcd "github.com/coreos/etcd/client"
2727

28-
"github.com/skydive-project/skydive/common"
2928
"github.com/skydive-project/skydive/graffiti/logging"
3029
"github.com/skydive-project/skydive/graffiti/service"
3130
)
@@ -37,6 +36,29 @@ const (
3736
DefaultServer = "127.0.0.1"
3837
)
3938

39+
// MasterElectionListener describes the multi election mechanism
40+
type MasterElectionListener interface {
41+
OnStartAsMaster()
42+
OnStartAsSlave()
43+
OnSwitchToMaster()
44+
OnSwitchToSlave()
45+
}
46+
47+
// MasterElection describes the master election mechanism
48+
type MasterElection interface {
49+
Start()
50+
StartAndWait()
51+
Stop()
52+
IsMaster() bool
53+
AddEventListener(listener MasterElectionListener)
54+
TTL() time.Duration
55+
}
56+
57+
// MasterElectionService describes the election service mechanism
58+
type MasterElectionService interface {
59+
NewElection(key string) MasterElection
60+
}
61+
4062
// Client describes a ETCD configuration client
4163
type Client struct {
4264
service service.Service
@@ -90,7 +112,7 @@ func (client *Client) Stop() {
90112
}
91113

92114
// NewElection creates a new ETCD master elector
93-
func (client *Client) NewElection(name string) common.MasterElection {
115+
func (client *Client) NewElection(name string) MasterElection {
94116
return NewMasterElector(client, name)
95117
}
96118

graffiti/etcd/client/election.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/safchain/insanelock"
2626
"golang.org/x/net/context"
2727

28-
"github.com/skydive-project/skydive/common"
2928
"github.com/skydive-project/skydive/graffiti/logging"
3029
"github.com/skydive-project/skydive/graffiti/service"
3130
)
@@ -40,7 +39,7 @@ type MasterElector struct {
4039
EtcdKeyAPI etcd.KeysAPI
4140
Host string
4241
path string
43-
listeners []common.MasterElectionListener
42+
listeners []MasterElectionListener
4443
cancel context.CancelFunc
4544
master bool
4645
state service.State
@@ -204,7 +203,7 @@ func (le *MasterElector) Stop() {
204203
}
205204

206205
// AddEventListener registers a new listener
207-
func (le *MasterElector) AddEventListener(listener common.MasterElectionListener) {
206+
func (le *MasterElector) AddEventListener(listener MasterElectionListener) {
208207
le.listeners = append(le.listeners, listener)
209208
}
210209

graffiti/graph/elasticsearch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
"github.com/olivere/elastic/v7"
2727

28-
"github.com/skydive-project/skydive/common"
28+
etcd "github.com/skydive-project/skydive/graffiti/etcd/client"
2929
"github.com/skydive-project/skydive/graffiti/filters"
3030
"github.com/skydive-project/skydive/graffiti/logging"
3131
es "github.com/skydive-project/skydive/graffiti/storage/elasticsearch"
@@ -94,7 +94,7 @@ type ElasticSearchBackend struct {
9494
PersistentBackend
9595
client es.ClientInterface
9696
prevRevision map[Identifier]*rawData
97-
election common.MasterElection
97+
election etcd.MasterElection
9898
liveIndex es.Index
9999
archiveIndex es.Index
100100
logger logging.Logger
@@ -541,7 +541,7 @@ func (b *ElasticSearchBackend) OnStarted() {
541541

542542
// newElasticSearchBackendFromClient creates a new graph backend using the given elasticsearch
543543
// client connection
544-
func newElasticSearchBackendFromClient(client es.ClientInterface, liveIndex, archiveIndex es.Index, electionService common.MasterElectionService, logger logging.Logger) *ElasticSearchBackend {
544+
func newElasticSearchBackendFromClient(client es.ClientInterface, liveIndex, archiveIndex es.Index, electionService etcd.MasterElectionService, logger logging.Logger) *ElasticSearchBackend {
545545
if logger == nil {
546546
logger = logging.GetLogger()
547547
}
@@ -562,7 +562,7 @@ func newElasticSearchBackendFromClient(client es.ClientInterface, liveIndex, arc
562562
}
563563

564564
// NewElasticSearchBackendFromConfig creates a new graph backend from an ES configuration structure
565-
func NewElasticSearchBackendFromConfig(cfg es.Config, extraDynamicTemplates map[string]interface{}, electionService common.MasterElectionService, logger logging.Logger) (*ElasticSearchBackend, error) {
565+
func NewElasticSearchBackendFromConfig(cfg es.Config, extraDynamicTemplates map[string]interface{}, electionService etcd.MasterElectionService, logger logging.Logger) (*ElasticSearchBackend, error) {
566566
mapping := make(map[string]interface{})
567567
if err := json.Unmarshal([]byte(graphElementMapping), &mapping); err != nil {
568568
return nil, err

graffiti/graph/orientdb.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"fmt"
2323
"strings"
2424

25-
"github.com/skydive-project/skydive/common"
25+
etcd "github.com/skydive-project/skydive/graffiti/etcd/client"
2626
"github.com/skydive-project/skydive/graffiti/filters"
2727
"github.com/skydive-project/skydive/graffiti/logging"
2828
"github.com/skydive-project/skydive/graffiti/storage/orientdb"
@@ -32,8 +32,8 @@ import (
3232
type OrientDBBackend struct {
3333
PersistentBackend
3434
client orientdb.ClientInterface
35-
election common.MasterElection
3635
logger logging.Logger
36+
election etcd.MasterElection
3737
}
3838

3939
type eventTime struct {
@@ -357,8 +357,7 @@ func (o *OrientDBBackend) OnStarted() {
357357
}
358358
}
359359

360-
361-
func newOrientDBBackend(client orientdb.ClientInterface, electionService common.MasterElectionService, logger logging.Logger) (*OrientDBBackend, error) {
360+
func newOrientDBBackend(client orientdb.ClientInterface, electionService etcd.MasterElectionService, logger logging.Logger) (*OrientDBBackend, error) {
362361
if logger == nil {
363362
logger = logging.GetLogger()
364363
}
@@ -435,7 +434,7 @@ func newOrientDBBackend(client orientdb.ClientInterface, electionService common.
435434

436435
// NewOrientDBBackend creates a new graph backend and
437436
// connect to an OrientDB instance
438-
func NewOrientDBBackend(addr string, database string, username string, password string, electionService common.MasterElectionService, logger logging.Logger) (*OrientDBBackend, error) {
437+
func NewOrientDBBackend(addr string, database string, username string, password string, electionService etcd.MasterElectionService, logger logging.Logger) (*OrientDBBackend, error) {
439438
client, err := orientdb.NewClient(addr, database, username, password)
440439
if err != nil {
441440
return nil, err

graffiti/ondemand/client/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/safchain/insanelock"
2828
"github.com/skydive-project/go-debouncer"
2929

30-
"github.com/skydive-project/skydive/common"
3130
"github.com/skydive-project/skydive/graffiti/api/rest"
3231
etcd "github.com/skydive-project/skydive/graffiti/etcd/client"
3332
"github.com/skydive-project/skydive/graffiti/filters"
@@ -56,7 +55,7 @@ type OnDemandClientHandler interface {
5655
// OnDemandClient describes an ondemand task client based on a websocket
5756
type OnDemandClient struct {
5857
insanelock.RWMutex
59-
common.MasterElection
58+
etcd.MasterElection
6059
graph.DefaultGraphListener
6160
graph *graph.Graph
6261
apiHandler rest.WatchableHandler

graffiti/storage/elasticsearch/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
esconfig "github.com/olivere/elastic/v7/config"
3333

3434
"github.com/skydive-project/skydive/common"
35+
etcd "github.com/skydive-project/skydive/graffiti/etcd/client"
3536
"github.com/skydive-project/skydive/graffiti/filters"
3637
"github.com/skydive-project/skydive/graffiti/logging"
3738
"github.com/skydive-project/skydive/graffiti/storage"
@@ -445,7 +446,7 @@ func (c *Client) AddEventListener(listener storage.EventListener) {
445446
}
446447

447448
// NewClient creates a new ElasticSearch client based on configuration
448-
func NewClient(indices []Index, cfg Config, electionService common.MasterElectionService) (*Client, error) {
449+
func NewClient(indices []Index, cfg Config, electionService etcd.MasterElectionService) (*Client, error) {
449450
url, err := urlFromHost(cfg.ElasticHost)
450451
if err != nil {
451452
return nil, err

graffiti/storage/elasticsearch/rollindex.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
"github.com/pierrec/xxHash/xxHash64"
3030

31-
"github.com/skydive-project/skydive/common"
31+
etcd "github.com/skydive-project/skydive/graffiti/etcd/client"
3232
"github.com/skydive-project/skydive/graffiti/logging"
3333
)
3434

@@ -44,7 +44,7 @@ type rollIndexService struct {
4444
config Config
4545
indices []Index
4646
quit chan bool
47-
election common.MasterElection
47+
election etcd.MasterElection
4848
}
4949

5050
func (r *rollIndexService) cleanup(index Index) {
@@ -167,7 +167,7 @@ func SetRollingRate(rate time.Duration) {
167167
rollingRateLock.Unlock()
168168
}
169169

170-
func newRollIndexService(client *Client, indices []Index, cfg Config, electionService common.MasterElectionService) *rollIndexService {
170+
func newRollIndexService(client *Client, indices []Index, cfg Config, electionService etcd.MasterElectionService) *rollIndexService {
171171
hasher := xxHash64.New(0)
172172
for _, index := range indices {
173173
hasher.Write([]byte(index.Name))

0 commit comments

Comments
 (0)