-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from fly-apps/clusterStateMonitor
Replace readOnlyStateMonitor with clusterStateMonitor
- Loading branch information
Showing
6 changed files
with
91 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/fly-apps/postgres-flex/internal/flypg" | ||
) | ||
|
||
func monitorClusterState(ctx context.Context, node *flypg.Node) { | ||
ticker := time.NewTicker(clusterStateMonitorFrequency) | ||
defer ticker.Stop() | ||
for range ticker.C { | ||
if err := clusterStateMonitorTick(ctx, node); err != nil { | ||
log.Printf("clusterStateMonitorTick failed with: %s", err) | ||
} | ||
} | ||
} | ||
|
||
func clusterStateMonitorTick(ctx context.Context, node *flypg.Node) error { | ||
conn, err := node.RepMgr.NewLocalConnection(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to open local connection: %s", err) | ||
} | ||
defer conn.Close(ctx) | ||
|
||
member, err := node.RepMgr.Member(ctx, conn) | ||
if err != nil { | ||
return fmt.Errorf("failed to query local member: %s", err) | ||
} | ||
|
||
// We only need to monitor the primary | ||
if member.Role != flypg.PrimaryRoleName { | ||
return nil | ||
} | ||
|
||
primary, err := node.EvaluateClusterState(ctx, conn) | ||
if errors.Is(err, flypg.ErrZombieDiagnosisUndecided) || errors.Is(err, flypg.ErrZombieDiscovered) { | ||
if err := flypg.Quarantine(ctx, conn, node, primary); err != nil { | ||
return fmt.Errorf("failed to quarantine failed primary: %s", err) | ||
} | ||
return fmt.Errorf("primary has been quarantined: %s", err) | ||
} else if err != nil { | ||
return fmt.Errorf("failed to run zombie diagnosis: %s", err) | ||
} | ||
|
||
// Clear zombie lock if it exists | ||
if flypg.ZombieLockExists() { | ||
log.Println("Clearing zombie lock and enabling read/write") | ||
if err := flypg.RemoveZombieLock(); err != nil { | ||
return fmt.Errorf("failed to remove zombie lock: %s", err) | ||
} | ||
|
||
log.Println("Broadcasting readonly state change") | ||
if err := flypg.BroadcastReadonlyChange(ctx, node, false); err != nil { | ||
log.Printf("errors while disabling readonly: %s", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters