-
quota-
功能及代码分析
Cinder
配额使用介绍
一、封装抽象
quota
相关功能实现在
实现,
包
含了引擎、资源、驱动三个大类抽象封装。看
起来
用的是设计模式“抽象工厂模
式”,
可选择引擎做不
同的操作,
引擎里又可选
择驱动来操作管理资源,
资源里可注册不同的配
额项。
1.
资源
资源其实就是对配额的封装,
封装了资源名、
< br>默
认值、数据库统计函数
dbapi
等。
资源分类列表:
类名
描述
属性
name
(资源名)
、
.B
aseResource
定义单个
flag
(控制默认
配额资源
值
)
、
p
arent_project
类名
描述
属性
_id
(当前租户的
附租户)
、
quota
方法用于获取资
< br>源
的
使
用
量
、
default
方法用
户获取默认值
.A
bsoluteResourc
e
.R
eservableResou
r
ce
无预留的
资源
pass
无定义
sync
(
dbapi
方
可预留的
法名,统计配额
资源
使用信息。如范
例)
可统计的
.C
ountableResour
ce
资
源
,
cinder
代
码里没看
到使用
count
(
统
计
函
数)
.V
为卷类型
volume
_type_na
类名
olumeTypeResou
rce
描述
属性
定义的资
me
(卷类型名)
、
源,继承
volume_type_id
Reservab
leResour
ce <
/p>
(卷类型
id
)
注意:
?
ReservableResou
rce
:相比
BaseResource
,
多了
sync
方法,
sync
会被驱动调用,用于在
计算配额之前,
p>
先同步配额信息
(到本地和数
据
库
)
。
Reservab
leResource
只
能
用
于
project
绑定的资源。
?
CountableR
esource
:相比
BaseResource
,多
了
count
方法,<
/p>
count
方法必须给出一个函数,
自己
计算配额,
其返回值里会包含配额实际使
用值。
sync
范例:
ReservableResource
资源
'volume'
的
sync
:
._sync_volumes:
def
_sync_volumes
(context,
project_id, session, volume_type_id=None,
volume_type_name=None):
#
根据
volume_type_i
d
和
project_id
统计卷数量
和卷空间使用量
(volumes, _gigs)
= _volume_data_get_for_project(
context, project_id,
volume_type_id=volume_type_id, session=session)
key =
'volumes'
if
volume_type_name:
key +=
'_'
+ volume_type_name
return {key: volumes}
2.
引擎
定义了资源集。调用驱动来实现查询统计功能。
引擎列表:
类名
taEngine
描述
配额引擎,
基类
resources
'volumes',
'per_volume
_gigabytes'
,
umeTypeQuotaEngi
ne
卷
类
型
配
额引擎
'snapshots'
,
'gigabytes'
,
'backups',
'backup_gig
abytes'
uotaEngine
一
致
性
组
consiste
ncy
的
配
额
的
groups
类名
描述
引擎
resources
upQuotaEngine
3.
驱动
组
配
额
的
p>
引擎
groups
驱动是可配置的,对应配置项
quota_driver
,
默认值是
aDriver
(
'quota_driver
',
default=
,
help=
'Default
driver to use
for quota checks')
驱动列表:
名称
.D
bQuotaDriver
.N
estedDbQuotaDr
iver
描述
Number
tenant
Number
of
Block
Storage
snapshots
allowed
per
tenant.
of
volume
gigabytes
allowed
per
二、
quota
主要操作四张数据表:
1.
reserv
ations
表,定义每个项目配额的增量。
2.
qu
ota_usage
表,定义每个项目配额的已使用量和预留量。
3.
quota_classes
表,定义了配额类的配额。操作界面上的默认配额
就是保存在这个表里。
4.
quotas
表,定义了项目的配额。
是如果仅仅是调用
API
接口或者
p>
client
指
令
openstack
project create pro3
创建项
目,
是不会同时创建项目对应的专用配额的。
但
是
如果在管理界面上创建项目,
horizon
会同时
调
用
cinder
的
p>
quota
接
口
创
建
三
个
“gi
gabytes”、“volumes”、“snapshots”
cinder
p>
专用配额,另外还会调
neutron
、<
/p>
nova
的配额接口创建它们专用的配额。
三、
Quota driver
介绍
Driver
类关系图:
1.
aDriver
驱动类
1)
取得单个配额
方法名
说明
def
get_by_project(self,
根据项目
id
、
配<
/p>
context,
project_id,
< br>额
名
称
查
询
配
额,
quotas
表
根据配额类型、
def
get_by_class(self,
配
< br>额
名
称
查
询
quota_class,
配
表
def
get_default(self,
根据项目
id
、
配
额
,
quota_classes
context,
resource_name)
resource_name)
方法名
context,
project_id)
说明
resource,
额
名
称
查
询
默
认
配
额
,
quota_classes
表
2)
取得配额列表
方法名
def
get_defaults(self,
resources,
说明
查询默认配额列
表
表
根据配额类型查
询
配
< br>额
列
表
,
quota_classes
表
根
据项目
id
查
resources,
quotas
表没有,
,
quota_classes
context,
project_id=None)
def
get_class_quotas(self,
context,
quota_class,
defaults=True)
def
context,
resources,
get_project_quotas(self,
quotas
表,如果
方法名
project_id,
quota_class=None,
defaults=True,
usages=True)
3)
其他方法
说明
就
查<
/p>
询
quota_classes
表,得项
目配额
列表
方法名
def
说明
_get_quotas(self,
用于查询指定多
目
quota
,再查
quota_classes
调
用
context,
resources,
keys,
个配额,先查项
has_sync,
project_id=None)
def
limit_check(self,
_get_quota
s
()
resources,
检查<
/p>
values
是否
额
context,
values,
project_id=None)
有超过相应的配
def
context,
_reserve(self,
根据增量
deltas
resources,
计
算
quotas,
deltas,
expire,
quota_usage
表
方法名
project_id)
说明
里使用量、预留
量记录,插入预
留
作
为
reservations
字
典返回。这里没
有
update
数
据
库。
def
context,
deltas,
reserve(self,
resources,
expire=None,
调用
_r
eserve
()
计算预留、使用
量<
/p>
,
作
为
rese
rvations
字
典返回。
project_id=None)
def
commit(self,
context,
将
reservations
reservatio
ns,
project_id=None)
def
context,
字典提交修改进
数据库。
rollback(self,
回退之前提交修
reservations,
改
的
reservations
。
根据
project_id
p>
project_id=None)
def
方法名
说明
destroy_by_project(self,
删除其所有配额
context,
project_id)
把
退
2.
DbQuotaDriver
驱动类
过
期
的
def
expire(self,
context)
reservation
回
继承
aDriver
。适用于树
形项目的配额管理。咱们在目前
不用树形项目。
方法名
def
validate_nested_setup(sel
f,
ctxt,
resources,
project_tree,
fix_allocated_quotas=Fals
e)
def
_get_cur_project_allocat
e
d(self,
ctxt,
resource,
检
查
当
前
项
目
的配额
确
认
说明
project_tree
有嵌套型配额
方法名
project_tree)
说明
获
得<
/p>
单
个
默
认
配额,
调用父类
def
get_default(self,
DbQuotaDriver
resource,
方
法
get_default
。
如
果
项
目
有
父
项目,
配额为
0
。
获
得
默
认
配<
/p>
额
列表,
调用父类
def
get_defaults(self,
resources,
DbQuotaDriver
方
法
get_defaults
。
如
果
项
目
有
父
项目,
配额列表
全为
0
。
def
context,
_reserve(self,
resources,
预留
context,
project_id)
context,
project_id=None)
quotas,
deltas,
expire,
方法名
project_id)
四、指令功能介绍及代码分析
quotas
相关的指令:
说明
指令
quota-class-sh
ow
quota-class-up
date
quota-defaults
说明
Lists quotas for a quota
class.
Updates
quotas
for
a
quota
class.
Lists
default
quotas
for
a
tenant.
Delete
the
quotas
for
a
tenant.
Lists
quotas
for
a
tenant.
Updates
quotas
for
a
tenant.
Lists
quota
usage
for
a
quota-delete
quota-show
quota-update
quota-usage