GJL commented on a change in pull request #9060: [FLINK-13145][tests] Run HA dataset E2E test with new RestartPipelinedRegionStrategy URL: https://github.com/apache/flink/pull/9060#discussion_r305113537
########## File path: flink-end-to-end-tests/flink-dataset-fine-grained-recovery-test/src/main/java/org/apache/flink/batch/tests/util/FileBasedOneShotLatch.java ########## @@ -0,0 +1,125 @@ +/* + * 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.flink.batch.tests.util; + +import com.sun.nio.file.SensitivityWatchEventModifier; + +import javax.annotation.concurrent.NotThreadSafe; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.file.Files; +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 static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * A synchronization aid that allows a single thread to wait on the creation of a specified file. + */ +@NotThreadSafe +public class FileBasedOneShotLatch implements Closeable { + + private final Path latchFile; + + private final WatchService watchService; + + private boolean released; + + public FileBasedOneShotLatch(final Path latchFile) { + this.latchFile = checkNotNull(latchFile); + + final Path parentDir = checkNotNull(latchFile.getParent(), "latchFile must have a parent"); + this.watchService = initWatchService(parentDir); + } + + private static WatchService initWatchService(final Path parentDir) { + final WatchService watchService = createWatchService(parentDir); + watchForLatchFile(watchService, parentDir); + return watchService; + } + + private static WatchService createWatchService(final Path parentDir) { + try { + return parentDir.getFileSystem().newWatchService(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static void watchForLatchFile(final WatchService watchService, final Path parentDir) { + try { + parentDir.register( + watchService, + new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_CREATE}, + SensitivityWatchEventModifier.HIGH); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Waits until the latch file is created. + * + * @throws InterruptedException if interrupted while waiting + */ + public void await() throws InterruptedException { + if (isReleasedOrReleasable()) { + return; + } + + awaitLatchFile(watchService); + } + + private void awaitLatchFile(final WatchService watchService) throws InterruptedException { + while (true) { + WatchKey take = watchService.take(); Review comment: done ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services