您的位置:首页 > 移动开发 > Swift

Swift源码分析----swift-container-info

2014-07-24 18:05 344 查看
感谢朋友支持本博客,欢迎共同探讨交流,由于能力和时间有限,错误之处在所难免,欢迎指正!

如果转载,请保留作者信息。

博客地址:http://blog.csdn.net/gaoxingnengjisuan

邮箱地址:dong.liu@siat.ac.cn


PS:最近没有登录博客,很多朋友的留言没有看见,这里道歉!还有就是本人较少上QQ,可以邮件交流。



概述部分:

获取ContainerBroker类的初始化对象;

从数据库获取container的全局数据信息,包括account/container/created_at/put_timestamp/delete_timestamp/object_count/bytes_used/reported_put_timestamp/reported_delete_timestamp/reported_object_count/reported_bytes_used/hash/id等信息;

打印container的元数据信息;

初始化类Ring,实现加载/etc/swift/container.ring.gz中的数据,并以此初始化类RingData;

根据上面获取的ring的数据,获取container在ring上的位置信息;

所以获取的数据为:

获取container的元数据信息;

获取container在ring上的位置信息;

源码解析部分:

下面是这部分代码的主要执行流程,代码中较重要的部分已经进行了相关的注释;

import sys
from optparse import OptionParser
from swift.cli.info import print_info, InfoSystemExit

if __name__ == '__main__':
    parser = OptionParser('%prog [options] CONTAINER_DB_FILE')
    parser.add_option(
        '-d', '--swift-dir', default='/etc/swift',
        help="Pass location of swift directory")

    options, args = parser.parse_args()

    if len(args) != 1:
        sys.exit(parser.print_help())

    try:
        print_info('container', *args, **vars(options))
    except InfoSystemExit:
        sys.exit(1)


def print_info(db_type, db_file, swift_dir='/etc/swift'):
    """
    打印相关信息:
    如果指定数据类型是account,则:
        获取AccountBroker类的初始化对象;
        从数据库获取account的全局数据信息,包括account/created_at/put_timestamp/delete_timestamp/container_count/object_count/bytes_used/hash/id等信息;
        打印account的元数据信息;
        初始化类Ring,实现加载/etc/swift/account.ring.gz中的数据,并以此初始化类RingData;
        根据上面获取的ring的数据,获取account在ring上的位置信息;
    如果指定数据类型是container,则:
        获取ContainerBroker类的初始化对象;
        从数据库获取container的全局数据信息,包括account/container/created_at/put_timestamp/delete_timestamp/object_count/bytes_used/reported_put_timestamp/reported_delete_timestamp/reported_object_count/reported_bytes_used/hash/id等信息;
        打印container的元数据信息;
        初始化类Ring,实现加载/etc/swift/container.ring.gz中的数据,并以此初始化类RingData;
        根据上面获取的ring的数据,获取container在ring上的位置信息;
    """
    if db_type not in ('account', 'container'):
        print "Unrecognized DB type: internal error"
        raise InfoSystemExit()
    if not os.path.exists(db_file) or not db_file.endswith('.db'):
        print "DB file doesn't exist"
        raise InfoSystemExit()
    if not db_file.startswith(('/', './')):
        db_file = './' + db_file  # don't break if the bare db file is given
    
    # 获取<span style="font-family: KaiTi_GB2312; ">ContainerBroker</span>类的初始化对象;
    if db_type == 'account':
        broker = AccountBroker(db_file)
        datadir = ABDATADIR
    else:
        broker = ContainerBroker(db_file)
        datadir = CBDATADIR
    
    # 获取相关数据类型的全局信息;
    info = broker.get_info()
    account = info['account']
    container = info['container'] if db_type == 'container' else None
    
    # 打印指定数据类型的元数据信息;
    print_db_info_metadata(db_type, info, broker.metadata)
    
    # 初始化类Ring,实现加载/etc/swift/account.ring.gz中的数据,并以此初始化类RingData;
    try:
        ring = Ring(swift_dir, ring_name=db_type)
    except Exception:
        ring = None
    # 打印account或container在ring上的位置信息;
    else:
        print_ring_locations(ring, datadir, account, container)


def print_ring_locations(ring, datadir, account, container=None):
    """
    打印account或container在ring上的位置信息;
    """
    if ring is None or datadir is None or account is None:
        raise ValueError('None type')
    storage_type = 'account'
    if container:
        storage_type = 'container'
    try:
        part, nodes = ring.get_nodes(account, container, None)
    except (ValueError, AttributeError):
        raise ValueError('Ring error')
    else:
        path_hash = hash_path(account, container, None)
        print '\nRing locations:'
        for node in nodes:
            print ('  %s:%s - /srv/node/%s/%s/%s.db' %
                   (node['ip'], node['port'], node['device'],
                    storage_directory(datadir, part, path_hash),
                    path_hash))
        print '\nnote: /srv/node is used as default value of `devices`, the ' \
            'real value is set in the %s config file on each storage node.' % \
            storage_type
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: