ChenSammi commented on code in PR #5310: URL: https://github.com/apache/ozone/pull/5310#discussion_r1333992515
########## hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/upgrade/UpgradeTask.java: ########## @@ -0,0 +1,391 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hdds.scm.cli.container.upgrade; + +import com.google.common.base.Preconditions; +import org.apache.hadoop.hdds.StringUtils; +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos; +import org.apache.hadoop.hdds.utils.db.DBStore; +import org.apache.hadoop.hdds.utils.db.FixedLengthStringCodec; +import org.apache.hadoop.hdds.utils.db.Table; +import org.apache.hadoop.hdds.utils.db.TableIterator; +import org.apache.hadoop.io.nativeio.NativeIO; +import org.apache.hadoop.ozone.OzoneConsts; +import org.apache.hadoop.ozone.common.Storage; +import org.apache.hadoop.ozone.container.common.helpers.ContainerUtils; +import org.apache.hadoop.ozone.container.common.impl.ContainerData; +import org.apache.hadoop.ozone.container.common.impl.ContainerDataYaml; +import org.apache.hadoop.ozone.container.common.volume.HddsVolume; +import org.apache.hadoop.ozone.container.keyvalue.KeyValueContainer; +import org.apache.hadoop.ozone.container.keyvalue.KeyValueContainerData; +import org.apache.hadoop.ozone.container.keyvalue.helpers.BlockUtils; +import org.apache.hadoop.ozone.container.keyvalue.helpers.KeyValueContainerUtil; +import org.apache.hadoop.ozone.container.metadata.DatanodeSchemaThreeDBDefinition; +import org.apache.hadoop.ozone.container.metadata.DatanodeSchemaTwoDBDefinition; +import org.apache.hadoop.ozone.container.metadata.DatanodeStore; +import org.apache.hadoop.ozone.container.metadata.DatanodeStoreSchemaThreeImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * This class process upgrade v2 to v3 container. + */ +public class UpgradeTask { + + public static final Logger LOG = + LoggerFactory.getLogger(UpgradeTask.class); + + private final ConfigurationSource config; + private final HddsVolume hddsVolume; + private final DatanodeStoreSchemaThreeImpl datanodeStoreSchemaThree; + + private static final String BACKUP_CONTAINER_DATA_FILE_SUFFIX = + ".v2.container"; + + public UpgradeTask(ConfigurationSource config, + HddsVolume hddsVolume, + DatanodeStoreSchemaThreeImpl datanodeStoreSchemaThree) { + this.config = config; + this.hddsVolume = hddsVolume; + this.datanodeStoreSchemaThree = datanodeStoreSchemaThree; + } + + public CompletableFuture<UpgradeManager.Result> getUpgradeFutureByVolume() { + return CompletableFuture.supplyAsync(() -> { + + final UpgradeManager.Result result = + new UpgradeManager.Result(hddsVolume); + + List<Result> resultList = new ArrayList<>(); + final File hddsVolumeRootDir = hddsVolume.getHddsRootDir(); + + Preconditions.checkNotNull(hddsVolumeRootDir, "hddsVolumeRootDir" + + "cannot be null"); + + //filtering storage directory + File[] storageDirs = hddsVolumeRootDir.listFiles(File::isDirectory); + + if (storageDirs == null) { + LOG.error("IO error for the volume {}, skipped loading", + hddsVolumeRootDir); + return null; + } + + + if (storageDirs.length > 0) { + File clusterIDDir = new File(hddsVolumeRootDir, + hddsVolume.getClusterID()); + File idDir = clusterIDDir; + if (storageDirs.length == 1 && !clusterIDDir.exists()) { + idDir = storageDirs[0]; + } else { + if (!clusterIDDir.exists()) { + LOG.error("Volume {} is in an inconsistent state. Expected " + + "clusterID directory {} not found.", hddsVolumeRootDir, + clusterIDDir); + return null; + } + } + + LOG.info("Start to upgrade containers on volume {}", hddsVolumeRootDir); + File currentDir = new File(idDir, Storage.STORAGE_DIR_CURRENT); + File[] containerTopDirs = currentDir.listFiles(); + if (containerTopDirs != null) { + for (File containerTopDir : containerTopDirs) { + try { + final List<Result> results = + upgradeSubContainerDir(containerTopDir); + resultList.addAll(results); + } catch (IOException e) { + result.fail(e); + return result; + } + } + } + } + + result.setResultList(resultList); + result.success(); + return result; + }); + } + + private List<Result> upgradeSubContainerDir(File containerTopDir) + throws IOException { + List<Result> resultList = new ArrayList<>(); + if (containerTopDir.isDirectory()) { + File[] containerDirs = containerTopDir.listFiles(); + if (containerDirs != null) { + for (File containerDir : containerDirs) { + final ContainerData containerData = parseContainerData(containerDir); + if (containerData != null) { Review Comment: Check if the container is V2. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
