Skip to content

Commit 06f35df

Browse files
committed
swift-api fixes
1 parent da1ef00 commit 06f35df

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

Diff for: api/swift_api/views.py

+65-16
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def storage_policies(request):
100100
ring.save(get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, sp_id))
101101

102102
r.hmset(key, data)
103-
except:
103+
except Exception:
104104
return JSONResponse('Error creating the Storage Policy', status=status.HTTP_500_INTERNAL_SERVER_ERROR)
105105

106106
return JSONResponse('Account created successfully', status=status.HTTP_201_CREATED)
@@ -159,8 +159,17 @@ def storage_policy_detail(request, storage_policy_id):
159159
object_node_devices = json.loads(object_node['devices'])
160160
device_detail = object_node_devices[device_id]
161161
device_detail['id'] = device[0]
162-
device_detail['region'] = r.hgetall('region:' + object_node['region_id'])['name']
163-
device_detail['zone'] = r.hgetall('zone:' + object_node['zone_id'])['name']
162+
r_id = object_node['region_id']
163+
z_id = object_node['zone_id']
164+
if r.exists('region:' + r_id):
165+
device_detail['region'] = r.hgetall('region:' + object_node['region_id'])['name']
166+
else:
167+
device_detail['region'] = r_id
168+
169+
if r.exists('region:' + r_id):
170+
device_detail['zone'] = r.hgetall('zone:' + object_node['zone_id'])['name']
171+
else:
172+
device_detail['zone'] = z_id
164173
devices.append(device_detail)
165174
storage_policy['devices'] = devices
166175
return JSONResponse(storage_policy, status=status.HTTP_200_OK)
@@ -251,8 +260,17 @@ def storage_policy_disks(request, storage_policy_id):
251260
object_node = r.hgetall('object_node:' + object_node_id)
252261
device_detail = json.loads(object_node['devices'])[device_id]
253262
device_detail['id'] = device
254-
device_detail['region'] = r.hgetall('region:' + object_node['region_id'])['name']
255-
device_detail['zone'] = r.hgetall('zone:' + object_node['zone_id'])['name']
263+
r_id = object_node['region_id']
264+
z_id = object_node['zone_id']
265+
if r.exists('region:' + r_id):
266+
device_detail['region'] = r.hgetall('region:' + object_node['region_id'])['name']
267+
else:
268+
device_detail['region'] = r_id
269+
270+
if r.exists('region:' + r_id):
271+
device_detail['zone'] = r.hgetall('zone:' + object_node['zone_id'])['name']
272+
else:
273+
device_detail['zone'] = z_id
256274
available_devices_detail.append(device_detail)
257275
return JSONResponse(available_devices_detail, status=status.HTTP_200_OK)
258276
else:
@@ -359,9 +377,20 @@ def deploy_storage_policy(request, storage_policy_id):
359377
ringdata.save(deploy_gzip_filename)
360378

361379
data = r.hgetall(key)
362-
update_sp_files(settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id, {'name': data['name'],
363-
'deprecated': data['deprecated'],
364-
'default': data['default']})
380+
policy = {'name': data['name'],
381+
'deprecated': data['deprecated'],
382+
'default': data['default']}
383+
384+
if data['policy_type'] == 'EC':
385+
policy.update({'policy_type': 'erasure_coding',
386+
'ec_type': data['ec_type'],
387+
'ec_num_data_fragments': data['ec_num_data_fragments'],
388+
'ec_num_parity_fragments': data['ec_num_parity_fragments'],
389+
'ec_object_segment_size': data['ec_object_segment_size'],
390+
'ec_duplication_factor': data['ec_duplication_factor']})
391+
update_sp_files(settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id, policy)
392+
else:
393+
update_sp_files(settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id, policy)
365394

366395
copyfile(tmp_policy_file, deploy_policy_file)
367396
rsync_dir_with_nodes(settings.SWIFT_CFG_DEPLOY_DIR, '/etc/swift')
@@ -371,7 +400,7 @@ def deploy_storage_policy(request, storage_policy_id):
371400
return JSONResponse('Storage policy deployed correctly', status=status.HTTP_200_OK)
372401
except RedisError:
373402
return JSONResponse('Storage policy could not be deployed', status=status.HTTP_400_BAD_REQUEST)
374-
except exceptions.RingBuilderError, e:
403+
except exceptions.RingBuilderError as e:
375404
return JSONResponse('Storage policy could not be deployed. Error message: %s' % e.message, status=status.HTTP_400_BAD_REQUEST)
376405
else:
377406
return JSONResponse('Storage policy not found.', status=status.HTTP_404_NOT_FOUND)
@@ -426,13 +455,13 @@ def load_swift_policies(request):
426455
files = glob.glob(pattern)
427456

