mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Recheck annotations when reading leader object on 409 (#3174)
There are cases when we may send the same PATCH request more than one time to K8s API server and it could happen that the first request actually successfully updated the target and we cancelled while waiting for a response. The second PATCH request in this case will fail due to resource_version mismatch. So far our strategy for update_leader() method was - re-read the object and repeat the request with the new resource_version. However, we can avoid the update by comparing annotations on the read object with annotations that we wanted to set.
This commit is contained in:
@@ -422,14 +422,18 @@ class TestKubernetesEndpoints(BaseTestKubernetes):
|
||||
self.assertFalse(self.k.update_leader(cluster, '123'))
|
||||
mock_patch.side_effect = RetryFailedError('')
|
||||
self.assertRaises(KubernetesError, self.k.update_leader, cluster, '123')
|
||||
mock_patch.side_effect = k8s_client.rest.ApiException(409, '')
|
||||
mock_patch.side_effect = [k8s_client.rest.ApiException(409, ''),
|
||||
k8s_client.rest.ApiException(409, ''), mock_namespaced_kind()]
|
||||
mock_read.return_value.metadata.resource_version = '2'
|
||||
with patch('time.time', Mock(side_effect=[0, 0, 100, 200, 0, 0, 0, 0, 0, 100, 200])):
|
||||
self.assertFalse(self.k.update_leader(cluster, '123'))
|
||||
self.assertFalse(self.k.update_leader(cluster, '123'))
|
||||
mock_patch.side_effect = k8s_client.rest.ApiException(409, '')
|
||||
self.assertFalse(self.k.update_leader(cluster, '123'))
|
||||
mock_patch.side_effect = [k8s_client.rest.ApiException(409, ''), mock_namespaced_kind()]
|
||||
mock_read.return_value.metadata.resource_version = '2'
|
||||
self.assertIsNotNone(self.k._update_leader_with_retry({}, '1', []))
|
||||
self.assertTrue(self.k._update_leader_with_retry({}, '1', []))
|
||||
mock_patch.side_effect = [k8s_client.rest.ApiException(409, ''), mock_namespaced_kind()]
|
||||
self.assertIsNotNone(self.k._update_leader_with_retry({'foo': 'bar'}, '1', []))
|
||||
mock_patch.side_effect = k8s_client.rest.ApiException(409, '')
|
||||
mock_read.side_effect = RetryFailedError('')
|
||||
self.assertRaises(KubernetesError, self.k.update_leader, cluster, '123')
|
||||
|
||||
Reference in New Issue
Block a user