• Oracle-PDB资源限制


    前言:

    在使用CDB容器数据库时,由于某些PDB出现高并发或者慢SQL,可能会出现PDB资源恶意争用的情况,最终导致一个PDB耗尽全部CDB资源。所以,为了避免PDB出现资源恶意争用,我们需要对PDB的资源,尤其是内存、CPU、IO、并发进行限制,以避免出现一个PDB耗尽全部CDB资源的情况。

    资源限制维度:

    ​资源限制方法与参数
    CPU

    1 CPU_COUNT

    2 DBMS_RESOURCE_MANAGER

    内存

    1 SGA(SGA_TARGET+SGA_MIN_SIZE+DB_CACHE_SIZE+SHARED_POOL_SIZE)

    2 PGA(PGA_AGGREGATE_LIMIT+PGA_AGGREGATE_TARGET)

    IO

    1 MAX_IOP

    2 SMAX_MBPS

    连接数pdb目前无法限制单个pdb的连接数,processes无法在pdb里面单独设置
    并行服务进程

    1 PARALLEL_MAX_SERVERS+PARALLEL_SERVERS_TARGET

    2 DBMS_RESOURCE_MANAGER

    CPU资源限制(CPU_COUNT):

    1 通过在pdb里面设置cpu_count,限制单个pdb的cpu使用个数,如果pdb里面没有设置,则默认pdb的cpu_count等于根容器CDB$ROOT的cpu_count

    2 CDB$ROOT的默认cpu_count为服务器cpu逻辑数量

    3 设置pdb1的cpu_count为1,限制最多使用1个逻辑cpu

    1. [oracle@rac19a ~]$ sqlplus / as sysdba
    2. SQL*Plus: Release 19.0.0.0.0 - Production on Mon Aug 22 13:30:00 2022
    3. Version 19.3.0.0.0
    4. Copyright (c) 1982, 2019, Oracle. All rights reserved.
    5. Connected to:
    6. Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    7. Version 19.3.0.0.0
    8. SQL> show parameter cpu_count
    9. NAME TYPE VALUE
    10. ------------------------------------ ----------- ------------------------------
    11. cpu_count integer 2
    12. SQL> show pdbs;
    13. CON_ID CON_NAME OPEN MODE RESTRICTED
    14. ---------- ------------------------------ ---------- ----------
    15. 2 PDB$SEED READ ONLY NO
    16. 3 PDB3 READ WRITE NO
    17. 4 PDB1 READ WRITE NO
    18. 5 PDB2 READ WRITE NO
    19. 7 PDB4 READ WRITE NO
    20. SQL> alter session set container=pdb1;
    21. Session altered.
    22. SQL> alter system set cpu_count=1;
    23. System altered.
    24. SQL> show parameter cpu_count
    25. NAME TYPE VALUE
    26. ------------------------------------ ----------- ------------------------------
    27. cpu_count integer 1
    28. SQL>
    29. SQL> set linesize 400
    30. set pagesize 400
    31. col name for a50
    32. col value for a50
    33. col dbname for a10
    34. select a.con_id,nvl(b.name,'CDB$ROOT') as dbname,a.name,a.value from v$system_parameter a,v$pdbs b
    35. where a.con_id=b.con_id(+) and a.name ='cpu_count'
    36. order by a.con_id;
    37. SQL>
    38. CON_ID DBNAME NAME VALUE
    39. ---------- ---------- -------------------------------------------------- --------------------------------------------------
    40. 0 CDB$ROOT cpu_count 2
    41. 4 PDB1 cpu_count 1

    CPU资源限制(DBMS_RESOURCE_MANAGER):

    1 通过DBMS_RESOURCE_MANAGER进行限制,DBMS_RESOURCE_MANAGER可以创建不同的资源限制组(利用率、配额、并行不同),通过将不同PDB设置在不到的组里面,从而限制特定 PDB 的 CPU 使用率,并行执行服务器的数量,内存使用量(exadata)

    2 DBMS_RESOURCE_MANAGER操作步骤

    1. 1. 创建初始化区域
    2. In the CDB:
    3. exec DBMS_RESOURCE_MANAGER.CREATE_PENDING_AREA();
    4. 2. 创建CDB资源计划
    5. BEGIN
    6. DBMS_RESOURCE_MANAGER.CREATE_CDB_PLAN(
    7. plan => 'cdb_plan',
    8. comment => 'CDB resource plan for cdb');
    9. END;
    10. /
    11. 3. 使用CREATE_CDB_PROFILE_DIRECTIVE创建pdb资源限制策略,这里创建3个等级a,b,c,对不同的pdb进行分组
    12. #plan:所属CDB资源计划,填上一步创建的CDB资源计划即可
    13. #profile pdb资源策略名称
    14. #shares:资源配额,a,b,c的shares分别对应(3,2,1),所以a的资源配额占比为50%[3/(3+2+1)]
    15. #cpu限制:最高使用cdb的cpu百分比
    16. #并行服务限制:最高使用cdb的并行服务进程百分比
    17. ---创建A策略
    18. BEGIN
    19. DBMS_RESOURCE_MANAGER.CREATE_CDB_PROFILE_DIRECTIVE(
    20. plan => 'cdb_plan',
    21. profile => 'A',
    22. shares => 3,
    23. utilization_limit => 100,
    24. parallel_server_limit => 100);
    25. END;
    26. /
    27. ---创建B策略
    28. BEGIN
    29. DBMS_RESOURCE_MANAGER.CREATE_CDB_PROFILE_DIRECTIVE(
    30. plan => 'cdb_plan',
    31. profile => 'B',
    32. shares => 2,
    33. utilization_limit => 40,
    34. parallel_server_limit => 40);
    35. END;
    36. /
    37. ---创建C策略
    38. BEGIN
    39. DBMS_RESOURCE_MANAGER.CREATE_CDB_PROFILE_DIRECTIVE(
    40. plan => 'cdb_plan',
    41. profile => 'C',
    42. shares => 1,
    43. utilization_limit => 20,
    44. parallel_server_limit => 20);
    45. END;
    46. /
    47. 4 验证初始化区域
    48. exec DBMS_RESOURCE_MANAGER.VALIDATE_PENDING_AREA();
    49. 5 提交初始化区域
    50. exec DBMS_RESOURCE_MANAGER.SUBMIT_PENDING_AREA();
    51. 6 CDB设置资源计划
    52. (In the CDB)
    53. ALTER SYSTEM SET RESOURCE_MANAGER_PLAN = 'cdb_plan' scope=both;
    54. 7 在pdb层面设置不同的资源限制组
    55. alter session set container=PDB1;
    56. alter system set db_performance_profile='A' scope=spfile;
    57. alter session set container=PDB2;
    58. alter system set db_performance_profile='B' scope=spfile;
    59. alter session set container=PDB3;
    60. alter system set db_performance_profile='C' scope=spfile;
    61. 8 重启pdb生效
    62. alter pluggable database all close immediate;
    63. alter pluggable database all open;
    64. 9 检查pdb资源限制配置
    65. col name for a30
    66. col value for a30
    67. set linesize 400
    68. alter session set container=CDB$ROOT;
    69. select inst_id, name, con_id, value, ispdb_modifiable from gv$system_parameter2 where name = 'db_performance_profile' order by 1,2,3,4;
    70. INST_ID NAME CON_ID VALUE ISPDB
    71. ---------- ------------------------------ ---------- ------------------------------ -----
    72. 1 db_performance_profile 0 TRUE
    73. 1 db_performance_profile 2 TRUE
    74. 1 db_performance_profile 3 C TRUE
    75. 1 db_performance_profile 4 A TRUE
    76. 1 db_performance_profile 5 B TRUE
    77. 1 db_performance_profile 7 TRUE
    78. alter session set container=CDB$ROOT;
    79. select p.name, shares, utilization_limit, profile,INSTANCE_CAGING,CPU_MANAGED,PARALLEL_SERVER_LIMIT from v$rsrc_plan r, v$pdbs p where r.con_id = p.con_id;
    80. NAME SHARES UTILIZATION_LIMIT PROFILE INS CPU PARALLEL_SERVER_LIMIT
    81. --------------- ---------- ----------------- ------------ --- --- ---------------------
    82. PDB$SEED 1 OFF ON
    83. PDB3 1 20 C OFF ON 20
    84. PDB1 3 A OFF ON
    85. PDB2 2 40 B OFF ON 40
    86. PDB4 1 OFF ON
    87. #PDB1 因为UTILIZATION_LIMIT和PARALLEL_SERVER_LIMIT 都是100%,所以没有限制,查询结果为空

    内存资源限制SGA:

    1 PDB的SGA限制主要通过参数SGA_MIN_SIZE限制SGA内存下限以及通过参数SGA_TARGET限制SGA内存上限

    2 通过sga_target参数限制单个PDB的最大SGA大小,当前root容器的sga设置为1104M

    1. SQL> show parameter sga
    2. NAME TYPE VALUE
    3. ------------------------------------ ----------- ------------------------------
    4. allow_group_access_to_sga boolean FALSE
    5. lock_sga boolean FALSE
    6. pre_page_sga boolean TRUE
    7. sga_max_size big integer 1104M
    8. sga_min_size big integer 0
    9. sga_target big integer 1104M
    10. unified_audit_sga_queue_size integer 1048576

    3 将pdb1 的sga最大大小限制为800M

    1. ---pdb 默认sga_target=0
    2. SQL> alter session set container=pdb1;
    3. Session altered.
    4. SQL> show parameter sga
    5. NAME TYPE VALUE
    6. ------------------------------------ ----------- ------------------------------
    7. sga_max_size big integer 1104M
    8. sga_min_size big integer 0
    9. sga_target big integer 0
    10. ---将sga_target设置为800M
    11. SQL> alter system set sga_target=800M;
    12. System altered.
    13. SQL> show parameter sga
    14. NAME TYPE VALUE
    15. ------------------------------------ ----------- ------------------------------
    16. allow_group_access_to_sga boolean FALSE
    17. lock_sga boolean FALSE
    18. pre_page_sga boolean TRUE
    19. sga_max_size big integer 1104M
    20. sga_min_size big integer 0
    21. sga_target big integer 500M
    22. unified_audit_sga_queue_size integer 1048576

    4 通过sga_min_size参数限制单个PDB的最小SGA大小

    5 将pdb1 的sga最小值限制为400M,注意:sga_min_size<50%(sga_target)

    1. SQL> alter session set container=pdb1;
    2. Session altered.
    3. SQL> alter system set sga_min_size=400M;
    4. System altered.
    5. SQL> show parameter sga
    6. NAME TYPE VALUE
    7. ------------------------------------ ----------- ------------------------------
    8. allow_group_access_to_sga boolean FALSE
    9. lock_sga boolean FALSE
    10. pre_page_sga boolean TRUE
    11. sga_max_size big integer 1104M
    12. sga_min_size big integer 400M
    13. sga_target big integer 800M
    14. unified_audit_sga_queue_size integer 1048576

    6 此外,还可以进一步对pdb的db_cache_size以及shared_pool_size最小值进行限制,注意:db_cache_size or shared_pool_size<50%(sga_target)

    1. SQL> alter system set db_cache_size=400M;
    2. System altered.
    3. SQL> alter system set shared_pool_size=200M;
    4. System altered.
    5. SQL>
    6. SQL> show parameter db_cache
    7. NAME TYPE VALUE
    8. ------------------------------------ ----------- ------------------------------
    9. db_cache_advice string ON
    10. db_cache_size big integer 400M
    11. SQL> show parameter shared
    12. NAME TYPE VALUE
    13. ------------------------------------ ----------- ------------------------------
    14. hi_shared_memory_address integer 0
    15. max_shared_servers integer
    16. shared_memory_address integer 0
    17. shared_pool_reserved_size big integer 18454937
    18. shared_pool_size big integer 200M
    19. shared_server_sessions integer
    20. shared_servers integer 1

    内存资源限制PGA:

    1 PDB的PGA限制主要通过参数pga_aggregate_target以及pga_aggregate_limit限PGA内存的上限

    pga_aggregate_target在PDB的设置需要遵循以下规则:

    1 小于或等于CDB级别设置的PGA_AGGREGATE_TARGET

    2 小于或等于CDB级别设置的50% PGA_AGGREGATE_LIMIT

    3 小于或等于PDB级别设置的50%  PGA_AGGREGATE_LIMIT

    PGA_AGGREGATE_LIMIT在PDB的设置需要遵循以下规则:

    1 小于或等于CDB级别设置的PGA_AGGREGATE_LIMIT

    2 大于或等于两倍PDB级别设置的PGA_AGGREGATE_TARGET

    2 限制单个PDB的pga大小,当前CDB根容器pga_aggregate_limit为2G,pga_aggregate_target为1G

    1. SQL> alter session set container=pdb1;
    2. Session altered.
    3. SQL> show parameter pga
    4. NAME TYPE VALUE
    5. ------------------------------------ ----------- ------------------------------
    6. pga_aggregate_limit big integer 2G
    7. pga_aggregate_target big integer 1G
    8. SQL> ALTER SYSTEM SET PGA_AGGREGATE_LIMIT = 600M SCOPE = BOTH;
    9. System altered.
    10. SQL> ALTER SYSTEM SET PGA_AGGREGATE_TARGET =300M SCOPE = BOTH;
    11. System altered.
    12. SQL> show parameter pga
    13. NAME TYPE VALUE
    14. ------------------------------------ ----------- ------------------------------
    15. pga_aggregate_limit big integer 600M
    16. pga_aggregate_target big integer 300M

    3 可以通过检查监控PDB的内存使用情况

    1. COLUMN PDB_NAME FORMAT A10
    2. SELECT r.CON_ID, p.PDB_NAME, r.SGA_BYTES/1024/1024, r.PGA_BYTES/1024/1024, r.BUFFER_CACHE_BYTES/1024/1024, r.SHARED_POOL_BYTES/1024/1024 FROM V$RSRCPDBMETRIC r, CDB_PDBS p WHERE r.CON_ID = p.CON_ID;

    IO限制:

    1 IO限制是Oracle12.2之后推出的新功能,通过参数MAX_IOPS 以及max_mbps限制数据库每秒的IO数量以及吞吐量

    2 MAX_IOPS:每秒的IOPS数量,默认为0

    3 MAX_MBPS:每秒的IO吞吐量(MB),默认为0

    4 通过在PDB里面设置,可以限制PDB的IOPS以及MBPS

    1. SQL> alter session set container=pdb1;
    2. Session altered.
    3. SQL> ALTER SYSTEM SET MAX_IOPS = 1000 SCOPE = BOTH;
    4. System altered.
    5. SQL> ALTER SYSTEM SET MAX_MBPS = 50 SCOPE = BOTH;
    6. System altered.
    7. SQL> show parameter MAX_
    8. NAME TYPE VALUE
    9. ------------------------------------ ----------- ------------------------------
    10. max_iops integer 1000
    11. max_mbps integer 50

    ​5 PDB监控IO使用情况

    1. SELECT r.con_id,
    2. p.pdb_name,
    3. r.begin_time,
    4. r.end_time,
    5. r.iops,
    6. r.iombps,
    7. r.iops_throttle_exempt,
    8. r.iombps_throttle_exempt,
    9. r.avg_io_throttle
    10. FROM v$rsrcpdbmetric r,
    11. cdb_pdbs p
    12. WHERE r.con_id = p.con_id
    13. ORDER BY p.pdb_name;

    并行服务进程限制(PARALLEL参数):

    1 并行服务进程通过PARALLEL_MAX_SERVERS与PARALLEL_SERVERS_TARGET进行PDB的限制

    PARALLEL_MAX_SERVERS:用于控制数据库最大的并行进程处理数量,默认值为PARALLEL_THREADS_PER_CPU * CPU_COUNT * concurrent_parallel_users * 5

    PARALLEL_SERVERS_TARGET:用于控制单个执行语句的并行最大数量,默认值为

    For a CDB:

    Equal to the PARALLEL_MAX_SERVERS value for the CDB.

    For a PDB or non-CDB:

    PARALLEL_THREADS_PER_CPU * CPU_COUNT * concurrent_parallel_users * 2

    4 在PDB里面并行服务进程数量,当前根容器parallel_max_servers为40,parallel_servers_target为40

    1. SQL> alter session set container=pdb1;
    2. Session altered.
    3. SQL> alter system set parallel_max_servers=16;
    4. System altered.
    5. SQL> alter system set parallel_servers_target=8;
    6. System altered.
    7. SQL>
    8. SQL>
    9. SQL> show parameter parallel_
    10. NAME TYPE VALUE
    11. ------------------------------------ ----------- ------------------------------
    12. parallel_max_servers integer 16
    13. parallel_min_degree string 1
    14. parallel_min_percent integer 0
    15. parallel_min_servers integer 4
    16. parallel_min_time_threshold string AUTO
    17. parallel_servers_target integer 8
    18. parallel_threads_per_cpu integer 1
    19. recovery_parallelism integer 0
    20. SQL>

    并行服务进程限制(DBMS_RESOURCE_MANAGER):

    并行服务进程通过DBMS_RESOURCE_MANAGER进行限制的方法参考CPU资源限制(DBMS_RESOURCE_MANAGER)章节的内容。       

    CREATE_CDB_PROFILE_DIRECTIVE存储过程对parallel_server_limit的设置就是对并行服务的限制;

    总结:

    以上为CDB容器数据库PDB的资源限制方式以及方法,在实际PDB的资源划分以及限制中,我们还需要根据数据库业务系统的等级以及压测结果进行评估与调整,从而确保CDB与PDB的数据库的运行稳定以及资源的合理分配。

     

  • 相关阅读:
    Halide 配置 visual studio
    HCIA-R&S自用笔记(22)STP状态与计时器、STP拓扑变化、STP配置及实验
    时代落在英伟达身上的是粒什么沙,国产GPU的机会又在哪?
    java-net-php-python-springboot药膳食疗系统计算机毕业设计程序
    经典面试题:为什么 ConcurrentHashMap 的读操作不需要加锁?
    手术麻醉信息系统源码 医院麻醉监护的功能覆盖整个手术与麻醉的全过程
    【C++系列P4】‘类与对象‘-三部曲——[类](2/3)
    个人怎么投资伦敦金?
    【DevOps】Rundeck以及Jenkins
    Java | Maven(知识点查询)
  • 原文地址:https://blog.csdn.net/sinat_36757755/article/details/126452842