1 创建Ring 代码具体分析

OpenStack_Swift——Ring组织架构中我们具体分析了Ring的具体工作过程,以下就Ring中添加�设备,删除设备,已经又一次平衡的实现过程作具体的介绍。

首先看RingBuilder类

    def __init__(self, part_power, replicas, min_part_hours):
#why 最大 2**32
if part_power > 32:
raise ValueError("part_power must be at most 32 (was %d)"
% (part_power,))
if replicas < 1:
raise ValueError("replicas must be at least 1 (was %.6f)"
% (replicas,))
if min_part_hours < 0:
raise ValueError("min_part_hours must be non-negative (was %d)"
% (min_part_hours,)) self.part_power = part_power
self.replicas = replicas
self.min_part_hours = min_part_hours
self.parts = 2 ** self.part_power #分区数量即虚拟节点数量
self.devs = [] #用来存放设备的加入�的设备都会加入到devs里
self.devs_changed = False
self.version = 0
        self._last_part_moves_epoch = None
self._last_part_moves = None self._last_part_gather_start = 0
self._remove_devs = []
self._ring = None

当中devs里面没一个dev含有的属性为:  dev  =  {'weight': 100.0, 'zone': 0, 'ip': '127.0.0.1', 'region': 0, 'parts': 0, 'id': 0, 'meta': 'some meta data', 'device': 'sda1', 'parts_wanted': 96, 'port': 6000}

当中part_wanted 是在加入�设备后设备须要的虚拟节点数,parts为设备已经被分配的虚拟节点。

在swift-ring-builder object.builder create 18 3 1

会调用Command类的create方法,详细看此方法:

 def create(self):
"""
swift-ring-builder object.builder create 18 3 1
swift-ring-builder <builder_file> create <part_power> <replicas>
<min_part_hours>
Creates <builder_file> with 2^<part_power> partitions and <replicas>.
<min_part_hours> is number of hours to restrict(限制) moving a partition more
than once(不止一次).
"""
if len(argv) < 6:
print Commands.create.__doc__.strip()
exit(EXIT_ERROR)
builder = RingBuilder(int(argv[3]), float(argv[4]), int(argv[5]))
backup_dir = pathjoin(dirname(argv[1]), 'backups')
try:
mkdir(backup_dir)
except OSError as err:
if err.errno != EEXIST:
raise
builder.save(pathjoin(backup_dir, '%d.' % time() + basename(argv[1])))
builder.save(argv[1]) #序列化
exit(EXIT_SUCCESS)

在第一次创建时,首先会推断/etc/swift下是否有object.builder文件,没有须要创建此文件,并依据命令中个的 18 3 1 来实例化RingBuilder,最后将builder序列化。

2 为Ring加入�设备

创建了.builder文件后,就须要加入�设备,以下为加入�一个设备的命令:swift-ring-builder object.builder add z2-127.0.0.1:6020/sdb2 100

当中 add 指明为加入�设备 z2 为zone2,127.0.0.1:6020/sdb2为设备ip地址,绑定的port以及设备中存储文件的磁盘,100为设备的权重

以下看加入�设备的详细实现:

这种方法在main方法中药推断/etc/swift下是否有object.builder文件,在第一步create中我们已经创建了此文件,如今仅仅须要将他反序列化回来并用存的数据来实例化RingBuilder类,然后调用Commands类的add方法:

 def add(self):
"""
swift-ring-builder <builder_file> add
[r<region>]z<zone>-<ip>:<port>[R<r_ip>:<r_port>]/<device_name>_<meta>
<weight>
[[r<region>]z<zone>-<ip>:<port>[R<r_ip>:<r_port>]/<device_name>_<meta>
<weight>] ... Where <r_ip> and <r_port> are replication ip and port. or swift-ring-builder <builder_file> add
[--region <region>] --zone <zone> --ip <ip> --port <port>
--replication-ip <r_ip> --replication-port <r_port>
--device <device_name> --meta <meta> --weight <weight> Adds devices to the ring with the given information. No partitions will be
assigned to the new device until after running 'rebalance'. This is so you
can make multiple device changes and rebalance them all just once.
"""
if len(argv) < 5 or len(argv) % 2 != 1:
print Commands.add.__doc__.strip()
exit(EXIT_ERROR) for new_dev in _parse_add_values(argv[3:]):
for dev in builder.devs:
if dev is None:
continue
if dev['ip'] == new_dev['ip'] and \
dev['port'] == new_dev['port'] and \
dev['device'] == new_dev['device']:
print 'Device %d already uses %s:%d/%s.' % \
(dev['id'], dev['ip'], dev['port'], dev['device'])
print "The on-disk ring builder is unchanged.\n"
exit(EXIT_ERROR)
dev_id = builder.add_dev(new_dev) #调用RingBuilder类的add_dev方法
print('Device %s with %s weight got id %s' %
(format_device(new_dev), new_dev['weight'], dev_id)) builder.save(argv[1])
exit(EXIT_SUCCESS)

