diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/DatanodeLayoutStorage.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/DatanodeLayoutStorage.java index d0009aa66652..d933a04e0ff1 100644 --- a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/DatanodeLayoutStorage.java +++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/DatanodeLayoutStorage.java @@ -27,6 +27,8 @@ import org.apache.hadoop.hdds.conf.OzoneConfiguration; import org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeType; import org.apache.hadoop.hdds.server.ServerUtils; +import org.apache.hadoop.hdds.upgrade.HDDSLayoutFeature; +import org.apache.hadoop.hdds.utils.HddsServerUtil; import org.apache.hadoop.ozone.common.Storage; /** @@ -34,7 +36,6 @@ * StorageDirectories used by the DataNode. */ public class DatanodeLayoutStorage extends Storage { - /** * Construct DataNodeStorageConfig. * @throws IOException if any directories are inaccessible. @@ -42,7 +43,7 @@ public class DatanodeLayoutStorage extends Storage { public DatanodeLayoutStorage(OzoneConfiguration conf, String dataNodeId) throws IOException { super(NodeType.DATANODE, ServerUtils.getOzoneMetaDirPath(conf), - DATANODE_LAYOUT_VERSION_DIR, dataNodeId, maxLayoutVersion()); + DATANODE_LAYOUT_VERSION_DIR, dataNodeId, getDefaultLayoutVersion(conf)); } public DatanodeLayoutStorage(OzoneConfiguration conf, String dataNodeId, @@ -61,4 +62,36 @@ public File getCurrentDir() { protected Properties getNodeProperties() { return new Properties(); } + + /** + * Older versions of the code did not write a VERSION file to disk for the + * datanode. Therefore, When a datanode starts up and does not find a metadata + * layout version written to disk, it must figure out whether + * it is being installed on a new system, or as part of an upgrade to an + * existing system. To do this, it will use the presence of the datanode.id + * file to indicate that it is being started as part of an upgrade and + * should start with the initial layout version. Otherwise, it will use the + * latest layout version. + * + * This is only an issue when upgrading from software layout version + * {@link HDDSLayoutFeature#INITIAL_VERSION} to + * {@link HDDSLayoutFeature#DATANODE_SCHEMA_V2}. After this, the upgrade + * framework will write a VERSION file for the datanode and future software + * versions will always know the MLV to use. The result of this method will + * not be used in this case. + * + * @return The layout version that should be used for the datanode if no + * layout version is found on disk. + */ + private static int getDefaultLayoutVersion(OzoneConfiguration conf) { + int defaultLayoutVersion = maxLayoutVersion(); + + File dnIdFile = new File(HddsServerUtil.getDatanodeIdFilePath(conf)); + if (dnIdFile.exists()) { + defaultLayoutVersion = + HDDSLayoutFeature.INITIAL_VERSION.layoutVersion(); + } + + return defaultLayoutVersion; + } }