作者:姚嵩,不知道是地球人还是外星人,知道的可以留言告诉小编...
爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
本文约 1600 字,预计阅读需要 5 分钟。
在 OceanBase 中创建租户时,时常会遇到因内存或 CPU 等资源不足导致的租户创建失败的情况。
本文将讨论计算 OceanBase 4.x 可用 CPU 的核心逻辑。
OceanBase_CE-v4.2.1.8
在开始之前,需要先了解几个与 CPU 相关的配置。
enable_rebalance
不影响超分。cpu_count
中为系统/主机预留的 CPU 核数,剩余的 CPU 才会被数据库使用。可以通过以下 SQL 语句查询两个版本 OBServer 主机的 CPU 使用情况。
2.2.77 版本
select svr_ip, zone, cpu_total, cpu_assigned, cpu_assigned_percent,
cpu_capacity ,cpu_max_assigned ,
(cpu_capacity-cpu_assigned) cpu_remained ,
(cpu_total-cpu_max_assigned) cpu_max_remained
from oceanbase.__all_virtual_server_stat ;
4.2.1.8 版本
select svr_ip, zone, cpu_capacity_max, cpu_assigned,
cpu_assigned/cpu_capacity cpu_assigned_percent,
cpu_capacity ,cpu_assigned_max ,
(cpu_capacity-cpu_assigned) cpu_remained ,
(cpu_capacity_max-cpu_assigned_max) cpu_max_remained
from oceanbase.GV$OB_SERVERS ;
在上述 SQL 中,查询与 CPU 有关的字段:
cpu_capacity * resource_hard_limit(配置)
,即:(cpu_count(配置) - cpu_reserved(配置)) * resource_hard_limit(配置)
。cpu_total
。min_cpu
之和(oceanbase.gv$unit
表中 min_cpu
之和)。max_cpu
之和(oceanbase.gv$unit
表中 max_cpu
之和)。cpu_max_assigned
。cpu_assigned/cpu_capacity
)。cpu_count(配置) - cpu_reserved(配置)
。cpu_count(配置)
。min_cpu(最大不会超过 cpu_capacity)
。计算公式为:cpu_capacity - cpu_assigned
。cpu_total
)。计算公式为:cpu_total - cpu_max_assigned
。在定义 resource unit
的时候,unit 可以定义 min_cpu
和 max_cpu
。
参考以下四个原则:
min_cpu
之和要小于 oceanbase.__all_virtual_server_stat
表的 cpu_capacity
字段值。max_cpu
之和要小于 oceanbase.__all_virtual_server_stat
表的 cpu_total
字段值。min_cpu
总和的上限是物理 CPU 的核数。max_cpu
总和的上限是是物理 CPU 的核数 * 超分百分比。两个版本修改配置 cpu_count
的步骤。
2.2.77 版本
-- 查看 resource_hard_limit、cpu_count、cpu_reserved
show parameters where name in ('resource_hard_limit','cpu_count','cpu_reserved') \G
-- 查看允许 OB 使用的总 CPU(cpu_total)
select svr_ip, zone, cpu_total, cpu_assigned, cpu_assigned_percent,
cpu_capacity ,cpu_max_assigned
from oceanbase.__all_virtual_server_stat ;
-- 设置 cpu_count 为 20(低版本 cpu_count 的修改需要重启 OBServer 进程)
alter system set cpu_count=20 ;
-- 重启 OBServer (略)
-- 查看允许 OB 使用的 CPU(cpu_total)
select svr_ip, zone, cpu_total, cpu_assigned, cpu_assigned_percent,
cpu_capacity ,cpu_max_assigned ,(cpu_capacity-cpu_assigned) cpu_remained
from oceanbase.__all_virtual_server_stat ;
4.2.1.8 版本
-- 查看 resource_hard_limit 、cpu_count
show parameters where name in ('resource_hard_limit', 'cpu_count') \G
-- 查看允许 OB 使用的 CPU
select svr_ip, zone, cpu_capacity_max, cpu_assigned, cpu_assigned/cpu_capacity cpu_assigned_percent,
cpu_capacity ,cpu_assigned_max ,
(cpu_capacity-cpu_assigned) cpu_remained ,
(cpu_capacity_max-cpu_assigned_max) cpu_max_remained
from oceanbase.GV$OB_SERVERS ;
-- 设置 cpu_count 为 20
alter system set cpu_count=20 ;
-- 查看允许使用的 CPU
select svr_ip, zone, cpu_capacity_max, cpu_assigned, cpu_assigned/cpu_capacity cpu_assigned_percent,
cpu_capacity ,cpu_assigned_max ,
(cpu_capacity-cpu_assigned) cpu_remained ,
(cpu_capacity_max-cpu_assigned_max) cpu_max_remained
from oceanbase.GV$OB_SERVERS ;
测试两个版本在主机超分的情况下剩余的 min_cpu
和 max_cpu
。
2.2.77 版本
-- 配置设置
alter system set __min_full_resource_pool_memory=1073741824 ;
show parameters where name in ('resource_hard_limit','cpu_count','cpu_reserved') \G
alter system set resource_hard_limit=120 ;
alter system set cpu_count=40 ;
alter system set cpu_reserved=2 ;
show parameters where name in ('resource_hard_limit','cpu_count','cpu_reserved') \G
-- 重启 OB 集群(略)
-- 查看当前的 CPU 限额
select svr_ip, zone, cpu_total, cpu_assigned, cpu_assigned_percent,
cpu_capacity ,cpu_max_assigned ,
(cpu_capacity-cpu_assigned) cpu_remained ,
(cpu_total-cpu_max_assigned) cpu_max_remained
from oceanbase.__all_virtual_server_stat ;
点击放大,建议在 PC 下浏览
-- 设置一个 min_cpu 为 27.5,max_cpu 为 32.6 的租户(内存配置为 1G):
-- 创建 resource unit
create resource unit mini min_cpu=27.5, max_cpu=32.6, max_memory='1G',min_memory='1G',max_iops=10000,min_iops=1000,max_session_num=1000000,max_disk_size='10G';
-- 查看 zone 名称(假设只有一个 zone: META_ZONE_1)
select distinct zone from oceanbase.__all_zone ;
-- 创建 resource pool,创建 resource pool 时会校验主机资源规格是否充足
create resource pool mini_pool_1 unit=mini ,unit_num=1 ,zone_list=('META_ZONE_1');
-- 创建 tenant
create tenant t1 charset='utf8mb4',primary_zone='META_ZONE_1',resource_pool_list=('mini_pool_1') set ob_tcp_invited_nodes='%';
-- 调整 resource unit 的资源规格
alter resource unit mini min_cpu=27.6 ; -- 报错
alter resource unit mini max_cpu=32.7 ; -- 报错
-- 清理
drop tenant if exists t1 force ;
drop resource pool if exists mini_pool_1 ;
drop resource unit if exists mini ;
4.2.1.8 版本
-- 配置设置
alter system set __min_full_resource_pool_memory=1073741824 ;
show parameters where name in ('resource_hard_limit','cpu_count') \G
alter system set resource_hard_limit=120 ;
alter system set cpu_count=40 ;
show parameters where name in ('resource_hard_limit','cpu_count') \G
-- 查看当前的 CPU 限额
select svr_ip, zone, cpu_capacity_max, cpu_assigned,
cpu_assigned/cpu_capacity cpu_assigned_percent,
cpu_capacity ,cpu_assigned_max ,
(cpu_capacity-cpu_assigned) cpu_remained ,
(cpu_capacity_max-cpu_assigned_max) cpu_max_remained
from oceanbase.GV$OB_SERVERS ; -- cpu_capacity_max 等于 cpu_count(配置)
-- 设置一个 min_cpu 为 33,max_cpu 为 41 的租户:
-- 创建 resource unit
create resource unit mini max_cpu=41,min_cpu=33,memory_size='1G',log_disk_size='2G' ;
-- 查看 zone 名称(假设只有一个 zone: zone1)
select distinct zone from oceanbase.__all_zone ;
-- 创建 resource pool,创建 resource pool 时会校验主机资源规格是否充足
create resource pool mini_pool_1 unit=mini ,unit_num=1 ,zone_list=('zone1');
-- 创建 tenant
create tenant t1 charset='utf8mb4',primary_zone='zone1', resource_pool_list=('mini_pool_1') set ob_tcp_invited_nodes='%';
-- 调整 resource unit 的资源规格
alter resource unit mini min_cpu=33.1 ; -- 报错
alter resource unit mini max_cpu=41.1 ; -- 报错
-- 清理
drop tenant if exists t1 force ;
drop resource pool if exists mini_pool_1 ;
drop resource unit if exists mini ;
在 OceanBase 4.x 之前,cpu_count
表示主机的总 CPU 核数,cpu_reserved
表示预留给主机使用的 CPU 核数;除了预留给主机使用的 CPU 外,其他 CPU 都是给租户使用。
在 OceanBase 4.x 之后,cpu_count
表示供 OB 租户使用的总 CPU 核数,不再包含预留给主机使用的 CPU 了,cpu_reserved
配置也删除了。
min_cpu
的总和的上限cpu_count - cpu_reserved
cpu_count
max_cpu
的总和的上限max_cpu
的总和上限等于 min_cpu
max_cpu
的总和上限等于 min_cpu * resource_hard_limit
max_cpu - min_cpu
即是允许超分的 CPU 核数。cpu_count
的数量可以自行定义,即使超过主机 CPU 总核数也不会报错,如果将 cpu_count
设置的非常大,且将这些都分配给租户使用了,这实际上已经出现了 CPU 超分的情况,可能导致 CPU 使用率非常高,租户之间相互影响。