Java Code Examples for org.apache.flink.util.OperatingSystem#isWindows()

The following examples show how to use org.apache.flink.util.OperatingSystem#isWindows() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnvironmentInformation.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to retrieve the maximum number of open file handles. This method will only work on
 * UNIX-based operating systems with Sun/Oracle Java versions.
 * 
 * <p>If the number of max open file handles cannot be determined, this method returns {@code -1}.</p>
 * 
 * @return The limit of open file handles, or {@code -1}, if the limit could not be determined.
 */
public static long getOpenFileHandlesLimit() {
	if (OperatingSystem.isWindows()) { // getMaxFileDescriptorCount method is not available on Windows
		return -1L;
	}
	Class<?> sunBeanClass;
	try {
		sunBeanClass = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
	}
	catch (ClassNotFoundException e) {
		return -1L;
	}
	
	try {
		Method fhLimitMethod = sunBeanClass.getMethod("getMaxFileDescriptorCount");
		Object result = fhLimitMethod.invoke(ManagementFactory.getOperatingSystemMXBean());
		return (Long) result;
	}
	catch (Throwable t) {
		LOG.warn("Unexpected error when accessing file handle limit", t);
		return -1L;
	}
}
 
Example 2
Source File: EnvironmentInformation.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to retrieve the maximum number of open file handles. This method will only work on
 * UNIX-based operating systems with Sun/Oracle Java versions.
 * 
 * <p>If the number of max open file handles cannot be determined, this method returns {@code -1}.</p>
 * 
 * @return The limit of open file handles, or {@code -1}, if the limit could not be determined.
 */
public static long getOpenFileHandlesLimit() {
	if (OperatingSystem.isWindows()) { // getMaxFileDescriptorCount method is not available on Windows
		return -1L;
	}
	Class<?> sunBeanClass;
	try {
		sunBeanClass = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
	}
	catch (ClassNotFoundException e) {
		return -1L;
	}
	
	try {
		Method fhLimitMethod = sunBeanClass.getMethod("getMaxFileDescriptorCount");
		Object result = fhLimitMethod.invoke(ManagementFactory.getOperatingSystemMXBean());
		return (Long) result;
	}
	catch (Throwable t) {
		LOG.warn("Unexpected error when accessing file handle limit", t);
		return -1L;
	}
}
 
Example 3
Source File: PythonEnvironmentManagerUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
public static String getPythonUdfRunnerScript(
		String pythonExecutable,
		Map<String, String> environmentVariables) throws IOException {
	String runnerDir;
	if (environmentVariables.containsKey(PYFLINK_UDF_RUNNER_DIR)) {
		runnerDir = environmentVariables.get(PYFLINK_UDF_RUNNER_DIR);
	} else {
		String[] commands = new String[] { pythonExecutable, "-c", GET_RUNNER_DIR_SCRIPT};
		String out = execute(commands, environmentVariables, false);
		runnerDir = out.trim();
	}
	String runnerScriptPath;
	if (OperatingSystem.isWindows()) {
		runnerScriptPath = String.join(File.separator, runnerDir, PYFLINK_UDF_RUNNER_BAT);
	} else {
		runnerScriptPath = String.join(File.separator, runnerDir, PYFLINK_UDF_RUNNER_SH);
	}
	return runnerScriptPath;
}
 
Example 4
Source File: PythonEnvUtilsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetPythonExecutable() throws IOException {
	Configuration config = new Configuration();

	PythonEnvUtils.PythonEnvironment env = preparePythonEnvironment(config, null, tmpDirPath);
	if (OperatingSystem.isWindows()) {
		Assert.assertEquals("python.exe", env.pythonExec);
	} else {
		Assert.assertEquals("python", env.pythonExec);
	}

	Map<String, String> systemEnv = new HashMap<>(System.getenv());
	systemEnv.put(PYFLINK_CLIENT_EXECUTABLE, "python3");
	CommonTestUtils.setEnv(systemEnv);
	try {
		env = preparePythonEnvironment(config, null, tmpDirPath);
		Assert.assertEquals("python3", env.pythonExec);
	} finally {
		systemEnv.remove(PYFLINK_CLIENT_EXECUTABLE);
		CommonTestUtils.setEnv(systemEnv);
	}

	config.set(PYTHON_CLIENT_EXECUTABLE, "/usr/bin/python");
	env = preparePythonEnvironment(config, null, tmpDirPath);
	Assert.assertEquals("/usr/bin/python", env.pythonExec);
}
 
