feat: add proxy topology mode for SPQR-compatible routing - #33
Conversation
|
@Denchick Thanks for the PR. A few things to address before merge. 1. The new tests never run
Suggested wiring: "test:unit": "jest -c ./jest/unit.config.js",
"test": "npm run test:prepare && npm run test:unit && npm run test:run"2. The "no topology warnings" assertion is vacuous
expect(loggedErrorMessages(logger)).not.toEqual(
expect.arrayContaining([
'Multiple primary connections detected, something is wrong',
'No alive replica available, using master for read',
]),
);
Rather than matching on English sentences, please attach a stable
if (primaryConnections.length > 1) {
const error = new PDError('Multiple primary connections detected, something is wrong');
error.code = 'ERR_DB_MULTIPLE_PRIMARIES';
this.logger.error({
message: error.message,
error,
data: {primaryConnections: primaryConnections.map((c) => c.host)},
});
}
if (this.connections.length > 1) {
const error = new PDError('No alive replica available, using master for read');
error.code = 'ERR_DB_NO_ALIVE_REPLICA';
this.logger.error({message: error.message, error});
}Then in the test: const loggedErrorCodes = (logger) => logger.error.mock.calls.map(([, error]) => error.code);
// ...
const codes = loggedErrorCodes(logger);
expect(codes).not.toContain('ERR_DB_MULTIPLE_PRIMARIES');
expect(codes).not.toContain('ERR_DB_NO_ALIVE_REPLICA');3. Minor:
|
Got it, thanks! For now, let's add positive assertions for both messages: expect(errorMessages).toContain('Multiple primary connections detected, something is wrong');
expect(errorMessages).toContain('No alive replica available, using master for read');This way we explicitly verify the expected warnings are triggered. We can swap to error codes later in a follow-up PR. |
Problem
PostgresKit currently assumes that every connection string represents an individual node in a PostgreSQL primary/replica cluster.
To determine each node's role, it periodically executes:
SELECT pg_is_in_recovery();A
falseresult identifies a primary, whiletrueidentifies a replica.This assumption does not work for SPQR. The supplied endpoints represent equivalent SPQR router instances rather than individual PostgreSQL nodes. A router can accept both read and write queries and forward them to the appropriate backend.
SPQR routers report themselves as primary for
pg_is_in_recovery(). Therefore, when several router endpoints are configured, PostgresKit classifies all of them as primaries and may report:or:
Having multiple endpoints classified as “masters” is normal for an SPQR deployment: these are equivalent router instances, not multiple writable PostgreSQL primary nodes. The warnings are produced by PostgresKit because its primary/replica topology model does not match the actual SPQR topology.
Suppressing status logs does not solve the problem because it only hides the warnings while leaving the incorrect routing model in place.
Solution
This PR adds an optional dispatcher topology mode:
In
proxymode, PostgresKit:SELECT 1instead ofpg_is_in_recovery()for health checks;db.primaryanddb.replica;Backward compatibility
primary-replicaremains the default topology mode. WhentopologyModeis omitted, the existing health checks, routing behavior, warnings, and errors remain unchanged.Testing
Added tests covering:
db.primaryanddb.replica;