-
Notifications
You must be signed in to change notification settings - Fork 14
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
[feature] Support replication lag checking #4
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4 +/- ##
==========================================
- Coverage 85.25% 82.90% -2.35%
==========================================
Files 7 7
Lines 217 234 +17
==========================================
+ Hits 185 194 +9
- Misses 27 35 +8
Partials 5 5
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
return fmt.Errorf("cannot check node replication lag: %w", err) | ||
} | ||
if lag < maxLag { | ||
return fmt.Errorf("replication lag is too big: %s", lag) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need typed errors since tracer.NodeDead can return error for both 'cant check' and 'too much lag'.
The question is do we need to create an actualy type for replication lag? User might want to know the exact lag. Or he might not. Sentinels are a bit easier to work with but provide less info.
@@ -53,6 +53,9 @@ type Cluster struct { | |||
updateTimeout time.Duration | |||
checker NodeChecker | |||
picker NodePicker | |||
// Replication lag configuration | |||
lagChecker ReplicationLagChecker | |||
maxLagValue time.Duration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maxAllowedLag is more descriptive IMO.
Node Node | ||
Node Node | ||
|
||
Primary bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like adding more transitory values to node. This provokes accessing them after check itself when these values might be invalid.
Can you please add a comment before these values making it clear they are transitory and should not be used after the check itself? Also making them private seems like a good idea.
|
||
// PostgreSQLReplicationLag returns replication lag value for PostgreSQL replica node. | ||
func PostgreSQLReplicationLag(ctx context.Context, db *sql.DB) (time.Duration, error) { | ||
return ReplicationLag(ctx, db, "SELECT NOW() - pg_last_xact_replay_timestamp()") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately on low-activity cluster this would lead to marking all replicas as lagging.
We should probably consider using pg_stat_replication on primary with filter on reply_time
.
We should also consider a corner case with dead primary.
Any thoughts? |
Sounds good. The only possible problem I see is keeping state. That might be a bit more complicated than it sounds. But gotta try first. |
We should probably measure lag only from |
Also |
Hmm, should we?
|
This commit adds ability to filter replica nodes by replication lag. User can provide
ReplicationLagChecker
function andMaxreplicationLag
value as options to cluster constructor to check nodes and remove any replica which is too slow.Example: