From e62dbca11a612114f7526458441b089c1ce5b727 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Mon, 8 Feb 2021 21:54:38 +0100 Subject: [PATCH] cluster: add cluster package skeleton and LeaderElector interface --- cluster/interface.go | 26 ++++++++++++++++++++++++++ cluster/log.go | 27 +++++++++++++++++++++++++++ log.go | 2 ++ 3 files changed, 55 insertions(+) create mode 100644 cluster/interface.go create mode 100644 cluster/log.go diff --git a/cluster/interface.go b/cluster/interface.go new file mode 100644 index 00000000..8a317095 --- /dev/null +++ b/cluster/interface.go @@ -0,0 +1,26 @@ +package cluster + +import ( + "context" +) + +const ( + // EtcdLeaderElector is the id used when constructing an + // etcdLeaderElector instance through the factory. + EtcdLeaderElector = "etcd" +) + +// LeaderElector is a general interface implementing basic leader elections +// in a clustered environment. +type LeaderElector interface { + // Campaign starts a run for leadership. Campaign will block until + // the caller is elected as the leader. + Campaign(ctx context.Context) error + + // Resign resigns from the leader role, allowing other election members + // to take on leadership. + Resign() error + + // Leader returns the leader value for the current election. + Leader(ctx context.Context) (string, error) +} diff --git a/cluster/log.go b/cluster/log.go new file mode 100644 index 00000000..11ced087 --- /dev/null +++ b/cluster/log.go @@ -0,0 +1,27 @@ +package cluster + +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) + +// Subsystem defines the logging code for this subsystem. +const Subsystem = "CLUS" + +// log is a logger that is initialized with the btclog.Disabled logger. +var log btclog.Logger + +// The default amount of logging is none. +func init() { + UseLogger(build.NewSubLogger(Subsystem, nil)) +} + +// DisableLog disables all logging output. +func DisableLog() { + UseLogger(btclog.Disabled) +} + +// UseLogger uses a specified Logger to output package logging info. +func UseLogger(logger btclog.Logger) { + log = logger +} diff --git a/log.go b/log.go index 7b8170cb..feb961d2 100644 --- a/log.go +++ b/log.go @@ -14,6 +14,7 @@ import ( "github.com/lightningnetwork/lnd/chanfitness" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channelnotifier" + "github.com/lightningnetwork/lnd/cluster" "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/discovery" "github.com/lightningnetwork/lnd/funding" @@ -157,6 +158,7 @@ func SetupLoggers(root *build.RotatingLogWriter, interceptor signal.Interceptor) AddSubLogger(root, chainreg.Subsystem, interceptor, chainreg.UseLogger) AddSubLogger(root, chanacceptor.Subsystem, interceptor, chanacceptor.UseLogger) AddSubLogger(root, funding.Subsystem, interceptor, funding.UseLogger) + AddSubLogger(root, cluster.Subsystem, interceptor, cluster.UseLogger) } // AddSubLogger is a helper method to conveniently create and register the