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
|
import tests.pgautofailover_utils as pgautofailover
from nose.tools import eq_, raises
import os.path
import time
import subprocess
import pprint
cluster = None
monitor = None
coordinator1a = None
coordinator1b = None
# we will be creating a citus cluster with 2 workers
# each has a secondary for replication. Workers are numbered
# a/b indicates primary/secondary (only true before first failover)
# a/b swap around primary and secondary roles when failures are induced
worker1a = None
worker1b = None
worker2a = None
worker2b = None
def setup_module():
global cluster
cluster = pgautofailover.Cluster()
def teardown_module():
coordinator1b.run_sql_query("select public.wait_until_metadata_sync()")
coordinator1b.run_sql_query("DROP TABLE t1")
cluster.destroy()
def test_000_create_monitor():
global monitor
monitor = cluster.create_monitor("/tmp/citus/basic/monitor")
monitor.run()
def test_001_init_coordinator():
global coordinator1a
global coordinator1b
coordinator1a = cluster.create_datanode(
"/tmp/citus/basic/coordinator1a", role=pgautofailover.Role.Coordinator
)
coordinator1a.create()
coordinator1a.run()
assert coordinator1a.wait_until_state(target_state="single")
# we need to expose some Citus testing internals
assert coordinator1a.wait_until_pg_is_running()
coordinator1a.create_wait_until_metadata_sync()
# check that Citus did skip creating the SSL certificates (--no-ssl)
assert not os.path.isfile(
os.path.join("/tmp/citus/basic/coordinator1a", "server.key")
)
coordinator1b = cluster.create_datanode(
"/tmp/citus/basic/coordinator1b", role=pgautofailover.Role.Coordinator
)
coordinator1b.create()
coordinator1b.run()
assert coordinator1a.wait_until_state(target_state="primary")
assert coordinator1b.wait_until_state(target_state="secondary")
sql = "select nodename, nodeport from pg_dist_node"
nodes = coordinator1a.run_sql_query(sql)
assert nodes[0][0] == str(coordinator1a.vnode.address)
assert nodes[0][1] == coordinator1a.port
def test_002_init_workers():
global worker1a
global worker1b
global worker2a
global worker2b
worker1a = cluster.create_datanode(
"/tmp/citus/basic/worker1a", group=1, role=pgautofailover.Role.Worker
)
worker1a.create(name="worker1a")
worker1a.run()
worker1b = cluster.create_datanode(
"/tmp/citus/basic/worker1b", group=1, role=pgautofailover.Role.Worker
)
worker1b.create(name="worker1b")
worker1b.run()
worker2a = cluster.create_datanode(
"/tmp/citus/basic/worker2a", group=2, role=pgautofailover.Role.Worker
)
worker2a.create(name="worker2a")
worker2a.run()
worker2b = cluster.create_datanode(
"/tmp/citus/basic/worker2b", group=2, role=pgautofailover.Role.Worker
)
worker2b.create(name="worker2b")
worker2b.run()
# wait here till all workers are stable and in the desired state
# by not waiting on all workers separately this should save a bit of time
print() # make the debug output more readable
assert worker1a.wait_until_state(target_state="primary")
assert worker1b.wait_until_state(target_state="secondary")
assert worker2a.wait_until_state(target_state="primary")
assert worker2b.wait_until_state(target_state="secondary")
def test_003_create_distributed_table():
coordinator1a.run_sql_query("CREATE TABLE t1 (a int)")
coordinator1a.run_sql_query("SELECT create_distributed_table('t1', 'a')")
coordinator1a.run_sql_query("INSERT INTO t1 VALUES (1), (2)")
def test_004_001_fail_worker2():
worker2a.fail()
@raises(Exception)
def test_004_002_writes_via_coordinator_to_worker2_fail():
# value 3 is routed to the worker2 pair, which we just failed and
# didn't had time to fail over yet. This will give an error due
# to the failure of citus to contact the worker that just failed
coordinator1a.run_sql_query("INSERT INTO t1 VALUES (3)")
def test_004_003_reads_for_worker1_via_coordinator_work():
# value 2 is routed to the worker1 pair. Since there is no failure
# in this pair the coordinator is able to read the information
results = coordinator1a.run_sql_query("SELECT a FROM t1 WHERE a = 2")
eq_(results, [(2,)])
def test_004_004_wait_for_failover():
print() # make the debug output more readable
assert worker2b.wait_until_state(target_state="wait_primary")
def test_004_005_writes_via_coordinator_to_worker2_succeed_after_failover():
# same as test 004_002, but now that the failover has been performed
# the value can be correctly written to worker2b (worker2a is still dead)
coordinator1a.run_sql_query("INSERT INTO t1 VALUES (3)")
def test_005_read_from_workers_via_coordinator():
results = coordinator1a.run_sql_query("SELECT a FROM t1 ORDER BY a ASC")
eq_(results, [(1,), (2,), (3,)])
def test_006_writes_to_coordinator_succeed():
coordinator1a.run_sql_query("INSERT INTO t1 VALUES (4)")
results = coordinator1a.run_sql_query("SELECT a FROM t1 ORDER BY a ASC")
eq_(results, [(1,), (2,), (3,), (4,)])
def test_007_start_worker2_again():
worker2a.run()
print() # make the debug output more readable
assert worker2b.wait_until_state(target_state="primary")
assert worker2a.wait_until_state(target_state="secondary")
ssn = worker2b.run_sql_query("show synchronous_standby_names")[0][0]
eq_(ssn, "ANY 1 (pgautofailover_standby_5)")
def test_008_read_from_workers_via_coordinator():
results = coordinator1a.run_sql_query("SELECT a FROM t1 ORDER BY a ASC")
eq_(results, [(1,), (2,), (3,), (4,)])
def test_009_fail_worker2b():
worker2b.fail()
print() # make the debug output more readable
assert worker2a.wait_until_state(target_state="wait_primary")
def test_010_read_from_workers_via_coordinator():
# after failing back to worker2a verify the value written to worker2b
# has been safely replicated to worker2a
results = coordinator1a.run_sql_query("SELECT a FROM t1 ORDER BY a ASC")
eq_(results, [(1,), (2,), (3,), (4,)])
def test_011_start_worker2b_again():
worker2b.run()
print()
assert worker2b.wait_until_state(target_state="secondary")
assert worker2a.wait_until_state(target_state="primary")
def test_012_perform_failover_worker2():
print()
print("Calling pgautofailover.failover(group => 2) on the monitor")
cluster.monitor.failover(group=2)
assert worker2a.wait_until_state(target_state="secondary")
assert worker2b.wait_until_state(target_state="primary")
assert worker2a.has_needed_replication_slots()
assert worker2b.has_needed_replication_slots()
# we had a bug where if the old primary has already reached draining (both
# assigned and reported the state) when the secondary starts the transition
# from secondary to prepare_promotion, then the secondary transition fails
# because calling pgautofailover.get_primary() on the monitor returned no
# rows.
#
# that's fixed by not calling pgautofailover.get_primary() in the
# transition, it can be avoided easily.
#
def test_013_perform_failover_worker2b_draining():
print()
worker2a.stop_pg_autoctl()
print("Calling pgautofailover.failover(group => 2) on the monitor")
cluster.monitor.failover(group=2)
assert worker2b.wait_until_state(target_state="draining")
worker2a.run()
assert worker2b.wait_until_state(target_state="secondary")
assert worker2a.wait_until_state(target_state="primary")
assert worker2a.has_needed_replication_slots()
assert worker2b.has_needed_replication_slots()
def test_014_perform_failover_coordinator():
print()
print("Calling pgautofailover.failover(group => 0) on the monitor")
cluster.monitor.failover(group=0)
assert coordinator1a.wait_until_state(target_state="secondary")
assert coordinator1b.wait_until_state(target_state="primary")
assert coordinator1a.has_needed_replication_slots()
assert coordinator1b.has_needed_replication_slots()
|