Skip to content

Commit 66e2742

Browse files
authored
Switch application_name back to default value (#575)
* Double check config * Logging * Especially double check when bootstrapping * Check for new value * Revert "Especially double check when bootstrapping" This reverts commit f1ca8db. * Especially double check when bootstrapping * Revert "Check for new value" This reverts commit 4ecb5f5. * Fetch and use the actual postgres cluster name as application_name for. This resolves some issues when using sync replication. * Always fetch application_name when needed * make linter happy * Simplify * logging * Fix typos * Logging
1 parent 39bd425 commit 66e2742

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

controllers/postgres_controller.go

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,14 +1062,28 @@ func (r *PostgresReconciler) checkAndUpdatePatroniReplicationConfig(log logr.Log
10621062
return requeueAfterReconcile, nil
10631063
}
10641064
if instance.Spec.PostgresConnection.SynchronousReplication {
1065-
if resp.SynchronousNodesAdditional == nil || *resp.SynchronousNodesAdditional != instance.Spec.PostgresConnection.ConnectedPostgresID {
1065+
// fetch the sync standby to determine the correct application_name of the instance
1066+
log.V(debugLogLevel).Info("fetching the referenced sync standby")
1067+
var synchronousStandbyApplicationName *string
1068+
s := &pg.Postgres{}
1069+
ns := types.NamespacedName{
1070+
Name: instance.Spec.PostgresConnection.ConnectedPostgresID,
1071+
Namespace: instance.Namespace,
1072+
}
1073+
if err := r.CtrlClient.Get(ctx, ns, s); err != nil {
1074+
r.recorder.Eventf(s, "Warning", "Error", "failed to get referenced sync standby: %v", err)
1075+
synchronousStandbyApplicationName = nil
1076+
} else {
1077+
synchronousStandbyApplicationName = pointer.String(s.ToPeripheralResourceName())
1078+
}
1079+
if resp.SynchronousNodesAdditional == nil || *resp.SynchronousNodesAdditional != *synchronousStandbyApplicationName {
10661080
log.V(debugLogLevel).Info("synchronous_nodes_additional mismatch, updating and requeing", "response", resp)
1067-
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP)
1081+
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, synchronousStandbyApplicationName)
10681082
}
10691083
} else {
10701084
if resp.SynchronousNodesAdditional != nil {
10711085
log.V(debugLogLevel).Info("synchronous_nodes_additional mismatch, updating and requeing", "response", resp)
1072-
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP)
1086+
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, nil)
10731087
}
10741088
}
10751089

@@ -1078,25 +1092,25 @@ func (r *PostgresReconciler) checkAndUpdatePatroniReplicationConfig(log logr.Log
10781092
log.V(debugLogLevel).Info("standby_cluster mismatch, requeing", "response", resp)
10791093
return requeueAfterReconcile, nil
10801094
}
1081-
if resp.StandbyCluster.ApplicationName != instance.ObjectMeta.Name {
1082-
log.V(debugLogLevel).Info("application_name mismatch, updating and requeing", "response", resp)
1083-
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP)
1084-
}
1085-
if resp.SynchronousNodesAdditional != nil {
1086-
log.V(debugLogLevel).Info("synchronous_nodes_additional mismatch, updating and requeing", "response", resp)
1087-
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP)
1088-
}
10891095
if resp.StandbyCluster.CreateReplicaMethods == nil {
10901096
log.V(debugLogLevel).Info("create_replica_methods mismatch, updating and requeing", "response", resp)
1091-
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP)
1097+
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, nil)
10921098
}
10931099
if resp.StandbyCluster.Host != instance.Spec.PostgresConnection.ConnectionIP {
10941100
log.V(debugLogLevel).Info("host mismatch, updating and requeing", "updating", resp)
1095-
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP)
1101+
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, nil)
10961102
}
10971103
if resp.StandbyCluster.Port != int(instance.Spec.PostgresConnection.ConnectionPort) {
10981104
log.V(debugLogLevel).Info("port mismatch, updating and requeing", "updating", resp)
1099-
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP)
1105+
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, nil)
1106+
}
1107+
if resp.StandbyCluster.ApplicationName != instance.ToPeripheralResourceName() {
1108+
log.V(debugLogLevel).Info("application_name mismatch, updating and requeing", "response", resp)
1109+
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, nil)
1110+
}
1111+
if resp.SynchronousNodesAdditional != nil {
1112+
log.V(debugLogLevel).Info("synchronous_nodes_additional mismatch, updating and requeing", "response", resp)
1113+
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, nil)
11001114
}
11011115
}
11021116

