分享

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

tntzbzc 发表于 2014-11-20 15:34:49 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 12004
导读
概述:
获取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上的位置信息;







源码解析部分:
下面是这部分代码的主要执行流程,代码中较重要的部分已经进行了相关的注释;
  1. import sys
  2. from optparse import OptionParser
  3. from swift.cli.info import print_info, InfoSystemExit
  4. if __name__ == '__main__':
  5.     parser = OptionParser('%prog [options] CONTAINER_DB_FILE')
  6.     parser.add_option(
  7.         '-d', '--swift-dir', default='/etc/swift',
  8.         help="Pass location of swift directory")
  9.     options, args = parser.parse_args()
  10.     if len(args) != 1:
  11.         sys.exit(parser.print_help())
  12.     try:
  13.         print_info('container', *args, **vars(options))
  14.     except InfoSystemExit:
  15.         sys.exit(1)
复制代码

  1. def print_info(db_type, db_file, swift_dir='/etc/swift'):
  2.     """
  3.     打印相关信息:
  4.     如果指定数据类型是account,则:
  5.         获取AccountBroker类的初始化对象;
  6.         从数据库获取account的全局数据信息,包括account/created_at/put_timestamp/delete_timestamp/container_count/object_count/bytes_used/hash/id等信息;
  7.         打印account的元数据信息;
  8.         初始化类Ring,实现加载/etc/swift/account.ring.gz中的数据,并以此初始化类RingData;
  9.         根据上面获取的ring的数据,获取account在ring上的位置信息;
  10.     如果指定数据类型是container,则:
  11.         获取ContainerBroker类的初始化对象;
  12.         从数据库获取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等信息;
  13.         打印container的元数据信息;
  14.         初始化类Ring,实现加载/etc/swift/container.ring.gz中的数据,并以此初始化类RingData;
  15.         根据上面获取的ring的数据,获取container在ring上的位置信息;
  16.     """
  17.     if db_type not in ('account', 'container'):
  18.         print "Unrecognized DB type: internal error"
  19.         raise InfoSystemExit()
  20.     if not os.path.exists(db_file) or not db_file.endswith('.db'):
  21.         print "DB file doesn't exist"
  22.         raise InfoSystemExit()
  23.     if not db_file.startswith(('/', './')):
  24.         db_file = './' + db_file  # don't break if the bare db file is given
  25.    
  26.     # 获取ContainerBroker类的初始化对象;
  27.     if db_type == 'account':
  28.         broker = AccountBroker(db_file)
  29.         datadir = ABDATADIR
  30.     else:
  31.         broker = ContainerBroker(db_file)
  32.         datadir = CBDATADIR
  33.    
  34.     # 获取相关数据类型的全局信息;
  35.     info = broker.get_info()
  36.     account = info['account']
  37.     container = info['container'] if db_type == 'container' else None
  38.    
  39.     # 打印指定数据类型的元数据信息;
  40.     print_db_info_metadata(db_type, info, broker.metadata)
  41.    
  42.     # 初始化类Ring,实现加载/etc/swift/account.ring.gz中的数据,并以此初始化类RingData;
  43.     try:
  44.         ring = Ring(swift_dir, ring_name=db_type)
  45.     except Exception:
  46.         ring = None
  47.     # 打印account或container在ring上的位置信息;
  48.     else:
  49.         print_ring_locations(ring, datadir, account, container)
复制代码

  1. def print_ring_locations(ring, datadir, account, container=None):
  2.     """
  3.     打印account或container在ring上的位置信息;
  4.     """
  5.     if ring is None or datadir is None or account is None:
  6.         raise ValueError('None type')
  7.     storage_type = 'account'
  8.     if container:
  9.         storage_type = 'container'
  10.     try:
  11.         part, nodes = ring.get_nodes(account, container, None)
  12.     except (ValueError, AttributeError):
  13.         raise ValueError('Ring error')
  14.     else:
  15.         path_hash = hash_path(account, container, None)
  16.         print '\nRing locations:'
  17.         for node in nodes:
  18.             print ('  %s:%s - /srv/node/%s/%s/%s.db' %
  19.                    (node['ip'], node['port'], node['device'],
  20.                     storage_directory(datadir, part, path_hash),
  21.                     path_hash))
  22.         print '\nnote: /srv/node is used as default value of `devices`, the ' \
  23.             'real value is set in the %s config file on each storage node.' % \
  24.             storage_type
复制代码






博客地址:http://blog.csdn.net/gaoxingnengjisuan
邮箱地址:dong.liu@siat.ac.cn



没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条