Skip to content

Commit 723ee28

Browse files
RMTTmy-ship-it
authored andcommitted
Add one more hierarchy for resgroup cgroup root (#16732)
Add one more hierarchy for resource group when use cgroup v2. Current leaf node in the gpdb cgroup hierarchy is: /sys/fs/cgroup/gpdb/<oid>, it's ok for gpdb workflow. But for some extensions which want to use gpdb cgroup hierarchy, it's not convenient. Extensions like plcontainer want create sub-cgroup under /sys/fs/cgroup/<oid> as new leaf node, it's not possible in current hierarchy, because of no internal processes constraint of cgroup v2. This commit use a new hierarchy to adopt extensions which want to use gpdb cgroup hierarchy, and the modification is tiny: move processes from /sys/fs/cgroup/<oid>/cgroup.procs to /sys/fs/cgroup/gpdb/<oid>/queries/cgroup.procs, and keep limitations in /sys/fs/cgroup/<oid>. With this modification, extensions which want to use gpdb cgroup hierarchy can create sub cgroup under /sys/fs/cgroup/gpdb/<oid>. For example, plcontainer will create a cgroup /sys/fs/cgroup/gpdb/<oid>/docker-12345 and put processes into it.
1 parent 92bee97 commit 723ee28

6 files changed

Lines changed: 25 additions & 16 deletions

File tree

src/backend/utils/resgroup/cgroup-ops-linux-v1.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,11 @@ createcgroup_v1(Oid group)
652652
{
653653
int retry = 0;
654654

655-
if (!createDir(group, CGROUP_COMPONENT_CPU) ||
656-
!createDir(group, CGROUP_COMPONENT_CPUACCT) ||
657-
!createDir(group, CGROUP_COMPONENT_MEMORY) ||
655+
if (!createDir(group, CGROUP_COMPONENT_CPU, "") ||
656+
!createDir(group, CGROUP_COMPONENT_CPUACCT, "") ||
657+
!createDir(group, CGROUP_COMPONENT_MEMORY, "") ||
658658
(gp_resource_group_enable_cgroup_cpuset &&
659-
!createDir(group, CGROUP_COMPONENT_CPUSET)))
659+
!createDir(group, CGROUP_COMPONENT_CPUSET, "")))
660660
{
661661
CGROUP_ERROR("can't create cgroup for resource group '%d': %m", group);
662662
}
@@ -705,7 +705,7 @@ create_default_cpuset_group_v1(void)
705705
CGroupComponentType component = CGROUP_COMPONENT_CPUSET;
706706
int retry = 0;
707707

708-
if (!createDir(DEFAULT_CPUSET_GROUP_ID, component))
708+
if (!createDir(DEFAULT_CPUSET_GROUP_ID, component, ""))
709709
{
710710
CGROUP_ERROR("can't create cpuset cgroup for resgroup '%d': %m",
711711
DEFAULT_CPUSET_GROUP_ID);

src/backend/utils/resgroup/cgroup-ops-linux-v2.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ createcgroup_v2(Oid group)
386386
{
387387
int retry = 0;
388388

389-
if (!createDir(group, CGROUP_COMPONENT_PLAIN))
389+
if (!createDir(group, CGROUP_COMPONENT_PLAIN, "") ||
390+
!createDir(group, CGROUP_COMPONENT_PLAIN, CGROUPV2_LEAF_INDENTIFIER))
390391
{
391392
CGROUP_ERROR("can't create cgroup for resource group '%d': %m", group);
392393
}
@@ -418,7 +419,7 @@ create_default_cpuset_group_v2(void)
418419
CGroupComponentType component = CGROUP_COMPONENT_PLAIN;
419420
int retry = 0;
420421

421-
if (!createDir(DEFAULT_CPUSET_GROUP_ID, component))
422+
if (!createDir(DEFAULT_CPUSET_GROUP_ID, component, ""))
422423
{
423424
CGROUP_ERROR("can't create cpuset cgroup for resgroup '%d': %m",
424425
DEFAULT_CPUSET_GROUP_ID);
@@ -466,6 +467,7 @@ create_default_cpuset_group_v2(void)
466467
static void
467468
attachcgroup_v2(Oid group, int pid, bool is_cpuset_enabled)
468469
{
470+
char path_of_leaf[MAXPATHLEN];
469471
/*
470472
* needn't write to file if the pid has already been written in.
471473
* Unless it has not been written or the group has changed or
@@ -474,8 +476,9 @@ attachcgroup_v2(Oid group, int pid, bool is_cpuset_enabled)
474476
if (IsUnderPostmaster && group == currentGroupIdInCGroup)
475477
return;
476478

479+
pg_sprintf(path_of_leaf, "%s/cgroup.procs", CGROUPV2_LEAF_INDENTIFIER);
477480
writeInt64(group, BASEDIR_GPDB, CGROUP_COMPONENT_PLAIN,
478-
"cgroup.procs", pid);
481+
path_of_leaf, pid);
479482

480483
/*
481484
* Do not assign the process to cgroup/memory for now.
@@ -499,6 +502,7 @@ detachcgroup_v2(Oid group, CGroupComponentType component, int fd_dir)
499502
{
500503
char path[MAX_CGROUP_PATHLEN];
501504
size_t path_size = sizeof(path);
505+
char path_of_leaf[MAXPATHLEN];
502506

503507
char *buf;
504508
size_t buf_size;
@@ -533,7 +537,8 @@ detachcgroup_v2(Oid group, CGroupComponentType component, int fd_dir)
533537
} \
534538
} while (0)
535539

536-
buildPath(group, BASEDIR_GPDB, component, "cgroup.procs", path, path_size);
540+
pg_sprintf(path_of_leaf, "%s/cgroup.procs", CGROUPV2_LEAF_INDENTIFIER);
541+
buildPath(group, BASEDIR_GPDB, component, path_of_leaf, path, path_size);
537542

538543
fdr = open(path, O_RDONLY);
539544

@@ -561,7 +566,7 @@ detachcgroup_v2(Oid group, CGroupComponentType component, int fd_dir)
561566
if (buf_len == 0)
562567
return;
563568

564-
buildPath(DEFAULTRESGROUP_OID, BASEDIR_GPDB, component, "cgroup.procs",
569+
buildPath(DEFAULTRESGROUP_OID, BASEDIR_GPDB, component, path_of_leaf,
565570
path, path_size);
566571

567572
fdw = open(path, O_WRONLY);

src/backend/utils/resgroup/cgroup.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,12 @@ lockDir(const char *path, bool block)
285285
* Create cgroup dir
286286
*/
287287
bool
288-
createDir(Oid group, CGroupComponentType component)
288+
createDir(Oid group, CGroupComponentType component, char *filename)
289289
{
290290
char path[MAX_CGROUP_PATHLEN];
291291
size_t path_size = sizeof(path);
292292

293-
buildPath(group, BASEDIR_GPDB, component, "", path, path_size);
293+
buildPath(group, BASEDIR_GPDB, component, filename, path, path_size);
294294

295295
if (mkdir(path, 0755) && errno != EEXIST)
296296
return false;
@@ -468,12 +468,14 @@ deleteDir(Oid group, CGroupComponentType component, const char *filename, bool u
468468
{
469469

470470
char path[MAX_CGROUP_PATHLEN];
471+
char leaf_path[MAX_CGROUP_PATHLEN];
471472
size_t path_size = sizeof(path);
472473

473474
int retry = unassign ? 0 : MAX_RETRY - 1;
474475
int fd_dir;
475476

476477
buildPath(group, BASEDIR_GPDB, component, "", path, path_size);
478+
buildPath(group, BASEDIR_GPDB, component, CGROUPV2_LEAF_INDENTIFIER, leaf_path, path_size);
477479

478480
/*
479481
* To prevent race condition between multiple processes we require a dir
@@ -497,7 +499,7 @@ deleteDir(Oid group, CGroupComponentType component, const char *filename, bool u
497499
if (unassign)
498500
detachcgroup(group, component, fd_dir);
499501

500-
if (rmdir(path))
502+
if (rmdir(leaf_path) || rmdir(path))
501503
{
502504
int err = errno;
503505

src/include/utils/cgroup.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
/* This is the default value about Linux Control Group */
4848
#define DEFAULT_CPU_PERIOD_US 100000LL
4949

50+
/* The name of leaf cgroup when use cgroup v2 */
51+
#define CGROUPV2_LEAF_INDENTIFIER "queries"
5052

5153
/*
5254
* Resource Group underlying component types.
@@ -168,7 +170,7 @@ extern void setComponentDir(CGroupComponentType component, const char *dir);
168170
extern int lockDir(const char *path, bool block);
169171

170172
/* Create cgroup dir. */
171-
extern bool createDir(Oid group, CGroupComponentType comp);
173+
extern bool createDir(Oid group, CGroupComponentType comp, char *filename);
172174
/* Delete cgroup dir. */
173175
extern bool deleteDir(Oid group, CGroupComponentType component, const char *filename, bool unassign,
174176
void (*detachcgroup) (Oid group, CGroupComponentType component, int fd_dir));

src/test/isolation2/expected/resgroup/resgroup_auxiliary_tools_v2.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ sql = "select sess_id from pg_stat_activity where pid = '%d'" % pid result = plp
118118
sql = "select groupid from gp_toolkit.gp_resgroup_config where groupname='%s'" % groupname result = plpy.execute(sql) groupid = result[0]['groupid']
119119
sql = "select hostname from gp_segment_configuration group by hostname" result = plpy.execute(sql) hosts = [_['hostname'] for _ in result]
120120
def get_result(host): stdout = subprocess.run(["ssh", "{}".format(host), "ps -ef | grep postgres | grep con{} | grep -v grep | awk '{{print $2}}'".format(session_id)], stdout=subprocess.PIPE, check=True).stdout session_pids = stdout.splitlines()
121-
path = "/sys/fs/cgroup/gpdb/{}/cgroup.procs".format(groupid) stdout = subprocess.run(["ssh", "{}".format(host), "cat {}".format(path)], stdout=subprocess.PIPE, check=True).stdout cgroups_pids = stdout.splitlines()
121+
path = "/sys/fs/cgroup/gpdb/{}/queries/cgroup.procs".format(groupid) stdout = subprocess.run(["ssh", "{}".format(host), "cat {}".format(path)], stdout=subprocess.PIPE, check=True).stdout cgroups_pids = stdout.splitlines()
122122
return set(session_pids).issubset(set(cgroups_pids))
123123
for host in hosts: if not get_result(host): return False return True
124124
$$ LANGUAGE plpython3u;

src/test/isolation2/sql/resgroup/resgroup_auxiliary_tools_v2.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ $$ LANGUAGE plpython3u;
259259
stdout=subprocess.PIPE, check=True).stdout
260260
session_pids = stdout.splitlines()
261261

262-
path = "/sys/fs/cgroup/gpdb/{}/cgroup.procs".format(groupid)
262+
path = "/sys/fs/cgroup/gpdb/{}/queries/cgroup.procs".format(groupid)
263263
stdout = subprocess.run(["ssh", "{}".format(host), "cat {}".format(path)], stdout=subprocess.PIPE, check=True).stdout
264264
cgroups_pids = stdout.splitlines()
265265

0 commit comments

Comments
 (0)