You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/06/29 02:36:35 UTC

[GitHub] [flink] fsk119 commented on a diff in pull request #16108: [FLINK-22821][core] Stabilize NetUtils#getAvailablePort in order to avoid wrongly allocating any used ports

fsk119 commented on code in PR #16108:
URL: https://github.com/apache/flink/pull/16108#discussion_r909145606


##########
flink-core/src/main/java/org/apache/flink/util/FileLock.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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 org.apache.flink.annotation.Internal;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/** A file lock used for avoiding race condition among multiple threads/processes. */
+@Internal
+public class FileLock {
+    private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
+    private final File file;
+    private FileOutputStream outputStream;
+    private java.nio.channels.FileLock lock;
+
+    /**
+     * Initialize a FileLock using a file located at fullPath.
+     *
+     * @param fullPath The path of the locking file
+     */
+    public FileLock(String fullPath) {
+        Preconditions.checkNotNull(fullPath, "fullPath should not be null");
+        Path path = Paths.get(fullPath);
+        String normalizedFileName = normalizeFileName(path.getFileName().toString());
+        if (normalizedFileName.isEmpty()) {
+            throw new IllegalArgumentException("There are no legal characters in the file name");
+        }
+        this.file =
+                path.getParent() == null
+                        ? new File(normalizedFileName)

Review Comment:
   Currently when the path.getParent() is null, the path of the File is a relative path. It means the FileLock will write a file into the current user directory according to the java doc of the `java.io.File`. If I use the `NetUtils#getAvaliablePort` in the test, the file will be created in the Flink source dir. For example, I add a test in the flink-connector-hive, the path of the `FileLock` is `/Users/shengkai/Work/flink/flink-connector-hive/orgapacheflinkutilNetUtils60481` . The idea will automatically add the file into the change list, which is very annoying. 
   
   So I think think why we don't create the file in the `TEMP_DIR` if the parent is null? I think it has two benefits:
   1. It's much clean for the test that uses the `NetUtils#getAvaliablePort`. 
   2. It's better for different modules to check the port conflicts. For example, module A and module B run tests in parallel and both wants to use the same port. Currently the two module will create the file under their directoy rather than in the same directory.
   
   Any opinion are welcome. 
   



-- 
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-unsubscribe@flink.apache.org

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