Skip to content

Replication issue diagnostics #304

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .changeset/ninety-points-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@powersync/service-core': minor
'@powersync/service-image': minor
---

Report lack of commits or keepalives as issues in the diagnostics api.
31 changes: 31 additions & 0 deletions packages/service-core/src/api/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,37 @@ export async function getSyncRulesStatus(
})
);

if (live_status && status?.active) {
// Check replication lag for active sync rules.
if (sync_rules.last_checkpoint_ts == null && sync_rules.last_keepalive_ts == null) {
errors.push({
level: 'warning',
message: 'No checkpoint found, cannot calculate replication lag'
});
} else {
const lastTime = Math.max(
sync_rules.last_checkpoint_ts?.getTime() ?? 0,
sync_rules.last_keepalive_ts?.getTime() ?? 0
);
const lagSeconds = Math.round((Date.now() - lastTime) / 1000);
// On idle instances, keepalive messages are only persisted every 60 seconds.
// So we use 2 minutes as a threshold for warnings, and 15 minutes for critical.
// The replication lag metric should give a more granular value, but that is not available directly
// in the API containers used for diagnostics, and this should give a good enough indication.
if (lagSeconds > 15 * 60) {
errors.push({
level: 'fatal',
message: `No replicated commit in more than ${lagSeconds}s`
});
} else if (lagSeconds > 120) {
errors.push({
level: 'warning',
message: `No replicated commit in more than ${lagSeconds}s`
});
}
}
}

return {
content: include_content ? sync_rules.sync_rules_content : undefined,
connections: [
Expand Down
4 changes: 2 additions & 2 deletions packages/service-core/src/replication/AbstractReplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { AbstractReplicationJob } from './AbstractReplicationJob.js';
import { ErrorRateLimiter } from './ErrorRateLimiter.js';
import { ConnectionTestResult } from './ReplicationModule.js';

// 5 minutes
const PING_INTERVAL = 1_000_000_000n * 300n;
// 1 minute
const PING_INTERVAL = 1_000_000_000n * 60n;

export interface CreateJobOptions {
lock: storage.ReplicationLock;
Expand Down