-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix java test build * import sys for hybird stream test python * new a jni extractor jni for libstreaming java so After this jni util imported, we still fail to run hybird stream test. From test output logs, boost::asio::detail::epoll_reactor::interrupt crash thrown outside, which shall not show that rootcasue and problem details * append jna maven deps for jni util class * remove duplicated sys imported * lease hybird python stream test
- Loading branch information
Showing
9 changed files
with
180 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ng/java/streaming-runtime/src/main/java/io/ray/streaming/runtime/util/BinaryFileUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.ray.streaming.runtime.util; | ||
|
||
import com.google.common.base.Preconditions; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.RandomAccessFile; | ||
import java.nio.channels.FileLock; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.SystemUtils; | ||
|
||
public class BinaryFileUtil { | ||
|
||
public static final String STREAMING_JAVA_LIBRARY = "streaming_java"; | ||
|
||
/** | ||
* Extract a platform-native resource file to <code>destDir</code>. Note that this a process-safe | ||
* operation. If multi processes extract the file to same directory concurrently, this operation | ||
* will be protected by a file lock. | ||
* | ||
* @param destDir a directory to extract resource file to | ||
* @param fileName resource file name | ||
* @return extracted resource file | ||
*/ | ||
public static File getNativeFile(String destDir, String fileName) { | ||
final File dir = new File(destDir); | ||
if (!dir.exists()) { | ||
try { | ||
FileUtils.forceMkdir(dir); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Couldn't make directory: " + dir.getAbsolutePath(), e); | ||
} | ||
} | ||
String lockFilePath = destDir + File.separator + "file_lock"; | ||
try (FileLock ignored = new RandomAccessFile(lockFilePath, "rw").getChannel().lock()) { | ||
String resourceDir; | ||
if (SystemUtils.IS_OS_MAC) { | ||
resourceDir = "native/darwin/"; | ||
} else if (SystemUtils.IS_OS_LINUX) { | ||
resourceDir = "native/linux/"; | ||
} else { | ||
throw new UnsupportedOperationException("Unsupported os " + SystemUtils.OS_NAME); | ||
} | ||
/// File doesn't exist. Create a temp file and then rename it. | ||
final String tempFilePath = String.format("%s/%s.tmp", destDir, fileName); | ||
// Adding a temporary file here is used to fix the issue that when | ||
// a java worker crashes during extracting dynamic library file, next | ||
// java worker will use an incomplete file. The issue link is: | ||
// | ||
// https://github.com/ray-project/ray/issues/19341 | ||
File tempFile = new File(tempFilePath); | ||
|
||
String resourcePath = resourceDir + fileName; | ||
File destFile = new File(String.format("%s/%s", destDir, fileName)); | ||
if (destFile.exists()) { | ||
return destFile; | ||
} | ||
|
||
// File does not exist. | ||
try (InputStream is = BinaryFileUtil.class.getResourceAsStream("/" + resourcePath)) { | ||
Preconditions.checkNotNull(is, "{} doesn't exist.", resourcePath); | ||
Files.copy(is, Paths.get(tempFile.getCanonicalPath()), StandardCopyOption.REPLACE_EXISTING); | ||
if (!tempFile.renameTo(destFile)) { | ||
throw new RuntimeException( | ||
String.format( | ||
"Couldn't rename temp file(%s) to %s", | ||
tempFile.getAbsolutePath(), destFile.getAbsolutePath())); | ||
} | ||
return destFile; | ||
} catch (IOException e) { | ||
throw new RuntimeException("Couldn't get temp file from resource " + resourcePath, e); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
streaming/java/streaming-runtime/src/main/java/io/ray/streaming/runtime/util/JniUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package io.ray.streaming.runtime.util; | ||
|
||
import com.google.common.collect.Sets; | ||
import com.sun.jna.NativeLibrary; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Set; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class JniUtils { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(JniUtils.class); | ||
private static Set<String> loadedLibs = Sets.newHashSet(); | ||
private static String defaultDestDir; | ||
|
||
/** | ||
* Loads the native library specified by the <code>libraryName</code> argument. The <code> | ||
* libraryName</code> argument must not contain any platform specific prefix, file extension or | ||
* path. | ||
* | ||
* @param libraryName the name of the library. | ||
*/ | ||
public static synchronized void loadLibrary(String libraryName) { | ||
loadLibrary(getDefaultDestDir(), libraryName); | ||
} | ||
|
||
/** | ||
* Loads the native library specified by the <code>libraryName</code> argument. The <code> | ||
* libraryName</code> argument must not contain any platform specific prefix, file extension or | ||
* path. | ||
* | ||
* @param libraryName the name of the library. | ||
* @param exportSymbols export symbols of library so that it can be used by other libs. | ||
*/ | ||
public static synchronized void loadLibrary(String libraryName, boolean exportSymbols) { | ||
loadLibrary(getDefaultDestDir(), libraryName, exportSymbols); | ||
} | ||
|
||
/** | ||
* Loads the native library specified by the <code>libraryName</code> argument. The <code> | ||
* libraryName</code> argument must not contain any platform specific prefix, file extension or | ||
* path. | ||
* | ||
* @param destDir The destination dir the library to be extracted. | ||
* @param libraryName the name of the library. | ||
*/ | ||
public static synchronized void loadLibrary(String destDir, String libraryName) { | ||
loadLibrary(destDir, libraryName, false); | ||
} | ||
|
||
/** | ||
* Loads the native library specified by the <code>libraryName</code> argument. The <code> | ||
* libraryName</code> argument must not contain any platform specific prefix, file extension or | ||
* path. | ||
* | ||
* @param destDir The destination dir the library to be extracted. | ||
* @param libraryName the name of the library. | ||
* @param exportSymbols export symbols of library so that it can be used by other libs. | ||
*/ | ||
public static synchronized void loadLibrary( | ||
String destDir, String libraryName, boolean exportSymbols) { | ||
if (!loadedLibs.contains(libraryName)) { | ||
LOGGER.debug("Loading native library {} in {}.", libraryName, destDir); | ||
// Load native library. | ||
String fileName = System.mapLibraryName(libraryName); | ||
final File file = BinaryFileUtil.getNativeFile(destDir, fileName); | ||
|
||
if (exportSymbols) { | ||
// Expose library symbols using RTLD_GLOBAL which may be depended by other shared | ||
// libraries. | ||
NativeLibrary.getInstance(file.getAbsolutePath()); | ||
} | ||
System.load(file.getAbsolutePath()); | ||
LOGGER.debug("Native library loaded."); | ||
loadedLibs.add(libraryName); | ||
} | ||
} | ||
|
||
/** Cache the result so that multiple calls return the same dest dir. */ | ||
private static synchronized String getDefaultDestDir() { | ||
if (defaultDestDir == null) { | ||
try { | ||
defaultDestDir = Files.createTempDirectory("native_libs").toString(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
return defaultDestDir; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters