Skip to content

Commit 2fd7df4

Browse files
authored
TEZ-4396 Ensure utility classes have only private (default) constructors + several code refactors (#197) (Gergely Hanko reviewed by Laszlo Bodor)
1 parent 65f9ee3 commit 2fd7df4

82 files changed

Lines changed: 865 additions & 1124 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tez-api/src/main/java/org/apache/tez/client/TezClientUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,13 @@
116116
import com.google.common.collect.Lists;
117117

118118
@Private
119-
public class TezClientUtils {
119+
public final class TezClientUtils {
120120

121121
private static Logger LOG = LoggerFactory.getLogger(TezClientUtils.class);
122122
private static final int UTF8_CHUNK_SIZE = 16 * 1024;
123123

124+
private TezClientUtils() {}
125+
124126
private static FileStatus[] getLRFileStatus(String fileName, Configuration conf) throws
125127
IOException {
126128
URI uri;

tez-api/src/main/java/org/apache/tez/common/ATSConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,5 @@ public class ATSConstants {
138138
public static final String CALLER_TYPE = "callerType";
139139
public static final String DESCRIPTION = "description";
140140

141+
protected ATSConstants() {}
141142
}

tez-api/src/main/java/org/apache/tez/common/RPCUtil.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929

3030
import com.google.protobuf.ServiceException;
3131

32-
public class RPCUtil {
32+
public final class RPCUtil {
33+
34+
private RPCUtil() {}
3335

3436
/**
3537
* Returns an instance of {@link TezException}
@@ -55,17 +57,8 @@ private static <T extends Throwable> T instantiateException(
5557
return ex;
5658
// RemoteException contains useful information as against the
5759
// java.lang.reflect exceptions.
58-
} catch (NoSuchMethodException e) {
59-
throw re;
60-
} catch (IllegalArgumentException e) {
61-
throw re;
62-
} catch (SecurityException e) {
63-
throw re;
64-
} catch (InstantiationException e) {
65-
throw re;
66-
} catch (IllegalAccessException e) {
67-
throw re;
68-
} catch (InvocationTargetException e) {
60+
} catch (NoSuchMethodException | IllegalArgumentException | SecurityException | InstantiationException
61+
| IllegalAccessException | InvocationTargetException e) {
6962
throw re;
7063
}
7164
}
@@ -85,12 +78,6 @@ private static <T extends RuntimeException> T instantiateRuntimeException(
8578
return instantiateException(cls, re);
8679
}
8780

88-
private static <T extends SessionNotRunning> T instantiateSessionNotRunningException(
89-
Class<? extends T> cls, RemoteException re) throws RemoteException {
90-
return instantiateException(cls, re);
91-
}
92-
93-
9481
/**
9582
* Utility method that unwraps and returns appropriate exceptions.
9683
*
@@ -109,7 +96,7 @@ public static Void unwrapAndThrowException(ServiceException se)
10996
} else {
11097
if (cause instanceof RemoteException) {
11198
RemoteException re = (RemoteException) cause;
112-
Class<?> realClass = null;
99+
Class<?> realClass;
113100
try {
114101
realClass = Class.forName(re.getClassName());
115102
} catch (ClassNotFoundException cnf) {

tez-api/src/main/java/org/apache/tez/common/ReflectionUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
import org.apache.tez.dag.api.TezReflectionException;
3030

3131
@Private
32-
public class ReflectionUtils {
32+
public final class ReflectionUtils {
3333

34-
private static final Map<String, Class<?>> CLAZZ_CACHE = new ConcurrentHashMap<String, Class<?>>();
34+
private static final Map<String, Class<?>> CLAZZ_CACHE = new ConcurrentHashMap<>();
35+
36+
private ReflectionUtils() {}
3537

3638
@Private
3739
public static Class<?> getClazz(String className) throws TezReflectionException {

tez-api/src/main/java/org/apache/tez/common/TezCommonUtils.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import com.google.protobuf.ByteString;
5656

5757
@Private
58-
public class TezCommonUtils {
58+
public final class TezCommonUtils {
5959
public static final FsPermission TEZ_AM_DIR_PERMISSION = FsPermission
6060
.createImmutable((short) 0700); // rwx--------
6161
public static final FsPermission TEZ_AM_FILE_PERMISSION = FsPermission
@@ -64,6 +64,8 @@ public class TezCommonUtils {
6464

6565
public static final String TEZ_SYSTEM_SUB_DIR = ".tez";
6666

67+
private TezCommonUtils() {}
68+
6769
/**
6870
* <p>
6971
* This function returns the staging directory defined in the config with
@@ -222,7 +224,6 @@ public static Path getTezTextPlanStagingPath(Path tezSysStagingPath, String strA
222224
* @param conf
223225
* Tez configuration
224226
* @return App recovery path
225-
* @throws IOException
226227
*/
227228
@Private
228229
public static Path getRecoveryPath(Path tezSysStagingPath, Configuration conf)
@@ -288,7 +289,6 @@ public static Path getSummaryRecoveryPath(Path attemptRecoverPath) {
288289
* Filesystem
289290
* @param dir
290291
* directory to be created
291-
* @throws IOException
292292
*/
293293
public static void mkDirForAM(FileSystem fs, Path dir) throws IOException {
294294
FsPermission perm = new FsPermission(TEZ_AM_DIR_PERMISSION);
@@ -312,7 +312,6 @@ public static void mkDirForAM(FileSystem fs, Path dir) throws IOException {
312312
* @param filePath
313313
* file path to create the file
314314
* @return FSDataOutputStream
315-
* @throws IOException
316315
*/
317316
public static FSDataOutputStream createFileForAM(FileSystem fs, Path filePath) throws IOException {
318317
return FileSystem.create(fs, filePath, new FsPermission(TEZ_AM_FILE_PERMISSION));
@@ -417,7 +416,7 @@ public static String getCredentialsInfo(Credentials credentials, String identifi
417416
}
418417

419418
StringBuilder sb = new StringBuilder();
420-
sb.append("Credentials: #" + identifier + "Tokens=").append(credentials.numberOfTokens());
419+
sb.append("Credentials: #").append(identifier).append("Tokens=").append(credentials.numberOfTokens());
421420
if (credentials.numberOfTokens() > 0) {
422421
sb.append(", Services=");
423422
sb.append(credentials.getAllTokens().stream()
@@ -435,16 +434,14 @@ public static ByteBuffer convertJobTokenToBytes(
435434
Token<JobTokenIdentifier> jobToken) throws IOException {
436435
DataOutputBuffer dob = new DataOutputBuffer();
437436
jobToken.write(dob);
438-
ByteBuffer bb = ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
439-
return bb;
437+
return ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
440438
}
441439

442440
public static Credentials parseCredentialsBytes(byte[] credentialsBytes) throws IOException {
443441
Credentials credentials = new Credentials();
444442
DataInputBuffer dib = new DataInputBuffer();
445443
try {
446-
byte[] tokenBytes = credentialsBytes;
447-
dib.reset(tokenBytes, tokenBytes.length);
444+
dib.reset(credentialsBytes, credentialsBytes.length);
448445
credentials.readTokenStorageStream(dib);
449446
return credentials;
450447
} finally {
@@ -459,7 +456,7 @@ public static void logCredentials(Logger log, Credentials credentials, String id
459456
}
460457

461458
public static Collection<String> tokenizeString(String str, String delim) {
462-
List<String> values = new ArrayList<String>();
459+
List<String> values = new ArrayList<>();
463460
if (str == null || str.isEmpty())
464461
return values;
465462
StringTokenizer tokenizer = new StringTokenizer(str, delim);
@@ -533,7 +530,7 @@ public static long getAMClientHeartBeatTimeoutMillis(Configuration conf) {
533530
if (val > 0 && val < TezConstants.TEZ_AM_CLIENT_HEARTBEAT_TIMEOUT_SECS_MINIMUM) {
534531
return TezConstants.TEZ_AM_CLIENT_HEARTBEAT_TIMEOUT_SECS_MINIMUM * 1000;
535532
}
536-
return val * 1000;
533+
return val * 1000L;
537534
}
538535

539536
/**
@@ -570,7 +567,7 @@ public static long getDAGSessionTimeout(Configuration conf) {
570567
if (timeoutSecs == 0) {
571568
timeoutSecs = 1;
572569
}
573-
return 1000l * timeoutSecs;
570+
return 1000L * timeoutSecs;
574571
}
575572

576573
public static int getJavaVersion() {

tez-api/src/main/java/org/apache/tez/common/TezUtils.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.IOException;
2222
import java.io.OutputStream;
2323
import java.nio.ByteBuffer;
24-
import java.util.Iterator;
2524
import java.util.List;
2625
import java.util.Map;
2726
import java.util.Map.Entry;
@@ -49,10 +48,12 @@
4948
* {@link org.apache.hadoop.conf.Configuration} to {@link org.apache.tez.dag.api.UserPayload} etc.
5049
*/
5150
@InterfaceAudience.Public
52-
public class TezUtils {
51+
public final class TezUtils {
5352

5453
private static final Logger LOG = LoggerFactory.getLogger(TezUtils.class);
5554

55+
private TezUtils() {}
56+
5657
/**
5758
* Allows changing the log level for task / AM logging. </p>
5859
*
@@ -73,18 +74,12 @@ public static void addLog4jSystemProperties(String logLevel,
7374
* @param conf
7475
* : Configuration to be converted
7576
* @return PB ByteString (compressed)
76-
* @throws java.io.IOException
7777
*/
7878
public static ByteString createByteStringFromConf(Configuration conf) throws IOException {
7979
Objects.requireNonNull(conf, "Configuration must be specified");
8080
ByteString.Output os = ByteString.newOutput();
81-
SnappyOutputStream compressOs = new SnappyOutputStream(os);
82-
try {
81+
try (SnappyOutputStream compressOs = new SnappyOutputStream(os)) {
8382
writeConfInPB(compressOs, conf);
84-
} finally {
85-
if (compressOs != null) {
86-
compressOs.close();
87-
}
8883
}
8984
return os.toByteString();
9085
}
@@ -95,7 +90,6 @@ public static ByteString createByteStringFromConf(Configuration conf) throws IOE
9590
*
9691
* @param conf configuration to be converted
9792
* @return an instance of {@link org.apache.tez.dag.api.UserPayload}
98-
* @throws java.io.IOException
9993
*/
10094
public static UserPayload createUserPayloadFromConf(Configuration conf) throws IOException {
10195
return UserPayload.create(ByteBuffer.wrap(createByteStringFromConf(conf).toByteArray()));
@@ -113,11 +107,10 @@ private static DAGProtos.ConfigurationProto createConfProto(SnappyInputStream un
113107
* @param byteString byteString representation of the conf created using {@link
114108
* #createByteStringFromConf(org.apache.hadoop.conf.Configuration)}
115109
* @return Configuration
116-
* @throws java.io.IOException
117110
*/
118111
public static Configuration createConfFromByteString(ByteString byteString) throws IOException {
119112
Objects.requireNonNull(byteString, "ByteString must be specified");
120-
try(SnappyInputStream uncompressIs = new SnappyInputStream(byteString.newInput());) {
113+
try(SnappyInputStream uncompressIs = new SnappyInputStream(byteString.newInput())) {
121114
DAGProtos.ConfigurationProto confProto = createConfProto(uncompressIs);
122115
Configuration conf = new Configuration(false);
123116
readConfFromPB(confProto, conf);
@@ -156,7 +149,6 @@ public static void addToConfFromByteString(Configuration configuration, ByteStri
156149
* @param payload {@link org.apache.tez.dag.api.UserPayload} created using {@link
157150
* #createUserPayloadFromConf(org.apache.hadoop.conf.Configuration)}
158151
* @return Configuration
159-
* @throws java.io.IOException
160152
*/
161153
public static Configuration createConfFromUserPayload(UserPayload payload) throws IOException {
162154
return createConfFromByteString(ByteString.copyFrom(payload.getPayload()));
@@ -186,12 +178,10 @@ public static String convertToHistoryText(String description, Configuration conf
186178
}
187179
if (conf != null) {
188180
JSONObject confJson = new JSONObject();
189-
Iterator<Entry<String, String>> iter = conf.iterator();
190-
while (iter.hasNext()) {
191-
Entry<String, String> entry = iter.next();
181+
for (Entry<String, String> entry : conf) {
192182
String key = entry.getKey();
193183
String val = conf.get(entry.getKey());
194-
if(val != null) {
184+
if (val != null) {
195185
confJson.put(key, val);
196186
} else {
197187
LOG.debug("null value in Configuration after replacement for key={}. Skipping.", key);

tez-api/src/main/java/org/apache/tez/common/TezYARNUtils.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.apache.tez.dag.api.TezConstants;
3636

3737
@Private
38-
public class TezYARNUtils {
38+
public final class TezYARNUtils {
3939
private static Logger LOG = LoggerFactory.getLogger(TezYARNUtils.class);
4040

4141
public static final String ENV_NAME_REGEX = "[A-Za-z_][A-Za-z0-9_]*";
@@ -49,6 +49,8 @@ public class TezYARNUtils {
4949
+ "([^,]*)" // val group
5050
);
5151

52+
private TezYARNUtils() {}
53+
5254
public static String getFrameworkClasspath(Configuration conf, boolean usingArchive) {
5355
StringBuilder classpathBuilder = new StringBuilder();
5456
boolean userClassesTakesPrecedence =
@@ -126,9 +128,11 @@ private static void addUserSpecifiedClasspath(StringBuilder classpathBuilder,
126128

127129
// Add PWD:PWD/*
128130
classpathBuilder.append(Environment.PWD.$())
129-
.append(File.pathSeparator)
130-
.append(Environment.PWD.$() + File.separator + "*")
131-
.append(File.pathSeparator);
131+
.append(File.pathSeparator)
132+
.append(Environment.PWD.$())
133+
.append(File.separator)
134+
.append("*")
135+
.append(File.pathSeparator);
132136
}
133137

134138
public static void appendToEnvFromInputString(Map<String, String> env,
@@ -161,7 +165,7 @@ public static void appendToEnvFromInputString(Map<String, String> env,
161165
public static void setEnvIfAbsentFromInputString(Map<String, String> env,
162166
String envString) {
163167
if (envString != null && envString.length() > 0) {
164-
String childEnvs[] = envString.split(",");
168+
String[] childEnvs = envString.split(",");
165169
for (String cEnv : childEnvs) {
166170
String[] parts = cEnv.split("="); // split on '='
167171
Matcher m = VAR_SUBBER .matcher(parts[1]);

tez-api/src/main/java/org/apache/tez/common/security/Master.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929

3030
@Private
3131
@Unstable
32-
public class Master {
32+
public final class Master {
3333

3434
public enum State {
35-
INITIALIZING, RUNNING;
35+
INITIALIZING, RUNNING
3636
}
3737

38+
private Master() {}
39+
3840
public static String getMasterUserName(Configuration conf) {
3941
return conf.get(YarnConfiguration.RM_PRINCIPAL);
4042
}

0 commit comments

Comments
 (0)