Skip to content

Commit 183f09b

Browse files
authored
HDFS-16717. Replace NPE with IOException in DataNode.class (#4699). Contributed by ZanderXu.
Signed-off-by: He Xiaoqiao <hexiaoqiao@apache.org>
1 parent c249db8 commit 183f09b

File tree

1 file changed

+25
-11
lines changed
  • hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode

1 file changed

+25
-11
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,11 @@ private String reconfDfsUsageParameters(String property, String newVal)
889889
String result = null;
890890
try {
891891
LOG.info("Reconfiguring {} to {}", property, newVal);
892+
if (data == null) {
893+
LOG.debug("FsDatasetSpi has not been initialized.");
894+
throw new IOException("FsDatasetSpi has not been initialized");
895+
}
892896
if (property.equals(FS_DU_INTERVAL_KEY)) {
893-
Preconditions.checkNotNull(data, "FsDatasetSpi has not been initialized.");
894897
long interval = (newVal == null ? FS_DU_INTERVAL_DEFAULT :
895898
Long.parseLong(newVal));
896899
result = Long.toString(interval);
@@ -902,7 +905,6 @@ private String reconfDfsUsageParameters(String property, String newVal)
902905
}
903906
}
904907
} else if (property.equals(FS_GETSPACEUSED_JITTER_KEY)) {
905-
Preconditions.checkNotNull(data, "FsDatasetSpi has not been initialized.");
906908
long jitter = (newVal == null ? FS_GETSPACEUSED_JITTER_DEFAULT :
907909
Long.parseLong(newVal));
908910
result = Long.toString(jitter);
@@ -914,7 +916,6 @@ private String reconfDfsUsageParameters(String property, String newVal)
914916
}
915917
}
916918
} else if (property.equals(FS_GETSPACEUSED_CLASSNAME)) {
917-
Preconditions.checkNotNull(data, "FsDatasetSpi has not been initialized.");
918919
Class<? extends GetSpaceUsed> klass;
919920
if (newVal == null) {
920921
if (Shell.WINDOWS) {
@@ -1174,7 +1175,7 @@ private void refreshVolumes(String newVolumes) throws IOException {
11741175
.newFixedThreadPool(changedVolumes.newLocations.size());
11751176
List<Future<IOException>> exceptions = Lists.newArrayList();
11761177

1177-
Preconditions.checkNotNull(data, "Storage not yet initialized");
1178+
checkStorageState("refreshVolumes");
11781179
for (final StorageLocation location : changedVolumes.newLocations) {
11791180
exceptions.add(service.submit(new Callable<IOException>() {
11801181
@Override
@@ -1274,7 +1275,7 @@ private synchronized void removeVolumes(
12741275
clearFailure, Joiner.on(",").join(storageLocations)));
12751276

12761277
IOException ioe = null;
1277-
Preconditions.checkNotNull(data, "Storage not yet initialized");
1278+
checkStorageState("removeVolumes");
12781279
// Remove volumes and block infos from FsDataset.
12791280
data.removeVolumes(storageLocations, clearFailure);
12801281

@@ -2301,7 +2302,7 @@ public BlockLocalPathInfo getBlockLocalPathInfo(ExtendedBlock block,
23012302
Token<BlockTokenIdentifier> token) throws IOException {
23022303
checkBlockLocalPathAccess();
23032304
checkBlockToken(block, token, BlockTokenIdentifier.AccessMode.READ);
2304-
Preconditions.checkNotNull(data, "Storage not yet initialized");
2305+
checkStorageState("getBlockLocalPathInfo");
23052306
BlockLocalPathInfo info = data.getBlockLocalPathInfo(block);
23062307
if (info != null) {
23072308
LOG.trace("getBlockLocalPathInfo successful " +
@@ -2351,7 +2352,7 @@ FileInputStream[] requestShortCircuitFdsForRead(final ExtendedBlock blk,
23512352
FileInputStream fis[] = new FileInputStream[2];
23522353

23532354
try {
2354-
Preconditions.checkNotNull(data, "Storage not yet initialized");
2355+
checkStorageState("requestShortCircuitFdsForRead");
23552356
fis[0] = (FileInputStream)data.getBlockInputStream(blk, 0);
23562357
fis[1] = DatanodeUtil.getMetaDataInputStream(blk, data);
23572358
} catch (ClassCastException e) {
@@ -3382,7 +3383,7 @@ public static void main(String args[]) {
33823383
@Override // InterDatanodeProtocol
33833384
public ReplicaRecoveryInfo initReplicaRecovery(RecoveringBlock rBlock)
33843385
throws IOException {
3385-
Preconditions.checkNotNull(data, "Storage not yet initialized");
3386+
checkStorageState("initReplicaRecovery");
33863387
return data.initReplicaRecovery(rBlock);
33873388
}
33883389

@@ -3393,7 +3394,7 @@ public ReplicaRecoveryInfo initReplicaRecovery(RecoveringBlock rBlock)
33933394
public String updateReplicaUnderRecovery(final ExtendedBlock oldBlock,
33943395
final long recoveryId, final long newBlockId, final long newLength)
33953396
throws IOException {
3396-
Preconditions.checkNotNull(data, "Storage not yet initialized");
3397+
checkStorageState("updateReplicaUnderRecovery");
33973398
final Replica r = data.updateReplicaUnderRecovery(oldBlock,
33983399
recoveryId, newBlockId, newLength);
33993400
// Notify the namenode of the updated block info. This is important
@@ -3679,10 +3680,23 @@ public void deleteBlockPool(String blockPoolId, boolean force)
36793680
"The block pool is still running. First do a refreshNamenodes to " +
36803681
"shutdown the block pool service");
36813682
}
3682-
Preconditions.checkNotNull(data, "Storage not yet initialized");
3683+
checkStorageState("deleteBlockPool");
36833684
data.deleteBlockPool(blockPoolId, force);
36843685
}
36853686

3687+
/**
3688+
* Check if storage has been initialized.
3689+
* @param methodName caller name
3690+
* @throws IOException throw IOException if not yet initialized.
3691+
*/
3692+
private void checkStorageState(String methodName) throws IOException {
3693+
if (data == null) {
3694+
String message = "Storage not yet initialized for " + methodName;
3695+
LOG.debug(message);
3696+
throw new IOException(message);
3697+
}
3698+
}
3699+
36863700
@Override // ClientDatanodeProtocol
36873701
public synchronized void shutdownDatanode(boolean forUpgrade) throws IOException {
36883702
checkSuperuserPrivilege();
@@ -4131,7 +4145,7 @@ public String getSlowDisks() {
41314145
@Override
41324146
public List<DatanodeVolumeInfo> getVolumeReport() throws IOException {
41334147
checkSuperuserPrivilege();
4134-
Preconditions.checkNotNull(data, "Storage not yet initialized");
4148+
checkStorageState("getVolumeReport");
41354149
Map<String, Object> volumeInfoMap = data.getVolumeInfoMap();
41364150
if (volumeInfoMap == null) {
41374151
LOG.warn("DataNode volume info not available.");

0 commit comments

Comments
 (0)