Skip to content

Commit

Permalink
fix: add updater logic
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Oct 21, 2024
1 parent 3d82e4a commit 939d2d1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
36 changes: 30 additions & 6 deletions pkg/config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,50 @@ type (
}
)

// Compare two pointers values handling the nil case
func isPointerValueEquals[T comparable](a, b *T) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return *a == *b
}

// Compare two db config, if changes requires restart return true, return false otherwise
func requireDbRestart(a *db, b *db) bool {
return !isPointerValueEquals(a.MaxConnections, b.MaxConnections) ||
!isPointerValueEquals(a.MaxWorkerProcesses, b.MaxWorkerProcesses) ||
!isPointerValueEquals(a.MaxParallelWorkers, b.MaxParallelWorkers) ||
!isPointerValueEquals(a.MaxWalSenders, b.MaxWalSenders) ||
!isPointerValueEquals(a.MaxReplicationSlots, b.MaxReplicationSlots) ||
!isPointerValueEquals(a.SharedBuffers, b.SharedBuffers)
}

func (a *db) ToUpdatePostgresConfigBody() v1API.UpdatePostgresConfigBody {
body := v1API.UpdatePostgresConfigBody{}

// Parameters that require restart
body.MaxConnections = cast.UintToIntPtr(a.MaxConnections)
body.MaxWorkerProcesses = cast.UintToIntPtr(a.MaxWorkerProcesses)
body.MaxParallelWorkers = cast.UintToIntPtr(a.MaxParallelWorkers)
body.MaxWalSenders = cast.UintToIntPtr(a.MaxWalSenders)
body.MaxReplicationSlots = cast.UintToIntPtr(a.MaxReplicationSlots)
body.SharedBuffers = a.SharedBuffers

// Parameters that can be changed without restart
body.EffectiveCacheSize = a.EffectiveCacheSize
body.LogicalDecodingWorkMem = a.LogicalDecodingWorkMem
body.MaintenanceWorkMem = a.MaintenanceWorkMem
body.MaxConnections = cast.UintToIntPtr(a.MaxConnections)
body.MaxLocksPerTransaction = cast.UintToIntPtr(a.MaxLocksPerTransaction)
body.MaxParallelMaintenanceWorkers = cast.UintToIntPtr(a.MaxParallelMaintenanceWorkers)
body.MaxParallelWorkers = cast.UintToIntPtr(a.MaxParallelWorkers)
body.MaxParallelWorkersPerGather = cast.UintToIntPtr(a.MaxParallelWorkersPerGather)
body.MaxReplicationSlots = cast.UintToIntPtr(a.MaxReplicationSlots)
body.MaxSlotWalKeepSize = a.MaxSlotWalKeepSize
body.MaxStandbyArchiveDelay = a.MaxStandbyArchiveDelay
body.MaxStandbyStreamingDelay = a.MaxStandbyStreamingDelay
body.MaxWalSenders = cast.UintToIntPtr(a.MaxWalSenders)
body.MaxWalSize = a.MaxWalSize
body.MaxWorkerProcesses = cast.UintToIntPtr(a.MaxWorkerProcesses)
body.SessionReplicationRole = (*v1API.UpdatePostgresConfigBodySessionReplicationRole)(a.SessionReplicationRole)
body.SharedBuffers = a.SharedBuffers
body.StatementTimeout = a.StatementTimeout
body.WalKeepSize = a.WalKeepSize
body.WalSenderTimeout = a.WalSenderTimeout
Expand Down
35 changes: 34 additions & 1 deletion pkg/config/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func (u *ConfigUpdater) UpdateRemoteConfig(ctx context.Context, remote baseConfi
if err := u.UpdateApiConfig(ctx, remote.ProjectId, remote.Api); err != nil {
return err
}
// TODO: implement other service configs, ie. auth
if err := u.UpdateDbConfig(ctx, remote.ProjectId, remote.Db); err != nil {
return err
}
return nil
}

Expand All @@ -40,10 +42,41 @@ func (u *ConfigUpdater) UpdateApiConfig(ctx context.Context, projectRef string,
return nil
}
fmt.Fprintln(os.Stderr, "Updating API service with config:", string(apiDiff))

if resp, err := u.client.V1UpdatePostgrestServiceConfigWithResponse(ctx, projectRef, c.ToUpdatePostgrestConfigBody()); err != nil {
return errors.Errorf("failed to update API config: %w", err)
} else if resp.JSON200 == nil {
return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body))
}
return nil
}

func (u *ConfigUpdater) UpdateDbConfig(ctx context.Context, projectRef string, c db) error {
dbConfig, err := u.client.V1GetPostgresConfigWithResponse(ctx, projectRef)
if err != nil {
return errors.Errorf("failed to read DB config: %w", err)
} else if dbConfig.JSON200 == nil {
return errors.Errorf("unexpected status %d: %s", dbConfig.StatusCode(), string(dbConfig.Body))
}
dbDiff, err := c.DiffWithRemote(*dbConfig.JSON200)
if err != nil {
return err
} else if len(dbDiff) == 0 {
fmt.Fprintln(os.Stderr, "Remote DB config is up to date.")
return nil
}
fmt.Fprintln(os.Stderr, "Updating DB service with config:", string(dbDiff))
remoteConfig := c.fromRemoteApiConfig(*dbConfig.JSON200)
restartRequired := requireDbRestart(&c, &remoteConfig)
if restartRequired {
fmt.Fprintln(os.Stderr, "DB service updates will require database restart...")
}
updateBody := c.ToUpdatePostgresConfigBody()
updateBody.RestartDatabase = &restartRequired
if resp, err := u.client.V1UpdatePostgresConfigWithResponse(ctx, projectRef, updateBody); err != nil {
return errors.Errorf("failed to update DB config: %w", err)
} else if resp.JSON200 == nil {
return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body))
}
return nil
}

0 comments on commit 939d2d1

Please sign in to comment.