summaryrefslogtreecommitdiff
path: root/bin/swift-dispersion-populate
blob: a54c77ed0e323dfcb19a1f77b6e5c178e6240fb1 (plain)
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
282
283
#!/usr/bin/env python
# Copyright (c) 2010-2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function

import io
import traceback
from optparse import OptionParser
from sys import exit, stdout
from time import time

from eventlet import GreenPool, patcher, sleep
from eventlet.pools import Pool
import six
from six.moves import range
from six.moves.configparser import ConfigParser

from swift.common.internal_client import SimpleClient
from swift.common.ring import Ring
from swift.common.utils import compute_eta, get_time_units, config_true_value
from swift.common.storage_policy import POLICIES

insecure = False


def put_container(connpool, container, report, headers):
    global retries_done
    try:
        with connpool.item() as conn:
            conn.put_container(container, headers=headers)
            retries_done += conn.attempts - 1
        if report:
            report(True)
    except Exception:
        if report:
            report(False)
        raise


def put_object(connpool, container, obj, report):
    global retries_done
    try:
        with connpool.item() as conn:
            data = io.BytesIO(obj if six.PY2 else obj.encode('utf8'))
            conn.put_object(container, obj, data,
                            headers={'x-object-meta-dispersion': obj})
            retries_done += conn.attempts - 1
        if report:
            report(True)
    except Exception:
        if report:
            report(False)
        raise


def report(success):
    global begun, created, item_type, next_report, need_to_create, retries_done
    if not success:
        traceback.print_exc()
        exit('Gave up due to error(s).')
    created += 1
    if time() < next_report:
        return
    next_report = time() + 5
    eta, eta_unit = compute_eta(begun, created, need_to_create)
    print('\r\x1B[KCreating %s: %d of %d, %d%s left, %d retries'
          % (item_type, created, need_to_create, round(eta), eta_unit,
             retries_done), end='')
    stdout.flush()


