You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by GitBox <gi...@apache.org> on 2022/01/21 18:45:02 UTC

[GitHub] [accumulo] milleruntime commented on a change in pull request #2425: Separate native map loading from native map code

milleruntime commented on a change in pull request #2425:
URL: https://github.com/apache/accumulo/pull/2425#discussion_r789909142



##########
File path: server/tserver/src/main/java/org/apache/accumulo/tserver/memory/NativeMapLoader.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.accumulo.tserver.memory;
+
+import java.io.File;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.accumulo.core.conf.Property;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+
+public class NativeMapLoader {
+
+  private static final Logger log = LoggerFactory.getLogger(NativeMapLoader.class);
+  private static final Pattern dotSuffix = Pattern.compile("[.][^.]*$");
+  private static final String PROP_NAME = "accumulo.native.lib.path";
+  private static final AtomicBoolean loaded = new AtomicBoolean(false);
+
+  // don't allow instantiation
+  private NativeMapLoader() {}
+
+  public synchronized static void load() {
+    // load at most once; System.exit if loading fails
+    if (loaded.compareAndSet(false, true)) {
+      if (loadFromSearchPath(System.getProperty(PROP_NAME)) || loadFromSystemLinker()) {
+        return;
+      }
+      log.error(
+          "FATAL! Accumulo native libraries were requested but could not"
+              + " be be loaded. Either set '{}' to false in accumulo.properties or make"
+              + " sure native libraries are created in directories set by the JVM"
+              + " system property '{}' in accumulo-env.sh!",
+          Property.TSERV_NATIVEMAP_ENABLED, PROP_NAME);
+      System.exit(1);
+    }
+  }
+
+  public static void loadForTest(List<File> locations, Runnable onFail) {
+    // if the library can't be loaded at the given path, execute the failure task
+    var searchPath = locations.stream().map(File::getAbsolutePath).collect(Collectors.joining(":"));
+    if (!loadFromSearchPath(searchPath)) {
+      onFail.run();
+    }
+  }
+
+  /**
+   * The specified search path will be used to attempt to load them. Directories will be searched by
+   * using the system-specific library naming conventions. A path directly to a file can also be
+   * provided. Loading will continue until the search path is exhausted, or until the native
+   * libraries are found and successfully loaded, whichever occurs first.
+   */
+  private static boolean loadFromSearchPath(String searchPath) {
+    // Attempt to load from these directories, using standard names, or by an exact file name
+    if (searchPath != null) {
+      if (Stream.of(searchPath.split(":")).flatMap(NativeMapLoader::mapLibraryNames)
+          .anyMatch(NativeMapLoader::loadNativeLib)) {
+        return true;
+      }
+      log.error("Tried and failed to load Accumulo native library from property {} set to {}",
+          PROP_NAME, searchPath);
+    }
+    return false;
+  }
+
+  // Check LD_LIBRARY_PATH (DYLD_LIBRARY_PATH on Mac)
+  private static boolean loadFromSystemLinker() {
+    String propName = "java.library.path";

Review comment:
       Could make this final and put at top with the other static final variables. Then you'd have to rename the system properties to be more descriptive.

##########
File path: server/tserver/src/main/java/org/apache/accumulo/tserver/TabletServerResourceManager.java
##########
@@ -253,8 +254,10 @@ public TabletServerResourceManager(ServerContext context) {
     final AccumuloConfiguration acuConf = context.getConfiguration();
 
     long maxMemory = acuConf.getAsBytes(Property.TSERV_MAXMEM);
-    boolean usingNativeMap =
-        acuConf.getBoolean(Property.TSERV_NATIVEMAP_ENABLED) && NativeMap.isLoaded();
+    boolean usingNativeMap = acuConf.getBoolean(Property.TSERV_NATIVEMAP_ENABLED);
+    if (usingNativeMap) {
+      NativeMapLoader.load();

Review comment:
       Won't this cause the native map to get loaded twice because of the static code block being executed when `NativeMap` class is loaded? If we have this do we still need the static code block?

##########
File path: server/tserver/src/main/java/org/apache/accumulo/tserver/memory/NativeMapLoader.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.accumulo.tserver.memory;
+
+import java.io.File;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.accumulo.core.conf.Property;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+
+public class NativeMapLoader {
+
+  private static final Logger log = LoggerFactory.getLogger(NativeMapLoader.class);
+  private static final Pattern dotSuffix = Pattern.compile("[.][^.]*$");
+  private static final String PROP_NAME = "accumulo.native.lib.path";
+  private static final AtomicBoolean loaded = new AtomicBoolean(false);
+
+  // don't allow instantiation
+  private NativeMapLoader() {}

Review comment:
       It seems like this class could be a regular java object and have its static methods converted to non static methods. It might make it easier to test.




-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org