本文整理汇总了Python中swift.common.ring.Ring.get_part方法的典型用法代码示例。如果您正苦于以下问题:Python Ring.get_part方法的具体用法?Python Ring.get_part怎么用?Python Ring.get_part使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swift.common.ring.Ring
的用法示例。
在下文中一共展示了Ring.get_part方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SwiftUsageInput
# 需要导入模块: from swift.common.ring import Ring [as 别名]
# 或者: from swift.common.ring.Ring import get_part [as 别名]
class SwiftUsageInput(IDataSource):
def __init__(self, project, username, password, auth_url, ring_location="/etc/swift/account.ring.gz"):
self.__project=project
self.__username=username
self.__password=password
self.__auth_url=auth_url
self.__ring_location=ring_location
self.__ring=None
def get_data(self, **kwargs):
query_id=str(uuid.uuid4())
data_list=[]
from keystoneclient.v2_0 import client
from swift.common.ring import Ring
keystone=client.Client(username=self.__username, password=self.__password, tenant_name=self.__project, auth_url=self.__auth_url)
self.__ring = Ring(self.__ring_location)
tenants = [tenant.id for tenant in keystone.tenants.list()]
random.shuffle(tenants)
data=init_message()
data["swift"] = {}
for tenant, stats in zip(tenants, ThreadPool().map(self.fetch, tenants)):
if stats is not None:
data["swift"][tenant] = stats
return data
def fetch(self, tenant):
account = "AUTH_%s" % tenant
partition = self.__ring.get_part(account, None, None)
nodes = self.__ring.get_part_nodes(partition)
random.shuffle(nodes)
for node in nodes:
url = "http://%s:%s/%s/%s/%s" % (node["ip"], node["port"], node["device"], partition, account)
try:
response = requests.head(url, timeout=5)
if response.status_code == 204:
return {
"containers" : int(response.headers["x-account-container-count"]),
"objects" : int(response.headers["x-account-object-count"]),
"bytes" : int(response.headers["x-account-bytes-used"]),
"quota" : int(response.headers["x-account-meta-quota-bytes"]) if "x-account-meta-quota-bytes" in response.headers else None
}
elif response.status_code == 404:
return None
else:
log.warning("error fetching %s [HTTP %s]", url, response.status_code)
except:
log.warning("error fetching %s", url, exc_info=True)
log.error("failed to fetch info for tenant %s", tenant)
return None