将新加入�的设备会加入到devs[]中,当中会调用add_dev,以下看此方法的详细实现

 def add_dev(self, dev):
"""
Add a device to the ring. This device dict should have a minimum of the
following keys: ====== ===============================================================
id unique integer identifier amongst devices. Defaults to the next
id if the 'id' key is not provided in the dict
weight a float of the relative weight of this device as compared to
others; this indicates how many partitions the builder will try
to assign to this device
region integer indicating which region the device is in
zone integer indicating which zone the device is in; a given
partition will not be assigned to multiple devices within the
same (region, zone) pair if there is any alternative
ip the ip address of the device
port the tcp port of the device
device the device's name on disk (sdb1, for example)
meta general use 'extra' field; for example: the online date, the
hardware description
====== =============================================================== .. note::
This will not rebalance the ring immediately as you may want to
make multiple changes for a single rebalance. :param dev: device dict :returns: id of device
dev = {'weight': 100.0, 'zone': 0, 'ip': '127.0.0.1', 'region': 0, 'parts': 0, 'id': 0, 'meta': 'some meta data', 'device': 'sda1', 'parts_wanted': 96, 'port': 6000}
"""
if 'id' not in dev:
dev['id'] = 0
if self.devs:
dev['id'] = max(d['id'] for d in self.devs if d) + 1
if dev['id'] < len(self.devs) and self.devs[dev['id']] is not None:
raise exceptions.DuplicateDeviceError(
'Duplicate device id: %d' % dev['id'])
# Add holes to self.devs to ensure self.devs[dev['id']] will be the dev
while dev['id'] >= len(self.devs):
self.devs.append(None)
dev['weight'] = float(dev['weight'])
dev['parts'] = 0
self.devs[dev['id']] = dev
self._set_parts_wanted() #设置part_wanted
self.devs_changed = True
self.version += 1
return dev['id']

此方法主要是获得weight,并为设备dev中的id赋值,以及part_wanted赋值。当中part_wanted值的计算为:

((self.parts * self.replicas / sum(d['weight'] for d in self._iter_devs())) * dev['weight']) - dev['parts']

parts=2**power(此处在create方法中的18) replicas为备份数,此处为3

即part_wanted=((虚拟节点数*备份数/全部已加入�的节点的weight和)*当前设备的weight)-已被分配给此设备的虚拟节点数

至此 创建Ring和为Ring加入�设备解说完毕,再完毕这两个步骤后,就须要平衡环,并为replica2part2dev(备份到分区到设备的映射)赋值,平衡算法是Ring 的核心算法,在下一篇文章中详细解说。

因为本人水平有限,文中难免出现理解错误,敬请指正、交流,谢谢!

