|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.gobblin.yarn; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import org.apache.hadoop.fs.FileSystem; |
| 23 | +import org.apache.hadoop.fs.Path; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import com.google.common.annotations.VisibleForTesting; |
| 28 | +import com.typesafe.config.Config; |
| 29 | + |
| 30 | +import org.apache.gobblin.util.ConfigUtils; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Utility class for resolving the jar cache directory path by validating filesystem on path existence and applying fallback logic. |
| 35 | + * |
| 36 | + * <p>Resolution logic:</p> |
| 37 | + * <ol> |
| 38 | + * <li>If JAR_CACHE_DIR is explicitly configured, uses it as-is (for backward compatibility)</li> |
| 39 | + * <li>Otherwise, validates JAR_CACHE_ROOT_DIR exists on filesystem</li> |
| 40 | + * <li>If not found, tries FALLBACK_JAR_CACHE_ROOT_DIR</li> |
| 41 | + * <li>Combines validated root with JAR_CACHE_SUFFIX (or default suffix) to form final path</li> |
| 42 | + * <li>If no valid root found, throws IOException</li> |
| 43 | + * </ol> |
| 44 | + */ |
| 45 | +public class JarCachePathResolver { |
| 46 | + private static final Logger LOGGER = LoggerFactory.getLogger(JarCachePathResolver.class); |
| 47 | + // Note: Trailing slash will be normalized away by Hadoop Path |
| 48 | + private static final String DEFAULT_JAR_CACHE_SUFFIX = ".gobblinCache/gobblin-temporal"; |
| 49 | + |
| 50 | + // Private constructor to prevent instantiation |
| 51 | + private JarCachePathResolver() { |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Resolves the jar cache directory path, applying validation and fallback logic. |
| 56 | + * |
| 57 | + * @param config the configuration |
| 58 | + * @param fs the filesystem to use for validation |
| 59 | + * @return the resolved jar cache directory path |
| 60 | + * @throws IOException if filesystem operations fail or no valid root directory is found |
| 61 | + */ |
| 62 | + public static Path resolveJarCachePath(Config config, FileSystem fs) throws IOException { |
| 63 | + // If JAR_CACHE_DIR is explicitly set, use it as-is |
| 64 | + if (config.hasPath(GobblinYarnConfigurationKeys.JAR_CACHE_DIR)) { |
| 65 | + String explicitCacheDir = config.getString(GobblinYarnConfigurationKeys.JAR_CACHE_DIR); |
| 66 | + LOGGER.info("Using explicitly configured JAR_CACHE_DIR: {}", explicitCacheDir); |
| 67 | + return new Path(explicitCacheDir); |
| 68 | + } |
| 69 | + |
| 70 | + // Get suffix from config, or use default if not configured or empty |
| 71 | + String suffix = ConfigUtils.getString(config, GobblinYarnConfigurationKeys.JAR_CACHE_SUFFIX, ""); |
| 72 | + if (suffix == null || suffix.trim().isEmpty()) { |
| 73 | + LOGGER.info("JAR_CACHE_SUFFIX not configured or empty, using default: {}", DEFAULT_JAR_CACHE_SUFFIX); |
| 74 | + suffix = DEFAULT_JAR_CACHE_SUFFIX; |
| 75 | + } |
| 76 | + |
| 77 | + // Try primary root directory |
| 78 | + if (config.hasPath(GobblinYarnConfigurationKeys.JAR_CACHE_ROOT_DIR)) { |
| 79 | + String rootDir = config.getString(GobblinYarnConfigurationKeys.JAR_CACHE_ROOT_DIR); |
| 80 | + Path resolvedPath = validateAndComputePath(fs, rootDir, suffix, GobblinYarnConfigurationKeys.JAR_CACHE_ROOT_DIR); |
| 81 | + if (resolvedPath != null) { |
| 82 | + return resolvedPath; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Try fallback root directory |
| 87 | + if (config.hasPath(GobblinYarnConfigurationKeys.FALLBACK_JAR_CACHE_ROOT_DIR)) { |
| 88 | + String fallbackRootDir = config.getString(GobblinYarnConfigurationKeys.FALLBACK_JAR_CACHE_ROOT_DIR); |
| 89 | + Path resolvedPath = validateAndComputePath(fs, fallbackRootDir, suffix, GobblinYarnConfigurationKeys.FALLBACK_JAR_CACHE_ROOT_DIR); |
| 90 | + if (resolvedPath != null) { |
| 91 | + return resolvedPath; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // No valid root directory found - fail |
| 96 | + throw new IOException("No valid jar cache root directory found. Please configure " |
| 97 | + + GobblinYarnConfigurationKeys.JAR_CACHE_ROOT_DIR + " or " |
| 98 | + + GobblinYarnConfigurationKeys.FALLBACK_JAR_CACHE_ROOT_DIR |
| 99 | + + " with a valid directory path, or explicitly set " |
| 100 | + + GobblinYarnConfigurationKeys.JAR_CACHE_DIR); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Validates if the root directory exists and computes the full path with suffix. |
| 105 | + * |
| 106 | + * @param fs the filesystem to check |
| 107 | + * @param rootDir the root directory to validate |
| 108 | + * @param suffix the suffix to append |
| 109 | + * @param configName the config name for logging |
| 110 | + * @return the computed path if valid, null otherwise |
| 111 | + * @throws IOException if filesystem operations fail |
| 112 | + */ |
| 113 | + @VisibleForTesting |
| 114 | + static Path validateAndComputePath(FileSystem fs, String rootDir, String suffix, String configName) throws IOException { |
| 115 | + Path rootPath = new Path(rootDir); |
| 116 | + if (fs.exists(rootPath)) { |
| 117 | + // Strip leading '/' from suffix to ensure proper concatenation |
| 118 | + // Otherwise, Hadoop Path treats it as absolute path and ignores the parent |
| 119 | + String normalizedSuffix = suffix.startsWith("/") ? suffix.substring(1) : suffix; |
| 120 | + Path fullPath = new Path(rootPath, normalizedSuffix); |
| 121 | + LOGGER.info("{} exists: {}, resolved JAR_CACHE_DIR to: {}", configName, rootDir, fullPath); |
| 122 | + return fullPath; |
| 123 | + } |
| 124 | + LOGGER.warn("Configured {} does not exist: {}", configName, rootDir); |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | +} |
| 129 | + |
0 commit comments