zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tumativ <...@git.apache.org>
Subject [GitHub] zookeeper pull request #680: ZOOKEEPER-3174: Quorum TLS - support reloading ...
Date Fri, 02 Nov 2018 18:19:50 GMT
Github user tumativ commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/680#discussion_r230463114
  
    --- Diff: zookeeper-server/src/main/java/org/apache/zookeeper/common/FileChangeWatcher.java
---
    @@ -0,0 +1,180 @@
    +/**
    + * 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.zookeeper.common;
    +
    +import com.sun.nio.file.SensitivityWatchEventModifier;
    +import org.apache.zookeeper.server.ZooKeeperThread;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +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;
    +
    +/**
    + * 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>
    + */
    +public final class FileChangeWatcher {
    +    private static final Logger LOG = LoggerFactory.getLogger(FileChangeWatcher.class);
    +
    +    private final WatcherThread watcherThread;
    +
    +    /**
    +     * Creates a watcher that watches <code>dirPath</code> and invokes <code>callback</code>
on changes.
    +     *
    +     * @param dirPath the directory to watch.
    +     * @param callback the callback to invoke with events. <code>event.kind()</code>
will return the type of event,
    +     *                 and <code>event.context()</code> will return the filename
relative to <code>dirPath</code>.
    +     * @throws IOException if there is an error creating the WatchService.
    +     */
    +    public FileChangeWatcher(Path dirPath, Consumer<WatchEvent<?>> callback)
throws IOException {
    +        FileSystem fs = dirPath.getFileSystem();
    +        WatchService watchService = fs.newWatchService();
    +        if (LOG.isDebugEnabled()) {
    +            LOG.debug("Registering with watch service: " + dirPath);
    +        }
    +        dirPath.register(
    +                watchService,
    +                new WatchEvent.Kind<?>[]{
    +                        StandardWatchEventKinds.ENTRY_CREATE,
    +                        StandardWatchEventKinds.ENTRY_DELETE,
    +                        StandardWatchEventKinds.ENTRY_MODIFY,
    +                        StandardWatchEventKinds.OVERFLOW},
    +                SensitivityWatchEventModifier.HIGH);
    +        this.watcherThread = new WatcherThread(watchService, callback);
    +        this.watcherThread.setDaemon(true);
    +        this.watcherThread.start();
    +    }
    +
    +    /**
    +     * Waits for the background thread to enter the main loop before returning. This
method exists mostly to make
    +     * the unit tests simpler, which is why it is package private.
    +     *
    +     * @throws InterruptedException if this thread is interrupted while waiting for the
background thread to start.
    +     */
    +    void waitForBackgroundThreadToStart() throws InterruptedException {
    --- End diff --
    
    not able to mock or manage the file watcher generally smell some design improvement.The
same logic implemented in test case if we expose the states of file watcher, locking also
not required if we expose the state of  file watcher.It is good to avoid adding code for testing
purpose


---

Mime
View raw message