@@ -63,35 +63,55 @@ public class ResourceHandlerModule {
6363 * as resource metrics functionality. We need to ensure that the same
6464 * instance is used for both.
6565 */
66+ private static volatile CGroupsHandler cGroupV1Handler ;
67+ private static volatile CGroupsHandler cGroupV2Handler ;
6668 private static volatile TrafficControlBandwidthHandlerImpl
6769 trafficControlBandwidthHandler ;
6870 private static volatile NetworkPacketTaggingHandlerImpl
6971 networkPacketTaggingHandlerImpl ;
70- private static volatile CGroupsHandler cGroupsHandler ;
7172 private static volatile CGroupsBlkioResourceHandlerImpl
7273 cGroupsBlkioResourceHandler ;
7374 private static volatile MemoryResourceHandler
7475 cGroupsMemoryResourceHandler ;
7576 private static volatile CpuResourceHandler
7677 cGroupsCpuResourceHandler ;
7778
78- /**
79- * Returns an initialized, thread-safe CGroupsHandler instance.
80- */
81- private static CGroupsHandler getInitializedCGroupsHandler (Configuration conf )
79+ private static void initializeCGroupHandlers (Configuration conf )
80+ throws ResourceHandlerException {
81+ initializeCGroupV1Handler (conf );
82+ if (cgroupsV2Enabled ) {
83+ initializeCGroupV2Handler (conf );
84+ }
85+ }
86+
87+ private static void initializeCGroupV1Handler (Configuration conf )
8288 throws ResourceHandlerException {
83- if (cGroupsHandler == null ) {
89+ if (cGroupV1Handler == null ) {
8490 synchronized (CGroupsHandler .class ) {
85- if (cGroupsHandler == null ) {
86- cGroupsHandler = cgroupsV2Enabled
87- ? new CGroupsV2HandlerImpl (conf , PrivilegedOperationExecutor .getInstance (conf ))
88- : new CGroupsHandlerImpl (conf , PrivilegedOperationExecutor .getInstance (conf ));
89- LOG .debug ("Value of CGroupsHandler is: {}" , cGroupsHandler );
91+ if (cGroupV1Handler == null ) {
92+ cGroupV1Handler = new CGroupsHandlerImpl (
93+ conf , PrivilegedOperationExecutor .getInstance (conf ));
94+ LOG .debug ("Value of CGroupsV1Handler is: {}" , cGroupV1Handler );
9095 }
9196 }
9297 }
98+ }
9399
94- return cGroupsHandler ;
100+ private static void initializeCGroupV2Handler (Configuration conf )
101+ throws ResourceHandlerException {
102+ if (cGroupV2Handler == null ) {
103+ synchronized (CGroupsHandler .class ) {
104+ if (cGroupV2Handler == null ) {
105+ cGroupV2Handler = new CGroupsV2HandlerImpl (
106+ conf , PrivilegedOperationExecutor .getInstance (conf ));
107+ LOG .debug ("Value of CGroupsV2Handler is: {}" , cGroupV2Handler );
108+ }
109+ }
110+ }
111+ }
112+
113+ private static boolean isMountedInCGroupsV2 (CGroupsHandler .CGroupController controller ) {
114+ return (cGroupV2Handler != null && cGroupV2Handler .getControllerPath (controller ) != null );
95115 }
96116
97117 /**
@@ -101,18 +121,18 @@ private static CGroupsHandler getInitializedCGroupsHandler(Configuration conf)
101121 */
102122
103123 public static CGroupsHandler getCGroupsHandler () {
104- return cGroupsHandler ;
124+ return cGroupV1Handler ;
105125 }
106126
107127 /**
108128 * Returns relative root for cgroups. Returns null if cGroupsHandler is
109129 * not initialized, or if the path is empty.
110130 */
111131 public static String getCgroupsRelativeRoot () {
112- if (cGroupsHandler == null ) {
132+ if (getCGroupsHandler () == null ) {
113133 return null ;
114134 }
115- String cGroupPath = cGroupsHandler .getRelativePathForCGroup ("" );
135+ String cGroupPath = getCGroupsHandler () .getRelativePathForCGroup ("" );
116136 if (cGroupPath == null || cGroupPath .isEmpty ()) {
117137 return null ;
118138 }
@@ -153,9 +173,13 @@ private static CpuResourceHandler initCGroupsCpuResourceHandler(
153173 synchronized (CpuResourceHandler .class ) {
154174 if (cGroupsCpuResourceHandler == null ) {
155175 LOG .debug ("Creating new cgroups cpu handler" );
156- cGroupsCpuResourceHandler = cgroupsV2Enabled
157- ? new CGroupsV2CpuResourceHandlerImpl (getInitializedCGroupsHandler (conf ))
158- : new CGroupsCpuResourceHandlerImpl (getInitializedCGroupsHandler (conf ));
176+
177+ initializeCGroupHandlers (conf );
178+ if (isMountedInCGroupsV2 (CGroupsHandler .CGroupController .CPU )) {
179+ cGroupsCpuResourceHandler = new CGroupsV2CpuResourceHandlerImpl (cGroupV2Handler );
180+ } else {
181+ cGroupsCpuResourceHandler = new CGroupsCpuResourceHandlerImpl (cGroupV1Handler );
182+ }
159183 return cGroupsCpuResourceHandler ;
160184 }
161185 }
@@ -173,9 +197,11 @@ private static CpuResourceHandler initCGroupsCpuResourceHandler(
173197 synchronized (OutboundBandwidthResourceHandler .class ) {
174198 if (trafficControlBandwidthHandler == null ) {
175199 LOG .info ("Creating new traffic control bandwidth handler." );
200+
201+ initializeCGroupHandlers (conf );
176202 trafficControlBandwidthHandler = new
177203 TrafficControlBandwidthHandlerImpl (PrivilegedOperationExecutor
178- .getInstance (conf ), getInitializedCGroupsHandler ( conf ) ,
204+ .getInstance (conf ), cGroupV1Handler ,
179205 new TrafficController (conf , PrivilegedOperationExecutor
180206 .getInstance (conf )));
181207 }
@@ -208,10 +234,11 @@ public static ResourceHandler getNetworkTaggingHandler(Configuration conf)
208234 synchronized (OutboundBandwidthResourceHandler .class ) {
209235 if (networkPacketTaggingHandlerImpl == null ) {
210236 LOG .info ("Creating new network-tagging-handler." );
237+
238+ initializeCGroupHandlers (conf );
211239 networkPacketTaggingHandlerImpl =
212240 new NetworkPacketTaggingHandlerImpl (
213- PrivilegedOperationExecutor .getInstance (conf ),
214- getInitializedCGroupsHandler (conf ));
241+ PrivilegedOperationExecutor .getInstance (conf ), cGroupV1Handler );
215242 }
216243 }
217244 }
@@ -239,9 +266,10 @@ private static CGroupsBlkioResourceHandlerImpl getCgroupsBlkioResourceHandler(
239266 synchronized (DiskResourceHandler .class ) {
240267 if (cGroupsBlkioResourceHandler == null ) {
241268 LOG .debug ("Creating new cgroups blkio handler" );
269+
270+ initializeCGroupHandlers (conf );
242271 cGroupsBlkioResourceHandler =
243- new CGroupsBlkioResourceHandlerImpl (
244- getInitializedCGroupsHandler (conf ));
272+ new CGroupsBlkioResourceHandlerImpl (cGroupV1Handler );
245273 }
246274 }
247275 }
@@ -263,9 +291,13 @@ public static MemoryResourceHandler initMemoryResourceHandler(
263291 if (cGroupsMemoryResourceHandler == null ) {
264292 synchronized (MemoryResourceHandler .class ) {
265293 if (cGroupsMemoryResourceHandler == null ) {
266- cGroupsMemoryResourceHandler = cgroupsV2Enabled
267- ? new CGroupsV2MemoryResourceHandlerImpl (getInitializedCGroupsHandler (conf ))
268- : new CGroupsMemoryResourceHandlerImpl (getInitializedCGroupsHandler (conf ));
294+
295+ initializeCGroupHandlers (conf );
296+ if (isMountedInCGroupsV2 (CGroupsHandler .CGroupController .MEMORY )) {
297+ cGroupsMemoryResourceHandler = new CGroupsV2MemoryResourceHandlerImpl (cGroupV2Handler );
298+ } else {
299+ cGroupsMemoryResourceHandler = new CGroupsMemoryResourceHandlerImpl (cGroupV1Handler );
300+ }
269301 }
270302 }
271303 }
@@ -327,9 +359,10 @@ private static void addHandlersFromConfiguredResourcePlugins(
327359 }
328360
329361 for (ResourcePlugin plugin : pluginMap .values ()) {
362+ initializeCGroupHandlers (conf );
330363 addHandlerIfNotNull (handlerList ,
331364 plugin .createResourceHandler (nmContext ,
332- getInitializedCGroupsHandler ( conf ) ,
365+ cGroupV1Handler ,
333366 PrivilegedOperationExecutor .getInstance (conf )));
334367 }
335368 }
@@ -360,21 +393,6 @@ static void nullifyResourceHandlerChain() throws ResourceHandlerException {
360393 resourceHandlerChain = null ;
361394 }
362395
363- @ VisibleForTesting
364- static void resetCgroupsHandler () {
365- cGroupsHandler = null ;
366- }
367-
368- @ VisibleForTesting
369- static void resetCpuResourceHandler () {
370- cGroupsCpuResourceHandler = null ;
371- }
372-
373- @ VisibleForTesting
374- static void resetMemoryResourceHandler () {
375- cGroupsMemoryResourceHandler = null ;
376- }
377-
378396 /**
379397 * If a cgroup mount directory is specified, it returns cgroup directories
380398 * with valid names.
0 commit comments