sreejasahithi commented on code in PR #10414: URL: https://github.com/apache/ozone/pull/10414#discussion_r3373784408
########## hadoop-ozone/cli-debug/src/main/java/org/apache/hadoop/ozone/debug/datanode/container/analyze/ContainerDirectoryScanner.java: ########## @@ -0,0 +1,282 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.ozone.debug.datanode.container.analyze; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import org.apache.commons.io.FileUtils; +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.hdds.scm.ScmConfigKeys; +import org.apache.hadoop.hdds.utils.HddsServerUtil; +import org.apache.hadoop.hdfs.server.datanode.StorageLocation; +import org.apache.hadoop.ozone.common.InconsistentStorageStateException; +import org.apache.hadoop.ozone.common.Storage; +import org.apache.hadoop.ozone.container.common.helpers.ContainerUtils; +import org.apache.hadoop.ozone.container.common.helpers.DatanodeVersionFile; +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.utils.StorageVolumeUtil; +import org.apache.hadoop.ozone.container.common.volume.HddsVolume; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Read-only walker for container directories under {@code hdds.datanode.dir}. + * + * <p>Unlike {@link org.apache.hadoop.ozone.container.ozoneimpl.ContainerReader}, + * this scanner records every container directory it finds, including those with + * missing or invalid metadata and duplicate copies across volumes. + */ +public class ContainerDirectoryScanner { + + private static final Logger LOG = LoggerFactory.getLogger(ContainerDirectoryScanner.class); + + /** + * Scan all configured DataNode storage volumes and return every container + * directory found on disk. + * + * @return map of container ID to all on-disk occurrences for that ID + */ + public Map<Long, List<ContainerDiskOccurrence>> scan(ConfigurationSource conf) throws IOException { + Collection<String> storageDirs = HddsServerUtil.getDatanodeStorageDirs(conf); + + if (storageDirs.isEmpty()) { + return Collections.emptyMap(); + } + + validateStorageDirectories(storageDirs); + Map<Long, List<ContainerDiskOccurrence>> result = new ConcurrentHashMap<>(); + int volumeCount = storageDirs.size(); + ExecutorService executor = Executors.newFixedThreadPool(volumeCount, + new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat("ContainerDirectoryScanner-%d") + .build()); + + try { + List<Future<?>> futures = new ArrayList<>(volumeCount); + for (String storageDir : storageDirs) { + String volumeRoot = StorageLocation.parse(storageDir).getUri().getPath(); + futures.add(executor.submit(() -> { + try { + scanVolume(volumeRoot, result); + } catch (IOException e) { + throw new RuntimeException( + "Failed to scan volume " + volumeRoot, e); + } + })); + } + for (Future<?> future : futures) { + try { + future.get(); + } catch (ExecutionException e) { + Throwable cause = e.getCause(); + if (cause instanceof RuntimeException) { + throw (RuntimeException) cause; + } + throw new IOException(cause); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("Volume scan interrupted", e); + } + } + } finally { + executor.shutdownNow(); + } + + return Collections.unmodifiableMap(toImmutableLists(result)); + } + + /** + * Scan a single DataNode storage volume root and merge results into {@code result}. + */ + private void scanVolume(String volumeRoot, Map<Long, List<ContainerDiskOccurrence>> result) throws IOException { + File hddsRoot = new File(volumeRoot, HddsVolume.HDDS_VOLUME_DIR); + if (!hddsRoot.isDirectory()) { + LOG.warn("HDDS root {} does not exist or is not a directory, skipping volume {}", hddsRoot, volumeRoot); + return; + } + + File versionFile = StorageVolumeUtil.getVersionFile(hddsRoot); + Properties props = DatanodeVersionFile.readFrom(versionFile); + if (props.isEmpty()) { + throw new IOException("Version file " + versionFile + " is missing or empty"); + } + String clusterId; + try { + clusterId = StorageVolumeUtil.getClusterID(props, versionFile, null); + } catch (InconsistentStorageStateException e) { + throw new IOException("Invalid version file " + versionFile, e); + } + + File currentDir = resolveCurrentDir(hddsRoot, clusterId); + if (currentDir == null || !currentDir.isDirectory()) { + LOG.info("No current container directory under {}, skipping volume {}", hddsRoot, volumeRoot); + return; + } + + LOG.info("Scanning container directories under {}", currentDir); + File[] containerTopDirs = currentDir.listFiles(File::isDirectory); Review Comment: The `current` dir resolution is now shared via `StorageVolumeUtil.resolveContainerCurrentDir()`. For the nested directory walk, I kept the loops separate intentionally because the per-directory behavior differs as `ContainerDirectoryScanner.scanVolume()` must record every on-disk container path including missing/invalid metadata and duplicates for the analysis. -- 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]
