Skip to content

Commit 0c0c3df

Browse files
committed
Fix build due to phasing off SecurityManager usage in favor of Java Agent
Signed-off-by: Andriy Redko <[email protected]>
1 parent 5e988e1 commit 0c0c3df

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

build.gradle

+25
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ buildscript {
8383
//****************************************************************************/
8484

8585
plugins {
86+
id 'eclipse'
8687
id 'java-library'
8788
id 'java-test-fixtures'
8889
id 'idea'
@@ -244,6 +245,29 @@ allprojects {
244245
project.noticeFile = rootProject.file('NOTICE.txt')
245246
project.forbiddenApis.ignoreFailures = true
246247
}
248+
249+
configurations {
250+
agent
251+
}
252+
253+
dependencies {
254+
}
255+
256+
task prepareAgent(type: Copy) {
257+
from(configurations.agent)
258+
into "$buildDir/agent"
259+
}
260+
261+
dependencies {
262+
agent "org.opensearch:opensearch-agent-bootstrap:${opensearch_version}"
263+
agent "org.opensearch:opensearch-agent:${opensearch_version}"
264+
agent "net.bytebuddy:byte-buddy:${versions.bytebuddy}"
265+
}
266+
267+
tasks.withType(Test) {
268+
dependsOn prepareAgent
269+
jvmArgs += ["-javaagent:" + project.layout.buildDirectory.file("agent/opensearch-agent-${opensearch_version}.jar").get()]
270+
}
247271
}
248272

249273
configurations {
@@ -610,3 +634,4 @@ task updateVersion {
610634
}
611635
}
612636
}
637+

src/main/java/org/opensearch/knn/jni/PlatformUtils.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.apache.commons.lang.StringUtils;
1616
import org.apache.logging.log4j.LogManager;
1717
import org.apache.logging.log4j.Logger;
18+
1819
import oshi.util.platform.mac.SysctlUtil;
1920

2021
import java.nio.file.Files;
@@ -27,7 +28,8 @@
2728
import java.util.stream.Stream;
2829

