forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support reading system configs from native in Java. (ray-project#17703)
* Support reading system configs from native in Java. * Fix lint * Lint cpp * Fix Java cases. * Address comments. * Address comments.
- Loading branch information
1 parent
623db7c
commit 6d6a1ea
Showing
7 changed files
with
149 additions
and
6 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
27 changes: 27 additions & 0 deletions
27
java/runtime/src/main/java/io/ray/runtime/util/SystemConfig.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,27 @@ | ||
package io.ray.runtime.util; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.gson.Gson; | ||
import java.util.HashMap; | ||
|
||
/** The utility class to read system config from native code. */ | ||
public class SystemConfig { | ||
|
||
private static Gson gson = new Gson(); | ||
|
||
/// A cache to avoid duplicated reading via JNI. | ||
private static HashMap<String, Object> cachedConfigs = new HashMap<>(); | ||
|
||
public static synchronized Object get(String key) { | ||
if (cachedConfigs.containsKey(key)) { | ||
return cachedConfigs.get(key); | ||
} | ||
|
||
Object val = gson.fromJson(nativeGetSystemConfig(key), Object.class); | ||
Preconditions.checkNotNull(val); | ||
cachedConfigs.put(key, val); | ||
return val; | ||
} | ||
|
||
private static native String nativeGetSystemConfig(String key); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.ray.test; | ||
|
||
import io.ray.runtime.util.SystemConfig; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
@Test | ||
public class SystemConfigTest extends BaseTest { | ||
|
||
public void testDefaultConfigs() { | ||
long ret = ((Double) SystemConfig.get("max_direct_call_object_size")).longValue(); | ||
Assert.assertEquals(ret, 100 * 1024); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/ray/core_worker/lib/java/io_ray_runtime_util_SystemConfig.cc
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,41 @@ | ||
// Copyright 2017 The Ray Authors. | ||
// | ||
// Licensed 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. | ||
#include "io_ray_runtime_util_SystemConfig.h" | ||
|
||
#include <jni.h> | ||
|
||
#include "jni_utils.h" | ||
#include "nlohmann/json.hpp" | ||
#include "ray/util/logging.h" | ||
|
||
using json = nlohmann::json; | ||
|
||
JNIEXPORT jstring JNICALL Java_io_ray_runtime_util_SystemConfig_nativeGetSystemConfig( | ||
JNIEnv *env, jclass clz, jstring java_key) { | ||
RAY_CHECK(java_key != nullptr); | ||
const auto key = JavaStringToNativeString(env, java_key); | ||
|
||
/// -----------Include ray_config_def.h to set config items.------------------- | ||
#define RAY_CONFIG(type, name, default_value) \ | ||
if (key == #name) { \ | ||
json j = RayConfig::instance().name(); \ | ||
return env->NewStringUTF(j.dump().c_str()); \ | ||
} | ||
|
||
#include "ray/common/ray_config_def.h" | ||
/// --------------------------------------------------------------------- | ||
#undef RAY_CONFIG | ||
RAY_LOG(FATAL) << "Unsupported system config: " << key; | ||
return nullptr; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/ray/core_worker/lib/java/io_ray_runtime_util_SystemConfig.h
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,37 @@ | ||
// Copyright 2017 The Ray Authors. | ||
// | ||
// Licensed 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. | ||
|
||
/* DO NOT EDIT THIS FILE - it is machine generated */ | ||
|
||
#include <jni.h> | ||
|
||
/* Header for class io_ray_runtime_util_SystemConfig */ | ||
#ifndef _Included_io_ray_runtime_util_SystemConfig | ||
#define _Included_io_ray_runtime_util_SystemConfig | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/* | ||
* Class: io_ray_runtime_util_SystemConfig | ||
* Method: nativeGetSystemConfig | ||
* Signature: (Ljava/lang/String;)Ljava/lang/String; | ||
*/ | ||
JNIEXPORT jstring JNICALL | ||
Java_io_ray_runtime_util_SystemConfig_nativeGetSystemConfig(JNIEnv *, jclass, jstring); | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif |