This is an automated email from the ASF dual-hosted git repository.

yjhjstz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 24e4c8863194148469ec1772b4249d957f501959
Author: Wenru Yan <[email protected]>
AuthorDate: Fri Feb 2 17:16:34 2024 +0800

    Fix query crash with minus memory_limit value in resgroup (#17053)
    
    Resgroup doesn't check memory_limit value when create a resource group,
    but when execute a query, and set query's memory limit, it will assert
    memory_limit=-1 if memory_limit < 0.
    
    Set memory_limit range to [0, INT_MAX] or set to default value -1.
---
 src/backend/commands/resgroupcmds.c                             | 8 ++++++++
 src/test/isolation2/expected/resgroup/resgroup_memory_limit.out | 8 ++++++++
 src/test/isolation2/sql/resgroup/resgroup_memory_limit.sql      | 5 +++++
 3 files changed, 21 insertions(+)

diff --git a/src/backend/commands/resgroupcmds.c 
b/src/backend/commands/resgroupcmds.c
index f799b99455..27b9993f5a 100644
--- a/src/backend/commands/resgroupcmds.c
+++ b/src/backend/commands/resgroupcmds.c
@@ -59,6 +59,9 @@
 #define RESGROUP_MIN_CPU_WEIGHT        (1)
 #define RESGROUP_MAX_CPU_WEIGHT        (500)
 
+#define RESGROUP_MIN_MEMORY_LIMIT (0)
+#define RESGROUP_DEFAULT_MEMORY_LIMIT (-1)
+
 #define RESGROUP_MIN_MIN_COST          (0)
 static int str2Int(const char *str, const char *prop);
 static ResGroupLimitType getResgroupOptionType(const char* defname);
@@ -907,6 +910,11 @@ checkResgroupCapLimit(ResGroupLimitType type, int value)
                                break;
 
                        case RESGROUP_LIMIT_TYPE_MEMORY_LIMIT:
+                               if (value < RESGROUP_MIN_MEMORY_LIMIT && value 
!= RESGROUP_DEFAULT_MEMORY_LIMIT)
+                                       ereport(ERROR,
+                                                       
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                                        errmsg("memory_limit 
range is [%d, INT_MAX] or equals to %d",
+                                                                   
RESGROUP_MIN_MEMORY_LIMIT, RESGROUP_DEFAULT_MEMORY_LIMIT)));
                                break;
 
                        case RESGROUP_LIMIT_TYPE_MIN_COST:
diff --git a/src/test/isolation2/expected/resgroup/resgroup_memory_limit.out 
b/src/test/isolation2/expected/resgroup/resgroup_memory_limit.out
index d06a4d4e30..d83db05430 100644
--- a/src/test/isolation2/expected/resgroup/resgroup_memory_limit.out
+++ b/src/test/isolation2/expected/resgroup/resgroup_memory_limit.out
@@ -9,6 +9,14 @@ DROP RESOURCE GROUP rg_memory_test;
 CREATE OR REPLACE FUNCTION func_memory_test (text) RETURNS text as /*in func*/ 
$$ /*in func*/ DECLARE /*in func*/ ln text; /*in func*/ tmp text[]; /*in func*/ 
match bool := false; /*in func*/ BEGIN /*in func*/ FOR ln IN execute 
format('explain analyze %s', $1) LOOP /*in func*/ IF NOT match THEN      /*in 
func*/ tmp := regexp_match(ln, 'Memory used:  (.*)'); /*in func*/ IF tmp IS NOT 
null THEN /*in func*/ match := true; /*in func*/ END IF; /*in func*/ END IF; 
/*in func*/ END LOOP; /*in fu [...]
 CREATE
 
+-- memory_limit range is [0, INT_MAX] or equals to -1
+CREATE RESOURCE GROUP rg_memory_range WITH(memory_limit=-100, 
cpu_max_percent=20, concurrency=2);
+ERROR:  memory_limit range is [0, INT_MAX] or equals to -1
+CREATE RESOURCE GROUP rg_memory_range WITH(memory_limit=-1, 
cpu_max_percent=20, concurrency=2);
+CREATE RESOURCE GROUP
+DROP RESOURCE GROUP rg_memory_range;
+DROP RESOURCE GROUP
+
 -- create a resource group with memory limit 100 Mb
 CREATE RESOURCE GROUP rg_memory_test WITH(memory_limit=100, 
cpu_max_percent=20, concurrency=2);
 CREATE
diff --git a/src/test/isolation2/sql/resgroup/resgroup_memory_limit.sql 
b/src/test/isolation2/sql/resgroup/resgroup_memory_limit.sql
index 4db7eb0995..1a0ea61ab2 100644
--- a/src/test/isolation2/sql/resgroup/resgroup_memory_limit.sql
+++ b/src/test/isolation2/sql/resgroup/resgroup_memory_limit.sql
@@ -26,6 +26,11 @@ END; /*in func*/
 $$ /*in func*/
 LANGUAGE plpgsql;
 
+-- memory_limit range is [0, INT_MAX] or equals to -1
+CREATE RESOURCE GROUP rg_memory_range WITH(memory_limit=-100, 
cpu_max_percent=20, concurrency=2);
+CREATE RESOURCE GROUP rg_memory_range WITH(memory_limit=-1, 
cpu_max_percent=20, concurrency=2);
+DROP RESOURCE GROUP rg_memory_range;
+
 -- create a resource group with memory limit 100 Mb
 CREATE RESOURCE GROUP rg_memory_test WITH(memory_limit=100, 
cpu_max_percent=20, concurrency=2);
 CREATE ROLE role_memory_test RESOURCE GROUP rg_memory_test;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to