Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import org.apache.commons.lang3.SystemUtils;
import org.apache.tsfile.utils.Pair;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -98,15 +99,71 @@ public static int[] searchAvailablePorts() {
}

private static boolean checkPortsAvailable(final List<Integer> ports) {
final String cmd = getSearchAvailablePortCmd(ports);
try {
return Runtime.getRuntime().exec(cmd).waitFor() == 1;
} catch (final IOException ignore) {
// ignore
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
return listPortOccupation(ports).isEmpty();
} catch (IOException e) {
IoTDBTestLogger.logger.error("Cannot check available ports", e);
return false;
}
return false;
}

public static Map<Integer, Long> listPortOccupation(final List<Integer> ports)
throws IOException {
return SystemUtils.IS_OS_WINDOWS
? listPortOccupationWindows(ports)
: listPortOccupationUnix(ports);
}

public static Map<Integer, Long> listPortOccupation(
final List<Integer> ports,
String cmd,
int targetColumnLength,
int addressColumnIndex,
int pidColumnIndex)
throws IOException {
Process process = Runtime.getRuntime().exec(cmd);
Map<Integer, Long> result = new HashMap<>();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
String[] split = line.trim().split("\\s+");
if (split.length != targetColumnLength) {
continue;
}
String localAddress = split[addressColumnIndex];
for (Integer port : ports) {
if (localAddress.endsWith(":" + port)) {
result.put(port, Long.parseLong(split[pidColumnIndex]));
break;
}
}
}
} catch (EOFException ignored) {
}
return result;
}

/**
* List occupied port and the associated pid on windows.
*
* @param ports ports to be checked
* @return (occupiedPort, pid) pairs
*/
public static Map<Integer, Long> listPortOccupationWindows(final List<Integer> ports)
throws IOException {
return listPortOccupation(ports, "netstat -aon -p tcp", 5, 1, 4);
}

/**
* List occupied port and the associated pid on Unix.
*
* @param ports ports to be checked
* @return (occupiedPort, pid) pairs
*/
public static Map<Integer, Long> listPortOccupationUnix(final List<Integer> ports)
throws IOException {
return listPortOccupation(ports, "lsof -iTCP -sTCP:LISTEN -P -n", 10, 9, 1);
}

private static String getSearchAvailablePortCmd(final List<Integer> ports) {
Expand All @@ -115,7 +172,7 @@ private static String getSearchAvailablePortCmd(final List<Integer> ports) {

private static String getWindowsSearchPortCmd(final List<Integer> ports) {
return "netstat -aon -p tcp | findStr "
+ ports.stream().map(v -> "/C:'127.0.0.1:" + v + "'").collect(Collectors.joining(" "));
+ ports.stream().map(v -> "/C:\"127.0.0.1:" + v + "\"").collect(Collectors.joining(" "));
}

private static String getUnixSearchPortCmd(final List<Integer> ports) {
Expand Down
Loading
Loading