Skip to content

Commit 97be5ee

Browse files
authored
use uint64 for replication lag from Patroni's member endpoint (zalando#1893)
* use int64 for replication lag from Patroni's member endpoint
1 parent 5dc9631 commit 97be5ee

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

pkg/cluster/pod.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,13 @@ func (c *Cluster) getSwitchoverCandidate(master *v1.Pod) (spec.NamespacedName, e
522522
// if sync_standby replicas were found assume synchronous_mode is enabled and ignore other candidates list
523523
if len(syncCandidates) > 0 {
524524
sort.Slice(syncCandidates, func(i, j int) bool {
525-
return util.IntFromIntStr(syncCandidates[i].Lag) < util.IntFromIntStr(syncCandidates[j].Lag)
525+
return syncCandidates[i].Lag < syncCandidates[j].Lag
526526
})
527527
return spec.NamespacedName{Namespace: master.Namespace, Name: syncCandidates[0].Name}, nil
528528
}
529529
if len(candidates) > 0 {
530530
sort.Slice(candidates, func(i, j int) bool {
531-
return util.IntFromIntStr(candidates[i].Lag) < util.IntFromIntStr(candidates[j].Lag)
531+
return candidates[i].Lag < candidates[j].Lag
532532
})
533533
return spec.NamespacedName{Namespace: master.Namespace, Name: candidates[0].Name}, nil
534534
}

pkg/util/patroni/patroni.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
8+
"math"
89
"net"
910
"net/http"
1011
"strconv"
@@ -16,7 +17,6 @@ import (
1617
"github.com/sirupsen/logrus"
1718
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
1819
v1 "k8s.io/api/core/v1"
19-
"k8s.io/apimachinery/pkg/util/intstr"
2020
)
2121

2222
const (
@@ -185,11 +185,27 @@ type ClusterMembers struct {
185185

186186
// ClusterMember cluster member data from Patroni API
187187
type ClusterMember struct {
188-
Name string `json:"name"`
189-
Role string `json:"role"`
190-
State string `json:"state"`
191-
Timeline int `json:"timeline"`
192-
Lag intstr.IntOrString `json:"lag,omitempty"`
188+
Name string `json:"name"`
189+
Role string `json:"role"`
190+
State string `json:"state"`
191+
Timeline int `json:"timeline"`
192+
Lag ReplicationLag `json:"lag,omitempty"`
193+
}
194+
195+
type ReplicationLag uint64
196+
197+
// UnmarshalJSON converts member lag (can be int or string) into uint64
198+
func (rl *ReplicationLag) UnmarshalJSON(data []byte) error {
199+
var lagUInt64 uint64
200+
if data[0] == '"' {
201+
*rl = math.MaxUint64
202+
return nil
203+
}
204+
if err := json.Unmarshal(data, &lagUInt64); err != nil {
205+
return err
206+
}
207+
*rl = ReplicationLag(lagUInt64)
208+
return nil
193209
}
194210

195211
// MemberDataPatroni child element

pkg/util/patroni/patroni_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io/ioutil"
8+
"math"
89
"net/http"
910
"reflect"
1011
"testing"
@@ -15,7 +16,6 @@ import (
1516

1617
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
1718
v1 "k8s.io/api/core/v1"
18-
"k8s.io/apimachinery/pkg/util/intstr"
1919
)
2020

2121
var logger = logrus.New().WithField("test", "patroni")
@@ -101,16 +101,27 @@ func TestGetClusterMembers(t *testing.T) {
101101
Role: "sync_standby",
102102
State: "running",
103103
Timeline: 1,
104-
Lag: intstr.IntOrString{IntVal: 0},
104+
Lag: 0,
105105
}, {
106106
Name: "acid-test-cluster-2",
107107
Role: "replica",
108108
State: "running",
109109
Timeline: 1,
110-
Lag: intstr.IntOrString{Type: 1, StrVal: "unknown"},
110+
Lag: math.MaxUint64,
111+
}, {
112+
Name: "acid-test-cluster-3",
113+
Role: "replica",
114+
State: "running",
115+
Timeline: 1,
116+
Lag: 3000000000,
111117
}}
112118

113-
json := `{"members": [{"name": "acid-test-cluster-0", "role": "leader", "state": "running", "api_url": "http://192.168.100.1:8008/patroni", "host": "192.168.100.1", "port": 5432, "timeline": 1}, {"name": "acid-test-cluster-1", "role": "sync_standby", "state": "running", "api_url": "http://192.168.100.2:8008/patroni", "host": "192.168.100.2", "port": 5432, "timeline": 1, "lag": 0}, {"name": "acid-test-cluster-2", "role": "replica", "state": "running", "api_url": "http://192.168.100.3:8008/patroni", "host": "192.168.100.3", "port": 5432, "timeline": 1, "lag": "unknown"}]}`
119+
json := `{"members": [
120+
{"name": "acid-test-cluster-0", "role": "leader", "state": "running", "api_url": "http://192.168.100.1:8008/patroni", "host": "192.168.100.1", "port": 5432, "timeline": 1},
121+
{"name": "acid-test-cluster-1", "role": "sync_standby", "state": "running", "api_url": "http://192.168.100.2:8008/patroni", "host": "192.168.100.2", "port": 5432, "timeline": 1, "lag": 0},
122+
{"name": "acid-test-cluster-2", "role": "replica", "state": "running", "api_url": "http://192.168.100.3:8008/patroni", "host": "192.168.100.3", "port": 5432, "timeline": 1, "lag": "unknown"},
123+
{"name": "acid-test-cluster-3", "role": "replica", "state": "running", "api_url": "http://192.168.100.3:8008/patroni", "host": "192.168.100.3", "port": 5432, "timeline": 1, "lag": 3000000000}
124+
]}`
114125
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
115126

116127
response := http.Response{

pkg/util/util.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/base64"
99
"encoding/hex"
1010
"fmt"
11-
"math"
1211
"math/big"
1312
"math/rand"
1413
"reflect"
@@ -338,14 +337,6 @@ func Bool2Int(flag bool) int {
338337
return 0
339338
}
340339

341-
// Get int from IntOrString and return max int if string
342-
func IntFromIntStr(intOrStr intstr.IntOrString) int {
343-
if intOrStr.Type == 1 {
344-
return math.MaxInt
345-
}
346-
return intOrStr.IntValue()
347-
}
348-
349340
// MaxInt32 : Return maximum of two integers provided via pointers. If one value
350341
// is not defined, return the other one. If both are not defined, result is also
351342
// undefined, caller needs to check for that.

0 commit comments

Comments
 (0)