Example 5
Source File: EnvironmentInformation.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to retrieve the maximum number of open file handles. This method will only work on
 * UNIX-based operating systems with Sun/Oracle Java versions.
 * 
 * <p>If the number of max open file handles cannot be determined, this method returns {@code -1}.</p>
 * 
 * @return The limit of open file handles, or {@code -1}, if the limit could not be determined.
 */
public static long getOpenFileHandlesLimit() {
	if (OperatingSystem.isWindows()) { // getMaxFileDescriptorCount method is not available on Windows
		return -1L;
	}
	Class<?> sunBeanClass;
	try {
		sunBeanClass = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
	}
	catch (ClassNotFoundException e) {
		return -1L;
	}
	
	try {
		Method fhLimitMethod = sunBeanClass.getMethod("getMaxFileDescriptorCount");
		Object result = fhLimitMethod.invoke(ManagementFactory.getOperatingSystemMXBean());
		return (Long) result;
	}
	catch (Throwable t) {
		LOG.warn("Unexpected error when accessing file handle limit", t);
		return -1L;
	}
}
 
Example 6
Source File: BootstrapToolsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDynamicPropertiesAsString() {
	final Configuration baseConfig = new Configuration();
	baseConfig.setString("key.a", "a");
	baseConfig.setString("key.b", "b1");

	final Configuration targetConfig = new Configuration();
	targetConfig.setString("key.b", "b2");
	targetConfig.setString("key.c", "c");

	final String dynamicProperties = BootstrapTools.getDynamicPropertiesAsString(baseConfig, targetConfig);
	if (OperatingSystem.isWindows()) {
		assertEquals("-Dkey.b=\"b2\" -Dkey.c=\"c\"", dynamicProperties);
	} else {
		assertEquals("-Dkey.b='b2' -Dkey.c='c'", dynamicProperties);
	}
}
 
Example 7
Source File: SignalHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Register some signal handlers.
 *
 * @param LOG The slf4j logger
 */
public static void register(final Logger LOG) {
	synchronized (SignalHandler.class) {
		if (registered) {
			return;
		}
		registered = true;

		final String[] SIGNALS = OperatingSystem.isWindows()
			? new String[]{ "TERM", "INT"}
			: new String[]{ "TERM", "HUP", "INT" };
		
		StringBuilder bld = new StringBuilder();
		bld.append("Registered UNIX signal handlers for [");
		
		String separator = "";
		for (String signalName : SIGNALS) {
			try {
				new Handler(signalName, LOG);
				bld.append(separator);
				bld.append(signalName);
				separator = ", ";
			} catch (Exception e) {
				LOG.info("Error while registering signal handler", e);
			}
		}
		bld.append("]");
		LOG.info(bld.toString());
	}
}
 
Example 8
Source File: SignalHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Register some signal handlers.
 *
 * @param LOG The slf4j logger
 */
public static void register(final Logger LOG) {
	synchronized (SignalHandler.class) {
		if (registered) {
			return;
		}
		registered = true;

		final String[] SIGNALS = OperatingSystem.isWindows()
			? new String[]{ "TERM", "INT"}
			: new String[]{ "TERM", "HUP", "INT" };
		
		StringBuilder bld = new StringBuilder();
		bld.append("Registered UNIX signal handlers for [");
		
		String separator = "";
		for (String signalName : SIGNALS) {
			try {
				new Handler(signalName, LOG);
				bld.append(separator);
				bld.append(signalName);
				separator = ", ";
			} catch (Exception e) {
				LOG.info("Error while registering signal handler", e);
			}
		}
		bld.append("]");
		LOG.info(bld.toString());
	}
}
 
