Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / thrash_pool_snaps.py
1 """
2 Thrash -- Simulate random osd failures.
3 """
4 import contextlib
5 import logging
6 import gevent
7 import time
8 import random
9
10
11 log = logging.getLogger(__name__)
12
13 @contextlib.contextmanager
14 def task(ctx, config):
15     """
16     "Thrash" snap creation and removal on the listed pools
17
18     Example:
19
20     thrash_pool_snaps:
21       pools: [.rgw.buckets, .rgw.buckets.index]
22       max_snaps: 10
23       min_snaps: 5
24       period: 10
25     """
26     stopping = False
27     def do_thrash():
28         pools = config.get('pools', [])
29         max_snaps = config.get('max_snaps', 10)
30         min_snaps = config.get('min_snaps', 5)
31         period = config.get('period', 30)
32         snaps = []
33         manager = ctx.managers['ceph']
34         def remove_snap():
35             assert len(snaps) > 0
36             snap = random.choice(snaps)
37             log.info("Removing snap %s" % (snap,))
38             for pool in pools:
39                 manager.remove_pool_snap(pool, str(snap))
40             snaps.remove(snap)
41         def add_snap(snap):
42             log.info("Adding snap %s" % (snap,))
43             for pool in pools:
44                 manager.add_pool_snap(pool, str(snap))
45             snaps.append(snap)
46         index = 0
47         while not stopping:
48             index += 1
49             time.sleep(period)
50             if len(snaps) <= min_snaps:
51                 add_snap(index)
52             elif len(snaps) >= max_snaps:
53                 remove_snap()
54             else:
55                 random.choice([lambda: add_snap(index), remove_snap])()
56         log.info("Stopping")
57     thread = gevent.spawn(do_thrash)
58     yield
59     stopping = True
60     thread.join()
61