Skip to content

Commit da25a21

Browse files
committed
Updated exception handling
1 parent 53f5f6e commit da25a21

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

samza-core/src/main/java/org/apache/samza/container/host/LinuxCgroupStatisticsGetter.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.apache.samza.container.host;
2121
import java.io.IOException;
22+
import java.net.URISyntaxException;
2223
import java.util.Properties;
2324
import java.io.FileReader;
2425
import java.nio.file.Files;
@@ -31,7 +32,7 @@
3132
import java.util.Optional;
3233
import java.nio.file.Path;
3334
import java.nio.file.Paths;
34-
35+
import java.io.File;
3536

3637
public class LinuxCgroupStatisticsGetter implements SystemStatisticsGetter {
3738
private static final Logger LOG = LoggerFactory.getLogger(LinuxCgroupStatisticsGetter.class.getName());
@@ -76,6 +77,10 @@ public LinuxCgroupStatistics getProcessCgroupStatistics() {
7677
}
7778

7879
private double getCPUStat() {
80+
if (this.containerID.equals("NOT_DETECTED")) {
81+
// return a sentinel value to signal this is not running on Hadoop
82+
return -2.0;
83+
}
7984
String[] controllers = {"cpu", "cpuacct", "cpu,cpuacct" };
8085
double cpuThrottledRatio = -1.0;
8186
String cpuStatPath;
@@ -93,7 +98,9 @@ private double getCPUStat() {
9398
cpuThrottledRatio = (double) nrThrottled / nrPeriod;
9499
break;
95100
} catch (IOException | RuntimeException e) {
96-
throw new RuntimeException("Caught exception reading cpu.stat file: ", e);
101+
LOG.debug("Caught exception reading cpu.stat file: ", e.getMessage());
102+
// return a sentinel value to signal an exception occurred.
103+
return -1.0;
97104
}
98105
}
99106
}
@@ -108,11 +115,15 @@ private boolean cpuStatExists(String cpuStatPath) {
108115
private Configuration getHadoopConf(String hConfDir) {
109116
Configuration hConf = new Configuration();
110117
try {
111-
String yarnSiteURI = "file://" + hConfDir + "/yarn-site.xml";
112-
LOG.debug("yarn-site.xml URI: " + yarnSiteURI);
113-
URL yarnSiteUrl = URI.create(yarnSiteURI).toURL();
118+
URI yarnSiteURI = new URI("file://" + hConfDir + "/yarn-site.xml");
119+
LOG.debug("yarn-site.xml URI: " + yarnSiteURI.toString());
120+
File yarnSiteXml = new File(yarnSiteURI);
121+
if (!yarnSiteXml.isFile() || !yarnSiteXml.canRead()) {
122+
throw new RuntimeException("Unable to access yarn-site.xml: " + yarnSiteXml.toString());
123+
}
124+
URL yarnSiteUrl = yarnSiteURI.toURL();
114125
hConf.addResource(yarnSiteUrl);
115-
} catch (MalformedURLException | IllegalArgumentException e) {
126+
} catch (MalformedURLException | URISyntaxException | RuntimeException e) {
116127
LOG.error("Unable to construct URL to yarn-site.xml: " + e.getMessage());
117128
}
118129
return hConf;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.apache.samza.container.host;
221

322
import org.junit.Test;
@@ -7,7 +26,7 @@
726
public class TestLinuxCgroupStatistics {
827
@Test
928
public void testGetCgroupCPUThrottleRatio() {
10-
LinuxCgroupStatistics linuxCgroupStatistics = new LinuxCgroupStatistics(10.0);
11-
assertEquals(linuxCgroupStatistics.getCgroupCpuThrottleRatio(), 10.0, 0.05);
29+
LinuxCgroupStatistics linuxCgroupStatistics = new LinuxCgroupStatistics(-1.0);
30+
assertEquals(linuxCgroupStatistics.getCgroupCpuThrottleRatio(), -1.0, 0.05);
1231
}
1332
}

samza-core/src/test/java/org/apache/samza/container/host/TestLinuxCgroupStatisticsGetter.java

+21
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ public void testGetThrottleValue() {
9393

9494
}
9595

96+
@Test
97+
public void testRunTimeIsNotHadoop() {
98+
// Validate that standalone applications return the expected sentinel of -2.0.
99+
environmentVariables.clear("CONTAINER_ID");
100+
LinuxCgroupStatisticsGetter linuxCgroupStatisticsGetter = new LinuxCgroupStatisticsGetter();
101+
LinuxCgroupStatistics cpuStat = linuxCgroupStatisticsGetter.getProcessCgroupStatistics();
102+
double throttleRatio = cpuStat.getCgroupCpuThrottleRatio();
103+
assertEquals(throttleRatio, -2.0, 0.05);
104+
}
105+
106+
@Test
107+
public void testExceptionReturnsNegativeOne() {
108+
// Validate that exceptions return a sentinel of -1.0.
109+
environmentVariables.set("CONTAINER_ID", "container_abc_123");
110+
environmentVariables.set("HADOOP_CONF_DIR", "/fake/path/to/non_existent/cgroup/directory");
111+
LinuxCgroupStatisticsGetter linuxCgroupStatisticsGetter = new LinuxCgroupStatisticsGetter();
112+
LinuxCgroupStatistics cpuStat = linuxCgroupStatisticsGetter.getProcessCgroupStatistics();
113+
double throttleRatio = cpuStat.getCgroupCpuThrottleRatio();
114+
assertEquals(throttleRatio, -1.0, 0.05);
115+
}
116+
96117
@Test(expected = UnsupportedOperationException.class)
97118
public void testGetSystemMemoryStatistics() {
98119
LinuxCgroupStatisticsGetter linuxCgroupStatisticsGetter = new LinuxCgroupStatisticsGetter();

0 commit comments

Comments
 (0)