Skip to content

Commit 61362f4

Browse files
author
zengqiang.xu
committed
HDFS-16717. Replace NPE with IOException in DataNode.class
1 parent 906ae51 commit 61362f4

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
@@ -3676,10 +3677,23 @@ public void deleteBlockPool(String blockPoolId, boolean force)
36763677
"The block pool is still running. First do a refreshNamenodes to " +
36773678
"shutdown the block pool service");
36783679
}
3679-
Preconditions.checkNotNull(data, "Storage not yet initialized");
3680+
checkStorageState("deleteBlockPool");
36803681
data.deleteBlockPool(blockPoolId, force);
36813682
}
36823683

3684+
/**
3685+
* Check if storage has been initialized.
3686+
* @param methodName caller name
3687+
* @throws IOException throw IOException if not yet initialized.
3688+
*/
3689+
private void checkStorageState(String methodName) throws IOException {
3690+
if (data == null) {
3691+
String message = "Storage not yet initialized for " + methodName;
3692+
LOG.debug(message);
3693+
throw new IOException(message);
3694+
}
3695+
}
3696+
36833697
@Override // ClientDatanodeProtocol
36843698
public synchronized void shutdownDatanode(boolean forUpgrade) throws IOException {
36853699
checkSuperuserPrivilege();
@@ -4128,7 +4142,7 @@ public String getSlowDisks() {
41284142
@Override
41294143
public List<DatanodeVolumeInfo> getVolumeReport() throws IOException {
41304144
checkSuperuserPrivilege();
4131-
Preconditions.checkNotNull(data, "Storage not yet initialized");
4145+
checkStorageState("getVolumeReport");
41324146
Map<String, Object> volumeInfoMap = data.getVolumeInfoMap();
41334147
if (volumeInfoMap == null) {
41344148
LOG.warn("DataNode volume info not available.");

0 commit comments

Comments
 (0)