paul8263 commented on a change in pull request #16108:
URL: https://github.com/apache/flink/pull/16108#discussion_r764562387



##########
File path: flink-core/src/main/java/org/apache/flink/util/FileLock.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.util;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.channels.OverlappingFileLockException;
+import java.nio.file.Paths;
+
+/** A file lock used for avoiding race condition among multiple unit test 
processes */
+public class FileLock {
+    private static final long TRY_LOCK_INTERVAL = 50;
+    private final File file;
+    private FileOutputStream outputStream;
+    private java.nio.channels.FileLock lock;
+
+    public FileLock(String fileName) {
+        this.file = new File(fileName);
+    }
+
+    private void init() throws IOException {
+        String filePath =
+                Paths.get(System.getProperty("java.io.tmpdir"), 
file.getName()).toString();
+        File lockFile = new File(filePath);
+        if (!lockFile.exists()) {
+            lockFile.createNewFile();
+        }
+
+        outputStream = new FileOutputStream(lockFile);
+    }

Review comment:
       Hi @AHeise ,
   `IOException` might not be thrown if the file already exists. We would get 
this exception if the parent directory of the file has not been created.
   Sorry that I messed up the code while I was testing the snipped somewhere 
else. There is no need to create another `lockFile`.
   I reconsidered the `tryLock` issue. I think the lock releasing/destroying 
should be outside `tryLock` and explicitly called even if failed to acquire the 
lock.
   Correct me if I am wrong.




-- 
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]


Reply via email to