分享

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

tntzbzc 发表于 2014-11-20 15:34:56 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 11881
导读
本文主要分析获取object的全局数据信息较简单





  1. if __name__ == '__main__':  
  2.     parser = OptionParser()  
  3.     parser.set_defaults(check_etag=True, swift_dir='/etc/swift')  
  4.     parser.add_option(  
  5.         '-n', '--no-check-etag',  
  6.         action="store_false", dest="check_etag",  
  7.         help="Don't verify file contents against stored etag")  
  8.     parser.add_option(  
  9.         '-d', '--swift-dir',  
  10.         help="Pass location of swift directory")  
  11.   
  12.     options, args = parser.parse_args()  
  13.   
  14.     if len(args) < 1:  
  15.         print "Usage: %s [-n] [-d] OBJECT_FILE" % sys.argv[0]  
  16.         sys.exit(1)  
  17.   
  18.     print_object_info(args[0], check_etag=options.check_etag,  
  19.                       swift_dir=options.swift_dir)  
复制代码


  1. def print_object_info(datafile, check_etag=True, swift_dir='/etc/swift'):  
  2.     if not os.path.exists(datafile) or not datafile.endswith('.data'):  
  3.         print "Data file doesn't exist"  
  4.         sys.exit(1)  
  5.     try:  
  6.         ring = Ring(swift_dir, ring_name='object')  
  7.     except Exception:  
  8.         ring = None  
  9.     fp = open(datafile, 'rb')  
  10.     metadata = read_metadata(fp)  
  11.     path = metadata.pop('name', '')  
  12.     content_type = metadata.pop('Content-Type', '')  
  13.     ts = metadata.pop('X-Timestamp', '')  
  14.     etag = metadata.pop('ETag', '')  
  15.     length = metadata.pop('Content-Length', '')  
  16.     if path:  
  17.         print 'Path: %s' % path  
  18.         account, container, obj = path.split('/', 3)[1:]  
  19.         print '  Account: %s' % account  
  20.         print '  Container: %s' % container  
  21.         print '  Object: %s' % obj  
  22.         obj_hash = hash_path(account, container, obj)  
  23.         print '  Object hash: %s' % obj_hash  
  24.     else:  
  25.         print 'Path: Not found in metadata'  
  26.     if content_type:  
  27.         print 'Content-Type: %s' % content_type  
  28.     else:  
  29.         print 'Content-Type: Not found in metadata'  
  30.     if ts:  
  31.         print 'Timestamp: %s (%s)' % (datetime.fromtimestamp(float(ts)), ts)  
  32.     else:  
  33.         print 'Timestamp: Not found in metadata'  
  34.   
  35.     file_len = None  
  36.     if check_etag:  
  37.         h = md5()  
  38.         file_len = 0  
  39.         while True:  
  40.             data = fp.read(64 * 1024)  
  41.             if not data:  
  42.                 break  
  43.             h.update(data)  
  44.             file_len += len(data)  
  45.         h = h.hexdigest()  
  46.         if etag:  
  47.             if h == etag:  
  48.                 print 'ETag: %s (valid)' % etag  
  49.             else:  
  50.                 print "Etag: %s doesn't match file hash of %s!" % (etag, h)  
  51.         else:  
  52.             print 'ETag: Not found in metadata'  
  53.     else:  
  54.         print 'ETag: %s (not checked)' % etag  
  55.         file_len = os.fstat(fp.fileno()).st_size  
  56.   
  57.     if length:  
  58.         if file_len == int(length):  
  59.             print 'Content-Length: %s (valid)' % length  
  60.         else:  
  61.             print "Content-Length: %s doesn't match file length of %s" % (  
  62.                 length, file_len)  
  63.     else:  
  64.         print 'Content-Length: Not found in metadata'  
  65.     print 'User Metadata: %s' % metadata  
  66.     if ring is not None:  
  67.         print 'Ring locations:'  
  68.         part, nodes = ring.get_nodes(account, container, obj)  
  69.         for node in nodes:  
  70.             print ('  %s:%s - /srv/node/%s/%s/%s.data' %  
  71.                    (node['ip'], node['port'], node['device'],  
  72.                     storage_directory('objects', part, obj_hash), ts))  
  73.         print  
  74.         print 'note: /srv/node is used as default value of `devices`, '\  
  75.               'the real value is set in object-server.conf '\  
  76.               'on each storage node.'  
  77.     fp.close()  
复制代码








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

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

本版积分规则

关闭

推荐上一条 /2 下一条