428457
try:
458+
sp_id_list = []
429459
for builder_file in files:
430460
builder = RingBuilder.load(builder_file)
431461
if '-' in builder_file:
432462
sp_id = builder_file.split('.')[0].split('-')[-1]
433463
key = 'storage-policy:' + sp_id
434-
if int(sp_id) > r.get('storage-policies:id'):
435-
r.set('storage-policies:id', sp_id)
464+
sp_id_list.append(int(sp_id))
436465
else:
437466
key = 'storage-policy:0'
438467

@@ -457,10 +486,12 @@ def load_swift_policies(request):
457486
nodes_data[node] = r.hgetall(node)
458487

459488
for device in builder.devs:
489+
if device is None:
490+
continue
460491
try:
461492
inet_aton(device['ip'])
462493
device['ip'] = next((nodes_data[node]['name'] for node in nodes_data if nodes_data[node]['ip'] == device['ip']), device['ip'])
463-
except:
494+
except Exception:
464495
pass
465496
devices.append((device['ip'] + ':' + device['device'], device['id']))
466497

@@ -470,18 +501,36 @@ def load_swift_policies(request):
470501
'time': builder.min_part_hours,
471502
'devices': json.dumps(devices),
472503
'deployed': 'True',
473-
'policy_type': policy_type if policy_type else 'Replication',
474504
'partition_power': int(math.log(builder.parts, 2)),
475505
'replicas': int(builder.replicas)
476506
}
507+
if policy_type == 'Replication':
508+
data.update({'policy_type': policy_type if policy_type else 'Replication'})
509+
else:
510+
ec_type = config_parser.get(key, 'ec_type') if config_parser.has_option(key, 'ec_type') else 'liberasurecode_rs_vand'
511+
ec_num_parity_fragments = config_parser.get(key, 'ec_num_parity_fragments')
512+
ec_num_data_fragments = config_parser.get(key, 'ec_num_data_fragments')
513+
ec_duplication_factor = config_parser.get(key, 'ec_duplication_factor') if config_parser.has_option(key, 'ec_duplication_factor') else '1'
514+
ec_object_segment_size = config_parser.get(key, 'ec_object_segment_size') if config_parser.has_option(key, 'ec_object_segment_size') else '1048576'
515+
data.update({'policy_type': 'EC',
516+
'ec_type': ec_type,
517+
'ec_num_data_fragments': ec_num_data_fragments,
518+
'ec_num_parity_fragments': ec_num_parity_fragments,
519+
'ec_duplication_factor': ec_duplication_factor,
520+
'ec_object_segment_size': ec_object_segment_size})
477521

478522
r.hmset(key, data)
479523

480524
else:
481525
pass
526+
if len(sp_id_list):
527+
max_sp_id = max(sp_id_list)
528+
r.set('storage-policies:id', max_sp_id)
529+
else:
530+
pass
482531

483532
except RedisError:
484-
return JSONResponse('Policies could not be loaded', status=status.HTTP_500_INTERNAL_SERVER_ERROR)
533+
return JSONResponse('Policies could not be loaded', status=status.HTTP_500_INTERNAL_SERVER_ERROR)
485534

486535
return JSONResponse('Policies loaded correctly', status=status.HTTP_200_OK)
487536

@@ -805,8 +854,8 @@ def create_container(request, project_id, container_name):
805854
url = settings.SWIFT_URL + "/AUTH_" + project_id
806855

807856
swift_client.put_container(url, token, container_name, headers)
808-
except Exception as ex:
809-
return JSONResponse(ex.message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
857+
except Exception as e:
858+
return JSONResponse(e.message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
810859

811860
return JSONResponse("Container Policy updated correctly", status=status.HTTP_201_CREATED)
812861
return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)

Diff for: requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
# be installed in a specific order.
88
#
99
Django<2.0,>=1.8
10-
redis==2.8.0
1110
djangorestframework<3.4,>=3.3
1211
pika==0.13.1
12+
redis
1313
pyparsing
1414
python-keystoneclient
1515
python-swiftclient

0 commit comments

Comments
 (0)