@@ -1142,7 +1156,7 @@ func (r *PostgresReconciler) updatePatroniReplicationConfigOnAllPods(log logr.Lo
11421156
for _, pod := range pods.Items {
11431157
pod := pod // pin!
11441158
podIP := pod.Status.PodIP
1145-
if err := r.httpPatchPatroni(log, ctx, instance, podIP); err != nil {
1159+
if err := r.httpPatchPatroni(log, ctx, instance, podIP, nil); err != nil {
11461160
lastErr = err
11471161
log.Info("failed to update pod")
11481162
}
@@ -1155,7 +1169,7 @@ func (r *PostgresReconciler) updatePatroniReplicationConfigOnAllPods(log logr.Lo
11551169
return nil
11561170
}
11571171

1158-
func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Context, instance *pg.Postgres, podIP string) error {
1172+
func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Context, instance *pg.Postgres, podIP string, synchronousStandbyApplicationName *string) error {
11591173
if podIP == "" {
11601174
return errors.New("podIP must not be empty")
11611175
}
@@ -1170,8 +1184,23 @@ func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Conte
11701184
StandbyCluster: nil,
11711185
}
11721186
if instance.Spec.PostgresConnection.SynchronousReplication {
1187+
if synchronousStandbyApplicationName == nil {
1188+
// fetch the sync standby to determine the correct application_name of the instance
1189+
log.V(debugLogLevel).Info("unexpectetly having to fetch the referenced sync standby")
1190+
s := &pg.Postgres{}
1191+
ns := types.NamespacedName{
1192+
Name: instance.Spec.PostgresConnection.ConnectedPostgresID,
1193+
Namespace: instance.Namespace,
1194+
}
1195+
if err := r.CtrlClient.Get(ctx, ns, s); err != nil {
1196+
r.recorder.Eventf(s, "Warning", "Error", "failed to get referenced sync standby: %v", err)
1197+
synchronousStandbyApplicationName = nil
1198+
} else {
1199+
synchronousStandbyApplicationName = pointer.String(s.ToPeripheralResourceName())
1200+
}
1201+
}
11731202
// enable sync replication
1174-
request.SynchronousNodesAdditional = pointer.String(instance.Spec.PostgresConnection.ConnectedPostgresID)
1203+
request.SynchronousNodesAdditional = synchronousStandbyApplicationName
11751204
} else {
11761205
// disable sync replication
11771206
request.SynchronousNodesAdditional = nil
@@ -1183,7 +1212,7 @@ func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Conte
11831212
CreateReplicaMethods: []string{"basebackup_fast_xlog"},
11841213
Host: instance.Spec.PostgresConnection.ConnectionIP,
11851214
Port: int(instance.Spec.PostgresConnection.ConnectionPort),
1186-
ApplicationName: instance.ObjectMeta.Name,
1215+
ApplicationName: instance.ToPeripheralResourceName(),
11871216
},
11881217
SynchronousNodesAdditional: nil,
11891218
}
@@ -1212,6 +1241,11 @@ func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Conte
12121241
}
12131242
defer resp.Body.Close()
12141243

1244+
// fake error when standbyApplicationName is required but not provided
1245+
if instance.IsReplicationPrimary() && instance.Spec.PostgresConnection.SynchronousReplication && synchronousStandbyApplicationName == nil {
1246+
return fmt.Errorf("missing application_name of synchronous standby, disable synchronous replication")
1247+
}
1248+
12151249
return nil
12161250
}
12171251

0 commit comments

Comments
 (0)