19
19
20
20
package org .apache .samza .container .host ;
21
21
import java .io .IOException ;
22
+ import java .net .URISyntaxException ;
22
23
import java .util .Properties ;
23
24
import java .io .FileReader ;
24
25
import java .nio .file .Files ;
31
32
import java .util .Optional ;
32
33
import java .nio .file .Path ;
33
34
import java .nio .file .Paths ;
34
-
35
+ import java . io . File ;
35
36
36
37
public class LinuxCgroupStatisticsGetter implements SystemStatisticsGetter {
37
38
private static final Logger LOG = LoggerFactory .getLogger (LinuxCgroupStatisticsGetter .class .getName ());
@@ -76,6 +77,10 @@ public LinuxCgroupStatistics getProcessCgroupStatistics() {
76
77
}
77
78
78
79
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
+ }
79
84
String [] controllers = {"cpu" , "cpuacct" , "cpu,cpuacct" };
80
85
double cpuThrottledRatio = -1.0 ;
81
86
String cpuStatPath ;
@@ -93,7 +98,9 @@ private double getCPUStat() {
93
98
cpuThrottledRatio = (double ) nrThrottled / nrPeriod ;
94
99
break ;
95
100
} 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 ;
97
104
}
98
105
}
99
106
}
@@ -108,11 +115,15 @@ private boolean cpuStatExists(String cpuStatPath) {
108
115
private Configuration getHadoopConf (String hConfDir ) {
109
116
Configuration hConf = new Configuration ();
110
117
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 ();
114
125
hConf .addResource (yarnSiteUrl );
115
- } catch (MalformedURLException | IllegalArgumentException e ) {
126
+ } catch (MalformedURLException | URISyntaxException | RuntimeException e ) {
116
127
LOG .error ("Unable to construct URL to yarn-site.xml: " + e .getMessage ());
117
128
}
118
129
return hConf ;
0 commit comments