1#!/usr/bin/env python
2
3import time
4import sys
5
6import clitool
7import cli_auth_utils
8import exceptions
9import mc_bin_client
10import memcacheConstants
11import sys
12
13cmd = cli_auth_utils.cmd_decorator
14
15@cmd
16def set_param(mc, type, key, val):
17    engine_param = None
18    if type == 'checkpoint_param':
19        engine_param = memcacheConstants.ENGINE_PARAM_CHECKPOINT
20    elif type == 'flush_param':
21        engine_param = memcacheConstants.ENGINE_PARAM_FLUSH
22    elif type == 'replication_param':
23        engine_param = memcacheConstants.ENGINE_PARAM_REPLICATION
24    elif type == 'dcp_param':
25        engine_param = memcacheConstants.ENGINE_PARAM_DCP
26    elif type == 'vbucket_param':
27        engine_param = memcacheConstants.ENGINE_PARAM_VBUCKET
28    else:
29        print 'Error: Bad parameter %s' % type
30
31    if key == 'replication_throttle_queue_cap' and val == 'infinite':
32        val = '-1'
33
34    if key == 'exp_pager_initial_run_time' and val == 'disable':
35        val = '-1'
36
37    if key == "mem_high_wat" or key == "mem_low_wat":
38        if val.endswith("%"):
39            _x_ = (val[:len(val)-1])
40            if not _x_.isdigit():
41                print 'Error: Invalid parameter %s' % val
42                return
43            if float(_x_) > 100:
44                print 'Error: Bad parameter %s' % val
45                return
46            _quota_ = int(mc.stats()['ep_max_size'])
47            val = str(int(float(_x_)*(_quota_)/100))
48    if (key == "mem_used_merge_threshold_percent" and
49        (float(val) > 100.0 or float(val) < 0.0)):
50        print 'Error: Invalid mem_used_merge_threshold_percent value %s' % val
51        return
52    try:
53        mc.set_param(0, key, val, engine_param)
54        print 'set %s to %s' %(key, val)
55    except mc_bin_client.MemcachedError, error:
56        print 'Error: %s' % error.msg
57    except mc_bin_client.TimeoutError, error:
58        print error
59    except Exception, e:
60        print 'Generic error (%s)' % e
61
62@cmd
63def set_vbucket_param(mc, key, vbucket, val):
64    engine_param = memcacheConstants.ENGINE_PARAM_VBUCKET
65
66    try:
67        mc.set_param(int(vbucket), key, val, engine_param)
68        print 'set %s to %s' %(key, val)
69    except mc_bin_client.MemcachedError, error:
70        print 'Error: %s' % error.msg
71    except mc_bin_client.TimeoutError, error:
72        print error
73    except Exception, e:
74        print 'Generic error (%s)' % e
75
76@cmd
77def stop(mc):
78    try:
79        mc.stop_persistence()
80        stopped = False
81        while not stopped:
82            time.sleep(0.5)
83            try:
84                stats = mc.stats()
85                success = True
86            except:
87                if success:
88                    mc = mc_bin_client.MemcachedClient(mc.host, mc.port)
89                    raise
90                else:
91                    raise
92            success = False
93            if stats['ep_flusher_state'] == 'paused':
94                stopped = True
95        print 'Persistence stopped'
96    except mc_bin_client.MemcachedError, error:
97        print 'Error: %s' % error.msg
98    except mc_bin_client.TimeoutError, error:
99        print error
100    except Exception, e:
101        print 'Generic error (%s)' % e
102
103@cmd
104def start(mc):
105    try:
106        mc.start_persistence()
107        print 'Persistence started'
108    except mc_bin_client.MemcachedError, error:
109        print 'Error: %s' % error.msg
110    except mc_bin_client.TimeoutError, error:
111        print error
112    except Exception, e:
113        print 'Generic error (%s)' % e
114
115@cmd
116def drain(mc):
117    try:
118        while True:
119            s = mc.stats()
120            if s['ep_queue_size'] == "0":
121                print("done")
122                return
123            time.sleep(2)
124            sys.stdout.write('.')
125            sys.stdout.flush()
126        print 'Write queues drained'
127    except mc_bin_client.MemcachedError, error:
128        print 'Error: %s' % error.msg
129    except mc_bin_client.TimeoutError, error:
130        print error
131    except Exception, e:
132        print 'Generic error (%s)' % e
133
134if __name__ == '__main__':
135
136    c = cli_auth_utils.get_authed_clitool("""
137Persistence:
138  stop           - stop persistence
139  start          - start persistence
140  drain          - wait until queues are drained
141
142
143Available params for "set":
144
145  Available params for set checkpoint_param:
146    chk_max_items                - Max number of items allowed in a checkpoint.
147    chk_period                   - Time bound (in sec.) on a checkpoint.
148    item_num_based_new_chk       - true if a new checkpoint can be created based
149                                   on.
150                                   the number of items in the open checkpoint.
151    keep_closed_chks             - true if we want to keep closed checkpoints in
152                                   memory.
153                                   as long as the current memory usage is below
154                                   high water mark.
155    max_checkpoints              - Max number of checkpoints allowed per vbucket.
156    enable_chk_merge             = True if merging closed checkpoints is enabled.
157
158
159  Available params for set flush_param:
160    access_scanner_enabled       - Enable or disable access scanner task (true/false)
161    alog_sleep_time              - Access scanner interval (minute)
162    alog_task_time               - Hour in UTC time when access scanner task is
163                                   next scheduled to run (0-23).
164    backfill_mem_threshold       - Memory threshold (%) on the current bucket quota
165                                   before backfill task is made to back off.
166    bg_fetch_delay               - Delay before executing a bg fetch (test
167                                   feature).
168    bfilter_enabled              - Enable or disable bloom filters (true/false)
169    bfilter_residency_threshold  - Resident ratio threshold below which all items
170                                   will be considered in the bloom filters in full
171                                   eviction policy (0.0 - 1.0)
172    compaction_exp_mem_threshold - Memory threshold (%) on the current bucket quota
173                                   after which compaction will not queue expired
174                                   items for deletion.
175    compaction_write_queue_cap   - Disk write queue threshold after which compaction
176                                   tasks will be made to snooze, if there are already
177                                   pending compaction tasks.
178    dcp_min_compression_ratio    - Minimum compression ratio of compressed doc against
179                                   the original doc. If compressed doc is greater than
180                                   this percentage of the original doc, then the doc
181                                   will be shipped as is by the DCP producer if value
182                                   compression is enabled by the DCP consumer. Applies
183                                   to all producers (Ideal range: 0.0 - 1.0)
184    defragmenter_enabled         - Enable or disable the defragmenter
185                                   (true/false).
186    defragmenter_interval        - How often defragmenter task should be run
187                                   (in seconds).
188    defragmenter_age_threshold   - How old (measured in number of defragmenter
189                                   passes) must a document be to be considered
190                                   for defragmentation.
191    defragmenter_stored_value_age_threshold   - How old (measured in number of defragmenter
192                                   passes) must a StoredValue (key + meta) be to be considered
193                                   for defragmentation.
194    defragmenter_chunk_duration  - Maximum time (in ms) defragmentation task
195                                   will run for before being paused (and
196                                   resumed at the next defragmenter_interval).
197    exp_pager_enabled            - Enable expiry pager.
198    exp_pager_stime              - Expiry Pager Sleeptime.
199    exp_pager_initial_run_time   - Expiry Pager first task time (UTC)
200                                   (Range: 0 - 23, Specify 'disable' to not delay the
201                                   the expiry pager, in which case first run will be
202                                   after exp_pager_stime seconds.)
203    item_compressor_interval     - How often the item compressor task should be run
204                                   (in milliseconds).
205    item_compressor_chunk_duration - Maximum time (in ms) the item compressor task
206                                   will run for before being paused (and resumed at
207                                   the next item compressor interval).
208    pager_active_vb_pcnt         - Percentage of active vbuckets items among
209                                   all ejected items by item pager.
210    max_size                     - Max memory used by the server.
211    mem_high_wat                 - High water mark (suffix with '%' to make it a
212                                   percentage of the RAM quota)
213    mem_low_wat                  - Low water mark. (suffix with '%' to make it a
214                                   percentage of the RAM quota)
215    min_compression_ratio        - Minimum compression ratio of uncompressed doc
216                                   against the compressed doc. If the ratio happens
217                                   to be lesser than this value, then a compressed
218                                   document will be stored as an uncompressed.
219    mutation_mem_threshold       - Memory threshold (%) on the current bucket quota
220                                   for accepting a new mutation.
221    timing_log                   - path to log detailed timing stats.
222    warmup_min_memory_threshold  - Memory threshold (%) during warmup to enable
223                                   traffic
224    warmup_min_items_threshold   - Item number threshold (%) during warmup to enable
225                                   traffic
226    num_reader_threads           - Override default number of global threads
227                                   that perform read operations.
228    num_writer_threads           - Override default number of global threads
229                                   that perform write operations.
230    num_auxio_threads            - Override default number of global threads
231                                   that perform auxio operations.
232    num_nonio_threads            - Override default number of global threads
233                                   that perform nonio operations.
234    xattr_enabled                - Enabled/Disable xattr support for the specified bucket.
235                                   Accepted input values are true or false.
236    max_ttl                      - A max TTL (1 to 2,147,483,647) to apply to all new
237                                   documents (or touched documents). 0 means this is
238                                   disabled and the protocol specified expiry value is used.
239    mem_used_merge_threshold_percent - A percentage used in calculating the threshold at which
240                                   a per core memory counter is accumulated into a global
241                                   memory used counter. This configuration parameter generates
242                                   a value which is n% of max_size / number of CPUs.
243
244  Available params for "set replication_param":
245    replication_throttle_queue_cap   - Max disk write queue size to throttle
246                                       replication streams ('infinite' means no
247                                       cap).
248    replication_throttle_cap_pcnt    - Percentage of total items in write queue
249                                       at which we throttle replication input
250    replication_throttle_threshold   - Percentage of memory in use to throttle
251                                       replication streams.
252
253  Available params for "set dcp_param":
254    dcp_consumer_process_buffered_messages_yield_limit - The threshold at which
255                                                         the Consumer will yield
256                                                         when processing items.
257
258    dcp_consumer_process_buffered_messages_batch_size - The number of items the
259                                                        DCP processor will consume
260                                                        in a single batch.
261
262Available params for "set_vbucket_param":
263    max_cas - Change the max_cas of a vbucket. The value and vbucket are specified as decimal
264              integers. The new-value is interpretted as an unsigned 64-bit integer.
265
266              cbepctl host:port -b default set_vbucket_param max_cas <vbucket-id> <new-value>
267
268    """)
269
270    c.addCommand('drain', drain, "drain")
271    c.addCommand('set', set_param, 'set type param value')
272    c.addCommand('set_vbucket_param', set_vbucket_param, 'type vbucket value')
273    c.addCommand('start', start, 'start')
274    c.addCommand('stop', stop, 'stop')
275
276    c.execute()
277