ChenSammi commented on code in PR #7379: URL: https://github.com/apache/ozone/pull/7379#discussion_r1827204506
########## hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/DomainSocketFactory.java: ########## @@ -0,0 +1,272 @@ +/** + * 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.hdds.scm.storage; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.SystemUtils; +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.hdds.scm.OzoneClientConfig; +import org.apache.hadoop.net.unix.DomainSocket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Closeable; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.ConcurrentHashMap; + +/** + * A factory to help create DomainSocket. + */ +public final class DomainSocketFactory implements Closeable { + private static final Logger LOG = LoggerFactory.getLogger( + DomainSocketFactory.class); + + /** + * Domain socket path state. + */ + public enum PathState { + NOT_CONFIGURED(false), + DISABLED(false), + VALID(true); + + PathState(boolean usableForShortCircuit) { + this.usableForShortCircuit = usableForShortCircuit; + } + + public boolean getUsableForShortCircuit() { + return usableForShortCircuit; + } + private final boolean usableForShortCircuit; + } + + /** + * Domain socket path. + */ + public static class PathInfo { + private static final PathInfo NOT_CONFIGURED = new PathInfo("", PathState.NOT_CONFIGURED); + private static final PathInfo DISABLED = new PathInfo("", PathState.DISABLED); + private static final PathInfo VALID = new PathInfo("", PathState.VALID); + + private final String path; + private final PathState state; + + PathInfo(String path, PathState state) { + this.path = path; + this.state = state; + } + + public String getPath() { + return path; + } + + public PathState getPathState() { + return state; + } + + @Override + public String toString() { + return "PathInfo{path=" + path + ", state=" + state + "}"; + } + } + + public static final String FEATURE = "short-circuit reads"; + public static final String FEATURE_FLAG = "SC"; + private static boolean nativeCodeLoaded = false; + private static String nativeLibraryLoadFailureReason; + private long pathExpireMills; + private final ConcurrentHashMap<String, PathInfo> pathMap; + private Timer timer; + private boolean isEnabled = false; + private String domainSocketPath; + + static { + // Try to load native hadoop library and set fallback flag appropriately + if (SystemUtils.IS_OS_WINDOWS) { + nativeLibraryLoadFailureReason = "UNIX Domain sockets are not available on Windows."; + } else { + LOG.info("Trying to load the custom-built native-hadoop library..."); + try { + System.loadLibrary("hadoop"); + LOG.info("Loaded the native-hadoop library"); + nativeCodeLoaded = true; + } catch (Throwable t) { + // Ignore failure to continue + LOG.info("Failed to load native-hadoop with error: " + t); + LOG.info("java.library.path=" + System.getProperty("java.library.path")); + nativeLibraryLoadFailureReason = "libhadoop cannot be loaded."; + } + + if (!nativeCodeLoaded) { + LOG.warn("Unable to load native-hadoop library for your platform... " + + "using builtin-java classes where applicable"); + } + } + } + + private static volatile DomainSocketFactory instance = null; + + public static DomainSocketFactory getInstance(ConfigurationSource conf) { + if (instance == null) { + synchronized (DomainSocketFactory.class) { + if (instance == null) { + instance = new DomainSocketFactory(conf); + } + } + } + return instance; + } + + private DomainSocketFactory(ConfigurationSource conf) { + OzoneClientConfig clientConfig = conf.getObject(OzoneClientConfig.class); + boolean shortCircuitEnabled = clientConfig.isShortCircuitEnabled(); + PathInfo pathInfo; + long startTime = System.nanoTime(); + if (!shortCircuitEnabled) { + LOG.info(FEATURE + " is disabled."); + pathInfo = PathInfo.NOT_CONFIGURED; Review Comment: The [DomainSocketFactory.java](https://github.com/apache/ozone/pull/7379/files/bbca6a60f3a27e65cc6ed98b48c7e0600e6f0b5d#diff-c1beecf1fb0820e1cfcf962cd28ff86f197938addcfe5bfd4be074843c1a2fb2) is an singleton instance. It is only instantiated once. Later reference will call isServiceEnabled and isServiceReady to determine whether DomainSocket can be created or not. -- 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]
