use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testRethrottle.
public void testRethrottle() {
TaskId taskId = new TaskId(randomAlphaOfLength(10), randomIntBetween(1, 100));
RethrottleRequest rethrottleRequest;
Float requestsPerSecond;
Map<String, String> expectedParams = new HashMap<>();
if (frequently()) {
requestsPerSecond = (float) randomDoubleBetween(0.0, 100.0, true);
rethrottleRequest = new RethrottleRequest(taskId, requestsPerSecond);
expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, Float.toString(requestsPerSecond));
} else {
rethrottleRequest = new RethrottleRequest(taskId);
expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, "-1");
}
expectedParams.put("group_by", "none");
List<Tuple<String, Supplier<Request>>> variants = new ArrayList<>();
variants.add(new Tuple<String, Supplier<Request>>("_reindex", () -> RequestConverters.rethrottleReindex(rethrottleRequest)));
variants.add(new Tuple<String, Supplier<Request>>("_update_by_query", () -> RequestConverters.rethrottleUpdateByQuery(rethrottleRequest)));
variants.add(new Tuple<String, Supplier<Request>>("_delete_by_query", () -> RequestConverters.rethrottleDeleteByQuery(rethrottleRequest)));
for (Tuple<String, Supplier<Request>> variant : variants) {
Request request = variant.v2().get();
assertEquals("/" + variant.v1() + "/" + taskId + "/_rethrottle", request.getEndpoint());
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals(expectedParams, request.getParameters());
assertNull(request.getEntity());
}
// test illegal RethrottleRequest values
Exception e = expectThrows(NullPointerException.class, () -> new RethrottleRequest(null, 1.0f));
assertEquals("taskId cannot be null", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> new RethrottleRequest(new TaskId("taskId", 1), -5.0f));
assertEquals("requestsPerSecond needs to be positive value but was [-5.0]", e.getMessage());
}
use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.
the class RestHighLevelClientTests method testApiNamingConventions.
public void testApiNamingConventions() throws Exception {
// this list should be empty once the high-level client is feature complete
String[] notYetSupportedApi = new String[] { "create", "get_script_context", "get_script_languages", "indices.exists_type", "indices.get_upgrade", "indices.put_alias", "render_search_template", "scripts_painless_execute", "indices.simulate_template", "indices.resolve_index", "indices.add_block" };
// These API are not required for high-level client feature completeness
String[] notRequiredApi = new String[] { "cluster.allocation_explain", "cluster.pending_tasks", "cluster.reroute", "cluster.state", "cluster.stats", "cluster.post_voting_config_exclusions", "cluster.delete_voting_config_exclusions", "dangling_indices.delete_dangling_index", "dangling_indices.import_dangling_index", "dangling_indices.list_dangling_indices", "indices.shard_stores", "indices.upgrade", "indices.recovery", "indices.segments", "indices.stats", "ingest.processor_grok", "nodes.info", "nodes.stats", "nodes.hot_threads", "nodes.usage", "nodes.reload_secure_settings", "search_shards" };
List<String> booleanReturnMethods = Arrays.asList("security.enable_user", "security.disable_user", "security.change_password");
Set<String> deprecatedMethods = new HashSet<>();
deprecatedMethods.add("indices.force_merge");
deprecatedMethods.add("multi_get");
deprecatedMethods.add("multi_search");
deprecatedMethods.add("search_scroll");
ClientYamlSuiteRestSpec restSpec = ClientYamlSuiteRestSpec.load("/rest-api-spec/api");
Set<String> apiSpec = restSpec.getApis().stream().map(ClientYamlSuiteRestApi::getName).collect(Collectors.toSet());
Set<String> apiUnsupported = new HashSet<>(apiSpec);
Set<String> apiNotFound = new HashSet<>();
Set<String> topLevelMethodsExclusions = new HashSet<>();
topLevelMethodsExclusions.add("getLowLevelClient");
topLevelMethodsExclusions.add("close");
Map<String, Set<Method>> methods = Arrays.stream(RestHighLevelClient.class.getMethods()).filter(method -> method.getDeclaringClass().equals(RestHighLevelClient.class) && topLevelMethodsExclusions.contains(method.getName()) == false).map(method -> Tuple.tuple(toSnakeCase(method.getName()), method)).flatMap(tuple -> tuple.v2().getReturnType().getName().endsWith("Client") ? getSubClientMethods(tuple.v1(), tuple.v2().getReturnType()) : Stream.of(tuple)).filter(tuple -> tuple.v2().getAnnotation(Deprecated.class) == null).collect(Collectors.groupingBy(Tuple::v1, Collectors.mapping(Tuple::v2, Collectors.toSet())));
// TODO remove in 8.0 - we will undeprecate indices.get_template because the current getIndexTemplate
// impl will replace the existing getTemplate method.
// The above general-purpose code ignores all deprecated methods which in this case leaves `getTemplate`
// looking like it doesn't have a valid implementatation when it does.
apiUnsupported.remove("indices.get_template");
// Synced flush is deprecated
apiUnsupported.remove("indices.flush_synced");
for (Map.Entry<String, Set<Method>> entry : methods.entrySet()) {
String apiName = entry.getKey();
for (Method method : entry.getValue()) {
assertTrue("method [" + apiName + "] is not final", Modifier.isFinal(method.getClass().getModifiers()) || Modifier.isFinal(method.getModifiers()));
assertTrue("method [" + method + "] should be public", Modifier.isPublic(method.getModifiers()));
// we convert all the method names to snake case, hence we need to look for the '_async' suffix rather than 'Async'
if (apiName.endsWith("_async")) {
assertAsyncMethod(methods, method, apiName);
} else if (isSubmitTaskMethod(apiName)) {
assertSubmitTaskMethod(methods, method, apiName, restSpec);
} else {
assertSyncMethod(method, apiName, booleanReturnMethods);
apiUnsupported.remove(apiName);
if (apiSpec.contains(apiName) == false) {
if (deprecatedMethods.contains(apiName)) {
assertTrue("method [" + method.getName() + "], api [" + apiName + "] should be deprecated", method.isAnnotationPresent(Deprecated.class));
} else {
if (// can get rid of 7.0's deprecated "getTemplate"
apiName.equals("indices.get_index_template") == false) {
apiNotFound.add(apiName);
}
}
}
}
}
}
assertThat("Some client method doesn't match a corresponding API defined in the REST spec: " + apiNotFound, apiNotFound.size(), equalTo(0));
// we decided not to support cat API in the high-level REST client, they are supposed to be used from a low-level client
apiUnsupported.removeIf(api -> api.startsWith("cat."));
Stream.concat(Arrays.stream(notYetSupportedApi), Arrays.stream(notRequiredApi)).forEach(api -> assertTrue(api + " API is either not defined in the spec or already supported by the high-level client", apiUnsupported.remove(api)));
assertThat("Some API are not supported but they should be: " + apiUnsupported, apiUnsupported.size(), equalTo(0));
}
use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.
the class AddFileKeyStoreCommandTests method testAddMultipleFiles.
public void testAddMultipleFiles() throws Exception {
final String password = "keystorepassword";
createKeystore(password);
final int n = randomIntBetween(1, 8);
final List<Tuple<String, Path>> settingFilePairs = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
settingFilePairs.add(Tuple.tuple("foo" + i, createRandomFile()));
}
terminal.addSecretInput(password);
execute(settingFilePairs.stream().flatMap(t -> Stream.of(t.v1(), t.v2().toString())).toArray(String[]::new));
for (int i = 0; i < n; i++) {
assertSecureFile(settingFilePairs.get(i).v1(), settingFilePairs.get(i).v2(), password);
}
}
use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.
the class UpgradeCli method execute.
/**
* Executes the upgrade task. This retrieves an instance of {@link UpgradeTask} which is composed
* of smaller individual tasks that perform specific operations as part of the overall process.
*
* @param terminal current terminal the command is running
* @param options options supplied to the command
* @param env current environment in which this cli tool is running.
* @throws UserException if any exception is thrown from the tasks
*/
@Override
protected void execute(final Terminal terminal, final OptionSet options, final Environment env) throws UserException {
try {
final Tuple<TaskInput, Terminal> input = new Tuple<>(new TaskInput(env), terminal);
UpgradeTask.getTask().accept(input);
terminal.println("Done!");
terminal.println("Next Steps: ");
terminal.println(" Stop the running elasticsearch on this node.");
terminal.println(" Start OpenSearch on this node.");
} catch (RuntimeException ex) {
throw new UserException(ExitCodes.DATA_ERROR, ex.getMessage());
}
}
use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.
the class DetectEsInstallationTaskTests method testTaskExecution.
@SuppressForbidden(reason = "Read config directory from test resources.")
public void testTaskExecution() throws Exception {
Path esConfig = new File(getClass().getResource("/config").getPath()).toPath();
// path for es_home
terminal.addTextInput(esConfig.getParent().toString());
// path for es_config
terminal.addTextInput(esConfig.toString());
TaskInput taskInput = new TaskInput(env);
Tuple<TaskInput, Terminal> input = new Tuple<>(taskInput, terminal);
task.accept(input);
assertThat(taskInput.getEsConfig(), is(esConfig));
assertThat(taskInput.getBaseUrl(), is("http://localhost:42123"));
assertThat(taskInput.getPlugins(), hasSize(0));
assertThat(taskInput.getNode(), is("node-x"));
assertThat(taskInput.getCluster(), is("my-cluster"));
}
Aggregations