-
Notifications
You must be signed in to change notification settings - Fork 125
/
KNNSettingsTests.java
111 lines (98 loc) · 4.73 KB
/
KNNSettingsTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.knn.index;
import lombok.SneakyThrows;
import org.opensearch.cluster.ClusterName;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.network.NetworkModule;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.env.Environment;
import org.opensearch.knn.KNNTestCase;
import org.opensearch.knn.plugin.KNNPlugin;
import org.opensearch.node.MockNode;
import org.opensearch.node.Node;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.InternalTestCluster;
import org.opensearch.test.MockHttpTransport;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.opensearch.test.NodeRoles.dataNode;
public class KNNSettingsTests extends KNNTestCase {
@SneakyThrows
public void testGetSettingValueFromConfig() {
long expectedKNNCircuitBreakerLimit = 13;
Node mockNode = createMockNode(
Map.of(KNNSettings.KNN_MEMORY_CIRCUIT_BREAKER_LIMIT, "\"" + expectedKNNCircuitBreakerLimit + "kb\"")
);
mockNode.start();
ClusterService clusterService = mockNode.injector().getInstance(ClusterService.class);
KNNSettings.state().setClusterService(clusterService);
long actualKNNCircuitBreakerLimit = ((ByteSizeValue) KNNSettings.state()
.getSettingValue(KNNSettings.KNN_MEMORY_CIRCUIT_BREAKER_LIMIT)).getKb();
mockNode.close();
assertEquals(expectedKNNCircuitBreakerLimit, actualKNNCircuitBreakerLimit);
assertWarnings();
}
@SneakyThrows
public void testGetSettingValueDefault() {
Node mockNode = createMockNode(Collections.emptyMap());
mockNode.start();
ClusterService clusterService = mockNode.injector().getInstance(ClusterService.class);
KNNSettings.state().setClusterService(clusterService);
long actualKNNCircuitBreakerLimit = ((ByteSizeValue) KNNSettings.state()
.getSettingValue(KNNSettings.KNN_MEMORY_CIRCUIT_BREAKER_LIMIT)).getKb();
mockNode.close();
assertEquals(
((ByteSizeValue) KNNSettings.dynamicCacheSettings.get(KNNSettings.KNN_MEMORY_CIRCUIT_BREAKER_LIMIT).getDefault(Settings.EMPTY))
.getKb(),
actualKNNCircuitBreakerLimit
);
// set warning for deprecation of index.store.hybrid.mmap.extensions as expected temporarily, need to work on proper strategy of
// switching to new setting in core
// no-jdk distributions expected warning is a workaround for running tests locally
assertWarnings();
}
private Node createMockNode(Map<String, Object> configSettings) throws IOException {
Path configDir = createTempDir();
File configFile = configDir.resolve("opensearch.yml").toFile();
FileWriter configFileWriter = new FileWriter(configFile);
for (Map.Entry<String, Object> setting : configSettings.entrySet()) {
configFileWriter.write("\"" + setting.getKey() + "\": " + setting.getValue());
}
configFileWriter.close();
return new MockNode(baseSettings().build(), basePlugins(), configDir, true);
}
private List<Class<? extends Plugin>> basePlugins() {
List<Class<? extends Plugin>> plugins = new ArrayList<>();
plugins.add(getTestTransportPlugin());
plugins.add(MockHttpTransport.TestPlugin.class);
plugins.add(KNNPlugin.class);
return plugins;
}
private static Settings.Builder baseSettings() {
final Path tempDir = createTempDir();
return Settings.builder()
.put(ClusterName.CLUSTER_NAME_SETTING.getKey(), InternalTestCluster.clusterName("single-node-cluster", randomLong()))
.put(Environment.PATH_HOME_SETTING.getKey(), tempDir)
.put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType())
.put(dataNode());
}
private void assertWarnings() {
// set warning for deprecation of index.store.hybrid.mmap.extensions as expected temporarily, need to work on proper strategy of
// switching to new setting in core
// no-jdk distributions expected warning is a workaround for running tests locally
assertWarnings(
"[index.store.hybrid.mmap.extensions] setting was deprecated in OpenSearch and will be removed in a future release! See the breaking changes documentation for the next major version.",
"no-jdk distributions that do not bundle a JDK are deprecated and will be removed in a future release"
);
}
}