if __name__ == '__main__':
    global begun, created, item_type, next_report, need_to_create, retries_done
    patcher.monkey_patch()
    try:
        # Delay importing so urllib3 will import monkey-patched modules
        from swiftclient import get_auth
    except ImportError:
        from swift.common.internal_client import get_auth

    conffile = '/etc/swift/dispersion.conf'

    parser = OptionParser(usage='''
Usage: %%prog [options] [conf_file]

[conf_file] defaults to %s'''.strip() % conffile)
    parser.add_option('--container-only', action='store_true', default=False,
                      help='Only run container population')
    parser.add_option('--object-only', action='store_true', default=False,
                      help='Only run object population')
    parser.add_option('--container-suffix-start', type=int, default=0,
                      help='container suffix start value, defaults to 0')
    parser.add_option('--object-suffix-start', type=int, default=0,
                      help='object suffix start value, defaults to 0')
    parser.add_option('--insecure', action='store_true', default=False,
                      help='Allow accessing insecure keystone server. '
                           'The keystone\'s certificate will not be verified.')
    parser.add_option('--no-overlap', action='store_true', default=False,
                      help="No overlap of partitions if running populate \
                      more than once. Will increase coverage by amount shown \
                      in dispersion.conf file")
    parser.add_option('-P', '--policy-name', dest='policy_name',
                      help="Specify storage policy name")

    options, args = parser.parse_args()

    if args:
        conffile = args.pop(0)

    c = ConfigParser()
    if not c.read(conffile):
        exit('Unable to read config file: %s' % conffile)
    conf = dict(c.items('dispersion'))

    if options.policy_name is None:
        policy = POLICIES.default
    else:
        policy = POLICIES.get_by_name(options.policy_name)
        if policy is None:
            exit('Unable to find policy: %s' % options.policy_name)
    print('Using storage policy: %s ' % policy.name)

    swift_dir = conf.get('swift_dir', '/etc/swift')
    dispersion_coverage = float(conf.get('dispersion_coverage', 1))
    retries = int(conf.get('retries', 5))
    concurrency = int(conf.get('concurrency', 25))
    endpoint_type = str(conf.get('endpoint_type', 'publicURL'))
    region_name = str(conf.get('region_name', ''))
    user_domain_name = str(conf.get('user_domain_name', ''))
    project_domain_name = str(conf.get('project_domain_name', ''))
    project_name = str(conf.get('project_name', ''))
    insecure = options.insecure \
        or config_true_value(conf.get('keystone_api_insecure', 'no'))
    container_populate = config_true_value(
        conf.get('container_populate', 'yes')) and not options.object_only
    object_populate = config_true_value(
        conf.get('object_populate', 'yes')) and not options.container_only

    if not (object_populate or container_populate):
        exit("Neither container or object populate is set to run")

    coropool = GreenPool(size=concurrency)
    retries_done = 0

    os_options = {'endpoint_type': endpoint_type}
    if user_domain_name:
        os_options['user_domain_name'] = user_domain_name
    if project_domain_name:
        os_options['project_domain_name'] = project_domain_name
    if project_name:
        os_options['project_name'] = project_name
    if region_name:
        os_options['region_name'] = region_name

    url, token = get_auth(conf['auth_url'], conf['auth_user'],
                          conf['auth_key'],
                          auth_version=conf.get('auth_version', '1.0'),
                          os_options=os_options,
                          insecure=insecure)
    account = url.rsplit('/', 1)[1]
    connpool = Pool(max_size=concurrency)
    headers = {}
    headers['X-Storage-Policy'] = policy.name
    connpool.create = lambda: SimpleClient(
        url=url, token=token, retries=retries)

    if container_populate:
        container_ring = Ring(swift_dir, ring_name='container')
        parts_left = dict((x, x)
                          for x in range(container_ring.partition_count))

        if options.no_overlap:
            with connpool.item() as conn:
                containers = [cont['name'] for cont in conn.get_account(
                    prefix='dispersion_%d' % policy.idx, full_listing=True)[1]]
            containers_listed = len(containers)
            if containers_listed > 0:
                for container in containers:
                    partition, _junk = container_ring.get_nodes(account,
                                                                container)
                    if partition in parts_left:
                        del parts_left[partition]

        item_type = 'containers'
        created = 0
        retries_done = 0
        need_to_create = need_to_queue = \
            dispersion_coverage / 100.0 * container_ring.partition_count
        begun = next_report = time()
        next_report += 2
        suffix = 0
        while need_to_queue >= 1 and parts_left:
            container = 'dispersion_%d_%d' % (policy.idx, suffix)
            part = container_ring.get_part(account, container)
            if part in parts_left:
                if suffix >= options.container_suffix_start:
                    coropool.spawn(put_container, connpool, container, report,
                                   headers)
                    sleep()
                else:
                    report(True)
                del parts_left[part]
                need_to_queue -= 1
            suffix += 1
        coropool.waitall()
        elapsed, elapsed_unit = get_time_units(time() - begun)
        print('\r\x1B[KCreated %d containers for dispersion reporting, '
              '%d%s, %d retries' %
              ((need_to_create - need_to_queue), round(elapsed), elapsed_unit,
               retries_done))
        if options.no_overlap:
            con_coverage = container_ring.partition_count - len(parts_left)
            print('\r\x1B[KTotal container coverage is now %.2f%%.' %
                  ((float(con_coverage) / container_ring.partition_count
                    * 100)))
        stdout.flush()

    if object_populate:
        container = 'dispersion_objects_%d' % policy.idx
        put_container(connpool, container, None, headers)
        object_ring = Ring(swift_dir, ring_name=policy.ring_name)
        parts_left = dict((x, x) for x in range(object_ring.partition_count))

        if options.no_overlap:
            with connpool.item() as conn:
                obj_container = [cont_b['name'] for cont_b in conn.get_account(
                    prefix=container, full_listing=True)[1]]
            if obj_container:
                with connpool.item() as conn:
                    objects = [o['name'] for o in
                               conn.get_container(container,
                                                  prefix='dispersion_',
                                                  full_listing=True)[1]]
                for my_object in objects:
                    partition = object_ring.get_part(account, container,
                                                     my_object)
                    if partition in parts_left:
                        del parts_left[partition]

        item_type = 'objects'
        created = 0
        retries_done = 0
        need_to_create = need_to_queue = \
            dispersion_coverage / 100.0 * object_ring.partition_count
        begun = next_report = time()
        next_report += 2
        suffix = 0
        while need_to_queue >= 1 and parts_left:
            obj = 'dispersion_%d' % suffix
            part = object_ring.get_part(account, container, obj)
            if part in parts_left:
                if suffix >= options.object_suffix_start:
                    coropool.spawn(
                        put_object, connpool, container, obj, report)
                    sleep()
                else:
                    report(True)
                del parts_left[part]
                need_to_queue -= 1
            suffix += 1
        coropool.waitall()
        elapsed, elapsed_unit = get_time_units(time() - begun)
        print('\r\x1B[KCreated %d objects for dispersion reporting, '
              '%d%s, %d retries' %
              ((need_to_create - need_to_queue), round(elapsed), elapsed_unit,
               retries_done))
        if options.no_overlap:
            obj_coverage = object_ring.partition_count - len(parts_left)
            print('\r\x1B[KTotal object coverage is now %.2f%%.' %
                  ((float(obj_coverage) / object_ring.partition_count * 100)))
        stdout.flush()