Skip to content

Commit

Permalink
Make HAManager configurable by Blip users
Browse files Browse the repository at this point in the history
  • Loading branch information
prudhvi committed Dec 19, 2023
1 parent 6d6adfc commit f490324
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
51 changes: 51 additions & 0 deletions ha/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ha

import (
"fmt"
"github.com/cashapp/blip"
"sync"
)

type HAManagerFactory interface {
Make(monitor blip.ConfigMonitor) (Manager, error)
}

type defaultFactory struct {
}

var df = &defaultFactory{}

func Register(f HAManagerFactory) error {
hf.Lock()
defer hf.Unlock()
// Check if it's different from the default factory
if hf.ha != df {
return fmt.Errorf("HA already registered")
}
hf.ha = f
blip.Debug("register HA")
return nil
}

func Make(args blip.ConfigMonitor) (Manager, error) {
hf.Lock()
defer hf.Unlock()
if hf.ha == nil {
return nil, fmt.Errorf("HA not registered")
}
return hf.ha.Make(args)
}

type haf struct {
*sync.Mutex
ha HAManagerFactory
}

var hf = &haf{
Mutex: &sync.Mutex{},
ha: df,
}

func (f *defaultFactory) Make(_ blip.ConfigMonitor) (Manager, error) {
return Disabled, nil
}
9 changes: 9 additions & 0 deletions monitor/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"github.com/cashapp/blip/ha"
"os"
"sync"
"time"
Expand Down Expand Up @@ -463,11 +464,19 @@ func (ml *Loader) makeMonitor(cfg blip.ConfigMonitor) (*Monitor, error) {
sinks = append(sinks, sink)
}

// Configure the HA Manager for the monitor
var ham ha.Manager
ham, err := ha.Make(cfg)
if err != nil {
return nil, err
}

mon := NewMonitor(MonitorArgs{
Config: cfg,
DbMaker: ml.factory.DbConn,
PlanLoader: ml.planLoader,
Sinks: sinks,
HA: ham,
TransformMetric: ml.plugin.TransformMetrics,
})
return mon, nil
Expand Down
5 changes: 4 additions & 1 deletion monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type Monitor struct {

event event.MonitorReceiver
retry *backoff.ExponentialBackOff
ha ha.Manager
}

// MonitorArgs are required arguments to NewMonitor.
Expand All @@ -82,6 +83,7 @@ type MonitorArgs struct {
PlanLoader *plan.Loader
Sinks []blip.Sink
TransformMetric func(metrics *blip.Metrics) error
HA ha.Manager
}

// NewMonitor creates a new Monitor with the given arguments. The caller must
Expand All @@ -98,6 +100,7 @@ func NewMonitor(args MonitorArgs) *Monitor {
planLoader: args.PlanLoader,
sinks: args.Sinks,
transformMetric: args.TransformMetric,
ha: args.HA,
// --
runMux: &sync.RWMutex{},
wg: sync.WaitGroup{},
Expand Down Expand Up @@ -422,7 +425,7 @@ func (m *Monitor) startup() error {
Config: m.cfg.Plans.Change,
DB: m.db,
LCO: m.lco,
HA: ha.Disabled,
HA: m.ha,
})

m.wg.Add(1)
Expand Down

0 comments on commit f490324

Please sign in to comment.