Skip to content

Commit

Permalink
implement highAvailability feature (#2506)
Browse files Browse the repository at this point in the history
* implement highAvailability feature
---------

Co-authored-by: Marco Mariani <marco@crowdsec.net>
  • Loading branch information
he2ss and mmetc authored Feb 14, 2024
1 parent 8de8bf0 commit 97c441d
Show file tree
Hide file tree
Showing 19 changed files with 2,096 additions and 8 deletions.
12 changes: 12 additions & 0 deletions pkg/apiserver/apic.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,13 @@ func (a *apic) PullTop(forcePull bool) error {
}
}

log.Debug("Acquiring lock for pullCAPI")
err = a.dbClient.AcquirePullCAPILock()
if a.dbClient.IsLocked(err) {
log.Info("PullCAPI is already running, skipping")
return nil
}

log.Infof("Starting community-blocklist update")

data, _, err := a.apiClient.Decisions.GetStreamV3(context.Background(), apiclient.DecisionsStreamOpts{Startup: a.startup})
Expand Down Expand Up @@ -684,6 +691,11 @@ func (a *apic) PullTop(forcePull bool) error {
return fmt.Errorf("while updating blocklists: %w", err)
}

log.Debug("Releasing lock for pullCAPI")
if err := a.dbClient.ReleasePullCAPILock(); err != nil {
return fmt.Errorf("while releasing lock: %w", err)
}

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/apiserver/apic_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func TestAPICSendMetrics(t *testing.T) {
}{
{
name: "basic",
duration: time.Millisecond * 60,
metricsInterval: time.Millisecond * 10,
duration: time.Millisecond * 120,
metricsInterval: time.Millisecond * 20,
expectedCalls: 5,
setUp: func(api *apic) {},
},
{
name: "with some metrics",
duration: time.Millisecond * 60,
metricsInterval: time.Millisecond * 10,
duration: time.Millisecond * 120,
metricsInterval: time.Millisecond * 20,
expectedCalls: 5,
setUp: func(api *apic) {
api.dbClient.Ent.Machine.Delete().ExecX(context.Background())
Expand Down
152 changes: 148 additions & 4 deletions pkg/database/ent/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/database/ent/ent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/database/ent/hook/hook.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 97c441d

Please sign in to comment.