Example 9
Source File: RocksDBOperationUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static void throwExceptionIfPathLengthExceededOnWindows(String path, Exception cause) throws IOException {
	// max directory path length on Windows is 247.
	// the maximum path length is 260, subtracting one file name length (12 chars) and one NULL terminator.
	final int maxWinDirPathLen = 247;

	if (path.length() > maxWinDirPathLen && OperatingSystem.isWindows()) {
		throw new IOException(String.format(
			"The directory path length (%d) is longer than the directory path length limit for Windows (%d): %s",
			path.length(), maxWinDirPathLen, path), cause);
	}
}
 
Example 10
Source File: SignalHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Register some signal handlers.
 *
 * @param LOG The slf4j logger
 */
public static void register(final Logger LOG) {
	synchronized (SignalHandler.class) {
		if (registered) {
			return;
		}
		registered = true;

		final String[] SIGNALS = OperatingSystem.isWindows()
			? new String[]{ "TERM", "INT"}
			: new String[]{ "TERM", "HUP", "INT" };
		
		StringBuilder bld = new StringBuilder();
		bld.append("Registered UNIX signal handlers for [");
		
		String separator = "";
		for (String signalName : SIGNALS) {
			try {
				new Handler(signalName, LOG);
				bld.append(separator);
				bld.append(signalName);
				separator = ", ";
			} catch (Exception e) {
				LOG.info("Error while registering signal handler", e);
			}
		}
		bld.append("]");
		LOG.info(bld.toString());
	}
}
 
Example 11
Source File: ShellScript.java    From flink with Apache License 2.0 4 votes vote down vote up
public static ShellScriptBuilder createShellScriptBuilder() {
	if (OperatingSystem.isWindows()) {
		return new WindowsShellScriptBuilder();
	}
	return new UnixShellScriptBuilder();
}
 
Example 12
Source File: ShellScript.java    From flink with Apache License 2.0 4 votes vote down vote up
public static String getScriptExtension() {
	return OperatingSystem.isWindows() ? ".cmd" : ".sh";
}
 
Example 13
Source File: TaskManagerLoadingDynamicPropertiesITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
private String getJavaCommandWithOS() {
	if (OperatingSystem.isWindows()) {
		return "\"" + getJavaCommandPath() + "\"";
	}
	return getJavaCommandPath();
}
 
Example 14
Source File: TaskManagerLoadingDynamicPropertiesITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
private String getInternalClassNameWithOS(String className) {
	if (!OperatingSystem.isWindows()) {
		return className.replace("$", "'$'");
	}
	return className;
}
 
Example 15
Source File: BootstrapTools.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Escape all the dynamic property values.
 * For Unix-like OS(Linux, MacOS, FREE_BSD, etc.), each value will be surrounded with single quotes. This works for
 * all chars except single quote itself. To escape the single quote, close the quoting before it, insert the escaped
 * single quote, and then re-open the quoting. For example, the value is foo'bar and the escaped value is
 * 'foo'\''bar'. See <a href="https://stackoverflow.com/questions/15783701/which-characters-need-to-be-escaped-when-using-bash">https://stackoverflow.com/questions/15783701/which-characters-need-to-be-escaped-when-using-bash</a>
 * for more information about Unix escaping.
 *
 * <p>For Windows OS, each value will be surrounded with double quotes. The double quote itself needs to be escaped with
 * back slash. Also the caret symbol need to be escaped with double carets since Windows uses it to escape characters.
 * See <a href="https://en.wikibooks.org/wiki/Windows_Batch_Scripting">https://en.wikibooks.org/wiki/Windows_Batch_Scripting</a>
 * for more information about Windows escaping.
 *
 * @param value value to be escaped
 * @return escaped value
 */
public static String escapeForDifferentOS(String value) {
	if (OperatingSystem.isWindows()) {
		return escapeWithDoubleQuote(value);
	} else {
		return escapeWithSingleQuote(value);
	}
}