2930
public class PlatformUtils {
30-
31+
private static volatile Boolean isAVX2Supported;
32+
private static volatile Boolean isAVX512Supported;
3133
private static final Logger logger = LogManager.getLogger(PlatformUtils.class);
3234

3335
/**
@@ -41,22 +43,26 @@ public class PlatformUtils {
4143
*/
4244
public static boolean isAVX2SupportedBySystem() {
4345
if (!Platform.isIntel() || Platform.isWindows()) {
44-
return false;
46+
isAVX2Supported = false;
4547
}
4648

47-
if (Platform.isMac()) {
49+
if (isAVX2Supported != null) {
50+
return isAVX2Supported;
51+
}
4852

53+
if (Platform.isMac()) {
4954
// sysctl or system control retrieves system info and allows processes with appropriate privileges
5055
// to set system info. This system info contains the machine dependent cpu features that are supported by it.
5156
// On MacOS, if the underlying processor supports AVX2 instruction set, it will be listed under the "leaf7"
5257
// subset of instructions ("sysctl -a | grep machdep.cpu.leaf7_features").
5358
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sysctl.3.html
5459
try {
55-
return AccessController.doPrivileged((PrivilegedExceptionAction<Boolean>) () -> {
60+
isAVX2Supported = AccessController.doPrivileged((PrivilegedExceptionAction<Boolean>) () -> {
5661
String flags = SysctlUtil.sysctl("machdep.cpu.leaf7_features", "empty");
5762
return (flags.toLowerCase(Locale.ROOT)).contains("avx2");
5863
});
5964
} catch (Exception e) {
65+
isAVX2Supported = false;
6066
logger.error("[KNN] Error fetching cpu flags info. [{}]", e.getMessage(), e);
6167
}
6268

@@ -70,17 +76,18 @@ public static boolean isAVX2SupportedBySystem() {
7076
// https://ark.intel.com/content/www/us/en/ark/products/199285/intel-pentium-gold-g6600-processor-4m-cache-4-20-ghz.html
7177
String fileName = "/proc/cpuinfo";
7278
try {
73-
return AccessController.doPrivileged(
79+
isAVX2Supported = AccessController.doPrivileged(
7480
(PrivilegedExceptionAction<Boolean>) () -> (Boolean) Files.lines(Paths.get(fileName))
7581
.filter(s -> s.startsWith("flags"))
7682
.anyMatch(s -> StringUtils.containsIgnoreCase(s, "avx2"))
7783
);
7884

7985
} catch (Exception e) {
86+
isAVX2Supported = false;
8087
logger.error("[KNN] Error reading file [{}]. [{}]", fileName, e.getMessage(), e);
8188
}
8289
}
83-
return false;
90+
return isAVX2Supported;
8491
}
8592

8693
public static boolean isAVX512SupportedBySystem() {
@@ -97,7 +104,11 @@ private static boolean areAVX512FlagsAvailable(String[] avx512) {
97104
// https://github.com/facebookresearch/faiss/blob/main/faiss/CMakeLists.txt
98105

99106
if (!Platform.isIntel() || Platform.isMac() || Platform.isWindows()) {
100-
return false;
107+
isAVX512Supported = false;
108+
}
109+
110+
if (isAVX512Supported != null) {
111+
return isAVX512Supported;
101112
}
102113

103114
if (Platform.isLinux()) {
@@ -110,16 +121,16 @@ private static boolean areAVX512FlagsAvailable(String[] avx512) {
110121
String fileName = "/proc/cpuinfo";
111122

112123
try {
113-
return AccessController.doPrivileged((PrivilegedExceptionAction<Boolean>) () -> {
124+
isAVX512Supported = AccessController.doPrivileged((PrivilegedExceptionAction<Boolean>) () -> {
114125
Stream<String> linestream = Files.lines(Paths.get(fileName));
115126
String flags = linestream.filter(line -> line.startsWith("flags")).findFirst().orElse("");
116127
return Arrays.stream(avx512).allMatch(flags::contains);
117128
});
118-
119129
} catch (PrivilegedActionException e) {
130+
isAVX512Supported = false;
120131
logger.error("[KNN] Error reading file [{}]. [{}]", fileName, e.getMessage(), e);
121132
}
122133
}
123-
return false;
134+
return isAVX512Supported;
124135
}
125136
}

src/main/java/org/opensearch/knn/plugin/KNNPlugin.java

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.opensearch.knn.indices.ModelCache;
4848
import org.opensearch.knn.indices.ModelDao;
4949
import org.opensearch.knn.indices.ModelGraveyard;
50+
import org.opensearch.knn.jni.PlatformUtils;
5051
import org.opensearch.knn.plugin.rest.RestClearCacheHandler;
5152
import org.opensearch.knn.plugin.rest.RestDeleteModelHandler;
5253
import org.opensearch.knn.plugin.rest.RestGetModelHandler;
@@ -174,6 +175,11 @@ public class KNNPlugin extends Plugin
174175
private ClusterService clusterService;
175176
private Supplier<RepositoriesService> repositoriesServiceSupplier;
176177

178+
static {
179+
PlatformUtils.isAVX2SupportedBySystem();
180+
PlatformUtils.isAVX512SPRSupportedBySystem();
181+
}
182+
177183
@Override
178184
public Map<String, Mapper.TypeParser> getMappers() {
179185
return Collections.singletonMap(

src/main/plugin-metadata/plugin-security.policy

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ grant {
99
permission java.lang.RuntimePermission "accessDeclaredMembers";
1010
permission java.io.FilePermission "/proc/cpuinfo", "read";
1111
};
12+
13+
grant codeBase "${codebase.opensearch-cli}" {
14+
permission java.io.FilePermission "/proc/cpuinfo", "read";
15+
};
16+
17+
grant codeBase "${codebase.opensearch}" {
18+
permission java.io.FilePermission "/proc/cpuinfo", "read";
19+
};

0 commit comments

Comments
 (0)