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
|
from tasks.ceph_test_case import CephTestCase
import logging
import json
from tasks.netsplit import disconnect, reconnect, get_ip_and_ports
import itertools
import time
from io import StringIO
log = logging.getLogger(__name__)
class TestNetSplit(CephTestCase):
MON_LIST = ["mon.a", "mon.d", "mon.g"]
CLIENT = "client.0"
CLUSTER = "ceph"
WRITE_PERIOD = 10
READ_PERIOD = 10
RECOVERY_PERIOD = WRITE_PERIOD * 6
SUCCESS_HOLD_TIME = 10
PEERING_CRUSH_BUCKET_COUNT = 2
PEERING_CRUSH_BUCKET_TARGET = 3
PEERING_CRUSH_BUCKET_BARRIER = 'datacenter'
POOL = 'pool_stretch'
CRUSH_RULE = 'replicated_rule_custom'
SIZE = 6
MIN_SIZE = 3
BUCKET_MAX = SIZE // PEERING_CRUSH_BUCKET_TARGET
if (BUCKET_MAX * PEERING_CRUSH_BUCKET_TARGET) < SIZE:
BUCKET_MAX += 1
def setUp(self):
"""
Set up the cluster for the test.
"""
super(TestNetSplit, self).setUp()
def tearDown(self):
"""
Clean up the cluter after the test.
"""
super(TestNetSplit, self).tearDown()
def _setup_pool(self, size=None, min_size=None, rule=None):
"""
Create a pool and set its size.
"""
self.mgr_cluster.mon_manager.create_pool(self.POOL, min_size=min_size)
if size is not None:
self.mgr_cluster.mon_manager.raw_cluster_cmd(
'osd', 'pool', 'set', self.POOL, 'size', str(size))
if rule is not None:
self.mgr_cluster.mon_manager.raw_cluster_cmd(
'osd', 'pool', 'set', self.POOL, 'crush_rule', rule)
def _get_pg_stats(self):
"""
Dump the cluster and get pg stats
"""
(client,) = self.ctx.cluster.only(self.CLIENT).remotes.keys()
arg = ['ceph', 'pg', 'dump', '--format=json']
proc = client.run(args=arg, wait=True, stdout=StringIO(), timeout=30)
if proc.exitstatus != 0:
log.error("pg dump failed")
raise Exception("pg dump failed")
out = proc.stdout.getvalue()
j = json.loads('\n'.join(out.split('\n')[1:]))
try:
return j['pg_map']['pg_stats']
except KeyError:
return j['pg_stats']
def _get_active_pg(self, pgs):
"""
Get the number of active PGs
"""
num_active = 0
for pg in pgs:
if pg['state'].count('active') and not pg['state'].count('stale'):
num_active += 1
return num_active
def _print_not_active_clean_pg(self, pgs):
"""
Print the PGs that are not active+clean.
"""
for pg in pgs:
if not (pg['state'].count('active') and
pg['state'].count('clean') and
not pg['state'].count('stale')):
log.debug(
"PG %s is not active+clean, but %s",
pg['pgid'], pg['state']
)
def _print_not_active_pg(self, pgs):
"""
Print the PGs that are not active.
"""
for pg in pgs:
if not (pg['state'].count('active') and
not pg['state'].count('stale')):
log.debug(
"PG %s is not active, but %s",
pg['pgid'], pg['state']
)
def _pg_all_active(self):
"""
Check if all pgs are active.
"""
pgs = self._get_pg_stats()
result = self._get_active_pg(pgs) == len(pgs)
if result:
log.debug("All PGs are active")
else:
log.debug("Not all PGs are active")
self._print_not_active_pg(pgs)
return result
def _get_active_clean_pg(self, pgs):
"""
Get the number of active+clean PGs
"""
num_active_clean = 0
for pg in pgs:
if (pg['state'].count('active') and
pg['state'].count('clean') and
not pg['state'].count('stale') and
not pg['state'].count('laggy')):
num_active_clean += 1
return num_active_clean
def _pg_all_active_clean(self):
"""
Check if all pgs are active and clean.
"""
pgs = self._get_pg_stats()
result = self._get_active_clean_pg(pgs) == len(pgs)
if result:
log.debug("All PGs are active+clean")
else:
log.debug("Not all PGs are active+clean")
self._print_not_active_clean_pg(pgs)
return result
def _disconnect_mons(self, config):
"""
Disconnect the mons in the <config> list.
"""
disconnect(self.ctx, config)
def _reconnect_mons(self, config):
"""
Reconnect the mons in the <config> list.
"""
reconnect(self.ctx, config)
def _reply_to_mon_command(self):
"""
Check if the cluster is accessible.
"""
try:
self.mgr_cluster.mon_manager.raw_cluster_cmd('status')
return True
except Exception:
return False
def _check_if_disconnect(self, config):
"""
Check if the mons in the <config> list are disconnected.
"""
assert config[0].startswith('mon.')
assert config[1].startswith('mon.')
log.info("Checking if the {} and {} are disconnected".format(
config[0], config[1]))
(ip1, _) = get_ip_and_ports(self.ctx, config[0])
(ip2, _) = get_ip_and_ports(self.ctx, config[1])
(host1,) = self.ctx.cluster.only(config[0]).remotes.keys()
(host2,) = self.ctx.cluster.only(config[1]).remotes.keys()
assert host1 is not None
assert host2 is not None
# if the mons are disconnected, the ping should fail (exitstatus = 1)
try:
if (host1.run(args=["ping", "-c", "1", ip2]).exitstatus == 0 or
host2.run(args=["ping", "-c", "1", ip1]).exitstatus == 0):
return False
except Exception:
return True
def _check_if_connect(self, config):
"""
Check if the mons in the <config> list are connected.
"""
assert config[0].startswith('mon.')
assert config[1].startswith('mon.')
log.info("Checking if {} and {} are connected".format(
config[0], config[1]))
(ip1, _) = get_ip_and_ports(self.ctx, config[0])
(ip2, _) = get_ip_and_ports(self.ctx, config[1])
(host1,) = self.ctx.cluster.only(config[0]).remotes.keys()
(host2,) = self.ctx.cluster.only(config[1]).remotes.keys()
assert host1 is not None
assert host2 is not None
# if the mons are connected, the ping should succeed (exitstatus = 0)
try:
if (host1.run(args=["ping", "-c", "1", ip2]).exitstatus == 0 and
host2.run(args=["ping", "-c", "1", ip1]).exitstatus == 0):
return True
except Exception:
return False
def test_mon_netsplit(self):
"""
Test the mon netsplit scenario, if cluster is actually accessible.
"""
log.info("Running test_mon_netsplit")
self._setup_pool(
self.SIZE,
min_size=self.MIN_SIZE,
rule=self.CRUSH_RULE
)
# set the pool to stretch
self.mgr_cluster.mon_manager.raw_cluster_cmd(
'osd', 'pool', 'stretch', 'set',
self.POOL, str(self.PEERING_CRUSH_BUCKET_COUNT),
str(self.PEERING_CRUSH_BUCKET_TARGET),
self.PEERING_CRUSH_BUCKET_BARRIER,
self.CRUSH_RULE, str(self.SIZE), str(self.MIN_SIZE))
# check if all the mons are connected
self.wait_until_true(
lambda: all(
[
self._check_if_connect([mon1, mon2])
for mon1, mon2 in itertools.combinations(self.MON_LIST, 2)
]
),
timeout=self.RECOVERY_PERIOD,
)
# wait for all PGs to become active
self.wait_until_true_and_hold(
lambda: self._pg_all_active(),
timeout=self.RECOVERY_PERIOD,
success_hold_time=self.SUCCESS_HOLD_TIME
)
# Scenario 1: disconnect Site 1 and Site 2
# Site 3 is still connected to both Site 1 and Site 2
config = ["mon.a", "mon.d"]
# disconnect the mons
self._disconnect_mons(config)
# wait for the mons to be disconnected
time.sleep(self.RECOVERY_PERIOD)
# check if the mons are disconnected
self.wait_until_true(
lambda: self._check_if_disconnect(config),
timeout=self.RECOVERY_PERIOD,
)
# check the cluster is accessible
self.wait_until_true_and_hold(
lambda: self._reply_to_mon_command(),
timeout=self.RECOVERY_PERIOD * 5,
success_hold_time=self.SUCCESS_HOLD_TIME
)
# reconnect the mons
self._reconnect_mons(config)
# wait for the mons to be reconnected
time.sleep(self.RECOVERY_PERIOD)
# check if the mons are reconnected
self.wait_until_true(
lambda: self._check_if_connect(config),
timeout=self.RECOVERY_PERIOD,
)
# wait for the PGs to recover
time.sleep(self.RECOVERY_PERIOD)
# check if all PGs are active+clean
self.wait_until_true_and_hold(
lambda: self._pg_all_active_clean(),
timeout=self.RECOVERY_PERIOD,
success_hold_time=self.SUCCESS_HOLD_TIME
)
log.info("test_mon_netsplit passed!")
|