wchevreuil commented on code in PR #4869:
URL: https://github.com/apache/hbase/pull/4869#discussion_r1018031436


##########
hbase-common/src/main/java/org/apache/hadoop/hbase/io/crypto/tls/X509Util.java:
##########
@@ -395,4 +411,57 @@ private static String[] getCipherSuites(Configuration 
config) {
       return cipherSuitesInput.split(",");
     }
   }
+
+  private static FileChangeWatcher newFileChangeWatcher(String fileLocation, 
Runnable resetContext)
+    throws IOException {
+    if (fileLocation == null || fileLocation.isEmpty() || resetContext == 
null) {
+      return null;
+    }
+    final Path filePath = Paths.get(fileLocation).toAbsolutePath();
+    Path parentPath = filePath.getParent();
+    if (parentPath == null) {
+      throw new IOException("Key/trust store path does not have a parent: " + 
filePath);
+    }
+    FileChangeWatcher fileChangeWatcher = new FileChangeWatcher(parentPath, 
watchEvent -> {
+      handleWatchEvent(filePath, watchEvent, resetContext);
+    });
+    fileChangeWatcher.start();
+    return fileChangeWatcher;
+  }
+
+  /**
+   * Handler for watch events that let us know a file we may care about has 
changed on disk.
+   * @param filePath the path to the file we are watching for changes.
+   * @param event    the WatchEvent.
+   */
+  private static void handleWatchEvent(Path filePath, WatchEvent<?> event, 
Runnable resetContext) {
+    boolean shouldResetContext = false;
+    Path dirPath = filePath.getParent();
+    if (event.kind().equals(StandardWatchEventKinds.OVERFLOW)) {
+      // If we get notified about possibly missed events, reload the key store 
/ trust store just to
+      // be sure.
+      shouldResetContext = true;
+    } else if (
+      event.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)
+        || event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)
+    ) {
+      Path eventFilePath = dirPath.resolve((Path) event.context());
+      if (filePath.equals(eventFilePath)) {
+        shouldResetContext = true;
+      }
+    }
+    // Note: we don't care about delete events
+    if (shouldResetContext) {
+      if (LOG.isDebugEnabled()) {

Review Comment:
   nit: Maybe worth info logging? My thought here is that this wouldn't be a 
frequent event, yet a important one to get logged at higher level than 
debugging. 



##########
hbase-common/src/main/java/org/apache/hadoop/hbase/io/crypto/tls/X509Util.java:
##########
@@ -395,4 +411,57 @@ private static String[] getCipherSuites(Configuration 
config) {
       return cipherSuitesInput.split(",");
     }
   }
+
+  private static FileChangeWatcher newFileChangeWatcher(String fileLocation, 
Runnable resetContext)
+    throws IOException {
+    if (fileLocation == null || fileLocation.isEmpty() || resetContext == 
null) {
+      return null;
+    }
+    final Path filePath = Paths.get(fileLocation).toAbsolutePath();
+    Path parentPath = filePath.getParent();
+    if (parentPath == null) {
+      throw new IOException("Key/trust store path does not have a parent: " + 
filePath);
+    }
+    FileChangeWatcher fileChangeWatcher = new FileChangeWatcher(parentPath, 
watchEvent -> {
+      handleWatchEvent(filePath, watchEvent, resetContext);
+    });
+    fileChangeWatcher.start();
+    return fileChangeWatcher;
+  }
+
+  /**
+   * Handler for watch events that let us know a file we may care about has 
changed on disk.
+   * @param filePath the path to the file we are watching for changes.
+   * @param event    the WatchEvent.
+   */
+  private static void handleWatchEvent(Path filePath, WatchEvent<?> event, 
Runnable resetContext) {
+    boolean shouldResetContext = false;
+    Path dirPath = filePath.getParent();
+    if (event.kind().equals(StandardWatchEventKinds.OVERFLOW)) {
+      // If we get notified about possibly missed events, reload the key store 
/ trust store just to
+      // be sure.
+      shouldResetContext = true;
+    } else if (
+      event.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)
+        || event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)
+    ) {
+      Path eventFilePath = dirPath.resolve((Path) event.context());
+      if (filePath.equals(eventFilePath)) {
+        shouldResetContext = true;
+      }
+    }
+    // Note: we don't care about delete events
+    if (shouldResetContext) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("Attempting to reset default SSL context after receiving 
watch event: "
+          + event.kind() + " with context: " + event.context());
+      }
+      resetContext.run();
+    } else {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("Ignoring watch event and keeping previous default SSL 
context. Event kind: "

Review Comment:
   nit: parameterized logging?



##########
hbase-common/src/main/java/org/apache/hadoop/hbase/io/FileChangeWatcher.java:
##########
@@ -0,0 +1,251 @@
+/*
+ * 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.hbase.io;
+
+import java.io.IOException;
+import java.nio.file.ClosedWatchServiceException;
+import java.nio.file.FileSystem;
+import java.nio.file.Path;
+import java.nio.file.StandardWatchEventKinds;
+import java.nio.file.WatchEvent;
+import java.nio.file.WatchKey;
+import java.nio.file.WatchService;
+import java.util.function.Consumer;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.apache.zookeeper.server.ZooKeeperThread;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Instances of this class can be used to watch a directory for file changes. 
When a file is added
+ * to, deleted from, or is modified in the given directory, the callback 
provided by the user will
+ * be called from a background thread. Some things to keep in mind:
+ * <ul>
+ * <li>The callback should be thread-safe.</li>
+ * <li>Changes that happen around the time the thread is started may be 
missed.</li>
+ * <li>There is a delay between a file changing and the callback firing.</li>
+ * <li>The watch is not recursive - changes to subdirectories will not trigger 
a callback.</li>
+ * </ul>
+ * <p/>
+ * This file has been copied from the Apache ZooKeeper project.
+ * @see <a href=
+ *      
"https://github.com/apache/zookeeper/blob/8148f966947d3ecf3db0b756d93c9ffa88174af9/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileChangeWatcher.java";>Base
+ *      revision</a>
+ */
+@InterfaceAudience.Private
+public final class FileChangeWatcher {

Review Comment:
   Is this a straight copy from Zookeeper? Couldn't we just reuse the ZK impl 
directly, as we already have ZK as a dependency?



-- 
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: issues-unsubscr...@hbase.apache.org

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

Reply via email to