Skip to content

Commit 7968035

Browse files
author
Jeff McCormick
committed
update pgo failover query info to include node name and ready status
1 parent 3074ac0 commit 7968035

File tree

3 files changed

+120
-7
lines changed

3 files changed

+120
-7
lines changed

apiserver/failoverservice/failoverimpl.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func QueryFailover(name string) msgs.QueryFailoverResponse {
141141
target := msgs.FailoverTargetSpec{}
142142
target.Name = dep.Name
143143
//get the pod status
144-
target.ReadyStatus = getPodStatus(dep.Name)
144+
target.ReadyStatus, target.Node = getPodStatus(dep.Name)
145145
//get the rep status
146146
resp.Targets = append(resp.Targets, target)
147147
}
@@ -171,25 +171,26 @@ func validateDeploymentName(deployName string) (*v1beta1.Deployment, error) {
171171

172172
}
173173

174-
func getPodStatus(deployName string) string {
174+
func getPodStatus(deployName string) (string, string) {
175175

176176
//get pods with replica-name=deployName
177177
pods, err := kubeapi.GetPods(apiserver.Clientset, util.LABEL_REPLICA_NAME+"="+deployName, apiserver.Namespace)
178178
if err != nil {
179-
return "error"
179+
return "error", "error"
180180
}
181181

182182
p := pods.Items[0]
183+
nodeName := p.Spec.NodeName
183184
for _, c := range p.Status.ContainerStatuses {
184185
if c.Name == "database" {
185186
if c.Ready {
186-
return "Ready"
187+
return "Ready", nodeName
187188
} else {
188-
return "Not Ready"
189+
return "Not Ready", nodeName
189190
}
190191
}
191192
}
192193

193-
return "error2"
194+
return "error2", nodeName
194195

195196
}

pgo/cmd/failover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func queryFailover(args []string) {
162162
if len(response.Targets) > 0 {
163163
fmt.Println("Failover targets include:")
164164
for i := 0; i < len(response.Targets); i++ {
165-
fmt.Println("\t" + response.Targets[i].Name + " (" + response.Targets[i].ReadyStatus + ")")
165+
fmt.Println("\t" + response.Targets[i].Name + " (" + response.Targets[i].ReadyStatus + ") (" + response.Targets[i].Node + ")")
166166
}
167167
}
168168
for k := range response.Results {

util/replica.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2016-2018 Crunchy Data Solutions, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package util
17+
18+
import (
19+
"database/sql"
20+
"fmt"
21+
22+
log "github.com/Sirupsen/logrus"
23+
_ "github.com/lib/pq"
24+
)
25+
26+
const (
27+
replInfoQueryFormat = "SELECT %s(%s(), '0/0')::bigint, %s(%s(), '0/0')::bigint"
28+
29+
recvV9 = "pg_last_xlog_receive_location"
30+
replayV9 = "pg_last_xlog_replay_location"
31+
locationDiffV9 = "pg_xlog_location_diff"
32+
33+
recvV10 = "pg_last_wal_receive_lsn"
34+
replayV10 = "pg_last_wal_replay_lsn"
35+
locationDiffV10 = "pg_wal_lsn_diff"
36+
)
37+
38+
type Replica struct {
39+
Name string
40+
IP string
41+
Status *ReplicationInfo
42+
}
43+
44+
type ReplicationInfo struct {
45+
ReceiveLocation uint64
46+
ReplayLocation uint64
47+
}
48+
49+
func GetReplicationInfo(target string) (*ReplicationInfo, error) {
50+
conn, err := sql.Open("postgres", target)
51+
52+
if err != nil {
53+
log.Errorf("Could not connect to: %s", target)
54+
return nil, err
55+
}
56+
57+
defer conn.Close()
58+
59+
// Get PG version
60+
var version int
61+
62+
rows, err := conn.Query("SELECT current_setting('server_version_num')")
63+
64+
if err != nil {
65+
log.Errorf("Could not perform query for version: %s", target)
66+
return nil, err
67+
}
68+
69+
defer rows.Close()
70+
71+
for rows.Next() {
72+
if err := rows.Scan(&version); err != nil {
73+
return nil, err
74+
}
75+
}
76+
77+
// Get replication info
78+
var replicationInfoQuery string
79+
var recvLocation uint64
80+
var replayLocation uint64
81+
82+
if version < 100000 {
83+
replicationInfoQuery = fmt.Sprintf(
84+
replInfoQueryFormat,
85+
locationDiffV9, recvV9,
86+
locationDiffV9, replayV9,
87+
)
88+
} else {
89+
replicationInfoQuery = fmt.Sprintf(
90+
replInfoQueryFormat,
91+
locationDiffV10, recvV10,
92+
locationDiffV10, replayV10,
93+
)
94+
}
95+
96+
rows, err = conn.Query(replicationInfoQuery)
97+
98+
if err != nil {
99+
log.Errorf("Could not perform replication info query: %s", target)
100+
return nil, err
101+
}
102+
103+
defer rows.Close()
104+
105+
for rows.Next() {
106+
if err := rows.Scan(&recvLocation, &replayLocation); err != nil {
107+
return nil, err
108+
}
109+
}
110+
111+
return &ReplicationInfo{recvLocation, replayLocation}, nil
112+
}

0 commit comments

Comments
 (0)