OpenStack_Swift源代码分析——创建Ring及加入�设备源代码算法具体分析的更多相关文章

  1. Openck_Swift源代码分析——添加、删除设备时算法详细的实现过程

    1 初始加入设备后.上传Object的详细流程  前几篇博客中,我们讲到环的基本原理即详细的实现过程,加入我们在初始创建Ring是执行例如以下几条命令: •swift-ring-builder obj ...

  2. 线程系列1--Java创建线程的几种方式及源码分析

    线程--创建线程的几种方式及源码分析 开始整理下线程的知识,感觉这块一直是盲区,工作中这些东西一直没有实际使用过,感觉也只是停留在初步的认识.前段时间一个内推的面试被问到,感觉一脸懵逼.面试官说,我的 ...

  3. Linux内核分析-创建新进程的过程

    分析Linux内核创建一个新进程的过程 task_struct结构体分析 struct task_struct{ volatile long state; //进程的状态 unsigned long ...

  4. Spring AOP 源码分析 - 创建代理对象

    1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...

  5. Spring IOC 容器源码分析 - 创建原始 bean 对象

    1. 简介 本篇文章是上一篇文章(创建单例 bean 的过程)的延续.在上一篇文章中,我们从战略层面上领略了doCreateBean方法的全过程.本篇文章,我们就从战术的层面上,详细分析doCreat ...

  6. Spring IOC 容器源码分析 - 创建单例 bean 的过程

    1. 简介 在上一篇文章中,我比较详细的分析了获取 bean 的方法,也就是getBean(String)的实现逻辑.对于已实例化好的单例 bean,getBean(String) 方法并不会再一次去 ...

  7. 基于binlog来分析mysql的行记录修改情况(python脚本分析)

          最近写完mysql flashback,突然发现还有有这种使用场景:有些情况下,可能会统计在某个时间段内,MySQL修改了多少数据量?发生了多少事务?主要是哪些表格发生变动?变动的数量是怎 ...

  8. 背景建模技术(二):BgsLibrary的框架、背景建模的37种算法性能分析、背景建模技术的挑战

    背景建模技术(二):BgsLibrary的框架.背景建模的37种算法性能分析.背景建模技术的挑战 1.基于MFC的BgsLibrary软件下载 下载地址:http://download.csdn.ne ...

  9. 从flink-example分析flink组件(1)WordCount batch实战及源码分析

    上一章<windows下flink示例程序的执行> 简单介绍了一下flink在windows下如何通过flink-webui运行已经打包完成的示例程序(jar),那么我们为什么要使用fli ...

随机推荐

  1. sql server 表空间

    在SqlServer2005中,建表时是默认把所有的表都保存在PRIMARY默认表空间中的.当数据库中表很多,并且数据量很大时,会导致数据库性能严重下降,有必要将一些大的表放到不同的表空间中去.主要的 ...

  2. JQuery自学代码---(一)

    /** * Created by wyl on 15-3-4. */ //Jquery是一个JavaScrioe库,它极大的简化了JavaScript编程 $(document).ready(func ...

  3. POP3、SMTP、IMAP和Exchange的区别在哪里?

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:永遇乐链接:http://www.zhihu.com/question/24605584/answer/29039357来源: ...

  4. Android Gson使用笔记

    最近在做一个java web service项目,需要用到jason,本人对java不是特别精通,于是开始搜索一些java平台的json类库. 发现了google的gson,因为之前对于protoco ...

  5. JBossESB教程(一)——开发环境的搭建

    前言 上篇对SOA的概述里面,在说SOA构建需要考虑什么时,提到了ESB,它作为SOA的基础设施而存在. 从这篇开始,将对ESB的其中一个实现JBossESB进行一个从头开始的讲解,既然是从头开始,那 ...

  6. POJ_2914_Minimum_Cut_(Stoer_Wagner)

    描述 http://poj.org/problem?id=2914 求无向图中最小割. Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K T ...

  7. POJ_3579_Median_(二分,查找第k大的值)

    描述 http://poj.org/problem?id=3579 给你一串数,共C(n,2)个差值(绝对值),求差值从大到小排序的中值,偶数向下取. Median Time Limit: 1000M ...

  8. 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 二)

    原文网址:http://www.cnblogs.com/ChinaKingKong/p/4964549.html 前言部分 part二部分主要讲解 iOS App IDs 的创建.概要文件的配置.以及 ...

  9. 【转】SourceTree的简单使用

    原文网址:http://blog.csdn.net/u011439289/article/details/42126507 今天开始参与公司项目的代码编写,公司内部采用的是gitlib,所以用到了So ...

  10. Ubuntu 使用apt-get时提示错误:无法获得锁 /var/lib/dpkg/lock

    推荐博客:http://blog.sina.com.cn/s/blog_5c1450a8010188ju.html Ubuntu 使用apt-get时提示错误:无法获得锁 /var/lib/dpkg/ ...