Skip to content

Commit 5832fb2

Browse files
committed
feat: linux control group version 2 API support cgroup v2
1 parent 1abc7cc commit 5832fb2

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

src/integrationtests/java/com/aws/greengrass/integrationtests/lifecyclemanager/GenericExternalServiceIntegTest.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
import com.aws.greengrass.testcommons.testutilities.NoOpPathOwnershipHandler;
2323
import com.aws.greengrass.util.Pair;
2424
import com.aws.greengrass.util.platforms.SystemResourceController;
25-
import com.aws.greengrass.util.platforms.unix.linux.*;
25+
import com.aws.greengrass.util.platforms.unix.linux.Cgroup;
26+
import com.aws.greengrass.util.platforms.unix.linux.CgroupSubSystem;
27+
import com.aws.greengrass.util.platforms.unix.linux.CgroupSubSystemV2;
28+
import com.aws.greengrass.util.platforms.unix.linux.LinuxPlatform;
29+
import com.aws.greengrass.util.platforms.unix.linux.LinuxSystemResourceController;
30+
import com.aws.greengrass.util.platforms.unix.linux.LinuxSystemResourceControllerV2;
2631
import org.apache.commons.io.FileUtils;
2732
import org.apache.commons.lang3.SystemUtils;
2833
import org.apache.commons.lang3.reflect.FieldUtils;
@@ -36,7 +41,7 @@
3641
import org.junit.jupiter.params.ParameterizedTest;
3742
import org.junit.jupiter.params.provider.Arguments;
3843
import org.junit.jupiter.params.provider.MethodSource;
39-
import org.mockito.*;
44+
import org.mockito.Spy;
4045
import org.mockito.junit.jupiter.MockitoExtension;
4146

4247
import java.io.IOException;
@@ -46,7 +51,8 @@
4651
import java.nio.file.Files;
4752
import java.nio.file.Path;
4853
import java.nio.file.Paths;
49-
import java.util.*;
54+
import java.util.ArrayList;
55+
import java.util.List;
5056
import java.util.concurrent.CompletableFuture;
5157
import java.util.concurrent.CopyOnWriteArrayList;
5258
import java.util.concurrent.CountDownLatch;
@@ -88,16 +94,15 @@
8894

8995
@ExtendWith({GGExtension.class, MockitoExtension.class})
9096
class GenericExternalServiceIntegTest extends BaseITCase {
91-
9297
private Kernel kernel;
9398

99+
@Spy
94100
LinuxPlatform linuxPlatform;
95101

96102
private final static String ROOT_PATH_STRING = "/systest21/fs/cgroup";
97103
private final static String GG_PATH_STRING = "greengrass";
98104

99-
@Spy
100-
SystemResourceController spySystemResourceController;
105+
SystemResourceController systemResourceController;
101106

102107
static Stream<Arguments> posixTestUserConfig() {
103108
return Stream.of(
@@ -652,24 +657,25 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re
652657
}
653658
});
654659

655-
linuxPlatform = kernel.getContext().get(LinuxPlatform.class);
660+
linuxPlatform = spy(kernel.getContext().get(LinuxPlatform.class));
656661

657662
createComponentData(echoComponentName);
658663
createComponentData(mainComponentName);
659664

660-
setFinalStatic(linuxPlatform, LinuxPlatform.class.getDeclaredField("CGROUP_CONTROLLERS"), Paths.get(componentPathString + "/memory.max"));
661-
662-
spySystemResourceController = linuxPlatform.getSystemResourceController();
663-
LinuxSystemResourceControllerV2 controllerV2 = (LinuxSystemResourceControllerV2) spySystemResourceController;
664-
665+
// Due to cgroup v1 is active by default (in test platform), and the directories of cgroup v1 are read-only
666+
// therefore, here create some directories and files as fake cgroup v2 files to support testing
667+
Field controllerField = LinuxPlatform.class.getDeclaredField("CGROUP_CONTROLLERS");
668+
setFinalStatic(controllerField, Paths.get(componentPathString + "/memory.max"));
669+
systemResourceController = linuxPlatform.getSystemResourceController();
670+
LinuxSystemResourceControllerV2 controllerV2 = (LinuxSystemResourceControllerV2) systemResourceController;
665671
Field memoryCgroupField = LinuxSystemResourceControllerV2.class.getSuperclass().getDeclaredField("memoryCgroup");
666672
memoryCgroupField.setAccessible(true);
667673
Cgroup memoryCgroup = (Cgroup) memoryCgroupField.get(controllerV2);
668674
Field subsystem = memoryCgroup.getClass().getDeclaredField("subSystem");
669675
subsystem.setAccessible(true);
670676
CgroupSubSystemV2 cg = (CgroupSubSystemV2) subsystem.get(memoryCgroup);
671677
Field f = cg.getClass().getInterfaces()[0].getDeclaredField("CGROUP_ROOT");
672-
setFinalStatic(cg, f, Paths.get(ROOT_PATH_STRING));
678+
setFinalStatic(f, Paths.get(ROOT_PATH_STRING));
673679

674680
Field mountsField = LinuxSystemResourceControllerV2.class.getSuperclass().getDeclaredField("MOUNT_PATH");
675681
mountsField.setAccessible(true);
@@ -680,7 +686,7 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re
680686
Files.write(mountPathFilePath, String.format("test1 %s test2 test3 test4 test5", ROOT_PATH_STRING).getBytes(StandardCharsets.UTF_8));
681687

682688
}
683-
setFinalStatic(controllerV2, mountsField, mountPathFile);
689+
setFinalStatic(mountsField, mountPathFile);
684690

685691
kernel.launch();
686692
assertResourceLimits_V2(10240l * 1024, 1.5);
@@ -706,10 +712,10 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re
706712
FileUtils.deleteDirectory(Paths.get("/systest21").toFile());
707713
}
708714

709-
private void setFinalStatic(Object obj, Field field, Object newValue) throws Exception {
715+
private void setFinalStatic(Field field, Object newValue) throws Exception {
710716
field.setAccessible(true);
711717
FieldUtils.removeFinalModifier(field, true);
712-
field.set(obj, newValue);
718+
field.set(null, newValue);
713719
}
714720

715721
@Test
@@ -866,8 +872,8 @@ private void createComponentData(String componentName) throws IOException {
866872
Files.createFile(path.resolve("cpu.max"));
867873
}
868874
Files.write(path.resolve("cpu.max"), "max 100000".getBytes(StandardCharsets.UTF_8));
869-
if (!Files.exists(path.resolve("cgroup.proc"))) {
870-
Files.createFile(path.resolve("cgroup.proc"));
875+
if (!Files.exists(path.resolve("cgroup.procs"))) {
876+
Files.createFile(path.resolve("cgroup.procs"));
871877
}
872878
}
873879

src/main/java/com/aws/greengrass/util/platforms/unix/linux/LinuxPlatform.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
justification = "Cgroup Controller virtual filesystem path cannot be relative")
1818
public class LinuxPlatform extends UnixPlatform {
1919
private static final Path CGROUP_CONTROLLERS = Paths.get("/sys/fs/cgroup/cgroup.controllers");
20-
2120
SystemResourceController systemResourceController;
2221

2322
@Override
2423
public SystemResourceController getSystemResourceController() {
25-
//if the path exists, identify it as cgroupv1, otherwise identify it as cgroupv2
24+
//if the path exists, identify it as cgroupv2, otherwise identify it as cgroupv1
2625
if (Files.exists(CGROUP_CONTROLLERS)) {
2726
systemResourceController = new LinuxSystemResourceControllerV2(this);
2827
} else {

0 commit comments

Comments
 (0)