Repository navigation
Description
Hi,
It appears that, despite running a cluster having 2 synchronous replicas, a sequence of transitory events (i.e. no permanent disk errors or such) can cause data loss, and I wonder if this is intentional.
tl;dr; version: if nodes fail and then recover in a certain order, an out of date replica can assume the role of primary. Every transaction committed since this replica went offline, and this can be a long time since the remaining two nodes would have continued working, is then lost.
This is a condition Patroni explicitly guards against:
When in synchronous mode Patroni maintains synchronization state in the DCS, containing the latest primary and current synchronous standby databases. This state is updated with strict ordering constraints to ensure the following invariants:
A node must be marked as the latest leader whenever it can accept write transactions. Patroni crashing or PostgreSQL not shutting down can cause violations of this invariant.
A node must be set as the synchronous standby in PostgreSQL as long as it is published as the synchronous standby.
A node that is not the leader or current synchronous standby is not allowed to promote itself automatically.
To reproduce:
A database:
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: test
spec:
instances: 3
minSyncReplicas: 1
maxSyncReplicas: 1
replicationSlots:
highAvailability:
enabled: true
bootstrap:
initdb:
database: test
owner: app
postInitSQL:
- CREATE ROLE dennis SUPERUSER LOGIN PASSWORD 'dennis'
primaryUpdateStrategy: unsupervised
storage:
storageClass: openebs-zfspv
size: 1Gi
affinity:
podAntiAffinityType: required
which, with a few rows of data, results in:
Cluster Summary
Name: test
Namespace: default
System ID: 7209341545286758438
PostgreSQL Image: ghcr.io/cloudnative-pg/postgresql:15.2
Primary instance: test-3
Status: Cluster in healthy state
Instances: 3
Ready instances: 3
Current Write LSN: 0/1100B0F8 (Timeline: 7 - WAL File: 000000070000000000000011)
Certificates Status
Certificate Name Expiration Date Days Left Until Expiration
---------------- --------------- --------------------------
test-ca 2023-06-09 17:22:08 +0000 UTC 89.94
test-replication 2023-06-09 17:22:08 +0000 UTC 89.94
test-server 2023-06-09 17:22:08 +0000 UTC 89.94
Continuous Backup status
Not configured
Streaming Replication status
Replication Slots Enabled
Name Sent LSN Write LSN Flush LSN Replay LSN Write Lag Flush Lag Replay Lag State Sync State Sync Priority Replication Slot
---- -------- --------- --------- ---------- --------- --------- ---------- ----- ---------- ------------- ----------------
test-1 0/1100B0F8 0/1100B0F8 0/1100B0F8 0/1100B0F8 00:00:00 00:00:00 00:00:00 streaming quorum 1 active
test-2 0/1100B0F8 0/1100B0F8 0/1100B0F8 0/1100B0F8 00:00:00 00:00:00 00:00:00 streaming quorum 1 active
Unmanaged Replication Slot Status
No unmanaged replication slots found
Instances status
Name Database Size Current LSN Replication role Status QoS Manager Version Node
---- ------------- ----------- ---------------- ------ --- --------------- ----
test-3 29 MB 0/1100B0F8 Primary OK BestEffort 1.19.0 node-1
test-1 29 MB 0/1100B0F8 Standby (sync) OK BestEffort 1.19.0 node-3
test-2 29 MB 0/1100B0F8 Standby (sync) OK BestEffort 1.19.0 node-2
To simulate failures I set the ZFS dataset to readonly on the corresponding node, something like:
zfs set readonly=on tank/data/local/pvc-1383b82a-9a5b-4c68-b7b4-e631f2afe1e4
test=# insert into a values('test-3 primary, test-1 sync replica, test-2 sync replica');
INSERT 0 1
select * from a;
v | stamp
----------------------------------------------------------+-------------------------------
test-3 primary, test-1 sync replica, test-2 sync replica | 2023-03-11 18:49:54.830092+00
(1 row)
Fail test-2, then:
Instances status
Name Database Size Current LSN Replication role Status QoS Manager Version Node
---- ------------- ----------- ---------------- ------ --- --------------- ----
test-3 29 MB 0/1100B0F8 Primary OK BestEffort 1.19.0 node-1
test-1 29 MB 0/1100B0F8 Standby (sync) OK BestEffort 1.19.0 node-3
test-2 - - - pod not available BestEffort - node-2
test=# insert into a values('test-3 primary, test-1 sync replica, test-2 gone');
INSERT 0 1
Fail test-1, then:
test=# insert into a values('test-3 primary, test-1 gone, test-2 gone');
INSERT 0 1
(This works because CNPG will allow a standalone primary, see bottom notes. This transaction is thus only ever committed on test-3)
And finally fail test-3:
Instances status
Name Database Size Current LSN Replication role Status QoS Manager Version Node
---- ------------- ----------- ---------------- ------ --- --------------- ----
test-1 - - - pod not available BestEffort - node-3
test-2 - - - pod not available BestEffort - node-2
test-3 - - - pod not available BestEffort - node-1
Now let's assume our provider had a bad day, and slowly the nodes are getting back up, first test-2:
Instances status
Name Database Size Current LSN Replication role Status QoS Manager Version Node
---- ------------- ----------- ---------------- ------ --- --------------- ----
test-2 29 MB 0/1100D7B0 Primary OK BestEffort 1.19.0 node-2
test-1 - - - pod not available BestEffort - node-3
test-3 - - - pod not available BestEffort - node-1
test=# select * from a;
v | stamp
----------------------------------------------------------+-------------------------------
test-3 primary, test-1 sync replica, test-2 sync replica | 2023-03-11 18:49:54.830092+00
(1 row)
Because test-2 has assumed the role of primary, we have lost the last 2 INSERTs. Recovering test-1 and test-3:
Instances status
Name Database Size Current LSN Replication role Status QoS Manager Version Node
---- ------------- ----------- ---------------- ------ --- --------------- ----
test-2 29 MB 0/12000110 Primary OK BestEffort 1.19.0 node-2
test-1 29 MB 0/120006A0 Standby (file based) OK BestEffort 1.19.0 node-3
test-3 29 MB 0/12000110 Standby (sync) OK BestEffort 1.19.0 node-1
works, but those transactions are lost.
There are 2 parts to this:
-
CNPG loosens synchronous constraints when no replicas are available. This may/may not be appropriate for some. In my case I would agree this is reasonable, the only permanent data loss scenario in this case would be if the disks of the primary are permanently lost, but I consider this unlikely to happen simultaneously to either provider issues or disk issues on both other nodes (i.e. if nodes drop out like this it's more likely power or network and transient).
-
CNPG allows a node which was not previously a primary or a synchronous (up to date!) standby to become primary, with potential data loss. For my case I do not want to this to happen or be possible, if this happens the (IMHO) only acceptable automatic recovery is waiting for the original primary, or a fully up to date synchronous standby (which due to 1. and the commit quorum is difficult to guarantee), to recover. Anything else should require manual intervention.
I know risk tolerance is subjective, and "we prioritize availability over all else" is a valid standpoint, am just curious if I am missing something. IMHO if you choose to use synchronous replication it's because you care very much about durability of committed transactions.
Activity
Dennis4b commentedon Mar 14, 2023
So, new to K8s operators but the source code is quite readable. There is a lot of psql experience behind the cnpg team, so perhaps my opinions on administering psql are wrong/outdated, but I'd like to learn, and would appreciate corrections/comments on the following. I do really like how easy cnpg is to use and I find the arguments for using it leveraging k8s very compelling, however:
Synchronous replication is done by simply shoving all
nreplicas into a synchronous quorumANY m (1..n)wheremreplicas have to acknowledge writes. The implementation of this is rather straightforward, there is no need to track synchronous versus asynchronous replicas, all replicas can go in in the list and depending onmthere will or will not be synchronous replication. But, what is the point of this feature, without further guarantees? (see below)The reconciliation loop creates a sorted list of instances, first the primary, then the replicas in order of most-up-to-date. All instances not available are filtered from this list. If the first item in this list is then not the current (wanted) primary, then the first item in the list gets promoted to primary. There are, as far as I can tell, no constraints on this. Meaning, if you have a 3 month old stale replica that happens to ever show up on top of the list (see my initial comment) then cnpg will happily throw away all writes that happened in those 3 months. This ensures availability, and you could argue there would be backups of the "actual" primary timeline, but at this point you have a forked timeline and very confused and angry users.
Even for asynchronous replicas this possibility would make me very nervous. When it comes to the synchronous case you'd never want this to happen. You want to wait for the primary to come back online, but since this could take time, or may not even be possible, you can promote a known synchronous standby (I mean, that is what synchronous replication was designed for).
If you are using quorum writes, then you can only fail over to a synchronous standby if all standbys in the quorum are available, since only then can you compare LSNs to see which one is the most up to date (the one who acknowledged the very last commit). The cluster can only resume if either the primary comes back or all synchronous standbys are available. If you also lose a standby, you can not safely resume without risking data loss.
Having a designated synchronous standby (
FIRST m (1..n)) it is enough if it, or the primary, are available. Additionally, a dedicated synchronous standby allows a read-only endpoint that is fully up to date. Disadvantage is that quorum writes smooth out any hickups in the replicas or networks. So it is a tradeoff.I want to mention how Patroni handles this, not to argue it is better or not, but to point out that these issues are worth considering. Patroni has constraints on promoting replicas while using synchronous replication, and there are constraints on the maximum allowed data loss if promoting a replica (so you can say no more than 5 minutes out of date or else wait for an administrator - of course if a more appropriate replica, or the original primary, comes back you can still resume the cluster so it would still automatically recover from transient problems).
I believe the least disruptive change (and quickest, that I would probably be able to work on if cnpg would consider including such a change) would be for the failover in case of synchronous replication to only promote a replica if it can be determined to contain the last commit of the primary. This means all replicas that were available at the moment the primary disappeared must be available for inspection (who has the latest commit). If this can not be determined, or if there were no synchronous standbys, there will be no failover until either a) the primary comes back, or b) the required replica(s) come back, or c) the DBA manually promotes a replica.
Ideally, cnpg could maintain a pool of
n+designated synchronous replicas which can form their own non-stale read-only endpoint. Here you lose the smoothing of writes that a quorum offers, but it also simplifies failover (either one the designated replicas is available or it isn't). Note that this is what Patroni does.fkaempfer commentedon May 5, 2023
Question: This page (https://cloudnative-pg.io/documentation/1.20/operator_capability_levels/#zero-data-loss-clusters-through-synchronous-replication) has the formula
1 <= minSyncReplicas <= q <= maxSyncReplicas <= readyReplicasDoes this mean you can achieve zero data loss in a cluster of 3, where the quorum should be 2,
minSyncReplicasneeds to be at least 1 and maxSyncReplicas must be at least 2?sxd commentedon May 23, 2023
Hello @Dennis4b,
This looks more like a discussion than an issue yet, since there's a lot of discussion to clean before doing anything.
I'm turning this into a discussion, there's a good an really valid point here.
Regards!