3 Teuthology task for exercising CephFS client recovery
7 from textwrap
import dedent
9 import distutils
.version
as version
13 from teuthology
.orchestra
.run
import CommandFailedError
, ConnectionLostError
14 from tasks
.cephfs
.fuse_mount
import FuseMount
15 from tasks
.cephfs
.cephfs_test_case
import CephFSTestCase
16 from teuthology
.packaging
import get_package_version
17 from unittest
import SkipTest
20 log
= logging
.getLogger(__name__
)
23 # Arbitrary timeouts for operations involving restarting
24 # an MDS or waiting for it to come up
25 MDS_RESTART_GRACE
= 60
28 class TestClientNetworkRecovery(CephFSTestCase
):
29 REQUIRE_KCLIENT_REMOTE
= True
30 REQUIRE_ONE_CLIENT_REMOTE
= True
33 LOAD_SETTINGS
= ["mds_reconnect_timeout", "ms_max_backoff"]
35 # Environment references
36 mds_reconnect_timeout
= None
39 def test_network_death(self
):
41 Simulate software freeze or temporary network failure.
43 Check that the client blocks I/O during failure, and completes
47 session_timeout
= self
.fs
.get_var("session_timeout")
48 self
.fs
.mds_asok(['config', 'set', 'mds_defer_session_stale', 'false'])
50 # We only need one client
51 self
.mount_b
.umount_wait()
53 # Initially our one client session should be visible
54 client_id
= self
.mount_a
.get_global_id()
55 ls_data
= self
._session
_list
()
56 self
.assert_session_count(1, ls_data
)
57 self
.assertEqual(ls_data
[0]['id'], client_id
)
58 self
.assert_session_state(client_id
, "open")
60 # ...and capable of doing I/O without blocking
61 self
.mount_a
.create_files()
63 # ...but if we turn off the network
64 self
.fs
.set_clients_block(True)
66 # ...and try and start an I/O
67 write_blocked
= self
.mount_a
.write_background()
69 # ...then it should block
70 self
.assertFalse(write_blocked
.finished
)
71 self
.assert_session_state(client_id
, "open")
72 time
.sleep(session_timeout
* 1.5) # Long enough for MDS to consider session stale
73 self
.assertFalse(write_blocked
.finished
)
74 self
.assert_session_state(client_id
, "stale")
76 # ...until we re-enable I/O
77 self
.fs
.set_clients_block(False)
79 # ...when it should complete promptly
81 self
.wait_until_true(lambda: write_blocked
.finished
, self
.ms_max_backoff
* 2)
82 write_blocked
.wait() # Already know we're finished, wait() to raise exception on errors
83 recovery_time
= time
.time() - a
84 log
.info("recovery time: {0}".format(recovery_time
))
85 self
.assert_session_state(client_id
, "open")
88 class TestClientRecovery(CephFSTestCase
):
89 REQUIRE_KCLIENT_REMOTE
= True
92 LOAD_SETTINGS
= ["mds_reconnect_timeout", "ms_max_backoff"]
94 # Environment references
95 mds_reconnect_timeout
= None
99 # Check that two clients come up healthy and see each others' files
100 # =====================================================
101 self
.mount_a
.create_files()
102 self
.mount_a
.check_files()
103 self
.mount_a
.umount_wait()
105 self
.mount_b
.check_files()
108 self
.mount_a
.wait_until_mounted()
110 # Check that the admin socket interface is correctly reporting
112 # =====================================================
113 ls_data
= self
._session
_list
()
114 self
.assert_session_count(2, ls_data
)
117 set([l
['id'] for l
in ls_data
]),
118 {self
.mount_a
.get_global_id(), self
.mount_b
.get_global_id()}
121 def test_restart(self
):
122 # Check that after an MDS restart both clients reconnect and continue
124 # =====================================================
125 self
.fs
.mds_fail_restart()
126 self
.fs
.wait_for_state('up:active', timeout
=MDS_RESTART_GRACE
)
128 self
.mount_a
.create_destroy()
129 self
.mount_b
.create_destroy()
131 def _session_num_caps(self
, client_id
):
132 ls_data
= self
.fs
.mds_asok(['session', 'ls'])
133 return int(self
._session
_by
_id
(ls_data
).get(client_id
, {'num_caps': None})['num_caps'])
135 def test_reconnect_timeout(self
):
138 # Check that if I stop an MDS and a client goes away, the MDS waits
139 # for the reconnect period
143 mount_a_client_id
= self
.mount_a
.get_global_id()
144 self
.mount_a
.umount_wait(force
=True)
146 self
.fs
.mds_restart()
148 self
.fs
.wait_for_state('up:reconnect', reject
='up:active', timeout
=MDS_RESTART_GRACE
)
149 # Check that the MDS locally reports its state correctly
150 status
= self
.fs
.mds_asok(['status'])
151 self
.assertIn("reconnect_status", status
)
153 ls_data
= self
._session
_list
()
154 self
.assert_session_count(2, ls_data
)
156 # The session for the dead client should have the 'reconnect' flag set
157 self
.assertTrue(self
.get_session(mount_a_client_id
)['reconnecting'])
159 # Wait for the reconnect state to clear, this should take the
160 # reconnect timeout period.
161 in_reconnect_for
= self
.fs
.wait_for_state('up:active', timeout
=self
.mds_reconnect_timeout
* 2)
162 # Check that the period we waited to enter active is within a factor
163 # of two of the reconnect timeout.
164 self
.assertGreater(in_reconnect_for
, self
.mds_reconnect_timeout
/ 2,
165 "Should have been in reconnect phase for {0} but only took {1}".format(
166 self
.mds_reconnect_timeout
, in_reconnect_for
169 self
.assert_session_count(1)
171 # Check that the client that timed out during reconnect can
172 # mount again and do I/O
174 self
.mount_a
.wait_until_mounted()
175 self
.mount_a
.create_destroy()
177 self
.assert_session_count(2)
179 def test_reconnect_eviction(self
):
180 # Eviction during reconnect
181 # =========================
182 mount_a_client_id
= self
.mount_a
.get_global_id()
187 # The mount goes away while the MDS is offline
190 self
.fs
.mds_restart()
192 # Enter reconnect phase
193 self
.fs
.wait_for_state('up:reconnect', reject
='up:active', timeout
=MDS_RESTART_GRACE
)
194 self
.assert_session_count(2)
196 # Evict the stuck client
197 self
.fs
.mds_asok(['session', 'evict', "%s" % mount_a_client_id
])
198 self
.assert_session_count(1)
200 # Observe that we proceed to active phase without waiting full reconnect timeout
201 evict_til_active
= self
.fs
.wait_for_state('up:active', timeout
=MDS_RESTART_GRACE
)
202 # Once we evict the troublemaker, the reconnect phase should complete
203 # in well under the reconnect timeout.
204 self
.assertLess(evict_til_active
, self
.mds_reconnect_timeout
* 0.5,
205 "reconnect did not complete soon enough after eviction, took {0}".format(
209 # We killed earlier so must clean up before trying to use again
210 self
.mount_a
.kill_cleanup()
212 # Bring the client back
214 self
.mount_a
.wait_until_mounted()
215 self
.mount_a
.create_destroy()
217 def _test_stale_caps(self
, write
):
218 session_timeout
= self
.fs
.get_var("session_timeout")
220 # Capability release from stale session
221 # =====================================
223 cap_holder
= self
.mount_a
.open_background()
225 self
.mount_a
.run_shell(["touch", "background_file"])
226 self
.mount_a
.umount_wait()
228 self
.mount_a
.wait_until_mounted()
229 cap_holder
= self
.mount_a
.open_background(write
=False)
231 self
.assert_session_count(2)
232 mount_a_gid
= self
.mount_a
.get_global_id()
234 # Wait for the file to be visible from another client, indicating
235 # that mount_a has completed its network ops
236 self
.mount_b
.wait_for_visible()
238 # Simulate client death
241 # wait for it to die so it doesn't voluntarily release buffer cap
245 # Now, after session_timeout seconds, the waiter should
246 # complete their operation when the MDS marks the holder's
248 cap_waiter
= self
.mount_b
.write_background()
253 # Should have succeeded
254 self
.assertEqual(cap_waiter
.exitstatus
, 0)
257 self
.assert_session_count(1)
259 self
.assert_session_state(mount_a_gid
, "stale")
262 log
.info("cap_waiter waited {0}s".format(cap_waited
))
263 self
.assertTrue(session_timeout
/ 2.0 <= cap_waited
<= session_timeout
* 2.0,
264 "Capability handover took {0}, expected approx {1}".format(
265 cap_waited
, session_timeout
268 cap_holder
.stdin
.close()
271 except (CommandFailedError
, ConnectionLostError
):
272 # We killed it (and possibly its node), so it raises an error
275 # teardown() doesn't quite handle this case cleanly, so help it out
276 self
.mount_a
.kill_cleanup()
279 self
.mount_a
.wait_until_mounted()
281 def test_stale_read_caps(self
):
282 self
._test
_stale
_caps
(False)
284 def test_stale_write_caps(self
):
285 self
._test
_stale
_caps
(True)
287 def test_evicted_caps(self
):
288 # Eviction while holding a capability
289 # ===================================
291 session_timeout
= self
.fs
.get_var("session_timeout")
293 # Take out a write capability on a file on client A,
294 # and then immediately kill it.
295 cap_holder
= self
.mount_a
.open_background()
296 mount_a_client_id
= self
.mount_a
.get_global_id()
298 # Wait for the file to be visible from another client, indicating
299 # that mount_a has completed its network ops
300 self
.mount_b
.wait_for_visible()
302 # Simulate client death
305 # wait for it to die so it doesn't voluntarily release buffer cap
309 # The waiter should get stuck waiting for the capability
310 # held on the MDS by the now-dead client A
311 cap_waiter
= self
.mount_b
.write_background()
313 self
.assertFalse(cap_waiter
.finished
)
315 self
.fs
.mds_asok(['session', 'evict', "%s" % mount_a_client_id
])
316 # Now, because I evicted the old holder of the capability, it should
317 # immediately get handed over to the waiter
322 log
.info("cap_waiter waited {0}s".format(cap_waited
))
323 # This is the check that it happened 'now' rather than waiting
324 # for the session timeout
325 self
.assertLess(cap_waited
, session_timeout
/ 2.0,
326 "Capability handover took {0}, expected less than {1}".format(
327 cap_waited
, session_timeout
/ 2.0
330 cap_holder
.stdin
.close()
333 except (CommandFailedError
, ConnectionLostError
):
334 # We killed it (and possibly its node), so it raises an error
337 self
.mount_a
.kill_cleanup()
340 self
.mount_a
.wait_until_mounted()
342 def test_trim_caps(self
):
343 # Trim capability when reconnecting MDS
344 # ===================================
347 # Create lots of files
348 for i
in range(count
):
349 self
.mount_a
.run_shell(["touch", "f{0}".format(i
)])
351 # Populate mount_b's cache
352 self
.mount_b
.run_shell(["ls", "-l"])
354 client_id
= self
.mount_b
.get_global_id()
355 num_caps
= self
._session
_num
_caps
(client_id
)
356 self
.assertGreaterEqual(num_caps
, count
)
358 # Restart MDS. client should trim its cache when reconnecting to the MDS
359 self
.fs
.mds_fail_restart()
360 self
.fs
.wait_for_state('up:active', timeout
=MDS_RESTART_GRACE
)
362 num_caps
= self
._session
_num
_caps
(client_id
)
363 self
.assertLess(num_caps
, count
,
364 "should have less than {0} capabilities, have {1}".format(
368 def _is_flockable(self
):
369 a_version_str
= get_package_version(self
.mount_a
.client_remote
, "fuse")
370 b_version_str
= get_package_version(self
.mount_b
.client_remote
, "fuse")
371 flock_version_str
= "2.9"
373 version_regex
= re
.compile(r
"[0-9\.]+")
374 a_result
= version_regex
.match(a_version_str
)
375 self
.assertTrue(a_result
)
376 b_result
= version_regex
.match(b_version_str
)
377 self
.assertTrue(b_result
)
378 a_version
= version
.StrictVersion(a_result
.group())
379 b_version
= version
.StrictVersion(b_result
.group())
380 flock_version
=version
.StrictVersion(flock_version_str
)
382 if (a_version
>= flock_version
and b_version
>= flock_version
):
383 log
.info("flock locks are available")
386 log
.info("not testing flock locks, machines have versions {av} and {bv}".format(
387 av
=a_version_str
,bv
=b_version_str
))
390 def test_filelock(self
):
392 Check that file lock doesn't get lost after an MDS restart
395 flockable
= self
._is
_flockable
()
396 lock_holder
= self
.mount_a
.lock_background(do_flock
=flockable
)
398 self
.mount_b
.wait_for_visible("background_file-2")
399 self
.mount_b
.check_filelock(do_flock
=flockable
)
401 self
.fs
.mds_fail_restart()
402 self
.fs
.wait_for_state('up:active', timeout
=MDS_RESTART_GRACE
)
404 self
.mount_b
.check_filelock(do_flock
=flockable
)
406 # Tear down the background process
407 lock_holder
.stdin
.close()
410 except (CommandFailedError
, ConnectionLostError
):
411 # We killed it, so it raises an error
414 def test_filelock_eviction(self
):
416 Check that file lock held by evicted client is given to
419 if not self
._is
_flockable
():
420 self
.skipTest("flock is not available")
422 lock_holder
= self
.mount_a
.lock_background()
423 self
.mount_b
.wait_for_visible("background_file-2")
424 self
.mount_b
.check_filelock()
426 lock_taker
= self
.mount_b
.lock_and_release()
427 # Check the taker is waiting (doesn't get it immediately)
429 self
.assertFalse(lock_holder
.finished
)
430 self
.assertFalse(lock_taker
.finished
)
433 mount_a_client_id
= self
.mount_a
.get_global_id()
434 self
.fs
.mds_asok(['session', 'evict', "%s" % mount_a_client_id
])
436 # Evicting mount_a should let mount_b's attempt to take the lock
438 self
.wait_until_true(lambda: lock_taker
.finished
, timeout
=10)
440 # teardown() doesn't quite handle this case cleanly, so help it out
442 self
.mount_a
.kill_cleanup()
444 # Bring the client back
446 self
.mount_a
.wait_until_mounted()
448 def test_dir_fsync(self
):
449 self
._test
_fsync
(True);
451 def test_create_fsync(self
):
452 self
._test
_fsync
(False);
454 def _test_fsync(self
, dirfsync
):
456 That calls to fsync guarantee visibility of metadata to another
457 client immediately after the fsyncing client dies.
460 # Leave this guy out until he's needed
461 self
.mount_b
.umount_wait()
463 # Create dir + child dentry on client A, and fsync the dir
464 path
= os
.path
.join(self
.mount_a
.mountpoint
, "subdir")
465 self
.mount_a
.run_python(
472 print "Starting creation..."
476 dfd = os.open(path, os.O_DIRECTORY)
478 fd = open(os.path.join(path, "childfile"), "w")
479 print "Finished creation in {{0}}s".format(time.time() - start)
481 print "Starting fsync..."
487 print "Finished fsync in {{0}}s".format(time.time() - start)
488 """.format(path
=path
,dirfsync
=str(dirfsync
)))
491 # Immediately kill the MDS and then client A
495 self
.mount_a
.kill_cleanup()
497 # Restart the MDS. Wait for it to come up, it'll have to time out in clientreplay
498 self
.fs
.mds_restart()
499 log
.info("Waiting for reconnect...")
500 self
.fs
.wait_for_state("up:reconnect")
501 log
.info("Waiting for active...")
502 self
.fs
.wait_for_state("up:active", timeout
=MDS_RESTART_GRACE
+ self
.mds_reconnect_timeout
)
503 log
.info("Reached active...")
505 # Is the child dentry visible from mount B?
507 self
.mount_b
.wait_until_mounted()
508 self
.mount_b
.run_shell(["ls", "subdir/childfile"])
510 def test_unmount_for_evicted_client(self
):
511 """Test if client hangs on unmount after evicting the client."""
512 mount_a_client_id
= self
.mount_a
.get_global_id()
513 self
.fs
.mds_asok(['session', 'evict', "%s" % mount_a_client_id
])
515 self
.mount_a
.umount_wait(require_clean
=True, timeout
=30)
517 def test_stale_renew(self
):
518 if not isinstance(self
.mount_a
, FuseMount
):
519 raise SkipTest("Require FUSE client to handle signal STOP/CONT")
521 session_timeout
= self
.fs
.get_var("session_timeout")
523 self
.mount_a
.run_shell(["mkdir", "testdir"])
524 self
.mount_a
.run_shell(["touch", "testdir/file1"])
525 # populate readdir cache
526 self
.mount_a
.run_shell(["ls", "testdir"])
527 self
.mount_b
.run_shell(["ls", "testdir"])
529 # check if readdir cache is effective
530 initial_readdirs
= self
.fs
.mds_asok(['perf', 'dump', 'mds_server', 'req_readdir_latency'])
531 self
.mount_b
.run_shell(["ls", "testdir"])
532 current_readdirs
= self
.fs
.mds_asok(['perf', 'dump', 'mds_server', 'req_readdir_latency'])
533 self
.assertEqual(current_readdirs
, initial_readdirs
);
535 mount_b_gid
= self
.mount_b
.get_global_id()
536 mount_b_pid
= self
.mount_b
.get_client_pid()
537 # stop ceph-fuse process of mount_b
538 self
.mount_b
.client_remote
.run(args
=["sudo", "kill", "-STOP", mount_b_pid
])
540 self
.assert_session_state(mount_b_gid
, "open")
541 time
.sleep(session_timeout
* 1.5) # Long enough for MDS to consider session stale
543 self
.mount_a
.run_shell(["touch", "testdir/file2"])
544 self
.assert_session_state(mount_b_gid
, "stale")
546 # resume ceph-fuse process of mount_b
547 self
.mount_b
.client_remote
.run(args
=["sudo", "kill", "-CONT", mount_b_pid
])
548 # Is the new file visible from mount_b? (caps become invalid after session stale)
549 self
.mount_b
.run_shell(["ls", "testdir/file2"])
551 def test_abort_conn(self
):
553 Check that abort_conn() skips closing mds sessions.
555 if not isinstance(self
.mount_a
, FuseMount
):
556 raise SkipTest("Testing libcephfs function")
558 self
.fs
.mds_asok(['config', 'set', 'mds_defer_session_stale', 'false'])
559 session_timeout
= self
.fs
.get_var("session_timeout")
561 self
.mount_a
.umount_wait()
562 self
.mount_b
.umount_wait()
564 gid_str
= self
.mount_a
.run_python(dedent("""
565 import cephfs as libcephfs
566 cephfs = libcephfs.LibCephFS(conffile='')
568 client_id = cephfs.get_instance_id()
575 self
.assert_session_state(gid
, "open")
576 time
.sleep(session_timeout
* 1.5) # Long enough for MDS to consider session stale
577 self
.assert_session_state(gid
, "stale")
579 def test_dont_mark_unresponsive_client_stale(self
):
581 Test that an unresponsive client holding caps is not marked stale or
582 evicted unless another clients wants its caps.
584 if not isinstance(self
.mount_a
, FuseMount
):
585 self
.skipTest("Require FUSE client to handle signal STOP/CONT")
587 # XXX: To conduct this test we need at least two clients since a
588 # single client is never evcited by MDS.
590 SESSION_AUTOCLOSE
= 50
591 time_at_beg
= time
.time()
592 mount_a_gid
= self
.mount_a
.get_global_id()
593 mount_a_pid
= self
.mount_a
.client_pid
594 self
.fs
.set_var('session_timeout', SESSION_TIMEOUT
)
595 self
.fs
.set_var('session_autoclose', SESSION_AUTOCLOSE
)
596 self
.assert_session_count(2, self
.fs
.mds_asok(['session', 'ls']))
598 # test that client holding cap not required by any other client is not
599 # marked stale when it becomes unresponsive.
600 self
.mount_a
.run_shell(['mkdir', 'dir'])
601 self
.mount_a
.send_signal('sigstop')
602 time
.sleep(SESSION_TIMEOUT
+ 2)
603 self
.assert_session_state(mount_a_gid
, "open")
605 # test that other clients have to wait to get the caps from
606 # unresponsive client until session_autoclose.
607 self
.mount_b
.run_shell(['stat', 'dir'])
608 self
.assert_session_count(1, self
.fs
.mds_asok(['session', 'ls']))
609 self
.assertLess(time
.time(), time_at_beg
+ SESSION_AUTOCLOSE
)
611 self
.mount_a
.send_signal('sigcont')