forked from bitnine-oss/postgres-xl-ha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres-xl-data
executable file
·264 lines (234 loc) · 7.26 KB
/
postgres-xl-data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/sh
#
# Resource Agent for managing Postgres-XL datanode resources.
#
# License: PostgreSQL License.
# (c) 2017 Bitnine Global, Inc.
: ${OCF_FUNCTIONS_DIR=$OCF_ROOT/lib/heartbeat}
. $OCF_FUNCTIONS_DIR/ocf-shellfuncs
OCF_RESKEY_user_default=postgres
OCF_RESKEY_bindir_default=/usr/local/pgsql/bin
OCF_RESKEY_host_default=$(hostname)
OCF_RESKEY_port_default=5432
: ${OCF_RESKEY_user=$OCF_RESKEY_user_default}
: ${OCF_RESKEY_bindir=$OCF_RESKEY_bindir_default}
: ${OCF_RESKEY_host=$OCF_RESKEY_host_default}
: ${OCF_RESKEY_port=$OCF_RESKEY_port_default}
pg_ctl=$OCF_RESKEY_bindir/pg_ctl
pg_isready=$OCF_RESKEY_bindir/pg_isready
psql=$OCF_RESKEY_bindir/psql
runasowner() {
local loglevel=''
case $1 in
-info|-warn)
loglevel=$1
shift
;;
esac
ocf_run $loglevel su $OCF_RESKEY_user -c "cd $OCF_RESKEY_datadir; $*"
}
meta_data() {
cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="postgres-xl-data">
<version>1.0</version>
<longdesc lang="en">
Resource agent for a datanode in a Postgres-XL cluster. It manages a datanode as an HA resource.
</longdesc>
<shortdesc lang="en">Resource agent for a datanode in a Postgres-XL cluster</shortdesc>
<parameters>
<parameter name="user">
<longdesc lang="en">
The user who starts datanode.
</longdesc>
<shortdesc lang="en">The user who starts datanode</shortdesc>
<content type="string" default="$OCF_RESKEY_user_default" />
</parameter>
<parameter name="bindir">
<longdesc lang="en">
Path to the directory storing the Postgres-XL binaries. The resource agent uses pg_ctl and pg_isready.
</longdesc>
<shortdesc lang="en">Path to the Postgres-XL binaries</shortdesc>
<content type="string" default="$OCF_RESKEY_bindir_default" />
</parameter>
<parameter name="datadir" required="true">
<longdesc lang="en">
Path to the datanode data directory.
</longdesc>
<shortdesc lang="en">Path to the datanode data directory</shortdesc>
<content type="string" />
</parameter>
<parameter name="host">
<longdesc lang="en">
Host IP address or unix socket directory the instance is listening on.
</longdesc>
<shortdesc lang="en">Instance IP or unix socket directory</shortdesc>
<content type="string" default="$OCF_RESKEY_host_default" />
</parameter>
<parameter name="port">
<longdesc lang="en">
Port the instance is listening on.
</longdesc>
<shortdesc lang="en">Instance port</shortdesc>
<content type="integer" default="$OCF_RESKEY_port_default" />
</parameter>
<parameter name="nodename" required="true">
<longdesc lang="en">
The name of datanode.
</longdesc>
<shortdesc lang="en">The name of datanode</shortdesc>
<content type="string" />
</parameter>
</parameters>
<actions>
<action name="start" timeout="60" />
<action name="stop" timeout="60" />
<action name="monitor" timeout="10" interval="15" depth="0" />
<action name="meta-data" timeout="5" />
<action name="promote" timeout="30" />
<action name="demote" timeout="120" />
</actions>
</resource-agent>
EOF
}
ocf_start() {
local is_slave
local status
test -f $OCF_RESKEY_datadir/recovery.conf
is_slave=$?
ocf_monitor
status=$?
if [ $is_slave -eq 0 ]; then
if [ $status -eq $OCF_SUCCESS ]; then
ocf_log info 'Datanode is already running (slave)'
return $OCF_SUCCESS
fi
else
if [ $status -eq $OCF_RUNNING_MASTER ]; then
ocf_log info 'Datanode is already running (master)'
return $OCF_SUCCESS
fi
fi
runasowner "$pg_ctl start -Z datanode -w -D $OCF_RESKEY_datadir -l datanode.logfile"
if [ $? -eq 0 ]; then
ocf_log info 'Datanode is started'
return $OCF_SUCCESS
else
ocf_exit_reason "Can't start Datanode"
return $OCF_ERR_GENERIC
fi
}
ocf_stop() {
ocf_monitor -info
if [ $? -eq $OCF_NOT_RUNNING ]; then
ocf_log info 'Datanode already stopped'
return $OCF_SUCCESS
fi
runasowner "$pg_ctl stop -w -D $OCF_RESKEY_datadir"
if [ $? -eq 0 ]; then
return $OCF_SUCCESS
else
ocf_exit_reason 'Datanode failed to stop'
return $OCF_ERR_GENERIC
fi
}
ocf_monitor() {
local loglevel=$1
if ! runasowner "test -w $OCF_RESKEY_datadir"; then
ocf_log info "$OCF_RESKEY_datadir does not exist"
return $OCF_NOT_RUNNING
fi
runasowner $loglevel "$pg_isready -h $OCF_RESKEY_host -p $OCF_RESKEY_port"
case $? in
0) # accepting connections
crm_master -l reboot -v 1000
return $OCF_RUNNING_MASTER
;;
1) # rejecting connections (slave)
crm_master -l reboot -v 0
return $OCF_SUCCESS
;;
2) # no response
crm_master -l reboot -D
return $OCF_NOT_RUNNING
;;
*)
exit $OCF_ERR_GENERIC
esac
}
ocf_promote() {
ocf_monitor
case $? in
$OCF_RUNNING_MASTER)
ocf_log info "$OCF_RESKEY_nodename is already running as a master"
return $OCF_SUCCESS
;;
$OCF_SUCCESS)
ocf_log debug "$OCF_RESKEY_nodename is running as a slave"
;;
$OCF_NOT_RUNNING)
ocf_log err "$OCF_RESKEY_nodename is currently not running"
return $OCF_ERR_GENERIC
esac
runasowner "$pg_ctl promote -Z datanode -w -D $OCF_RESKEY_datadir"
if [ $? -ne 0 ]; then
return $OCF_ERR_GENERIC
fi
while [ ocf_monitor -ne $OCF_RUNNING_MASTER ]; do
ocf_log info 'waiting for the promote to complete'
sleep 1;
done
while true; do
ocf_monitor
if [ $? -eq $OCF_RUNNING_MASTER ]; then
break
fi
ocf_log debug 'waiting for the promote to complete'
sleep 1;
done
output=$(su $OCF_RESKEY_user -c "$psql -h $OCF_RESKEY_host -p $OCF_RESKEY_port -Atc \"SELECT node_name, node_host, node_port FROM pgxc_node\"")
for node in $output; do
IFS='|' read -a info <<< "$node"
if [ "${info[0]}" = "$OCF_RESKEY_nodename" ]; then
ocf_log info "ALTER NODE $OCF_RESKEY_nodename"
runasowner "$psql -h $OCF_RESKEY_host -p $OCF_RESKEY_port -c \"ALTER NODE $OCF_RESKEY_nodename WITH (HOST='$OCF_RESKEY_host', PORT=$OCF_RESKEY_port); SELECT pgxc_pool_reload()\""
else
ocf_log info "EXECUTE DIRECT ON ($nodename)"
runasowner "$psql -h ${info[1]} -p ${info[2]} -c \"ALTER NODE $OCF_RESKEY_nodename WITH (HOST='$OCF_RESKEY_host', PORT=$OCF_RESKEY_port); SELECT pgxc_pool_reload()\""
fi
done
crm_master -l reboot -v 1000
ocf_log info "$OCF_RESKEY_datadir: promoted to master"
return $OCF_SUCCESS
}
ocf_demote() {
crm_master -l reboot -v 0
return $OCF_SUCCESS
}
case $__OCF_ACTION in
meta-data)
meta_data
exit $OCF_SUCCESS
;;
start)
ocf_start
exit $?
;;
stop)
ocf_stop
exit $?
;;
monitor)
ocf_monitor
exit $?
;;
promote)
ocf_promote
exit $?
;;
demote)
ocf_demote
exit $?
;;
esac