Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HA as configurable plugin by Blip users #116

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this all in ha/ha.go since it's all closely related--one less file.


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

type HAManagerFactory interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "HA" prefix. In Go, pkg name is considered part of the full name context, so ha.ManagerFactory is the idiomatic style.

Make(monitor blip.ConfigMonitor) (Manager, error)
}

type defaultFactory struct {
}

var df = &defaultFactory{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In keeping with other pkgs (iirc):

// Internal factory disabled by default (HA not used).
var f = Disabled

Then put the factor Make method on disabled (so it implements both Manager and ManagerFactory interfaces).

Then in Register, use sync.Once like you did in an earlier rev to avoid having locks and such.


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"
Copy link
Member

@daniel-nichter daniel-nichter Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort the imports:

built-in_a
built-in_b

other_a
other_b

"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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ham" 😆 This component will now officially be known as "the ham".

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
Loading