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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
import tests.pgautofailover_utils as pgautofailover
from nose.tools import raises, eq_
import time
import os.path
cluster = None
monitor = None
node1 = None
node2 = None
node3 = None
node4 = None
node5 = None
node6 = None
def setup_module():
global cluster
cluster = pgautofailover.Cluster()
def teardown_module():
cluster.destroy()
def test_000_create_monitor():
global monitor
monitor = cluster.create_monitor("/tmp/multi_standby/monitor")
monitor.run()
monitor.wait_until_pg_is_running()
def test_001_init_primary():
global node1
node1 = cluster.create_datanode("/tmp/multi_standby/node1")
node1.create()
node1.run()
assert node1.wait_until_state(target_state="single")
def test_002_candidate_priority():
assert node1.get_candidate_priority() == 50
assert not node1.set_candidate_priority(-1)
assert node1.get_candidate_priority() == 50
assert node1.set_candidate_priority(99)
assert node1.get_candidate_priority() == 99
def test_003_replication_quorum():
assert node1.get_replication_quorum()
assert not node1.set_replication_quorum("wrong quorum")
assert node1.get_replication_quorum()
assert node1.set_replication_quorum("false")
assert not node1.get_replication_quorum()
assert node1.set_replication_quorum("true")
assert node1.get_replication_quorum()
def test_004_001_add_three_standbys():
# the next test wants to set number_sync_standbys to 2
# so we need at least 3 standbys to allow that
global node2
node2 = cluster.create_datanode("/tmp/multi_standby/node2")
node2.create()
node2.run()
assert node2.wait_until_state(target_state="secondary")
assert node2.wait_until_pg_is_running()
assert node1.has_needed_replication_slots()
assert node2.has_needed_replication_slots()
# with one standby, we have number_sync_standbys set to 0 still
assert node1.get_number_sync_standbys() == 0
def test_004_002_add_three_standbys():
global node3
# refrain from waiting for the primary to be ready, to trigger a race
# condition that could segfault the monitor (if the code was less
# careful than it is now)
# assert node1.wait_until_state(target_state="primary")
node3 = cluster.create_datanode("/tmp/multi_standby/node3")
node3.create()
node3.run()
assert node3.wait_until_state(target_state="secondary")
assert node1.wait_until_state(target_state="primary")
assert node3.wait_until_pg_is_running()
assert node1.has_needed_replication_slots()
assert node2.has_needed_replication_slots()
assert node3.has_needed_replication_slots()
# the formation number_sync_standbys is expected to be set to 1 now
assert node1.get_number_sync_standbys() == 1
def test_004_003_add_three_standbys():
global node4
node4 = cluster.create_datanode("/tmp/multi_standby/node4")
node4.create()
node4.run()
assert node4.wait_until_state(target_state="secondary")
# make sure we reached primary on node1 before next tests
assert node1.wait_until_state(target_state="primary")
assert node4.wait_until_pg_is_running()
assert node1.has_needed_replication_slots()
assert node2.has_needed_replication_slots()
assert node3.has_needed_replication_slots()
assert node4.has_needed_replication_slots()
def test_005_number_sync_standbys():
print()
assert node1.get_number_sync_standbys() == 1
node1.set_number_sync_standbys(-1)
assert node1.get_number_sync_standbys() == 1
print("set number_sync_standbys = 2")
assert node1.set_number_sync_standbys(2)
assert node1.get_number_sync_standbys() == 2
ssn = "ANY 2 (pgautofailover_standby_2, pgautofailover_standby_3, pgautofailover_standby_4)"
node1.check_synchronous_standby_names(ssn)
print("set number_sync_standbys = 0")
assert node1.set_number_sync_standbys(0)
assert node1.get_number_sync_standbys() == 0
ssn = "ANY 1 (pgautofailover_standby_2, pgautofailover_standby_3, pgautofailover_standby_4)"
node1.check_synchronous_standby_names(ssn)
print("set number_sync_standbys = 1")
assert node1.set_number_sync_standbys(1)
assert node1.get_number_sync_standbys() == 1
# same ssn as before
eq_(node1.get_synchronous_standby_names(), ssn)
eq_(node1.get_synchronous_standby_names_local(), ssn)
def test_006_number_sync_standbys_trigger():
assert node1.set_number_sync_standbys(2)
assert node1.get_number_sync_standbys() == 2
node4.drop()
assert node1.wait_until_state(target_state="primary")
ssn = "ANY 1 (pgautofailover_standby_2, pgautofailover_standby_3)"
node1.check_synchronous_standby_names(ssn)
# there's no state change to instruct us that the replication slot
# maintenance is now done, so we have to wait for awhile instead.
node1.pg_autoctl.sighup() # wake up from the 10s node_active delay
node2.pg_autoctl.sighup() # wake up from the 10s node_active delay
node3.pg_autoctl.sighup() # wake up from the 10s node_active delay
time.sleep(6)
assert node1.has_needed_replication_slots()
assert node2.has_needed_replication_slots()
assert node3.has_needed_replication_slots()
def test_007_create_t1():
node1.run_sql_query("CREATE TABLE t1(a int)")
node1.run_sql_query("INSERT INTO t1 VALUES (1), (2)")
node1.run_sql_query("CHECKPOINT")
def test_008_set_candidate_priorities():
# set priorities in a way that we know the candidate: node2
node1.set_candidate_priority(90) # current primary
node2.set_candidate_priority(90)
node3.set_candidate_priority(70)
print()
assert node1.wait_until_state(target_state="primary")
def test_009_failover():
print()
print("Calling pgautofailover.failover() on the monitor")
monitor.failover()
assert node2.wait_until_state(target_state="primary")
assert node3.wait_until_state(target_state="secondary")
assert node1.wait_until_state(target_state="secondary")
ssn = "ANY 1 (pgautofailover_standby_1, pgautofailover_standby_3)"
node2.check_synchronous_standby_names(ssn)
assert node1.has_needed_replication_slots()
assert node2.has_needed_replication_slots()
assert node3.has_needed_replication_slots()
def test_010_read_from_nodes():
assert node1.run_sql_query("SELECT * FROM t1") == [(1,), (2,)]
assert node2.run_sql_query("SELECT * FROM t1") == [(1,), (2,)]
assert node3.run_sql_query("SELECT * FROM t1") == [(1,), (2,)]
def test_011_write_into_new_primary():
node2.run_sql_query("INSERT INTO t1 VALUES (3), (4)")
results = node2.run_sql_query("SELECT * FROM t1")
assert results == [(1,), (2,), (3,), (4,)]
# generate more WAL traffic for replication
node2.run_sql_query("CHECKPOINT")
def test_012_fail_primary():
print()
print("Failing current primary node 2")
node2.fail()
# explicitly allow for the 30s timeout in stop_replication
assert node1.wait_until_state(target_state="stop_replication")
assert node1.wait_until_state(target_state="primary")
assert node3.wait_until_state(target_state="secondary")
ssn = "ANY 1 (pgautofailover_standby_2, pgautofailover_standby_3)"
node1.check_synchronous_standby_names(ssn)
def test_013_restart_node2():
node2.run()
assert node1.wait_until_state(target_state="primary")
assert node2.wait_until_state(target_state="secondary")
assert node3.wait_until_state(target_state="secondary")
ssn = "ANY 1 (pgautofailover_standby_2, pgautofailover_standby_3)"
node1.check_synchronous_standby_names(ssn)
#
# When the two standby nodes are lost and then assigned catchingup, and the
# primary is now blocking writes, we can set number-sync-standbys to 0 to
# unblock writes, causing the primary to reach wait_primary
#
def test_014_001_fail_set_properties():
eq_(node1.get_number_sync_standbys(), 1)
node1.set_candidate_priority(50)
node2.set_candidate_priority(50)
node3.set_candidate_priority(50)
node1.wait_until_state(target_state="primary")
assert node1.get_replication_quorum()
assert node2.get_replication_quorum()
assert node3.get_replication_quorum()
def test_014_002_fail_two_standby_nodes():
node2.fail()
node3.fail()
node2.wait_until_assigned_state(target_state="catchingup")
node3.wait_until_assigned_state(target_state="catchingup")
# node1 remains a primary, blocking writes, at this stage
node1.wait_until_state(target_state="primary")
ssn = "ANY 1 (pgautofailover_standby_2, pgautofailover_standby_3)"
node1.check_synchronous_standby_names(ssn)
def test_014_003_unblock_writes():
node1.set_number_sync_standbys(0)
# node1 unblocks writes because number-sync-standbys is now zero
node1.wait_until_state(target_state="wait_primary")
eq_(node1.get_number_sync_standbys(), 0)
node1.check_synchronous_standby_names(ssn="")
def test_014_004_restart_nodes():
node3.run()
node2.run()
assert node3.wait_until_state(target_state="secondary")
assert node2.wait_until_state(target_state="secondary")
assert node1.wait_until_state(target_state="primary")
ssn = "ANY 1 (pgautofailover_standby_2, pgautofailover_standby_3)"
node1.check_synchronous_standby_names(ssn)
#
# Now if number-sync-standbys is zero already, then when we lose all the
# standby nodes the primary is assigned wait_primary to unblock writes
#
def test_015_001_set_properties():
node1.wait_until_state(target_state="primary")
eq_(node1.get_number_sync_standbys(), 0)
def test_015_002_fail_two_standby_nodes():
node2.fail()
node3.fail()
# node 1 is assigned wait_primary as soon as we lose all the candidates
node1.wait_until_state(target_state="wait_primary")
ssn = ""
eq_(node1.get_synchronous_standby_names(), ssn)
eq_(node1.get_synchronous_standby_names_local(), ssn)
def test_015_003_set_properties():
# stop the data leak by re-implementing sync rep
#
# the primary is now expected to be in apply_settings/primary, and fail
# to reach primary, which causes the set number-sync-standbys command to
# fail.
#
# instead of using the command line (which waits for 60s and fail),
# let's use the monitor SQL API instead
q = "select pgautofailover.set_formation_number_sync_standbys('default', 1)"
monitor.run_sql_query(q)
assert node1.wait_until_assigned_state(target_state="primary")
eq_(node1.get_number_sync_standbys(), 1)
ssn = "ANY 1 (pgautofailover_standby_2, pgautofailover_standby_3)"
node1.check_synchronous_standby_names(ssn)
def test_015_004_restart_nodes():
node3.run()
node2.run()
assert node3.wait_until_state(target_state="secondary")
assert node2.wait_until_state(target_state="secondary")
assert node1.wait_until_state(target_state="primary")
ssn = "ANY 1 (pgautofailover_standby_2, pgautofailover_standby_3)"
node1.check_synchronous_standby_names(ssn)
#
# Now test a failover when all the nodes have candidate priority set to zero
#
def test_016_001_set_candidate_priorities_to_zero():
node1.set_candidate_priority(0)
node2.set_candidate_priority(0)
node3.set_candidate_priority(0)
# no candidate for failover, we're wait_primary
node1.wait_until_state(target_state="primary")
def test_016_002_trigger_failover():
print()
print("Calling pgautofailover.failover() on the monitor")
monitor.failover()
assert node3.wait_until_state(target_state="report_lsn")
assert node2.wait_until_state(target_state="report_lsn")
assert node1.wait_until_state(target_state="report_lsn")
def test_016_003_set_candidate_priority_to_one():
node2.set_candidate_priority(1)
# no candidate for failover, we're wait_primary
node2.wait_until_state(target_state="primary")
node1.wait_until_state(target_state="secondary")
node3.wait_until_state(target_state="secondary")
def test_016_004_reset_candidate_priority():
node2.set_candidate_priority(0)
node2.wait_until_state(target_state="primary")
node1.wait_until_state(target_state="secondary")
node3.wait_until_state(target_state="secondary")
def test_016_005_perform_promotion():
print()
print("Calling pg_autoctl perform promotion on node 1")
node1.perform_promotion()
node1.wait_until_state(target_state="primary")
node2.wait_until_state(target_state="secondary")
node3.wait_until_state(target_state="secondary")
def test_017_remove_old_primary():
node2.drop()
assert node1.wait_until_state(target_state="primary")
assert node3.wait_until_state(target_state="secondary")
|