diff --git a/.ci/bwcVersions b/.ci/bwcVersions index 59b860cc6324b..32ecda5d76cb3 100644 --- a/.ci/bwcVersions +++ b/.ci/bwcVersions @@ -77,4 +77,6 @@ BWC_VERSION: - "1.0.0" - "1.0.1" - "1.1.0" + - "1.1.1" - "1.2.0" + - "1.3.0" diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000000000..5dfa9099866d1 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,3 @@ +# This should match the owning team set up in https://github.com/orgs/opensearch-project/teams +* @opensearch-project/opensearch-core + diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml new file mode 100644 index 0000000000000..c0d07fef80ffe --- /dev/null +++ b/.github/workflows/backport.yml @@ -0,0 +1,16 @@ +name: Backport +on: + pull_request: + types: + - closed + - labeled + +jobs: + backport: + runs-on: ubuntu-latest + name: Backport + steps: + - name: Backport + uses: tibdex/backport@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/dco.yml b/.github/workflows/dco.yml new file mode 100644 index 0000000000000..cf30ea89dcbcb --- /dev/null +++ b/.github/workflows/dco.yml @@ -0,0 +1,18 @@ +name: Developer Certificate of Origin Check + +on: [pull_request] + +jobs: + check: + runs-on: ubuntu-latest + + steps: + - name: Get PR Commits + id: 'get-pr-commits' + uses: tim-actions/get-pr-commits@v1.1.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} + - name: DCO Check + uses: tim-actions/dco@v1.1.0 + with: + commits: ${{ steps.get-pr-commits.outputs.commits }} diff --git a/.whitesource b/.whitesource new file mode 100644 index 0000000000000..7dd2131e7c7d3 --- /dev/null +++ b/.whitesource @@ -0,0 +1,15 @@ +{ + "scanSettings": { + "configMode": "LOCAL", + "configExternalURL": "", + "projectToken": "", + "baseBranches": [] + }, + "checkRunSettings": { + "vulnerableCheckRunConclusionLevel": "failure", + "displayMode": "diff" + }, + "issueSettings": { + "minSeverityLevel": "LOW" + } +} \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 012ffa5368f28..9a68cde006a6f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -20,7 +20,7 @@ OpenSearch is a community project that is built and maintained by people just li ## Ways to Contribute -**Please note:** OpenSearch is a fork of [Elasticsearch 7.10.2](https://github.com/elastic/elasticsearch), and is work in progress. If you do find references to Elasticsearch (outside of attributions and copyrights!) please [open an issue](https://github.com/opensearch-project/OpenSearch/issues). +**Please note:** OpenSearch is a fork of [Elasticsearch 7.10.2](https://github.com/elastic/elasticsearch). If you do find references to Elasticsearch (outside of attributions and copyrights!) please [open an issue](https://github.com/opensearch-project/OpenSearch/issues). ### Bug Reports diff --git a/DEVELOPER_GUIDE.md b/DEVELOPER_GUIDE.md index 1aea3c5bd53b7..9fb6f91e31110 100644 --- a/DEVELOPER_GUIDE.md +++ b/DEVELOPER_GUIDE.md @@ -3,7 +3,7 @@ - [Git Clone OpenSearch Repo](#git-clone-opensearch-repo) - [Install Prerequisites](#install-prerequisites) - [JDK 11](#jdk-11) - - [JDK 8 and 17](#jdk-8-and-17) + - [JDK 8 and 14](#jdk-8-and-14) - [Runtime JDK](#runtime-jdk) - [Windows](#windows) - [Docker](#docker) @@ -44,6 +44,7 @@ - [Aggregations](#aggregations) - [Distributed Framework](#distributed-framework) - [Submitting Changes](#submitting-changes) + - [Backports](#backports) # Developer Guide @@ -63,9 +64,9 @@ OpenSearch builds using Java 11 at a minimum. This means you must have a JDK 11 Download Java 11 from [here](https://adoptium.net/releases.html?variant=openjdk11). -#### JDK 8 and 17 +#### JDK 8 and 14 -To run the full suite of tests, download and install [JDK 8](https://adoptium.net/releases.html?variant=openjdk8) and [17](https://adoptium.net/releases.html?variant=openjdk17) and set `JAVA8_HOME`, `JAVA11_HOME`, and `JAVA14_HOME`. They are required by the [backwards compatibility test](./TESTING.md#testing-backwards-compatibility). +To run the full suite of tests, download and install [JDK 8](https://adoptium.net/releases.html?variant=openjdk8) and [JDK 14](https://jdk.java.net/archive/) and set `JAVA8_HOME`, `JAVA11_HOME`, and `JAVA14_HOME`. They are required by the [backwards compatibility test](./TESTING.md#testing-backwards-compatibility). #### Runtime JDK @@ -408,3 +409,7 @@ Includes: ## Submitting Changes See [CONTRIBUTING](CONTRIBUTING.md). + +## Backports + +The Github workflow in [`backport.yml`](.github/workflows/backport.yml) creates backport PRs automatically when the original PR with an appropriate label `backport ` is merged to main. For example, if a PR on main needs to be backported to `1.x` branch, add a label `backport 1.x` to the PR. Once this PR is merged to main, the workflow will create a backport PR to the `1.x` branch. \ No newline at end of file diff --git a/MAINTAINERS.md b/MAINTAINERS.md index 7c374cc909685..3429d8bd0f9b0 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -7,7 +7,7 @@ | Harold Wang | [harold-wang](https://github.com/harold-wang) | Amazon | | Himanshu Setia | [setiah](https://github.com/setiah) | Amazon | | Nick Knize | [nknize](https://github.com/nknize) | Amazon | -| Rabi Panda | [adnapibar](adnapibar) | Amazon | +| Rabi Panda | [adnapibar](https://github.com/adnapibar) | Amazon | | Sarat Vemulapalli | [saratvemulapalli](https://github.com/saratvemulapalli) | Amazon | | Tianli Feng | [tlfeng](https://github.com/tlfeng) | Amazon | | Gopala Krishna Ambareesh | [krishna-ggk](https://github.com/krishna-ggk) |Amazon | diff --git a/build.gradle b/build.gradle index 08c993410a997..9c4fd3cdcb600 100644 --- a/build.gradle +++ b/build.gradle @@ -313,7 +313,7 @@ allprojects { } } boolean hasShadow = project.plugins.hasPlugin(ShadowPlugin) - project.configurations.compile.dependencies + project.configurations.implementation.dependencies .findAll() .toSorted(sortClosure) .each({ c -> depJavadocClosure(hasShadow, c) }) diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index b5e5be949b6e9..dd3284cc32168 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -104,27 +104,27 @@ dependencies { api 'commons-codec:commons-codec:1.13' api 'org.apache.commons:commons-compress:1.21' - api 'org.apache.ant:ant:1.10.9' + api 'org.apache.ant:ant:1.10.12' api 'com.netflix.nebula:gradle-extra-configurations-plugin:3.0.3' api 'com.netflix.nebula:nebula-publishing-plugin:4.4.4' api 'com.netflix.nebula:gradle-info-plugin:7.1.3' - api 'org.apache.rat:apache-rat:0.11' + api 'org.apache.rat:apache-rat:0.13' + api 'commons-io:commons-io:2.7' api "net.java.dev.jna:jna:5.5.0" api 'com.github.jengelman.gradle.plugins:shadow:6.0.0' api 'de.thetaphi:forbiddenapis:3.0' api 'com.avast.gradle:gradle-docker-compose-plugin:0.12.1' api 'org.apache.maven:maven-model:3.6.2' api 'com.networknt:json-schema-validator:1.0.36' + api 'com.fasterxml.jackson.core:jackson-databind:2.12.5' - compileOnly "com.puppycrawl.tools:checkstyle:${props.getProperty('checkstyle')}" - testImplementation "com.puppycrawl.tools:checkstyle:${props.getProperty('checkstyle')}" testFixturesApi "junit:junit:${props.getProperty('junit')}" testFixturesApi "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${props.getProperty('randomizedrunner')}" testFixturesApi gradleApi() testFixturesApi gradleTestKit() testImplementation 'com.github.tomakehurst:wiremock-jre8-standalone:2.23.2' testImplementation "org.mockito:mockito-core:${props.getProperty('mockito')}" - integTestImplementation('org.spockframework:spock-core:1.3-groovy-2.5') { + integTestImplementation('org.spockframework:spock-core:2.0-groovy-3.0') { exclude module: "groovy" } } @@ -260,6 +260,9 @@ if (project != rootProject) { */ afterEvaluate { generatePomFileForPluginMavenPublication.enabled = false + tasks.matching { it.name.contains("PluginMaven") }*.configure { + dependsOn("generatePomFileForNebulaPublication") + } } publishing.publications.named("nebula").configure { diff --git a/buildSrc/src/integTest/java/org/opensearch/gradle/ExportOpenSearchBuildResourcesTaskIT.java b/buildSrc/src/integTest/java/org/opensearch/gradle/ExportOpenSearchBuildResourcesTaskIT.java index cccd294f5dd36..b05250f8a66a7 100644 --- a/buildSrc/src/integTest/java/org/opensearch/gradle/ExportOpenSearchBuildResourcesTaskIT.java +++ b/buildSrc/src/integTest/java/org/opensearch/gradle/ExportOpenSearchBuildResourcesTaskIT.java @@ -33,34 +33,11 @@ package org.opensearch.gradle; import org.opensearch.gradle.test.GradleIntegrationTestCase; -import org.gradle.testkit.runner.BuildResult; public class ExportOpenSearchBuildResourcesTaskIT extends GradleIntegrationTestCase { public static final String PROJECT_NAME = "opensearch-build-resources"; - public void testUpToDateWithSourcesConfigured() { - getGradleRunner(PROJECT_NAME).withArguments("clean", "-s").build(); - - BuildResult result = getGradleRunner(PROJECT_NAME).withArguments("buildResources", "-s", "-i").build(); - assertTaskSuccessful(result, ":buildResources"); - assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml"); - assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml"); - - result = getGradleRunner(PROJECT_NAME).withArguments("buildResources", "-s", "-i").build(); - assertTaskUpToDate(result, ":buildResources"); - assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml"); - assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml"); - } - - public void testOutputAsInput() { - BuildResult result = getGradleRunner(PROJECT_NAME).withArguments("clean", "sampleCopy", "-s", "-i").build(); - - assertTaskSuccessful(result, ":sampleCopy"); - assertBuildFileExists(result, PROJECT_NAME, "sampleCopy/checkstyle.xml"); - assertBuildFileExists(result, PROJECT_NAME, "sampleCopy/checkstyle_suppressions.xml"); - } - public void testIncorrectUsage() { assertOutputContains( getGradleRunner(PROJECT_NAME).withArguments("noConfigAfterExecution", "-s", "-i").buildAndFail().getOutput(), diff --git a/buildSrc/src/main/groovy/org/opensearch/gradle/precommit/PrecommitTasks.groovy b/buildSrc/src/main/groovy/org/opensearch/gradle/precommit/PrecommitTasks.groovy index 7c4b3e6118da9..e5cd4cda65040 100644 --- a/buildSrc/src/main/groovy/org/opensearch/gradle/precommit/PrecommitTasks.groovy +++ b/buildSrc/src/main/groovy/org/opensearch/gradle/precommit/PrecommitTasks.groovy @@ -40,7 +40,6 @@ class PrecommitTasks { static void create(Project project, boolean includeDependencyLicenses) { - project.pluginManager.apply(CheckstylePrecommitPlugin) project.pluginManager.apply(ForbiddenApisPrecommitPlugin) project.pluginManager.apply(JarHellPrecommitPlugin) project.pluginManager.apply(ForbiddenPatternsPrecommitPlugin) diff --git a/buildSrc/src/main/java/org/opensearch/gradle/BwcVersions.java b/buildSrc/src/main/java/org/opensearch/gradle/BwcVersions.java index 67581a35cdc04..d6a770d7cf4ae 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/BwcVersions.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/BwcVersions.java @@ -174,7 +174,10 @@ public BwcVersions(SortedSet allVersions, Version currentVersionPropert private void assertNoOlderThanTwoMajors() { Set majors = groupByMajor.keySet(); - if (majors.size() != 2 && currentVersion.getMinor() != 0 && currentVersion.getRevision() != 0) { + // until OpenSearch 3.0 we will need to carry three major support + // (1, 7, 6) && (2, 1, 7) since OpenSearch 1.0 === Legacy 7.x + int numSupportedMajors = (currentVersion.getMajor() < 3) ? 3 : 2; + if (majors.size() != numSupportedMajors && currentVersion.getMinor() != 0 && currentVersion.getRevision() != 0) { throw new IllegalStateException("Expected exactly 2 majors in parsed versions but found: " + majors); } } diff --git a/buildSrc/src/main/java/org/opensearch/gradle/JdkDownloadPlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/JdkDownloadPlugin.java index 00f4e026d33b0..b9f3036f7aad9 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/JdkDownloadPlugin.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/JdkDownloadPlugin.java @@ -110,12 +110,17 @@ private void setupRepository(Project project, Jdk jdk) { if (jdk.getVendor().equals(VENDOR_ADOPTIUM)) { repoUrl = "https://github.com/adoptium/temurin" + jdk.getMajor() + "-binaries/releases/download/"; + // JDK updates are suffixed with 'U' (fe OpenJDK17U), whereas GA releases are not (fe OpenJDK17). + // To distinguish between those, the GA releases have only major version component (fe 17+32), + // the updates always have minor/patch components (fe 17.0.1+12), checking for the presence of + // version separator '.' should be enough. artifactPattern = "jdk-" + jdk.getBaseVersion() + "+" + jdk.getBuild() + "/OpenJDK" + jdk.getMajor() + + (jdk.getBaseVersion().contains(".") ? "U" : "") + "-jdk_[classifier]_[module]_hotspot_" + jdk.getBaseVersion() + "_" diff --git a/buildSrc/src/main/java/org/opensearch/gradle/LoggedExec.java b/buildSrc/src/main/java/org/opensearch/gradle/LoggedExec.java index 03873cc003e11..2831108b94452 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/LoggedExec.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/LoggedExec.java @@ -82,7 +82,7 @@ public LoggedExec(FileSystemOperations fileSystemOperations) { doLast(new Action() { @Override public void execute(Task task) { - if (LoggedExec.this.getExecResult().getExitValue() != 0) { + if (LoggedExec.this.getExecutionResult().get().getExitValue() != 0) { try { LoggedExec.this.getLogger().error("Output for " + LoggedExec.this.getExecutable() + ":"); outputLogger.accept(LoggedExec.this.getLogger()); @@ -94,7 +94,7 @@ public void execute(Task task) { "Process '%s %s' finished with non-zero exit value %d", LoggedExec.this.getExecutable(), LoggedExec.this.getArgs(), - LoggedExec.this.getExecResult().getExitValue() + LoggedExec.this.getExecutionResult().get().getExitValue() ) ); } diff --git a/buildSrc/src/main/java/org/opensearch/gradle/OpenSearchTestBasePlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/OpenSearchTestBasePlugin.java index b6dba360ddb27..556060d8afe4a 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/OpenSearchTestBasePlugin.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/OpenSearchTestBasePlugin.java @@ -74,7 +74,7 @@ public void apply(Project project) { File heapdumpDir = new File(project.getBuildDir(), "heapdump"); project.getTasks().withType(Test.class).configureEach(test -> { - File testOutputDir = new File(test.getReports().getJunitXml().getDestination(), "output"); + File testOutputDir = new File(test.getReports().getJunitXml().getOutputLocation().getAsFile().get(), "output"); ErrorReportingTestListener listener = new ErrorReportingTestListener(test.getTestLogging(), test.getLogger(), testOutputDir); test.getExtensions().add("errorReportingTestListener", listener); diff --git a/buildSrc/src/main/java/org/opensearch/gradle/checkstyle/SnippetLengthCheck.java b/buildSrc/src/main/java/org/opensearch/gradle/checkstyle/SnippetLengthCheck.java deleted file mode 100644 index 701f44b533ecb..0000000000000 --- a/buildSrc/src/main/java/org/opensearch/gradle/checkstyle/SnippetLengthCheck.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you 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. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.gradle.checkstyle; - -import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; -import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import com.puppycrawl.tools.checkstyle.api.FileText; - -import java.io.File; -import java.util.Arrays; -import java.util.Iterator; -import java.util.function.BiConsumer; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Checks the snippets included in the docs aren't too wide to fit on - * the page. - */ -public class SnippetLengthCheck extends AbstractFileSetCheck { - private static final Pattern START = Pattern.compile("^( *)//\\s*tag::(.+?)\\s*$", Pattern.MULTILINE); - private int max; - - /** - * The maximum width that a snippet may have. - */ - public void setMax(int max) { - this.max = max; - } - - @Override - protected void processFiltered(File file, FileText fileText) throws CheckstyleException { - checkFile((line, message) -> log(line, message), max, fileText.toLinesArray()); - } - - static void checkFile(BiConsumer log, int max, String... lineArray) { - LineItr lines = new LineItr(Arrays.asList(lineArray).iterator()); - while (lines.hasNext()) { - Matcher m = START.matcher(lines.next()); - if (m.matches()) { - checkSnippet(log, max, lines, m.group(1), m.group(2)); - } - } - } - - private static void checkSnippet(BiConsumer log, int max, LineItr lines, String leadingSpaces, String name) { - Pattern end = Pattern.compile("^ *//\\s*end::" + name + "\\s*$", Pattern.MULTILINE); - while (lines.hasNext()) { - String line = lines.next(); - if (end.matcher(line).matches()) { - return; - } - if (line.isEmpty()) { - continue; - } - if (false == line.startsWith(leadingSpaces)) { - log.accept(lines.lastLineNumber, "snippet line should start with [" + leadingSpaces + "]"); - continue; - } - int width = line.length() - leadingSpaces.length(); - if (width > max) { - log.accept(lines.lastLineNumber, "snippet line should be no more than [" + max + "] characters but was [" + width + "]"); - } - } - } - - private static class LineItr implements Iterator { - private final Iterator delegate; - private int lastLineNumber; - - LineItr(Iterator delegate) { - this.delegate = delegate; - } - - @Override - public boolean hasNext() { - return delegate.hasNext(); - } - - @Override - public String next() { - lastLineNumber++; - return delegate.next(); - } - } -} diff --git a/buildSrc/src/main/java/org/opensearch/gradle/info/GlobalBuildInfoPlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/info/GlobalBuildInfoPlugin.java index c1e96a3d9401e..60afe8334f05d 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/info/GlobalBuildInfoPlugin.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/info/GlobalBuildInfoPlugin.java @@ -41,11 +41,10 @@ import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; import org.gradle.api.model.ObjectFactory; -import org.gradle.api.provider.Provider; import org.gradle.api.provider.ProviderFactory; import org.gradle.internal.jvm.Jvm; -import org.gradle.jvm.toolchain.JavaInstallation; -import org.gradle.jvm.toolchain.JavaInstallationRegistry; +import org.gradle.internal.jvm.inspection.JvmInstallationMetadata; +import org.gradle.internal.jvm.inspection.JvmMetadataDetector; import org.gradle.util.GradleVersion; import javax.inject.Inject; @@ -77,13 +76,13 @@ public class GlobalBuildInfoPlugin implements Plugin { private static final String DEFAULT_VERSION_JAVA_FILE_PATH = "server/src/main/java/org/opensearch/Version.java"; private static Integer _defaultParallel = null; - private final JavaInstallationRegistry javaInstallationRegistry; + private final JvmMetadataDetector jvmMetadataDetector; private final ObjectFactory objects; private final ProviderFactory providers; @Inject - public GlobalBuildInfoPlugin(JavaInstallationRegistry javaInstallationRegistry, ObjectFactory objects, ProviderFactory providers) { - this.javaInstallationRegistry = javaInstallationRegistry; + public GlobalBuildInfoPlugin(JvmMetadataDetector jvmMetadataDetector, ObjectFactory objects, ProviderFactory providers) { + this.jvmMetadataDetector = jvmMetadataDetector; this.objects = objects; this.providers = providers; } @@ -115,7 +114,7 @@ public void apply(Project project) { params.setRuntimeJavaHome(runtimeJavaHome); params.setRuntimeJavaVersion(determineJavaVersion("runtime java.home", runtimeJavaHome, minimumRuntimeVersion)); params.setIsRutimeJavaHomeSet(runtimeJavaHomeOpt.isPresent()); - params.setRuntimeJavaDetails(getJavaInstallation(runtimeJavaHome).getImplementationName()); + params.setRuntimeJavaDetails(getJavaInstallation(runtimeJavaHome).getDisplayName()); params.setJavaVersions(getAvailableJavaVersions(minimumCompilerVersion)); params.setMinimumCompilerVersion(minimumCompilerVersion); params.setMinimumRuntimeVersion(minimumRuntimeVersion); @@ -160,14 +159,14 @@ private void logGlobalBuildInfo() { final String osVersion = System.getProperty("os.version"); final String osArch = System.getProperty("os.arch"); final Jvm gradleJvm = Jvm.current(); - final String gradleJvmDetails = getJavaInstallation(gradleJvm.getJavaHome()).getImplementationName(); + final String gradleJvmDetails = getJavaInstallation(gradleJvm.getJavaHome()).getDisplayName(); LOGGER.quiet("======================================="); LOGGER.quiet("OpenSearch Build Hamster says Hello!"); LOGGER.quiet(" Gradle Version : " + GradleVersion.current().getVersion()); LOGGER.quiet(" OS Info : " + osName + " " + osVersion + " (" + osArch + ")"); if (BuildParams.getIsRuntimeJavaHomeSet()) { - String runtimeJvmDetails = getJavaInstallation(BuildParams.getRuntimeJavaHome()).getImplementationName(); + String runtimeJvmDetails = getJavaInstallation(BuildParams.getRuntimeJavaHome()).getDisplayName(); LOGGER.quiet(" Runtime JDK Version : " + BuildParams.getRuntimeJavaVersion() + " (" + runtimeJvmDetails + ")"); LOGGER.quiet(" Runtime java.home : " + BuildParams.getRuntimeJavaHome()); LOGGER.quiet(" Gradle JDK Version : " + gradleJvm.getJavaVersion() + " (" + gradleJvmDetails + ")"); @@ -182,8 +181,8 @@ private void logGlobalBuildInfo() { } private JavaVersion determineJavaVersion(String description, File javaHome, JavaVersion requiredVersion) { - JavaInstallation installation = getJavaInstallation(javaHome); - JavaVersion actualVersion = installation.getJavaVersion(); + JvmInstallationMetadata installation = getJavaInstallation(javaHome); + JavaVersion actualVersion = installation.getLanguageVersion(); if (actualVersion.isCompatibleWith(requiredVersion) == false) { throwInvalidJavaHomeException( description, @@ -196,15 +195,8 @@ private JavaVersion determineJavaVersion(String description, File javaHome, Java return actualVersion; } - private JavaInstallation getJavaInstallation(File javaHome) { - JavaInstallation installation; - if (isCurrentJavaHome(javaHome)) { - installation = javaInstallationRegistry.getInstallationForCurrentVirtualMachine().get(); - } else { - installation = javaInstallationRegistry.installationForDirectory(objects.directoryProperty().fileValue(javaHome)).get(); - } - - return installation; + private JvmInstallationMetadata getJavaInstallation(File javaHome) { + return jvmMetadataDetector.getMetadata(javaHome); } private List getAvailableJavaVersions(JavaVersion minimumCompilerVersion) { @@ -214,11 +206,9 @@ private List getAvailableJavaVersions(JavaVersion minimumCompilerVersi String javaHomeEnvVarName = getJavaHomeEnvVarName(Integer.toString(version)); if (System.getenv(javaHomeEnvVarName) != null) { File javaHomeDirectory = new File(findJavaHome(Integer.toString(version))); - Provider javaInstallationProvider = javaInstallationRegistry.installationForDirectory( - objects.directoryProperty().fileValue(javaHomeDirectory) - ); + JvmInstallationMetadata javaInstallation = jvmMetadataDetector.getMetadata(javaHomeDirectory); JavaHome javaHome = JavaHome.of(version, providers.provider(() -> { - int actualVersion = Integer.parseInt(javaInstallationProvider.get().getJavaVersion().getMajorVersion()); + int actualVersion = Integer.parseInt(javaInstallation.getLanguageVersion().getMajorVersion()); if (actualVersion != version) { throwInvalidJavaHomeException("env variable " + javaHomeEnvVarName, javaHomeDirectory, version, actualVersion); } diff --git a/buildSrc/src/main/java/org/opensearch/gradle/internal/BwcSetupExtension.java b/buildSrc/src/main/java/org/opensearch/gradle/internal/BwcSetupExtension.java index 53b89a5d783e8..402ac6947b53a 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/internal/BwcSetupExtension.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/internal/BwcSetupExtension.java @@ -39,6 +39,7 @@ import org.gradle.api.Action; import org.gradle.api.GradleException; import org.gradle.api.Project; +import org.gradle.api.Task; import org.gradle.api.logging.LogLevel; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.TaskProvider; @@ -80,77 +81,83 @@ TaskProvider bwcTask(String name, Action configuration) } private TaskProvider createRunBwcGradleTask(Project project, String name, Action configAction) { - return project.getTasks().register(name, LoggedExec.class, loggedExec -> { - // TODO revisit - loggedExec.dependsOn("checkoutBwcBranch"); - loggedExec.setSpoolOutput(true); - loggedExec.setWorkingDir(checkoutDir.get()); - loggedExec.doFirst(t -> { - // Execution time so that the checkouts are available - String javaVersionsString = readFromFile(new File(checkoutDir.get(), ".ci/java-versions.properties")); - loggedExec.environment( - "JAVA_HOME", - getJavaHome( - Integer.parseInt( - Arrays.asList(javaVersionsString.split("\n")) - .stream() - .filter(l -> l.trim().startsWith("OPENSEARCH_BUILD_JAVA=")) - .map(l -> l.replace("OPENSEARCH_BUILD_JAVA=java", "").trim()) - .map(l -> l.replace("OPENSEARCH_BUILD_JAVA=openjdk", "").trim()) - .collect(Collectors.joining("!!")) - ) - ) - ); - loggedExec.environment( - "RUNTIME_JAVA_HOME", - getJavaHome( - Integer.parseInt( - Arrays.asList(javaVersionsString.split("\n")) - .stream() - .filter(l -> l.trim().startsWith("OPENSEARCH_RUNTIME_JAVA=")) - .map(l -> l.replace("OPENSEARCH_RUNTIME_JAVA=java", "").trim()) - .map(l -> l.replace("OPENSEARCH_RUNTIME_JAVA=openjdk", "").trim()) - .collect(Collectors.joining("!!")) - ) - ) - ); - }); - - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - loggedExec.executable("cmd"); - loggedExec.args("/C", "call", new File(checkoutDir.get(), "gradlew").toString()); - } else { - loggedExec.executable(new File(checkoutDir.get(), "gradlew").toString()); - } - if (project.getGradle().getStartParameter().isOffline()) { - loggedExec.args("--offline"); - } - // TODO resolve - String buildCacheUrl = System.getProperty("org.opensearch.build.cache.url"); - if (buildCacheUrl != null) { - loggedExec.args("-Dorg.opensearch.build.cache.url=" + buildCacheUrl); - } + return project.getTasks().register(name, LoggedExec.class, new Action() { + @Override + public void execute(LoggedExec loggedExec) { + // TODO revisit + loggedExec.dependsOn("checkoutBwcBranch"); + loggedExec.setSpoolOutput(true); + loggedExec.setWorkingDir(checkoutDir.get()); + loggedExec.doFirst(new Action() { + @Override + public void execute(Task t) { + // Execution time so that the checkouts are available + String javaVersionsString = readFromFile(new File(checkoutDir.get(), ".ci/java-versions.properties")); + loggedExec.environment( + "JAVA_HOME", + getJavaHome( + Integer.parseInt( + Arrays.asList(javaVersionsString.split("\n")) + .stream() + .filter(l -> l.trim().startsWith("OPENSEARCH_BUILD_JAVA=")) + .map(l -> l.replace("OPENSEARCH_BUILD_JAVA=java", "").trim()) + .map(l -> l.replace("OPENSEARCH_BUILD_JAVA=openjdk", "").trim()) + .collect(Collectors.joining("!!")) + ) + ) + ); + loggedExec.environment( + "RUNTIME_JAVA_HOME", + getJavaHome( + Integer.parseInt( + Arrays.asList(javaVersionsString.split("\n")) + .stream() + .filter(l -> l.trim().startsWith("OPENSEARCH_RUNTIME_JAVA=")) + .map(l -> l.replace("OPENSEARCH_RUNTIME_JAVA=java", "").trim()) + .map(l -> l.replace("OPENSEARCH_RUNTIME_JAVA=openjdk", "").trim()) + .collect(Collectors.joining("!!")) + ) + ) + ); + } + }); + + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + loggedExec.executable("cmd"); + loggedExec.args("/C", "call", new File(checkoutDir.get(), "gradlew").toString()); + } else { + loggedExec.executable(new File(checkoutDir.get(), "gradlew").toString()); + } + if (project.getGradle().getStartParameter().isOffline()) { + loggedExec.args("--offline"); + } + // TODO resolve + String buildCacheUrl = System.getProperty("org.opensearch.build.cache.url"); + if (buildCacheUrl != null) { + loggedExec.args("-Dorg.opensearch.build.cache.url=" + buildCacheUrl); + } - loggedExec.args("-Dbuild.snapshot=true"); - loggedExec.args("-Dscan.tag.NESTED"); - final LogLevel logLevel = project.getGradle().getStartParameter().getLogLevel(); - List nonDefaultLogLevels = Arrays.asList(LogLevel.QUIET, LogLevel.WARN, LogLevel.INFO, LogLevel.DEBUG); - if (nonDefaultLogLevels.contains(logLevel)) { - loggedExec.args("--" + logLevel.name().toLowerCase(Locale.ENGLISH)); - } - final String showStacktraceName = project.getGradle().getStartParameter().getShowStacktrace().name(); - assert Arrays.asList("INTERNAL_EXCEPTIONS", "ALWAYS", "ALWAYS_FULL").contains(showStacktraceName); - if (showStacktraceName.equals("ALWAYS")) { - loggedExec.args("--stacktrace"); - } else if (showStacktraceName.equals("ALWAYS_FULL")) { - loggedExec.args("--full-stacktrace"); - } - if (project.getGradle().getStartParameter().isParallelProjectExecutionEnabled()) { - loggedExec.args("--parallel"); + loggedExec.args("-Dbuild.snapshot=true"); + loggedExec.args("-Dscan.tag.NESTED"); + final LogLevel logLevel = project.getGradle().getStartParameter().getLogLevel(); + List nonDefaultLogLevels = Arrays.asList(LogLevel.QUIET, LogLevel.WARN, LogLevel.INFO, LogLevel.DEBUG); + if (nonDefaultLogLevels.contains(logLevel)) { + loggedExec.args("--" + logLevel.name().toLowerCase(Locale.ENGLISH)); + } + final String showStacktraceName = project.getGradle().getStartParameter().getShowStacktrace().name(); + assert Arrays.asList("INTERNAL_EXCEPTIONS", "ALWAYS", "ALWAYS_FULL").contains(showStacktraceName); + if (showStacktraceName.equals("ALWAYS")) { + loggedExec.args("--stacktrace"); + } else if (showStacktraceName.equals("ALWAYS_FULL")) { + loggedExec.args("--full-stacktrace"); + } + if (project.getGradle().getStartParameter().isParallelProjectExecutionEnabled()) { + loggedExec.args("--parallel"); + } + loggedExec.setStandardOutput(new IndentingOutputStream(System.out, unreleasedVersionInfo.get().version)); + loggedExec.setErrorOutput(new IndentingOutputStream(System.err, unreleasedVersionInfo.get().version)); + configAction.execute(loggedExec); } - loggedExec.setStandardOutput(new IndentingOutputStream(System.out, unreleasedVersionInfo.get().version)); - loggedExec.setErrorOutput(new IndentingOutputStream(System.err, unreleasedVersionInfo.get().version)); - configAction.execute(loggedExec); }); } diff --git a/buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionArchiveSetupPlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionArchiveSetupPlugin.java index 6153d5f6fe4a9..e0acdd11a6f76 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionArchiveSetupPlugin.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionArchiveSetupPlugin.java @@ -34,18 +34,22 @@ import org.opensearch.gradle.EmptyDirTask; import org.opensearch.gradle.tar.SymbolicLinkPreservingTar; +import org.gradle.api.Action; import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.Task; import org.gradle.api.artifacts.type.ArtifactTypeDefinition; import org.gradle.api.plugins.BasePlugin; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; import org.gradle.api.tasks.AbstractCopyTask; import org.gradle.api.tasks.Sync; import org.gradle.api.tasks.TaskContainer; import org.gradle.api.tasks.bundling.AbstractArchiveTask; import org.gradle.api.tasks.bundling.Compression; import org.gradle.api.tasks.bundling.Zip; - +import org.gradle.internal.os.OperatingSystem; import java.io.File; import static org.opensearch.gradle.util.Util.capitalize; @@ -66,6 +70,7 @@ * 4. Having per-distribution sub-projects means we can build them in parallel. */ public class InternalDistributionArchiveSetupPlugin implements Plugin { + private static final Logger LOGGER = Logging.getLogger(InternalDistributionArchiveSetupPlugin.class); public static final String DEFAULT_CONFIGURATION_NAME = "default"; public static final String EXTRACTED_CONFIGURATION_NAME = "extracted"; @@ -80,16 +85,39 @@ public void apply(Project project) { configureTarDefaults(project); } + private Action configure(String name) { + return (Task task) -> task.onlyIf(s -> { + if (OperatingSystem.current().isWindows()) { + // On Windows, include only Windows distributions and integTestZip + final String nameLowerCased = name.toLowerCase(); + final boolean skip = !(nameLowerCased.contains("windows") || nameLowerCased.contains("integtest")); + + if (skip) { + LOGGER.info("Skipping task " + name + " since it does not match current OS platform"); + } + + return !skip; + } else { + return true; + } + }); + } + private void registerAndConfigureDistributionArchivesExtension(Project project) { container = project.container(DistributionArchive.class, name -> { var subProjectDir = archiveToSubprojectName(name); var copyDistributionTaskName = "build" + capitalize(name.substring(0, name.length() - 3)); TaskContainer tasks = project.getTasks(); var explodedDist = tasks.register(copyDistributionTaskName, Sync.class, sync -> sync.into(subProjectDir + "/build/install/")); + explodedDist.configure(configure(name)); var archiveTaskName = "build" + capitalize(name); - return name.endsWith("Tar") - ? new DistributionArchive(tasks.register(archiveTaskName, SymbolicLinkPreservingTar.class), explodedDist, name) - : new DistributionArchive(tasks.register(archiveTaskName, Zip.class), explodedDist, name); + + var archiveTask = name.endsWith("Tar") + ? tasks.register(archiveTaskName, SymbolicLinkPreservingTar.class) + : tasks.register(archiveTaskName, Zip.class); + archiveTask.configure(configure(name)); + + return new DistributionArchive(archiveTask, explodedDist, name); }); // Each defined distribution archive is linked to a subproject. // A distribution archive definition not matching a sub project will result in build failure. diff --git a/buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionBwcSetupPlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionBwcSetupPlugin.java index e05c68e258ad6..dd2393702fe2b 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionBwcSetupPlugin.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionBwcSetupPlugin.java @@ -33,9 +33,11 @@ package org.opensearch.gradle.internal; import org.opensearch.gradle.BwcVersions; +import org.opensearch.gradle.LoggedExec; import org.opensearch.gradle.Version; import org.opensearch.gradle.info.BuildParams; import org.opensearch.gradle.info.GlobalBuildInfoPlugin; +import org.gradle.api.Action; import org.gradle.api.InvalidUserDataException; import org.gradle.api.Plugin; import org.gradle.api.Project; @@ -204,24 +206,30 @@ static void createBuildBwcTask( TaskProvider bwcTaskProvider ) { String bwcTaskName = buildBwcTaskName(projectName); - bwcSetupExtension.bwcTask(bwcTaskName, c -> { - c.getInputs().file(new File(project.getBuildDir(), "refspec")); - c.getOutputs().files(projectArtifact); - c.getOutputs().cacheIf("BWC distribution caching is disabled on 'master' branch", task -> { - String gitBranch = System.getenv("GIT_BRANCH"); - return BuildParams.isCi() && (gitBranch == null || gitBranch.endsWith("master") == false); - }); - c.args(projectPath.replace('/', ':') + ":assemble"); - if (project.getGradle().getStartParameter().isBuildCacheEnabled()) { - c.args("--build-cache"); - } - c.doLast(task -> { - if (projectArtifact.exists() == false) { - throw new InvalidUserDataException( - "Building " + bwcVersion.get() + " didn't generate expected file " + projectArtifact - ); + bwcSetupExtension.bwcTask(bwcTaskName, new Action() { + @Override + public void execute(LoggedExec c) { + c.getInputs().file(new File(project.getBuildDir(), "refspec")); + c.getOutputs().files(projectArtifact); + c.getOutputs().cacheIf("BWC distribution caching is disabled on 'master' branch", task -> { + String gitBranch = System.getenv("GIT_BRANCH"); + return BuildParams.isCi() && (gitBranch == null || gitBranch.endsWith("master") == false); + }); + c.args(projectPath.replace('/', ':') + ":assemble"); + if (project.getGradle().getStartParameter().isBuildCacheEnabled()) { + c.args("--build-cache"); } - }); + c.doLast(new Action() { + @Override + public void execute(Task task) { + if (projectArtifact.exists() == false) { + throw new InvalidUserDataException( + "Building " + bwcVersion.get() + " didn't generate expected file " + projectArtifact + ); + } + } + }); + } }); bwcTaskProvider.configure(t -> t.dependsOn(bwcTaskName)); } diff --git a/buildSrc/src/main/java/org/opensearch/gradle/precommit/CheckstylePrecommitPlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/precommit/CheckstylePrecommitPlugin.java deleted file mode 100644 index 30d26eff9f981..0000000000000 --- a/buildSrc/src/main/java/org/opensearch/gradle/precommit/CheckstylePrecommitPlugin.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you 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. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.gradle.precommit; - -import org.opensearch.gradle.VersionProperties; -import org.opensearch.gradle.util.Util; -import org.gradle.api.Action; -import org.gradle.api.Project; -import org.gradle.api.Task; -import org.gradle.api.artifacts.dsl.DependencyHandler; -import org.gradle.api.plugins.quality.Checkstyle; -import org.gradle.api.plugins.quality.CheckstyleExtension; -import org.gradle.api.tasks.TaskProvider; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; -import java.net.JarURLConnection; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; - -public class CheckstylePrecommitPlugin extends PrecommitPlugin { - @Override - public TaskProvider createTask(Project project) { - // Always copy the checkstyle configuration files to 'buildDir/checkstyle' since the resources could be located in a jar - // file. If the resources are located in a jar, Gradle will fail when it tries to turn the URL into a file - URL checkstyleConfUrl = CheckstylePrecommitPlugin.class.getResource("/checkstyle.xml"); - URL checkstyleSuppressionsUrl = CheckstylePrecommitPlugin.class.getResource("/checkstyle_suppressions.xml"); - File checkstyleDir = new File(project.getBuildDir(), "checkstyle"); - File checkstyleSuppressions = new File(checkstyleDir, "checkstyle_suppressions.xml"); - File checkstyleConf = new File(checkstyleDir, "checkstyle.xml"); - TaskProvider copyCheckstyleConf = project.getTasks().register("copyCheckstyleConf"); - - // configure inputs and outputs so up to date works properly - copyCheckstyleConf.configure(t -> t.getOutputs().files(checkstyleSuppressions, checkstyleConf)); - if ("jar".equals(checkstyleConfUrl.getProtocol())) { - try { - JarURLConnection jarURLConnection = (JarURLConnection) checkstyleConfUrl.openConnection(); - copyCheckstyleConf.configure(t -> t.getInputs().file(jarURLConnection.getJarFileURL())); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } else if ("file".equals(checkstyleConfUrl.getProtocol())) { - copyCheckstyleConf.configure(t -> t.getInputs().files(checkstyleConfUrl.getFile(), checkstyleSuppressionsUrl.getFile())); - } - - // Explicitly using an Action interface as java lambdas - // are not supported by Gradle up-to-date checks - copyCheckstyleConf.configure(t -> t.doLast(new Action() { - @Override - public void execute(Task task) { - checkstyleDir.mkdirs(); - try (InputStream stream = checkstyleConfUrl.openStream()) { - Files.copy(stream, checkstyleConf.toPath(), StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - try (InputStream stream = checkstyleSuppressionsUrl.openStream()) { - Files.copy(stream, checkstyleSuppressions.toPath(), StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - })); - - TaskProvider checkstyleTask = project.getTasks().register("checkstyle"); - checkstyleTask.configure(t -> t.dependsOn(project.getTasks().withType(Checkstyle.class))); - - // Apply the checkstyle plugin to create `checkstyleMain` and `checkstyleTest`. It only - // creates them if there is main or test code to check and it makes `check` depend - // on them. We also want `precommit` to depend on `checkstyle`. - project.getPluginManager().apply("checkstyle"); - CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class); - checkstyle.getConfigDirectory().set(checkstyleDir); - - DependencyHandler dependencies = project.getDependencies(); - String checkstyleVersion = VersionProperties.getVersions().get("checkstyle"); - dependencies.add("checkstyle", "com.puppycrawl.tools:checkstyle:" + checkstyleVersion); - dependencies.add("checkstyle", project.files(Util.getBuildSrcCodeSource())); - - project.getTasks().withType(Checkstyle.class).configureEach(t -> { - t.dependsOn(copyCheckstyleConf); - t.reports(r -> r.getHtml().setEnabled(false)); - }); - - return checkstyleTask; - } -} diff --git a/buildSrc/src/main/java/org/opensearch/gradle/precommit/JarHellPrecommitPlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/precommit/JarHellPrecommitPlugin.java index 04ae37f92bcc9..87bf03cec3587 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/precommit/JarHellPrecommitPlugin.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/precommit/JarHellPrecommitPlugin.java @@ -53,9 +53,16 @@ public TaskProvider createTask(Project project) { TaskProvider jarHell = project.getTasks().register("jarHell", JarHellTask.class); jarHell.configure(t -> { - SourceSet testSourceSet = Util.getJavaTestSourceSet(project).get(); + final Task testClassesTask = project.getTasks().getByName("testClasses"); + final SourceSet testSourceSet = Util.getJavaTestSourceSet(project).get(); t.setClasspath(testSourceSet.getRuntimeClasspath().plus(jarHellConfig)); - t.dependsOn(jarHellConfig); + t.dependsOn(jarHellConfig, testClassesTask); + + // if this a plugin, we need to add dependency on pluginProperties task + project.getPluginManager().withPlugin("opensearch.opensearchplugin", plugin -> { + Task propertiesTask = project.getTasks().getByName("pluginProperties"); + t.dependsOn(jarHellConfig, testClassesTask, propertiesTask); + }); }); return jarHell; diff --git a/buildSrc/src/main/java/org/opensearch/gradle/precommit/JarHellTask.java b/buildSrc/src/main/java/org/opensearch/gradle/precommit/JarHellTask.java index e642aaee754e7..c6a0d7abf7da1 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/precommit/JarHellTask.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/precommit/JarHellTask.java @@ -33,6 +33,7 @@ package org.opensearch.gradle.precommit; import org.opensearch.gradle.LoggedExec; + import org.gradle.api.file.FileCollection; import org.gradle.api.tasks.CacheableTask; import org.gradle.api.tasks.CompileClasspath; @@ -56,7 +57,7 @@ public JarHellTask() { public void runJarHellCheck() { LoggedExec.javaexec(getProject(), spec -> { spec.environment("CLASSPATH", getClasspath().getAsPath()); - spec.setMain("org.opensearch.bootstrap.JarHell"); + spec.getMainClass().set("org.opensearch.bootstrap.JarHell"); }); } diff --git a/buildSrc/src/main/java/org/opensearch/gradle/precommit/LoggerUsageTask.java b/buildSrc/src/main/java/org/opensearch/gradle/precommit/LoggerUsageTask.java index 7ce0c0cecff81..1fd092b7f268f 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/precommit/LoggerUsageTask.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/precommit/LoggerUsageTask.java @@ -61,7 +61,7 @@ public LoggerUsageTask() { @TaskAction public void runLoggerUsageTask() { LoggedExec.javaexec(getProject(), spec -> { - spec.setMain("org.opensearch.test.loggerusage.OpenSearchLoggerUsageChecker"); + spec.getMainClass().set("org.opensearch.test.loggerusage.OpenSearchLoggerUsageChecker"); spec.classpath(getClasspath()); getClassDirectories().forEach(spec::args); }); diff --git a/buildSrc/src/main/java/org/opensearch/gradle/precommit/ThirdPartyAuditTask.java b/buildSrc/src/main/java/org/opensearch/gradle/precommit/ThirdPartyAuditTask.java index 8be3f3b8e9b95..ee68d2740e279 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/precommit/ThirdPartyAuditTask.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/precommit/ThirdPartyAuditTask.java @@ -281,10 +281,6 @@ private void logForbiddenAPIsOutput(String forbiddenApisOutput) { getLogger().error("Forbidden APIs output:\n{}==end of forbidden APIs==", forbiddenApisOutput); } - private void throwNotConfiguredCorrectlyException() { - throw new IllegalArgumentException("Audit of third party dependencies is not configured correctly"); - } - private void extractJars(Set jars) { File jarExpandDir = getJarExpandDir(); // We need to clean up to make sure old dependencies don't linger @@ -360,7 +356,7 @@ private String runForbiddenAPIsCli() throws IOException { ); spec.jvmArgs("-Xmx1g"); spec.jvmArgs(LoggedExec.shortLivedArgs()); - spec.setMain("de.thetaphi.forbiddenapis.cli.CliMain"); + spec.getMainClass().set("de.thetaphi.forbiddenapis.cli.CliMain"); spec.args("-f", getSignatureFile().getAbsolutePath(), "-d", getJarExpandDir(), "--allowmissingclasses"); spec.setErrorOutput(errorOut); if (getLogger().isInfoEnabled() == false) { @@ -390,7 +386,7 @@ private Set runJdkJarHellCheck() throws IOException { getProject().getConfigurations().getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME) ); - spec.setMain(JDK_JAR_HELL_MAIN_CLASS); + spec.getMainClass().set(JDK_JAR_HELL_MAIN_CLASS); spec.args(getJarExpandDir()); spec.setIgnoreExitValue(true); if (javaHome != null) { diff --git a/buildSrc/src/main/java/org/opensearch/gradle/test/DistroTestPlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/test/DistroTestPlugin.java index 226875ec3d8e9..81b51f7f1f9ff 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/test/DistroTestPlugin.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/test/DistroTestPlugin.java @@ -76,7 +76,7 @@ public class DistroTestPlugin implements Plugin { private static final String SYSTEM_JDK_VERSION = "8u242+b08"; private static final String SYSTEM_JDK_VENDOR = "adoptopenjdk"; - private static final String GRADLE_JDK_VERSION = "17+35"; + private static final String GRADLE_JDK_VERSION = "17.0.1+12"; private static final String GRADLE_JDK_VENDOR = "adoptium"; // all distributions used by distro tests. this is temporary until tests are per distribution diff --git a/buildSrc/src/main/java/org/opensearch/gradle/testclusters/OpenSearchCluster.java b/buildSrc/src/main/java/org/opensearch/gradle/testclusters/OpenSearchCluster.java index 1e3d714fe186b..9e6984fd45007 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/testclusters/OpenSearchCluster.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/testclusters/OpenSearchCluster.java @@ -103,10 +103,9 @@ public OpenSearchCluster( this.nodes = project.container(OpenSearchNode.class); this.bwcJdk = bwcJdk; - this.nodes.add( - new OpenSearchNode(path, clusterName + "-0", project, reaper, fileSystemOperations, archiveOperations, workingDirBase, bwcJdk) - ); - // configure the cluster name eagerly so nodes know about it + // Always add the first node + addNode(clusterName + "-0"); + // configure the cluster name eagerly so all nodes know about it this.nodes.all((node) -> node.defaultConfig.put("cluster.name", safeName(clusterName))); addWaitForClusterHealth(); @@ -126,21 +125,26 @@ public void setNumberOfNodes(int numberOfNodes) { } for (int i = nodes.size(); i < numberOfNodes; i++) { - this.nodes.add( - new OpenSearchNode( - path, - clusterName + "-" + i, - project, - reaper, - fileSystemOperations, - archiveOperations, - workingDirBase, - bwcJdk - ) - ); + addNode(clusterName + "-" + i); } } + private void addNode(String nodeName) { + OpenSearchNode newNode = new OpenSearchNode( + path, + nodeName, + project, + reaper, + fileSystemOperations, + archiveOperations, + workingDirBase, + bwcJdk + ); + // configure the cluster name eagerly + newNode.defaultConfig.put("cluster.name", safeName(clusterName)); + this.nodes.add(newNode); + } + @Internal OpenSearchNode getFirstNode() { return nodes.getAt(clusterName + "-0"); diff --git a/buildSrc/src/main/java/org/opensearch/gradle/testclusters/OpenSearchNode.java b/buildSrc/src/main/java/org/opensearch/gradle/testclusters/OpenSearchNode.java index 17f8d455f6a27..be7a5352e31ce 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/testclusters/OpenSearchNode.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/testclusters/OpenSearchNode.java @@ -116,11 +116,7 @@ public class OpenSearchNode implements TestClusterConfiguration { private static final TimeUnit NODE_UP_TIMEOUT_UNIT = TimeUnit.MINUTES; private static final int ADDITIONAL_CONFIG_TIMEOUT = 15; private static final TimeUnit ADDITIONAL_CONFIG_TIMEOUT_UNIT = TimeUnit.SECONDS; - private static final List OVERRIDABLE_SETTINGS = Arrays.asList( - "path.repo", - "discovery.seed_providers" - - ); + private static final List OVERRIDABLE_SETTINGS = Arrays.asList("path.repo", "discovery.seed_providers", "discovery.seed_hosts"); private static final int TAIL_LOG_MESSAGES_COUNT = 40; private static final List MESSAGES_WE_DONT_CARE_ABOUT = Arrays.asList( @@ -1385,25 +1381,27 @@ private Path getExtractedDistributionDir() { return distributions.get(currentDistro).getExtracted().getSingleFile().toPath(); } - private List getInstalledFileSet(Action filter) { - return Stream.concat(plugins.stream().map(Provider::get), modules.stream().map(Provider::get)) - .filter(File::exists) - // TODO: We may be able to simplify this with Gradle 5.6 - // https://docs.gradle.org/nightly/release-notes.html#improved-handling-of-zip-archives-on-classpaths - .map(zipFile -> archiveOperations.zipTree(zipFile).matching(filter)) - .flatMap(tree -> tree.getFiles().stream()) - .sorted(Comparator.comparing(File::getName)) + private List>> getInstalledFileSet(Action filter) { + return Stream.concat(plugins.stream(), modules.stream()).map(p -> p.map(f -> { + if (f.exists()) { + final FileTree tree = archiveOperations.zipTree(f).matching(filter); + return tree.getFiles(); + } else { + return new HashSet(); + } + })) + .map(p -> p.map(f -> f.stream().sorted(Comparator.comparing(File::getName)).collect(Collectors.toList()))) .collect(Collectors.toList()); } @Classpath - public List getInstalledClasspath() { + public List>> getInstalledClasspath() { return getInstalledFileSet(filter -> filter.include("**/*.jar")); } @InputFiles @PathSensitive(PathSensitivity.RELATIVE) - public List getInstalledFiles() { + public List>> getInstalledFiles() { return getInstalledFileSet(filter -> filter.exclude("**/*.jar")); } diff --git a/buildSrc/src/main/java/org/opensearch/gradle/testclusters/RunTask.java b/buildSrc/src/main/java/org/opensearch/gradle/testclusters/RunTask.java index 20128620c5dae..01680050cf18f 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/testclusters/RunTask.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/testclusters/RunTask.java @@ -52,10 +52,17 @@ import java.util.function.Function; import java.util.stream.Collectors; +/** + * Implementation of the "run" Gradle task used in run.gradle + */ public class RunTask extends DefaultTestClustersTask { private static final Logger logger = Logging.getLogger(RunTask.class); public static final String CUSTOM_SETTINGS_PREFIX = "tests.opensearch."; + private static final int DEFAULT_HTTP_PORT = 9200; + private static final int DEFAULT_TRANSPORT_PORT = 9300; + private static final int DEFAULT_DEBUG_PORT = 5005; + public static final String LOCALHOST_ADDRESS_PREFIX = "127.0.0.1:"; private Boolean debug = false; @@ -112,9 +119,9 @@ public String getDataDir() { @Override public void beforeStart() { - int debugPort = 5005; - int httpPort = 9200; - int transportPort = 9300; + int debugPort = DEFAULT_DEBUG_PORT; + int httpPort = DEFAULT_HTTP_PORT; + int transportPort = DEFAULT_TRANSPORT_PORT; Map additionalSettings = System.getProperties() .entrySet() .stream() @@ -134,12 +141,22 @@ public void beforeStart() { } for (OpenSearchCluster cluster : getClusters()) { - cluster.getFirstNode().setHttpPort(String.valueOf(httpPort)); + // Configure the first node with the default ports first + OpenSearchNode firstNode = cluster.getFirstNode(); + firstNode.setHttpPort(String.valueOf(httpPort)); httpPort++; - cluster.getFirstNode().setTransportPort(String.valueOf(transportPort)); + firstNode.setTransportPort(String.valueOf(transportPort)); transportPort++; + firstNode.setting("discovery.seed_hosts", LOCALHOST_ADDRESS_PREFIX + DEFAULT_TRANSPORT_PORT); cluster.setPreserveDataDir(preserveData); for (OpenSearchNode node : cluster.getNodes()) { + if (node != firstNode) { + node.setHttpPort(String.valueOf(httpPort)); + httpPort++; + node.setTransportPort(String.valueOf(transportPort)); + transportPort++; + node.setting("discovery.seed_hosts", LOCALHOST_ADDRESS_PREFIX + DEFAULT_TRANSPORT_PORT); + } additionalSettings.forEach(node::setting); if (dataDir != null) { node.setDataPath(getDataPath.apply(node)); diff --git a/buildSrc/src/main/java/org/opensearch/gradle/testfixtures/TestFixturesPlugin.java b/buildSrc/src/main/java/org/opensearch/gradle/testfixtures/TestFixturesPlugin.java index d6a13f550dffb..2872f9d984c19 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/testfixtures/TestFixturesPlugin.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/testfixtures/TestFixturesPlugin.java @@ -112,27 +112,42 @@ public void apply(Project project) { project.getPluginManager().apply(BasePlugin.class); project.getPluginManager().apply(DockerComposePlugin.class); - TaskProvider preProcessFixture = project.getTasks().register("preProcessFixture", t -> { - t.getOutputs().dir(testfixturesDir); - t.doFirst(t2 -> { - try { - Files.createDirectories(testfixturesDir.toPath()); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }); + TaskProvider preProcessFixture = project.getTasks().register("preProcessFixture", new Action() { + @Override + public void execute(Task t) { + t.getOutputs().dir(testfixturesDir); + t.doFirst(new Action() { + @Override + public void execute(Task t2) { + { + try { + Files.createDirectories(testfixturesDir.toPath()); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + } + }); + } + }); + TaskProvider buildFixture = project.getTasks().register("buildFixture", new Action() { + @Override + public void execute(Task t) { + t.dependsOn(preProcessFixture, tasks.named("composeUp")); + } }); - TaskProvider buildFixture = project.getTasks() - .register("buildFixture", t -> t.dependsOn(preProcessFixture, tasks.named("composeUp"))); - - TaskProvider postProcessFixture = project.getTasks().register("postProcessFixture", task -> { - task.dependsOn(buildFixture); - configureServiceInfoForTask( - task, - project, - false, - (name, port) -> task.getExtensions().getByType(ExtraPropertiesExtension.class).set(name, port) - ); + + TaskProvider postProcessFixture = project.getTasks().register("postProcessFixture", new Action() { + @Override + public void execute(Task task) { + task.dependsOn(buildFixture); + configureServiceInfoForTask( + task, + project, + false, + (name, port) -> task.getExtensions().getByType(ExtraPropertiesExtension.class).set(name, port) + ); + } }); maybeSkipTask(dockerSupport, preProcessFixture); diff --git a/buildSrc/src/main/java/org/opensearch/gradle/util/GradleUtils.java b/buildSrc/src/main/java/org/opensearch/gradle/util/GradleUtils.java index 772e52a48a6f2..01622e6df3ee1 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/util/GradleUtils.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/util/GradleUtils.java @@ -187,9 +187,9 @@ public static void setupIdeForTestSourceSet(Project project, SourceSet testSourc */ public static void extendSourceSet(Project project, String parentSourceSetName, String childSourceSetName) { final List> configNameFunctions = Arrays.asList( - SourceSet::getCompileConfigurationName, + SourceSet::getCompileClasspathConfigurationName, SourceSet::getImplementationConfigurationName, - SourceSet::getRuntimeConfigurationName, + SourceSet::getRuntimeClasspathConfigurationName, SourceSet::getRuntimeOnlyConfigurationName ); SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); diff --git a/buildSrc/src/main/resources/checkstyle.xml b/buildSrc/src/main/resources/checkstyle.xml deleted file mode 100644 index e77a3efbfe573..0000000000000 --- a/buildSrc/src/main/resources/checkstyle.xml +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml deleted file mode 100644 index 879181365d524..0000000000000 --- a/buildSrc/src/main/resources/checkstyle_suppressions.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/buildSrc/src/test/java/org/opensearch/gradle/checkstyle/SnipptLengthCheckTests.java b/buildSrc/src/test/java/org/opensearch/gradle/checkstyle/SnipptLengthCheckTests.java deleted file mode 100644 index b25bdd045ddd2..0000000000000 --- a/buildSrc/src/test/java/org/opensearch/gradle/checkstyle/SnipptLengthCheckTests.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you 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. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.gradle.checkstyle; - -import org.opensearch.gradle.test.GradleUnitTestCase; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.function.BiConsumer; - -import static java.util.Collections.singletonList; -import static org.hamcrest.CoreMatchers.equalTo; - -public class SnipptLengthCheckTests extends GradleUnitTestCase { - public void testNoSnippets() { - SnippetLengthCheck.checkFile(failOnError(), 10, "There a no snippets"); - } - - public void testEmptySnippet() { - SnippetLengthCheck.checkFile(failOnError(), 10, "// tag::foo", "// end::foo"); - } - - public void testSnippetWithSmallText() { - SnippetLengthCheck.checkFile(failOnError(), 10, "// tag::foo", "some words", "// end::foo"); - } - - public void testSnippetWithLeadingSpaces() { - SnippetLengthCheck.checkFile(failOnError(), 10, " // tag::foo", " some words", " // end::foo"); - } - - public void testSnippetWithEmptyLine() { - SnippetLengthCheck.checkFile(failOnError(), 10, " // tag::foo", "", " some words", " // end::foo"); - } - - public void testSnippetBrokenLeadingSpaces() { - List collection = new ArrayList<>(); - SnippetLengthCheck.checkFile(collect(collection), 10, " // tag::foo", "some words", " // end::foo"); - assertThat(collection, equalTo(singletonList("2: snippet line should start with [ ]"))); - } - - public void testSnippetTooLong() { - List collection = new ArrayList<>(); - SnippetLengthCheck.checkFile(collect(collection), 10, " // tag::foo", " too long words", " // end::foo"); - assertThat(collection, equalTo(singletonList("2: snippet line should be no more than [10] characters but was [14]"))); - } - - public void testLotsOfErrors() { - List collection = new ArrayList<>(); - SnippetLengthCheck.checkFile(collect(collection), 10, " // tag::foo", "asdfadf", " too long words", "asdfadf", " // end::foo"); - assertThat( - collection, - equalTo( - Arrays.asList( - "2: snippet line should start with [ ]", - "3: snippet line should be no more than [10] characters but was [14]", - "4: snippet line should start with [ ]" - ) - ) - ); - } - - private BiConsumer failOnError() { - return (line, message) -> fail("checkstyle error on line [" + line + "] with message [" + message + "]"); - } - - private BiConsumer collect(List collection) { - return (line, message) -> collection.add(line + ": " + message); - } -} diff --git a/buildSrc/src/test/java/org/opensearch/gradle/precommit/DependencyLicensesTaskTests.java b/buildSrc/src/test/java/org/opensearch/gradle/precommit/DependencyLicensesTaskTests.java index 59f7a807476a0..6bb6b90116394 100644 --- a/buildSrc/src/test/java/org/opensearch/gradle/precommit/DependencyLicensesTaskTests.java +++ b/buildSrc/src/test/java/org/opensearch/gradle/precommit/DependencyLicensesTaskTests.java @@ -95,7 +95,7 @@ public void givenProjectWithoutLicensesDirButWithDependenciesThenShouldThrowExce expectedException.expect(GradleException.class); expectedException.expectMessage(containsString("does not exist, but there are dependencies")); - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); task.get().checkDependencies(); } @@ -110,10 +110,10 @@ public void givenProjectWithDependencyButNoShaFileThenShouldReturnException() th expectedException.expectMessage(containsString("Missing SHA for ")); File licensesDir = getLicensesDir(project); - createFileIn(licensesDir, "groovy-all-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); - createFileIn(licensesDir, "groovy-all-NOTICE.txt", ""); + createFileIn(licensesDir, "groovy-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); + createFileIn(licensesDir, "groovy-NOTICE.txt", ""); - project.getDependencies().add("compile", project.getDependencies().localGroovy()); + project.getDependencies().add("compileClasspath", project.getDependencies().localGroovy()); task.get().checkDependencies(); } @@ -122,7 +122,7 @@ public void givenProjectWithDependencyButNoLicenseFileThenShouldReturnException( expectedException.expect(GradleException.class); expectedException.expectMessage(containsString("Missing LICENSE for ")); - project.getDependencies().add("compile", project.getDependencies().localGroovy()); + project.getDependencies().add("compileClasspath", project.getDependencies().localGroovy()); getLicensesDir(project).mkdir(); updateShas.updateShas(); @@ -134,9 +134,9 @@ public void givenProjectWithDependencyButNoNoticeFileThenShouldReturnException() expectedException.expect(GradleException.class); expectedException.expectMessage(containsString("Missing NOTICE for ")); - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); - createFileIn(getLicensesDir(project), "groovy-all-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); + createFileIn(getLicensesDir(project), "groovy-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); updateShas.updateShas(); task.get().checkDependencies(); @@ -147,10 +147,10 @@ public void givenProjectWithStrictDependencyButNoSourcesFileThenShouldReturnExce expectedException.expect(GradleException.class); expectedException.expectMessage(containsString("Missing SOURCES for ")); - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); - createFileIn(getLicensesDir(project), "groovy-all-LICENSE.txt", STRICT_LICENSE_TEXT); - createFileIn(getLicensesDir(project), "groovy-all-NOTICE.txt", ""); + createFileIn(getLicensesDir(project), "groovy-LICENSE.txt", STRICT_LICENSE_TEXT); + createFileIn(getLicensesDir(project), "groovy-NOTICE.txt", ""); updateShas.updateShas(); task.get().checkDependencies(); @@ -158,11 +158,15 @@ public void givenProjectWithStrictDependencyButNoSourcesFileThenShouldReturnExce @Test public void givenProjectWithStrictDependencyAndEverythingInOrderThenShouldReturnSilently() throws Exception { - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); - createFileIn(getLicensesDir(project), "groovy-all-LICENSE.txt", STRICT_LICENSE_TEXT); - createFileIn(getLicensesDir(project), "groovy-all-NOTICE.txt", ""); - createFileIn(getLicensesDir(project), "groovy-all-SOURCES.txt", ""); + createFileIn(getLicensesDir(project), "groovy-LICENSE.txt", STRICT_LICENSE_TEXT); + createFileIn(getLicensesDir(project), "groovy-NOTICE.txt", ""); + createFileIn(getLicensesDir(project), "groovy-SOURCES.txt", ""); + + createFileIn(getLicensesDir(project), "javaparser-core-LICENSE.txt", STRICT_LICENSE_TEXT); + createFileIn(getLicensesDir(project), "javaparser-core-NOTICE.txt", ""); + createFileIn(getLicensesDir(project), "javaparser-core-SOURCES.txt", ""); updateShas.updateShas(); task.get().checkDependencies(); @@ -170,11 +174,11 @@ public void givenProjectWithStrictDependencyAndEverythingInOrderThenShouldReturn @Test public void givenProjectWithDependencyAndEverythingInOrderThenShouldReturnSilently() throws Exception { - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); File licensesDir = getLicensesDir(project); - createAllDefaultDependencyFiles(licensesDir, "groovy-all"); + createAllDefaultDependencyFiles(licensesDir, "groovy", "javaparser-core"); task.get().checkDependencies(); } @@ -183,10 +187,10 @@ public void givenProjectWithALicenseButWithoutTheDependencyThenShouldThrowExcept expectedException.expect(GradleException.class); expectedException.expectMessage(containsString("Unused license ")); - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); File licensesDir = getLicensesDir(project); - createAllDefaultDependencyFiles(licensesDir, "groovy-all"); + createAllDefaultDependencyFiles(licensesDir, "groovy", "javaparser-core"); createFileIn(licensesDir, "non-declared-LICENSE.txt", ""); task.get().checkDependencies(); @@ -197,10 +201,10 @@ public void givenProjectWithANoticeButWithoutTheDependencyThenShouldThrowExcepti expectedException.expect(GradleException.class); expectedException.expectMessage(containsString("Unused notice ")); - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); File licensesDir = getLicensesDir(project); - createAllDefaultDependencyFiles(licensesDir, "groovy-all"); + createAllDefaultDependencyFiles(licensesDir, "groovy", "javaparser-core"); createFileIn(licensesDir, "non-declared-NOTICE.txt", ""); task.get().checkDependencies(); @@ -211,10 +215,10 @@ public void givenProjectWithAShaButWithoutTheDependencyThenShouldThrowException( expectedException.expect(GradleException.class); expectedException.expectMessage(containsString("Unused sha files found: \n")); - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); File licensesDir = getLicensesDir(project); - createAllDefaultDependencyFiles(licensesDir, "groovy-all"); + createAllDefaultDependencyFiles(licensesDir, "groovy", "javaparser-core"); createFileIn(licensesDir, "non-declared.sha1", ""); task.get().checkDependencies(); @@ -225,10 +229,10 @@ public void givenProjectWithADependencyWithWrongShaThenShouldThrowException() th expectedException.expect(GradleException.class); expectedException.expectMessage(containsString("SHA has changed! Expected ")); - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); File licensesDir = getLicensesDir(project); - createAllDefaultDependencyFiles(licensesDir, "groovy-all"); + createAllDefaultDependencyFiles(licensesDir, "groovy"); Path groovySha = Files.list(licensesDir.toPath()).filter(file -> file.toFile().getName().contains("sha")).findFirst().get(); @@ -239,14 +243,14 @@ public void givenProjectWithADependencyWithWrongShaThenShouldThrowException() th @Test public void givenProjectWithADependencyMappingThenShouldReturnSilently() throws Exception { - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); File licensesDir = getLicensesDir(project); - createAllDefaultDependencyFiles(licensesDir, "groovy"); + createAllDefaultDependencyFiles(licensesDir, "groovy", "javaparser"); Map mappings = new HashMap<>(); - mappings.put("from", "groovy-all"); - mappings.put("to", "groovy"); + mappings.put("from", "javaparser-core"); + mappings.put("to", "javaparser"); task.get().mapping(mappings); task.get().checkDependencies(); @@ -254,13 +258,30 @@ public void givenProjectWithADependencyMappingThenShouldReturnSilently() throws @Test public void givenProjectWithAIgnoreShaConfigurationAndNoShaFileThenShouldReturnSilently() throws Exception { - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); File licensesDir = getLicensesDir(project); - createFileIn(licensesDir, "groovy-all-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); - createFileIn(licensesDir, "groovy-all-NOTICE.txt", ""); + createFileIn(licensesDir, "groovy-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); + createFileIn(licensesDir, "groovy-NOTICE.txt", ""); + + createFileIn(licensesDir, "javaparser-core-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); + createFileIn(licensesDir, "javaparser-core-NOTICE.txt", ""); + + task.get().ignoreSha("groovy"); + task.get().ignoreSha("groovy-ant"); + task.get().ignoreSha("groovy-astbuilder"); + task.get().ignoreSha("groovy-console"); + task.get().ignoreSha("groovy-datetime"); + task.get().ignoreSha("groovy-dateutil"); + task.get().ignoreSha("groovy-groovydoc"); + task.get().ignoreSha("groovy-json"); + task.get().ignoreSha("groovy-nio"); + task.get().ignoreSha("groovy-sql"); + task.get().ignoreSha("groovy-templates"); + task.get().ignoreSha("groovy-test"); + task.get().ignoreSha("groovy-xml"); + task.get().ignoreSha("javaparser-core"); - task.get().ignoreSha("groovy-all"); task.get().checkDependencies(); } @@ -279,9 +300,11 @@ private Project createProject() { return project; } - private void createAllDefaultDependencyFiles(File licensesDir, String dependencyName) throws IOException, NoSuchAlgorithmException { - createFileIn(licensesDir, dependencyName + "-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); - createFileIn(licensesDir, dependencyName + "-NOTICE.txt", ""); + private void createAllDefaultDependencyFiles(File licensesDir, String... dependencyNames) throws IOException, NoSuchAlgorithmException { + for (final String dependencyName : dependencyNames) { + createFileIn(licensesDir, dependencyName + "-LICENSE.txt", PERMISSIVE_LICENSE_TEXT); + createFileIn(licensesDir, dependencyName + "-NOTICE.txt", ""); + } updateShas.updateShas(); } @@ -316,6 +339,12 @@ private TaskProvider createDependencyLicensesTask(Projec @Override public void execute(DependencyLicensesTask dependencyLicensesTask) { dependencyLicensesTask.setDependencies(getDependencies(project)); + + final Map mappings = new HashMap<>(); + mappings.put("from", "groovy-.*"); + mappings.put("to", "groovy"); + + dependencyLicensesTask.mapping(mappings); } }); @@ -323,6 +352,6 @@ public void execute(DependencyLicensesTask dependencyLicensesTask) { } private FileCollection getDependencies(Project project) { - return project.getConfigurations().getByName("compile"); + return project.getConfigurations().getByName("compileClasspath"); } } diff --git a/buildSrc/src/test/java/org/opensearch/gradle/precommit/UpdateShasTaskTests.java b/buildSrc/src/test/java/org/opensearch/gradle/precommit/UpdateShasTaskTests.java index 084b3b9149e33..ded9c2e3bcddf 100644 --- a/buildSrc/src/test/java/org/opensearch/gradle/precommit/UpdateShasTaskTests.java +++ b/buildSrc/src/test/java/org/opensearch/gradle/precommit/UpdateShasTaskTests.java @@ -86,19 +86,19 @@ public void whenDependencyDoesntExistThenShouldDeleteDependencySha() throws IOEx @Test public void whenDependencyExistsButShaNotThenShouldCreateNewShaFile() throws IOException, NoSuchAlgorithmException { - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); getLicensesDir(project).mkdir(); task.updateShas(); Path groovySha = Files.list(getLicensesDir(project).toPath()).findFirst().get(); - assertTrue(groovySha.toFile().getName().startsWith("groovy-all")); + assertTrue(groovySha.toFile().getName().startsWith("groovy-")); } @Test public void whenDependencyAndWrongShaExistsThenShouldNotOverwriteShaFile() throws IOException, NoSuchAlgorithmException { - project.getDependencies().add("compile", dependency); + project.getDependencies().add("compileClasspath", dependency); File groovyJar = task.getParentTask().getDependencies().getFiles().iterator().next(); String groovyShaName = groovyJar.getName() + ".sha1"; @@ -163,6 +163,6 @@ public void execute(DependencyLicensesTask dependencyLicensesTask) { } private FileCollection getDependencies(Project project) { - return project.getConfigurations().getByName("compile"); + return project.getConfigurations().getByName("compileClasspath"); } } diff --git a/buildSrc/src/testFixtures/java/org/opensearch/gradle/test/GradleThreadsFilter.java b/buildSrc/src/testFixtures/java/org/opensearch/gradle/test/GradleThreadsFilter.java index 8f930bacdfa08..b64c719440733 100644 --- a/buildSrc/src/testFixtures/java/org/opensearch/gradle/test/GradleThreadsFilter.java +++ b/buildSrc/src/testFixtures/java/org/opensearch/gradle/test/GradleThreadsFilter.java @@ -43,6 +43,8 @@ public class GradleThreadsFilter implements ThreadFilter { @Override public boolean reject(Thread t) { - return t.getName().startsWith("Exec process") || t.getName().startsWith("File watcher consumer"); + return t.getName().startsWith("Exec process") + || t.getName().startsWith("Memory manager") + || t.getName().startsWith("File watcher consumer"); } } diff --git a/buildSrc/src/testKit/opensearch-build-resources/build.gradle b/buildSrc/src/testKit/opensearch-build-resources/build.gradle index b94bbe884b87d..3d939ccffc6e3 100644 --- a/buildSrc/src/testKit/opensearch-build-resources/build.gradle +++ b/buildSrc/src/testKit/opensearch-build-resources/build.gradle @@ -19,8 +19,6 @@ plugins { File buildResourcesDir = new File(project.getBuildDir(), 'build-tools-exported') TaskProvider buildResourcesTask = tasks.register('buildResources', ExportOpenSearchBuildResourcesTask) { outputDir = buildResourcesDir - copy 'checkstyle_suppressions.xml' - copy 'checkstyle.xml' } tasks.register("sampleCopy", Sync) { diff --git a/buildSrc/version.properties b/buildSrc/version.properties index 270b2cb236048..7b172165abefb 100644 --- a/buildSrc/version.properties +++ b/buildSrc/version.properties @@ -1,10 +1,10 @@ opensearch = 2.0.0 -lucene = 8.9.0 +lucene = 8.10.1 bundled_jdk_vendor = adoptium -bundled_jdk = 17+35 +bundled_jdk = 17.0.1+12 + -checkstyle = 8.29 # optional dependencies spatial4j = 0.7 diff --git a/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java index acafcf96c2a40..f0f33ae1e71fe 100644 --- a/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java @@ -363,6 +363,7 @@ static Request index(IndexRequest indexRequest) { parameters.withPipeline(indexRequest.getPipeline()); parameters.withRefreshPolicy(indexRequest.getRefreshPolicy()); parameters.withWaitForActiveShards(indexRequest.waitForActiveShards()); + parameters.withRequireAlias(indexRequest.isRequireAlias()); BytesRef source = indexRequest.source().toBytesRef(); ContentType contentType = createContentType(indexRequest.getContentType()); @@ -391,6 +392,7 @@ static Request update(UpdateRequest updateRequest) throws IOException { parameters.withRetryOnConflict(updateRequest.retryOnConflict()); parameters.withVersion(updateRequest.version()); parameters.withVersionType(updateRequest.versionType()); + parameters.withRequireAlias(updateRequest.isRequireAlias()); // The Java API allows update requests with different content types // set for the partial document and the upsert document. This client @@ -618,6 +620,7 @@ private static Request prepareReindexRequest(ReindexRequest reindexRequest, bool .withTimeout(reindexRequest.getTimeout()) .withWaitForActiveShards(reindexRequest.getWaitForActiveShards()) .withRequestsPerSecond(reindexRequest.getRequestsPerSecond()) + .withRequireAlias(reindexRequest.getDestination().isRequireAlias()) .withSlices(reindexRequest.getSlices()); if (reindexRequest.getScrollTime() != null) { @@ -964,6 +967,13 @@ Params withRequestsPerSecond(float requestsPerSecond) { } } + Params withRequireAlias(boolean requireAlias) { + if (requireAlias) { + return putParam("require_alias", Boolean.TRUE.toString()); + } + return this; + } + Params withRetryOnConflict(int retryOnConflict) { if (retryOnConflict > 0) { return putParam("retry_on_conflict", String.valueOf(retryOnConflict)); diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java index 56d4635772631..69602bb766d2f 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java @@ -686,6 +686,21 @@ public void testIndex() throws IOException { exception.getMessage() ); } + { + OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> { + IndexRequest indexRequest = new IndexRequest("index").id("missing_alias").setRequireAlias(true); + indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("field", "test").endObject()); + + execute(indexRequest, highLevelClient()::index, highLevelClient()::indexAsync); + }); + + assertEquals(RestStatus.NOT_FOUND, exception.status()); + assertEquals( + "OpenSearch exception [type=index_not_found_exception, reason=no such index [index]" + + " and [require_alias] request flag is [true] and [index] is not an alias]", + exception.getMessage() + ); + } { IndexRequest indexRequest = new IndexRequest("index").id("external_version_type"); indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("field", "test").endObject()); @@ -926,6 +941,18 @@ public void testUpdate() throws IOException { exception.getMessage() ); } + { + OpenSearchException exception = expectThrows(OpenSearchException.class, () -> { + UpdateRequest updateRequest = new UpdateRequest("index", "require_alias").setRequireAlias(true); + updateRequest.doc(new IndexRequest().source(Collections.singletonMap("field", "doc"), XContentType.JSON)); + execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync); + }); + assertEquals(RestStatus.NOT_FOUND, exception.status()); + assertEquals( + "OpenSearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias] request flag is [true] and [index] is not an alias]", + exception.getMessage() + ); + } } public void testUpdateWithTypes() throws IOException { diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/ReindexIT.java b/client/rest-high-level/src/test/java/org/opensearch/client/ReindexIT.java index 5576b9d5ef20c..78fb2aef02ee0 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/ReindexIT.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/ReindexIT.java @@ -32,6 +32,7 @@ package org.opensearch.client; +import org.opensearch.OpenSearchStatusException; import org.opensearch.action.ActionListener; import org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse; import org.opensearch.action.bulk.BulkItemResponse; @@ -102,6 +103,25 @@ public void testReindex() throws IOException { assertEquals(0, bulkResponse.getBulkFailures().size()); assertEquals(0, bulkResponse.getSearchFailures().size()); } + { + // set require_alias=true, but there exists no alias + ReindexRequest reindexRequest = new ReindexRequest(); + reindexRequest.setSourceIndices(sourceIndex); + reindexRequest.setDestIndex(destinationIndex); + reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1")); + reindexRequest.setRefresh(true); + reindexRequest.setRequireAlias(true); + + OpenSearchStatusException exception = expectThrows( + OpenSearchStatusException.class, + () -> { execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync); } + ); + assertEquals(RestStatus.NOT_FOUND, exception.status()); + assertEquals( + "OpenSearch exception [type=index_not_found_exception, reason=no such index [dest] and [require_alias] request flag is [true] and [dest] is not an alias]", + exception.getMessage() + ); + } } public void testReindexTask() throws Exception { diff --git a/distribution/docker/build.gradle b/distribution/docker/build.gradle index 72d44a17b4eaa..58840e9e5944e 100644 --- a/distribution/docker/build.gradle +++ b/distribution/docker/build.gradle @@ -16,6 +16,8 @@ import org.opensearch.gradle.VersionProperties import org.opensearch.gradle.docker.DockerBuildTask import org.opensearch.gradle.info.BuildParams import org.opensearch.gradle.testfixtures.TestFixturesPlugin +import org.gradle.internal.os.OperatingSystem + apply plugin: 'opensearch.standalone-rest-test' apply plugin: 'opensearch.test.fixtures' apply plugin: 'opensearch.internal-distribution-download' @@ -183,7 +185,7 @@ void addBuildDockerImage(Architecture architecture, DockerBase base) { final TaskProvider buildDockerImageTask = tasks.register(taskName("build", architecture, base, "DockerImage"), DockerBuildTask) { - onlyIf { Architecture.current() == architecture } + onlyIf { Architecture.current() == architecture && !OperatingSystem.current().isWindows() } TaskProvider copyContextTask = tasks.named(taskName("copy", architecture, base, "DockerContext")) dependsOn(copyContextTask) dockerContext.fileProvider(copyContextTask.map { it.destinationDir }) @@ -246,7 +248,7 @@ subprojects { Project subProject -> "opensearch:test" dependsOn(parent.path + ":" + buildTaskName) - onlyIf { Architecture.current() == architecture } + onlyIf { Architecture.current() == architecture && !OperatingSystem.current().isWindows()} } artifacts.add('default', file(tarFile)) { diff --git a/distribution/packages/build.gradle b/distribution/packages/build.gradle index 921439f750205..e5c75af5188e5 100644 --- a/distribution/packages/build.gradle +++ b/distribution/packages/build.gradle @@ -63,7 +63,7 @@ import java.util.regex.Pattern */ plugins { - id "nebula.ospackage-base" version "8.3.0" + id "nebula.ospackage-base" version "9.0.0" } void addProcessFilesTask(String type, boolean jdk) { @@ -136,9 +136,9 @@ Closure commonPackageConfig(String type, boolean jdk, String architecture) { // SystemPackagingTask overrides default archive task convention mappings, but doesn't provide a setter so we have to override the convention mapping itself // Deb convention uses a '_' for final separator before architecture, rpm uses a '.' if (type == 'deb') { - conventionMapping.archiveFile = { objects.fileProperty().fileValue(file("${destinationDirectory.get()}/${packageName}-min_${project.version}${jdkString}_${archString}.${type}")) } + archiveFileName.value(project.provider({ "${destinationDirectory.get()}/${packageName}-min_${project.version}${jdkString}_${archString}.${type}" })) } else { - conventionMapping.archiveFile = { objects.fileProperty().fileValue(file("${destinationDirectory.get()}/${packageName}-min-${project.version}${jdkString}.${archString}.${type}")) } + archiveFileName.value(project.provider({ "${destinationDirectory.get()}/${packageName}-min-${project.version}${jdkString}.${archString}.${type}" })) } String packagingFiles = "build/packaging/${jdk ? '' : 'no-jdk-'}${type}" diff --git a/distribution/tools/keystore-cli/build.gradle b/distribution/tools/keystore-cli/build.gradle index 90ea5a152c9cd..670c898019d28 100644 --- a/distribution/tools/keystore-cli/build.gradle +++ b/distribution/tools/keystore-cli/build.gradle @@ -35,5 +35,5 @@ dependencies { compileOnly project(":libs:opensearch-cli") testImplementation project(":test:framework") testImplementation 'com.google.jimfs:jimfs:1.1' - testRuntimeOnly 'com.google.guava:guava:18.0' + testRuntimeOnly 'com.google.guava:guava:30.1.1-jre' } diff --git a/distribution/tools/plugin-cli/build.gradle b/distribution/tools/plugin-cli/build.gradle index 21f59a8777488..be601924b75b7 100644 --- a/distribution/tools/plugin-cli/build.gradle +++ b/distribution/tools/plugin-cli/build.gradle @@ -39,7 +39,7 @@ dependencies { api "org.bouncycastle:bc-fips:1.0.2" testImplementation project(":test:framework") testImplementation 'com.google.jimfs:jimfs:1.1' - testRuntimeOnly 'com.google.guava:guava:18.0' + testRuntimeOnly 'com.google.guava:guava:30.1.1-jre' } tasks.named("dependencyLicenses").configure { diff --git a/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/DetectEsInstallationTaskTests.java b/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/DetectEsInstallationTaskTests.java index fcabf24e9bd35..1038e6d4a484f 100644 --- a/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/DetectEsInstallationTaskTests.java +++ b/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/DetectEsInstallationTaskTests.java @@ -49,7 +49,7 @@ public void testTaskExecution() throws Exception { task.accept(input); assertThat(taskInput.getEsConfig(), is(esConfig)); - assertThat(taskInput.getBaseUrl(), is("http://localhost:9200")); + assertThat(taskInput.getBaseUrl(), is("http://localhost:42123")); assertThat(taskInput.getPlugins(), hasSize(0)); assertThat(taskInput.getNode(), is("node-x")); assertThat(taskInput.getCluster(), is("my-cluster")); diff --git a/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/ImportYmlConfigTaskTests.java b/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/ImportYmlConfigTaskTests.java index ac30b2034b144..f328cc21685ad 100644 --- a/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/ImportYmlConfigTaskTests.java +++ b/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/ImportYmlConfigTaskTests.java @@ -59,8 +59,9 @@ public void testImportYmlConfigTask() throws IOException { taskInput.setEsConfig(esConfig); task.accept(new Tuple<>(taskInput, terminal)); Settings settings = Settings.builder().loadFromPath(taskInput.getOpenSearchConfig().resolve("opensearch.yml")).build(); - assertThat(settings.keySet(), contains("cluster.name", "node.name", "path.data", "path.logs")); + assertThat(settings.keySet(), contains("cluster.name", "http.port", "node.name", "path.data", "path.logs")); assertThat(settings.get("cluster.name"), is("my-cluster")); + assertThat(settings.get("http.port"), is("42123")); assertThat(settings.get("node.name"), is("node-x")); assertThat(settings.get("path.data"), is("[/mnt/data_1, /mnt/data_2]")); assertThat(settings.get("path.logs"), is("/var/log/eslogs")); diff --git a/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/UpgradeCliTests.java b/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/UpgradeCliTests.java index 863313d8b417b..3db782925a660 100644 --- a/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/UpgradeCliTests.java +++ b/distribution/tools/upgrade-cli/src/test/java/org/opensearch/upgrade/UpgradeCliTests.java @@ -113,6 +113,7 @@ private void assertYmlConfigImported() throws IOException { Arrays.asList( "---", "cluster.name: \"my-cluster\"", + "http.port: \"42123\"", "node.name: \"node-x\"", "path.data:", "- \"/mnt/data_1\"", diff --git a/distribution/tools/upgrade-cli/src/test/resources/config/elasticsearch.yml b/distribution/tools/upgrade-cli/src/test/resources/config/elasticsearch.yml index 4262adfba6d01..083b4c7a7452b 100644 --- a/distribution/tools/upgrade-cli/src/test/resources/config/elasticsearch.yml +++ b/distribution/tools/upgrade-cli/src/test/resources/config/elasticsearch.yml @@ -5,3 +5,4 @@ path: - /mnt/data_1 - /mnt/data_2 logs: /var/log/eslogs +http.port: 42123 diff --git a/gradle.properties b/gradle.properties index cf8878164ad99..0974eb6d6b7da 100644 --- a/gradle.properties +++ b/gradle.properties @@ -26,4 +26,4 @@ systemProp.org.gradle.warning.mode=fail systemProp.jdk.tls.client.protocols=TLSv1.2 # jvm args for faster test execution by default -systemProp.tests.jvm.argline=-XX:TieredStopAtLevel=1 +systemProp.tests.jvm.argline=-XX:TieredStopAtLevel=1 -XX:ReservedCodeCacheSize=64m diff --git a/gradle/code-coverage.gradle b/gradle/code-coverage.gradle index 34fdf5d99ed84..de041eae7b72d 100644 --- a/gradle/code-coverage.gradle +++ b/gradle/code-coverage.gradle @@ -22,6 +22,7 @@ allprojects { def codeCoverageReportTask = tasks.register("codeCoverageReport", JacocoReport) { description = 'Generates aggregate report from all subprojects.' executionData.setFrom fileTree(dir: '.', include: '**/build/jacoco/*.exec') + dependsOn subprojects.findAll(s -> s.tasks.findByName('check') != null).check } tasks.register("codeCoverageReportForUnitTest", JacocoReport) { @@ -46,10 +47,12 @@ tasks.withType(JacocoReport).configureEach { def projectsWithJavaPlugin = subprojects.findAll { it.pluginManager.hasPlugin('java') } def projectsWithUnitTest = projectsWithJavaPlugin.findAll { it.tasks.findByName('test').enabled } def projectsWithIntegTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('integTest')} + def projectsWithAsyncIntegTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('asyncIntegTest')} def projectsWithInternalClusterTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('internalClusterTest')} + def projectsWithPooledInternalClusterTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('pooledInternalClusterTest')} def projectsWithJavaRestTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('javaRestTest')} def projectsWithYamlRestTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('yamlRestTest')} - def projectsWithIntegrationTest = projectsWithIntegTest + projectsWithInternalClusterTest + projectsWithJavaRestTest + projectsWithYamlRestTest + def projectsWithIntegrationTest = projectsWithIntegTest + projectsWithAsyncIntegTest + projectsWithInternalClusterTest + projectsWithPooledInternalClusterTest + projectsWithJavaRestTest + projectsWithYamlRestTest def projectsWithTest = projectsWithUnitTest + projectsWithIntegrationTest def selectedProjects @@ -60,7 +63,9 @@ tasks.withType(JacocoReport).configureEach { break case "codeCoverageReportForIntegrationTest": dependsOn projectsWithIntegTest.integTest + dependsOn projectsWithAsyncIntegTest.asyncIntegTest dependsOn projectsWithInternalClusterTest.internalClusterTest + dependsOn projectsWithPooledInternalClusterTest.pooledInternalClusterTest dependsOn projectsWithJavaRestTest.javaRestTest dependsOn projectsWithYamlRestTest.yamlRestTest selectedProjects = projectsWithIntegrationTest @@ -68,20 +73,23 @@ tasks.withType(JacocoReport).configureEach { default: dependsOn projectsWithUnitTest.test dependsOn projectsWithIntegTest.integTest + dependsOn projectsWithAsyncIntegTest.asyncIntegTest dependsOn projectsWithInternalClusterTest.internalClusterTest + dependsOn projectsWithPooledInternalClusterTest.pooledInternalClusterTest dependsOn projectsWithJavaRestTest.javaRestTest dependsOn projectsWithYamlRestTest.yamlRestTest - selectedProjects = projectsWithTest + selectedProjects = projectsWithJavaPlugin break } + sourceDirectories.setFrom files(selectedProjects.sourceSets.main.allSource.srcDirs) classDirectories.setFrom files(selectedProjects.sourceSets.main.output) reports { // Code coverage report in HTML and CSV formats are on demand, in case they take extra disk space. - xml.enabled System.getProperty('tests.coverage.report.xml', 'true').toBoolean() - html.enabled System.getProperty('tests.coverage.report.html', 'false').toBoolean() - csv.enabled System.getProperty('tests.coverage.report.csv', 'false').toBoolean() + xml.getRequired().set(System.getProperty('tests.coverage.report.xml', 'true').toBoolean()) + html.getRequired().set(System.getProperty('tests.coverage.report.html', 'false').toBoolean()) + csv.getRequired().set(System.getProperty('tests.coverage.report.csv', 'false').toBoolean()) } } diff --git a/gradle/formatting.gradle b/gradle/formatting.gradle index 43cfa77a7d38a..19adea33b23b3 100644 --- a/gradle/formatting.gradle +++ b/gradle/formatting.gradle @@ -55,72 +55,8 @@ import org.opensearch.gradle.BuildPlugin * https://github.com/diffplug/spotless/tree/master/plugin-gradle */ -// Do not add new sub-projects here! -def projectPathsToExclude = [ - ':libs:opensearch-cli', - ':libs:opensearch-core', - ':libs:opensearch-dissect', - ':libs:opensearch-geo', - ':libs:opensearch-grok', - ':libs:opensearch-nio', - ':libs:opensearch-plugin-classloader', - ':libs:opensearch-secure-sm', - ':libs:opensearch-ssl-config', - ':libs:opensearch-x-content', - ':modules:aggs-matrix-stats', - ':modules:analysis-common', - ':modules:ingest-common', - ':modules:ingest-geoip', - ':modules:ingest-user-agent', - ':modules:lang-expression', - ':modules:lang-mustache', - ':modules:lang-painless', - ':modules:lang-painless:spi', - ':modules:mapper-extras', - ':modules:parent-join', - ':modules:percolator', - ':modules:rank-eval', - ':modules:reindex', - ':modules:repository-url', - ':modules:systemd', - ':modules:tasks', - ':modules:transport-netty4', - ':plugins:analysis-icu', - ':plugins:analysis-kuromoji', - ':plugins:analysis-nori', - ':plugins:analysis-phonetic', - ':plugins:analysis-smartcn', - ':plugins:analysis-stempel', - ':plugins:analysis-ukrainian', - ':plugins:discovery-azure-classic', - ':plugins:discovery-ec2', - ':plugins:discovery-gce', - ':plugins:ingest-attachment', - ':plugins:mapper-annotated-text', - ':plugins:mapper-murmur3', - ':plugins:mapper-size', - ':plugins:repository-azure', - ':plugins:repository-gcs', - ':plugins:repository-hdfs', - ':plugins:repository-s3', - ':plugins:store-smb', - ':plugins:transport-nio', - ':qa:die-with-dignity', - ':rest-api-spec', - ':test:fixtures:azure-fixture', - ':test:fixtures:gcs-fixture', - ':test:fixtures:hdfs-fixture', - ':test:fixtures:krb5kdc-fixture', - ':test:fixtures:minio-fixture', - ':test:fixtures:old-elasticsearch', - ':test:fixtures:s3-fixture', - ':test:framework', - ':test:logger-usage' -] - -subprojects { +allprojects { plugins.withType(BuildPlugin).whenPluginAdded { - if (projectPathsToExclude.contains(project.path) == false) { project.apply plugin: "com.diffplug.spotless" spotless { @@ -141,6 +77,5 @@ subprojects { } precommit.dependsOn 'spotlessJavaCheck' - } } } diff --git a/gradle/missing-javadoc.gradle b/gradle/missing-javadoc.gradle index 6f84ba595fe3a..85996b95d6a02 100644 --- a/gradle/missing-javadoc.gradle +++ b/gradle/missing-javadoc.gradle @@ -7,6 +7,7 @@ */ import javax.annotation.Nullable +import org.gradle.internal.jvm.Jvm /** * Checks for missing javadocs. @@ -300,9 +301,7 @@ class MissingJavadocTask extends DefaultTask { def javadocCmd = { if (executable == null) { - JavaInstallationRegistry registry = project.extensions.getByType(JavaInstallationRegistry) - JavaInstallation currentJvm = registry.installationForCurrentVirtualMachine.get() - return currentJvm.jdk.get().javadocExecutable.asFile + return Jvm.current().javadocExecutable } else { return project.file(executable) } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index e708b1c023ec8..7454180f2ae88 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f24ec6254c6bb..e7095967a87b7 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -11,7 +11,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionSha256Sum=11657af6356b7587bfb37287b5992e94a9686d5c8a0a1b60b87b9928a2decde5 +distributionSha256Sum=00b273629df4ce46e68df232161d5a7c4e495b9a029ce6e0420f071e21316867 diff --git a/libs/cli/src/main/java/org/opensearch/cli/Command.java b/libs/cli/src/main/java/org/opensearch/cli/Command.java index 668d9b4c223dc..eed5c4ba4ee6f 100644 --- a/libs/cli/src/main/java/org/opensearch/cli/Command.java +++ b/libs/cli/src/main/java/org/opensearch/cli/Command.java @@ -58,8 +58,8 @@ public abstract class Command implements Closeable { private final OptionSpec helpOption = parser.acceptsAll(Arrays.asList("h", "help"), "Show help").forHelp(); private final OptionSpec silentOption = parser.acceptsAll(Arrays.asList("s", "silent"), "Show minimal output"); - private final OptionSpec verboseOption = - parser.acceptsAll(Arrays.asList("v", "verbose"), "Show verbose output").availableUnless(silentOption); + private final OptionSpec verboseOption = parser.acceptsAll(Arrays.asList("v", "verbose"), "Show verbose output") + .availableUnless(silentOption); /** * Construct the command with the specified command description and runnable to execute before main is invoked. @@ -82,9 +82,7 @@ public final int main(String[] args, Terminal terminal) throws Exception { try { this.close(); } catch (final IOException e) { - try ( - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw)) { + try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) { e.printStackTrace(pw); terminal.errorPrintln(sw.toString()); } catch (final IOException impossible) { diff --git a/libs/cli/src/main/java/org/opensearch/cli/ExitCodes.java b/libs/cli/src/main/java/org/opensearch/cli/ExitCodes.java index e24035561de60..c705177b0d7b6 100644 --- a/libs/cli/src/main/java/org/opensearch/cli/ExitCodes.java +++ b/libs/cli/src/main/java/org/opensearch/cli/ExitCodes.java @@ -37,19 +37,19 @@ */ public class ExitCodes { public static final int OK = 0; - public static final int USAGE = 64; /* command line usage error */ - public static final int DATA_ERROR = 65; /* data format error */ - public static final int NO_INPUT = 66; /* cannot open input */ - public static final int NO_USER = 67; /* addressee unknown */ - public static final int NO_HOST = 68; /* host name unknown */ - public static final int UNAVAILABLE = 69; /* service unavailable */ - public static final int CODE_ERROR = 70; /* internal software error */ - public static final int CANT_CREATE = 73; /* can't create (user) output file */ - public static final int IO_ERROR = 74; /* input/output error */ - public static final int TEMP_FAILURE = 75; /* temp failure; user is invited to retry */ - public static final int PROTOCOL = 76; /* remote error in protocol */ - public static final int NOPERM = 77; /* permission denied */ - public static final int CONFIG = 78; /* configuration error */ + public static final int USAGE = 64; /* command line usage error */ + public static final int DATA_ERROR = 65; /* data format error */ + public static final int NO_INPUT = 66; /* cannot open input */ + public static final int NO_USER = 67; /* addressee unknown */ + public static final int NO_HOST = 68; /* host name unknown */ + public static final int UNAVAILABLE = 69; /* service unavailable */ + public static final int CODE_ERROR = 70; /* internal software error */ + public static final int CANT_CREATE = 73; /* can't create (user) output file */ + public static final int IO_ERROR = 74; /* input/output error */ + public static final int TEMP_FAILURE = 75; /* temp failure; user is invited to retry */ + public static final int PROTOCOL = 76; /* remote error in protocol */ + public static final int NOPERM = 77; /* permission denied */ + public static final int CONFIG = 78; /* configuration error */ private ExitCodes() { /* no instance, just constants */ } } diff --git a/libs/cli/src/main/java/org/opensearch/cli/SuppressForbidden.java b/libs/cli/src/main/java/org/opensearch/cli/SuppressForbidden.java index 7656aaed38495..a4ea1162369da 100644 --- a/libs/cli/src/main/java/org/opensearch/cli/SuppressForbidden.java +++ b/libs/cli/src/main/java/org/opensearch/cli/SuppressForbidden.java @@ -44,4 +44,3 @@ public @interface SuppressForbidden { String reason(); } - diff --git a/libs/cli/src/main/java/org/opensearch/cli/Terminal.java b/libs/cli/src/main/java/org/opensearch/cli/Terminal.java index 4865438a6a51f..657b95fa052ab 100644 --- a/libs/cli/src/main/java/org/opensearch/cli/Terminal.java +++ b/libs/cli/src/main/java/org/opensearch/cli/Terminal.java @@ -198,7 +198,7 @@ public static char[] readLineToCharArray(Reader reader, int maxLength) { len++; } - if (len > 0 && len < buf.length && buf[len-1] == '\r') { + if (len > 0 && len < buf.length && buf[len - 1] == '\r') { len--; } diff --git a/libs/core/src/main/java/org/opensearch/bootstrap/JarHell.java b/libs/core/src/main/java/org/opensearch/bootstrap/JarHell.java index 045bf183d4580..843a6b982d7ff 100644 --- a/libs/core/src/main/java/org/opensearch/bootstrap/JarHell.java +++ b/libs/core/src/main/java/org/opensearch/bootstrap/JarHell.java @@ -94,7 +94,7 @@ public static void checkJarHell(Consumer output) throws IOException, URI output.accept("java.class.path: " + System.getProperty("java.class.path")); output.accept("sun.boot.class.path: " + System.getProperty("sun.boot.class.path")); if (loader instanceof URLClassLoader) { - output.accept("classloader urls: " + Arrays.toString(((URLClassLoader)loader).getURLs())); + output.accept("classloader urls: " + Arrays.toString(((URLClassLoader) loader).getURLs())); } checkJarHell(parseClassPath(), output); } @@ -104,7 +104,7 @@ public static void checkJarHell(Consumer output) throws IOException, URI * @return array of URLs * @throws IllegalStateException if the classpath contains empty elements */ - public static Set parseClassPath() { + public static Set parseClassPath() { return parseClassPath(System.getProperty("java.class.path")); } @@ -124,13 +124,17 @@ static Set parseClassPath(String classPath) { // Technically empty classpath element behaves like CWD. // So below is the "correct" code, however in practice with ES, this is usually just a misconfiguration, // from old shell scripts left behind or something: - // if (element.isEmpty()) { - // element = System.getProperty("user.dir"); - // } + // if (element.isEmpty()) { + // element = System.getProperty("user.dir"); + // } // Instead we just throw an exception, and keep it clean. if (element.isEmpty()) { - throw new IllegalStateException("Classpath should not contain empty elements! (outdated shell script from a previous" + - " version?) classpath='" + classPath + "'"); + throw new IllegalStateException( + "Classpath should not contain empty elements! (outdated shell script from a previous" + + " version?) classpath='" + + classPath + + "'" + ); } // we should be able to just Paths.get() each element, but unfortunately this is not the // whole story on how classpath parsing works: if you want to know, start at sun.misc.Launcher, @@ -147,15 +151,16 @@ static Set parseClassPath(String classPath) { } // now just parse as ordinary file try { - if (element .equals("/")) { + if (element.equals("/")) { // Eclipse adds this to the classpath when running unit tests... continue; } URL url = PathUtils.get(element).toUri().toURL(); // junit4.childvm.count if (urlElements.add(url) == false && element.endsWith(".jar")) { - throw new IllegalStateException("jar hell!" + System.lineSeparator() + - "duplicate jar [" + element + "] on classpath: " + classPath); + throw new IllegalStateException( + "jar hell!" + System.lineSeparator() + "duplicate jar [" + element + "] on classpath: " + classPath + ); } } catch (MalformedURLException e) { // should not happen, as we use the filesystem API @@ -178,7 +183,7 @@ public static void checkJarHell(Set urls, Consumer output) throws U // a "list" at all. So just exclude any elements underneath the java home String javaHome = System.getProperty("java.home"); output.accept("java.home: " + javaHome); - final Map clazzes = new HashMap<>(32768); + final Map clazzes = new HashMap<>(32768); Set seenJars = new HashSet<>(); for (final URL url : urls) { final Path path = PathUtils.get(url.toURI()); @@ -189,8 +194,7 @@ public static void checkJarHell(Set urls, Consumer output) throws U } if (path.toString().endsWith(".jar")) { if (!seenJars.add(path)) { - throw new IllegalStateException("jar hell!" + System.lineSeparator() + - "duplicate jar on classpath: " + path); + throw new IllegalStateException("jar hell!" + System.lineSeparator() + "duplicate jar on classpath: " + path); } output.accept("examining jar: " + path); try (JarFile file = new JarFile(path.toString())) { @@ -248,12 +252,12 @@ private static void checkManifest(Manifest manifest, Path jar) { public static void checkVersionFormat(String targetVersion) { if (!JavaVersion.isValid(targetVersion)) { throw new IllegalStateException( - String.format( - Locale.ROOT, - "version string must be a sequence of nonnegative decimal integers separated by \".\"'s and may have " + - "leading zeros but was %s", - targetVersion - ) + String.format( + Locale.ROOT, + "version string must be a sequence of nonnegative decimal integers separated by \".\"'s and may have " + + "leading zeros but was %s", + targetVersion + ) ); } } @@ -266,13 +270,13 @@ public static void checkJavaVersion(String resource, String targetVersion) { JavaVersion version = JavaVersion.parse(targetVersion); if (JavaVersion.current().compareTo(version) < 0) { throw new IllegalStateException( - String.format( - Locale.ROOT, - "%s requires Java %s:, your system: %s", - resource, - targetVersion, - JavaVersion.current().toString() - ) + String.format( + Locale.ROOT, + "%s requires Java %s:, your system: %s", + resource, + targetVersion, + JavaVersion.current().toString() + ) ); } } @@ -291,14 +295,29 @@ private static void checkClass(Map clazzes, String clazz, Path jar // throw a better exception in this ridiculous case. // unfortunately the zip file format allows this buggy possibility // UweSays: It can, but should be considered as bug :-) - throw new IllegalStateException("jar hell!" + System.lineSeparator() + - "class: " + clazz + System.lineSeparator() + - "exists multiple times in jar: " + jarpath + " !!!!!!!!!"); + throw new IllegalStateException( + "jar hell!" + + System.lineSeparator() + + "class: " + + clazz + + System.lineSeparator() + + "exists multiple times in jar: " + + jarpath + + " !!!!!!!!!" + ); } else { - throw new IllegalStateException("jar hell!" + System.lineSeparator() + - "class: " + clazz + System.lineSeparator() + - "jar1: " + previous + System.lineSeparator() + - "jar2: " + jarpath); + throw new IllegalStateException( + "jar hell!" + + System.lineSeparator() + + "class: " + + clazz + + System.lineSeparator() + + "jar1: " + + previous + + System.lineSeparator() + + "jar2: " + + jarpath + ); } } } diff --git a/libs/core/src/main/java/org/opensearch/bootstrap/JavaVersion.java b/libs/core/src/main/java/org/opensearch/bootstrap/JavaVersion.java index 9c0e24b30876e..236563bf8bd89 100644 --- a/libs/core/src/main/java/org/opensearch/bootstrap/JavaVersion.java +++ b/libs/core/src/main/java/org/opensearch/bootstrap/JavaVersion.java @@ -104,10 +104,8 @@ public int compareTo(JavaVersion o) { for (int i = 0; i < len; i++) { int d = (i < version.size() ? version.get(i) : 0); int s = (i < o.version.size() ? o.version.get(i) : 0); - if (s < d) - return 1; - if (s > d) - return -1; + if (s < d) return 1; + if (s > d) return -1; } if (prePart != null && o.prePart == null) { return -1; @@ -121,11 +119,9 @@ public int compareTo(JavaVersion o) { private int comparePrePart(String prePart, String otherPrePart) { if (prePart.matches("\\d+")) { - return otherPrePart.matches("\\d+") ? - (new BigInteger(prePart)).compareTo(new BigInteger(otherPrePart)) : -1; + return otherPrePart.matches("\\d+") ? (new BigInteger(prePart)).compareTo(new BigInteger(otherPrePart)) : -1; } else { - return otherPrePart.matches("\\d+") ? - 1 : prePart.compareTo(otherPrePart); + return otherPrePart.matches("\\d+") ? 1 : prePart.compareTo(otherPrePart); } } diff --git a/libs/core/src/main/java/org/opensearch/common/Booleans.java b/libs/core/src/main/java/org/opensearch/common/Booleans.java index e22b8f16aca78..70536a8f4cc46 100644 --- a/libs/core/src/main/java/org/opensearch/common/Booleans.java +++ b/libs/core/src/main/java/org/opensearch/common/Booleans.java @@ -131,6 +131,7 @@ public static Boolean parseBooleanLenient(String value, Boolean defaultValue) { } return parseBooleanLenient(value, false); } + /** * Returns {@code false} if text is in "false", "0", "off", "no"; else, {@code true}. * @@ -178,8 +179,11 @@ public static boolean parseBooleanLenient(char[] text, int offset, int length, b return !(text[offset] == 'o' && text[offset + 1] == 'f' && text[offset + 2] == 'f'); } if (length == 5) { - return !(text[offset] == 'f' && text[offset + 1] == 'a' && text[offset + 2] == 'l' && text[offset + 3] == 's' && - text[offset + 4] == 'e'); + return !(text[offset] == 'f' + && text[offset + 1] == 'a' + && text[offset + 2] == 'l' + && text[offset + 3] == 's' + && text[offset + 4] == 'e'); } return true; } @@ -205,15 +209,18 @@ public static boolean isBooleanLenient(char[] text, int offset, int length) { return (text[offset] == 'n' && text[offset + 1] == 'o') || (text[offset] == 'o' && text[offset + 1] == 'n'); } if (length == 3) { - return (text[offset] == 'o' && text[offset + 1] == 'f' && text[offset + 2] == 'f') || - (text[offset] == 'y' && text[offset + 1] == 'e' && text[offset + 2] == 's'); + return (text[offset] == 'o' && text[offset + 1] == 'f' && text[offset + 2] == 'f') + || (text[offset] == 'y' && text[offset + 1] == 'e' && text[offset + 2] == 's'); } if (length == 4) { return (text[offset] == 't' && text[offset + 1] == 'r' && text[offset + 2] == 'u' && text[offset + 3] == 'e'); } if (length == 5) { - return (text[offset] == 'f' && text[offset + 1] == 'a' && text[offset + 2] == 'l' && text[offset + 3] == 's' && - text[offset + 4] == 'e'); + return (text[offset] == 'f' + && text[offset + 1] == 'a' + && text[offset + 2] == 'l' + && text[offset + 3] == 's' + && text[offset + 4] == 'e'); } return false; } diff --git a/libs/core/src/main/java/org/opensearch/common/Glob.java b/libs/core/src/main/java/org/opensearch/common/Glob.java index 1f40cf2793d97..650a38b83fbfc 100644 --- a/libs/core/src/main/java/org/opensearch/common/Glob.java +++ b/libs/core/src/main/java/org/opensearch/common/Glob.java @@ -75,9 +75,9 @@ public static boolean globMatch(String pattern, String str) { } return false; } - return (str.length() >= firstIndex && - pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex)) && - globMatch(pattern.substring(firstIndex), str.substring(firstIndex))); + return (str.length() >= firstIndex + && pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex)) + && globMatch(pattern.substring(firstIndex), str.substring(firstIndex))); } } diff --git a/libs/core/src/main/java/org/opensearch/common/Nullable.java b/libs/core/src/main/java/org/opensearch/common/Nullable.java index bd0c29336d8c1..8660f2dbb5995 100644 --- a/libs/core/src/main/java/org/opensearch/common/Nullable.java +++ b/libs/core/src/main/java/org/opensearch/common/Nullable.java @@ -51,6 +51,6 @@ @TypeQualifierNickname @CheckForNull @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD}) +@Target({ ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD }) public @interface Nullable { } diff --git a/libs/core/src/main/java/org/opensearch/common/SuppressForbidden.java b/libs/core/src/main/java/org/opensearch/common/SuppressForbidden.java index 8533512a4fec8..ed4045dd1ef3c 100644 --- a/libs/core/src/main/java/org/opensearch/common/SuppressForbidden.java +++ b/libs/core/src/main/java/org/opensearch/common/SuppressForbidden.java @@ -31,11 +31,11 @@ package org.opensearch.common; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; + /** * Annotation to suppress forbidden-apis errors inside a whole class, a method, or a field. */ diff --git a/libs/core/src/main/java/org/opensearch/common/collect/List.java b/libs/core/src/main/java/org/opensearch/common/collect/List.java index d7122b5942677..96bdacc276323 100644 --- a/libs/core/src/main/java/org/opensearch/common/collect/List.java +++ b/libs/core/src/main/java/org/opensearch/common/collect/List.java @@ -69,7 +69,7 @@ public static java.util.List of(T e1) { */ @SuppressWarnings("unchecked") public static java.util.List of(T e1, T e2) { - return List.of((T[]) new Object[]{e1, e2}); + return List.of((T[]) new Object[] { e1, e2 }); } /** diff --git a/libs/core/src/main/java/org/opensearch/common/collect/Map.java b/libs/core/src/main/java/org/opensearch/common/collect/Map.java index 8c5e05076ca48..3b401ee0e1c1b 100644 --- a/libs/core/src/main/java/org/opensearch/common/collect/Map.java +++ b/libs/core/src/main/java/org/opensearch/common/collect/Map.java @@ -83,40 +83,92 @@ public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, /** * Returns an unmodifiable map containing six mappings. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6) { + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); } /** * Returns an unmodifiable map containing seven mappings. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7) { + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) { return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7); } /** * Returns an unmodifiable map containing eight mappings. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8 + ) { return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8); } /** * Returns an unmodifiable map containing nine mappings. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8, + K k9, + V v9 + ) { return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9); } /** * Returns an unmodifiable map containing ten mappings. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8, + K k9, + V v9, + K k10, + V v10 + ) { return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10); } @@ -177,7 +229,7 @@ public static java.util.Map.Entry entry(K k, V v) { * @param map a {@code Map} from which entries are drawn, must be non-null * @return a {@code Map} containing the entries of the given {@code Map} */ - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public static java.util.Map copyOf(java.util.Map map) { return (java.util.Map) Map.ofEntries(map.entrySet().toArray(new java.util.Map.Entry[0])); } diff --git a/libs/core/src/main/java/org/opensearch/common/collect/Set.java b/libs/core/src/main/java/org/opensearch/common/collect/Set.java index 4d81be7e8ca51..921408b88241f 100644 --- a/libs/core/src/main/java/org/opensearch/common/collect/Set.java +++ b/libs/core/src/main/java/org/opensearch/common/collect/Set.java @@ -70,7 +70,7 @@ public static java.util.Set of(T e1) { */ @SuppressWarnings("unchecked") public static java.util.Set of(T e1, T e2) { - return Set.of((T[]) new Object[]{e1, e2}); + return Set.of((T[]) new Object[] { e1, e2 }); } /** diff --git a/libs/core/src/main/java/org/opensearch/common/io/PathUtils.java b/libs/core/src/main/java/org/opensearch/common/io/PathUtils.java index eb4d2f2d97b81..85b623ef4d06d 100644 --- a/libs/core/src/main/java/org/opensearch/common/io/PathUtils.java +++ b/libs/core/src/main/java/org/opensearch/common/io/PathUtils.java @@ -98,7 +98,7 @@ public static Path get(Path[] roots, String path) { for (Path root : roots) { Path normalizedRoot = root.normalize(); Path normalizedPath = normalizedRoot.resolve(path).normalize(); - if(normalizedPath.startsWith(normalizedRoot)) { + if (normalizedPath.startsWith(normalizedRoot)) { return normalizedPath; } } diff --git a/libs/core/src/main/java/org/opensearch/common/unit/TimeValue.java b/libs/core/src/main/java/org/opensearch/common/unit/TimeValue.java index 77ce969ae4d15..ee150bf4ff85a 100644 --- a/libs/core/src/main/java/org/opensearch/common/unit/TimeValue.java +++ b/libs/core/src/main/java/org/opensearch/common/unit/TimeValue.java @@ -395,8 +395,9 @@ public static TimeValue parseTimeValue(String sValue, TimeValue defaultValue, St return TimeValue.ZERO; } else { // Missing units: - throw new IllegalArgumentException("failed to parse setting [" + settingName + "] with value [" + sValue + - "] as a time value: unit is missing or unrecognized"); + throw new IllegalArgumentException( + "failed to parse setting [" + settingName + "] with value [" + sValue + "] as a time value: unit is missing or unrecognized" + ); } } @@ -406,13 +407,19 @@ private static long parse(final String initialInput, final String normalized, fi final long value = Long.parseLong(s); if (value < -1) { // -1 is magic, but reject any other negative values - throw new IllegalArgumentException("failed to parse setting [" + settingName + "] with value [" + initialInput + - "] as a time value: negative durations are not supported"); + throw new IllegalArgumentException( + "failed to parse setting [" + + settingName + + "] with value [" + + initialInput + + "] as a time value: negative durations are not supported" + ); } return value; } catch (final NumberFormatException e) { try { - @SuppressWarnings("unused") final double ignored = Double.parseDouble(s); + @SuppressWarnings("unused") + final double ignored = Double.parseDouble(s); throw new IllegalArgumentException("failed to parse [" + initialInput + "], fractional time values are not supported", e); } catch (final NumberFormatException ignored) { throw new IllegalArgumentException("failed to parse [" + initialInput + "]", e); diff --git a/libs/core/src/main/java/org/opensearch/common/util/FastMath.java b/libs/core/src/main/java/org/opensearch/common/util/FastMath.java index ab2726815b9b1..f3b04fda2cadb 100644 --- a/libs/core/src/main/java/org/opensearch/common/util/FastMath.java +++ b/libs/core/src/main/java/org/opensearch/common/util/FastMath.java @@ -50,22 +50,22 @@ */ final class FastMath { - private FastMath() { } + private FastMath() {} - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // RE-USABLE CONSTANTS - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- - private static final double ONE_DIV_F2 = 1/2.0; - private static final double ONE_DIV_F3 = 1/6.0; - private static final double ONE_DIV_F4 = 1/24.0; + private static final double ONE_DIV_F2 = 1 / 2.0; + private static final double ONE_DIV_F3 = 1 / 6.0; + private static final double ONE_DIV_F4 = 1 / 24.0; private static final double TWO_POW_N28 = Double.longBitsToDouble(0x3E30000000000000L); private static final double TWO_POW_66 = Double.longBitsToDouble(0x4410000000000000L); private static final double LOG_DOUBLE_MAX_VALUE = StrictMath.log(Double.MAX_VALUE); - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // CONSTANTS AND TABLES FOR ATAN - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // We use the formula atan(-x) = -atan(x) // ---> we only have to compute atan(x) on [0,+infinity[. @@ -75,9 +75,9 @@ private FastMath() { } // Supposed to be >= tan(67.7deg), as fdlibm code is supposed to work with values > 2.4375. private static final double ATAN_MAX_VALUE_FOR_TABS = StrictMath.tan(Math.toRadians(74.0)); - private static final int ATAN_TABS_SIZE = 1<<12 + 1; - private static final double ATAN_DELTA = ATAN_MAX_VALUE_FOR_TABS/(ATAN_TABS_SIZE - 1); - private static final double ATAN_INDEXER = 1/ATAN_DELTA; + private static final int ATAN_TABS_SIZE = 1 << 12 + 1; + private static final double ATAN_DELTA = ATAN_MAX_VALUE_FOR_TABS / (ATAN_TABS_SIZE - 1); + private static final double ATAN_INDEXER = 1 / ATAN_DELTA; private static final double[] atanTab = new double[ATAN_TABS_SIZE]; private static final double[] atanDer1DivF1Tab = new double[ATAN_TABS_SIZE]; private static final double[] atanDer2DivF2Tab = new double[ATAN_TABS_SIZE]; @@ -86,32 +86,32 @@ private FastMath() { } private static final double ATAN_HI3 = Double.longBitsToDouble(0x3ff921fb54442d18L); // 1.57079632679489655800e+00 atan(inf)hi private static final double ATAN_LO3 = Double.longBitsToDouble(0x3c91a62633145c07L); // 6.12323399573676603587e-17 atan(inf)lo - private static final double ATAN_AT0 = Double.longBitsToDouble(0x3fd555555555550dL); // 3.33333333333329318027e-01 + private static final double ATAN_AT0 = Double.longBitsToDouble(0x3fd555555555550dL); // 3.33333333333329318027e-01 private static final double ATAN_AT1 = Double.longBitsToDouble(0xbfc999999998ebc4L); // -1.99999999998764832476e-01 - private static final double ATAN_AT2 = Double.longBitsToDouble(0x3fc24924920083ffL); // 1.42857142725034663711e-01 + private static final double ATAN_AT2 = Double.longBitsToDouble(0x3fc24924920083ffL); // 1.42857142725034663711e-01 private static final double ATAN_AT3 = Double.longBitsToDouble(0xbfbc71c6fe231671L); // -1.11111104054623557880e-01 - private static final double ATAN_AT4 = Double.longBitsToDouble(0x3fb745cdc54c206eL); // 9.09088713343650656196e-02 + private static final double ATAN_AT4 = Double.longBitsToDouble(0x3fb745cdc54c206eL); // 9.09088713343650656196e-02 private static final double ATAN_AT5 = Double.longBitsToDouble(0xbfb3b0f2af749a6dL); // -7.69187620504482999495e-02 - private static final double ATAN_AT6 = Double.longBitsToDouble(0x3fb10d66a0d03d51L); // 6.66107313738753120669e-02 + private static final double ATAN_AT6 = Double.longBitsToDouble(0x3fb10d66a0d03d51L); // 6.66107313738753120669e-02 private static final double ATAN_AT7 = Double.longBitsToDouble(0xbfadde2d52defd9aL); // -5.83357013379057348645e-02 - private static final double ATAN_AT8 = Double.longBitsToDouble(0x3fa97b4b24760debL); // 4.97687799461593236017e-02 + private static final double ATAN_AT8 = Double.longBitsToDouble(0x3fa97b4b24760debL); // 4.97687799461593236017e-02 private static final double ATAN_AT9 = Double.longBitsToDouble(0xbfa2b4442c6a6c2fL); // -3.65315727442169155270e-02 private static final double ATAN_AT10 = Double.longBitsToDouble(0x3f90ad3ae322da11L); // 1.62858201153657823623e-02 static { // atan - for (int i=0;i ATAN_MAX_VALUE_FOR_TABS, or value is NaN // This part is derived from fdlibm. if (value < TWO_POW_66) { - double x = -1/value; - double x2 = x*x; - double x4 = x2*x2; - double s1 = x2*(ATAN_AT0+x4*(ATAN_AT2+x4*(ATAN_AT4+x4*(ATAN_AT6+x4*(ATAN_AT8+x4*ATAN_AT10))))); - double s2 = x4*(ATAN_AT1+x4*(ATAN_AT3+x4*(ATAN_AT5+x4*(ATAN_AT7+x4*ATAN_AT9)))); - double result = ATAN_HI3-((x*(s1+s2)-ATAN_LO3)-x); + double x = -1 / value; + double x2 = x * x; + double x4 = x2 * x2; + double s1 = x2 * (ATAN_AT0 + x4 * (ATAN_AT2 + x4 * (ATAN_AT4 + x4 * (ATAN_AT6 + x4 * (ATAN_AT8 + x4 * ATAN_AT10))))); + double s2 = x4 * (ATAN_AT1 + x4 * (ATAN_AT3 + x4 * (ATAN_AT5 + x4 * (ATAN_AT7 + x4 * ATAN_AT9)))); + double result = ATAN_HI3 - ((x * (s1 + s2) - ATAN_LO3) - x); return negateResult ? -result : result; } else { // value >= 2^66, or value is NaN if (Double.isNaN(value)) { return Double.NaN; } else { - return negateResult ? -Math.PI/2 : Math.PI/2; + return negateResult ? -Math.PI / 2 : Math.PI / 2; } } } diff --git a/libs/core/src/main/java/org/opensearch/common/util/concurrent/AbstractRefCounted.java b/libs/core/src/main/java/org/opensearch/common/util/concurrent/AbstractRefCounted.java index ac2865a3595ae..e55c87c34c795 100644 --- a/libs/core/src/main/java/org/opensearch/common/util/concurrent/AbstractRefCounted.java +++ b/libs/core/src/main/java/org/opensearch/common/util/concurrent/AbstractRefCounted.java @@ -90,7 +90,6 @@ public int refCount() { return this.refCount.get(); } - /** gets the name of this instance */ public String getName() { return name; diff --git a/libs/core/src/main/java/org/opensearch/core/internal/io/IOUtils.java b/libs/core/src/main/java/org/opensearch/core/internal/io/IOUtils.java index 6bf8eae6482ad..00ecb8b82d744 100644 --- a/libs/core/src/main/java/org/opensearch/core/internal/io/IOUtils.java +++ b/libs/core/src/main/java/org/opensearch/core/internal/io/IOUtils.java @@ -164,7 +164,7 @@ public static void closeWhileHandlingException(final Closeable... objects) { */ public static void closeWhileHandlingException(final Iterable objects) { for (final Closeable object : objects) { - closeWhileHandlingException(object); + closeWhileHandlingException(object); } } @@ -175,8 +175,7 @@ public static void closeWhileHandlingException(final Closeable closeable) { // noinspection EmptyCatchBlock try { close(closeable); - } catch (final IOException | RuntimeException e) { - } + } catch (final IOException | RuntimeException e) {} } /** @@ -212,21 +211,17 @@ public static void deleteFilesIgnoringExceptions(final Collection unremoved = rm(new LinkedHashMap<>(), locations); + final LinkedHashMap unremoved = rm(new LinkedHashMap<>(), locations); if (!unremoved.isEmpty()) { final StringBuilder b = new StringBuilder("could not remove the following files (in the order of attempts):\n"); - for (final Map.Entry kv : unremoved.entrySet()) { - b.append(" ") - .append(kv.getKey().toAbsolutePath()) - .append(": ") - .append(kv.getValue()) - .append("\n"); + for (final Map.Entry kv : unremoved.entrySet()) { + b.append(" ").append(kv.getKey().toAbsolutePath()).append(": ").append(kv.getValue()).append("\n"); } throw new IOException(b.toString()); } } - private static LinkedHashMap rm(final LinkedHashMap unremoved, final Path... locations) { + private static LinkedHashMap rm(final LinkedHashMap unremoved, final Path... locations) { if (locations != null) { for (final Path location : locations) { // TODO: remove this leniency @@ -306,9 +301,9 @@ public static void fsync(final Path fileToSync, final boolean isDir) throws IOEx file.force(true); } catch (final IOException e) { if (isDir) { - assert (LINUX || MAC_OS_X) == false : - "on Linux and MacOSX fsyncing a directory should not throw IOException, "+ - "we just don't want to rely on that in production (undocumented); got: " + e; + assert (LINUX || MAC_OS_X) == false : "on Linux and MacOSX fsyncing a directory should not throw IOException, " + + "we just don't want to rely on that in production (undocumented); got: " + + e; // ignore exception if it is a directory return; } diff --git a/libs/core/src/main/java/org/opensearch/core/internal/net/NetUtils.java b/libs/core/src/main/java/org/opensearch/core/internal/net/NetUtils.java index e101895b17725..6ea3b6f953c50 100644 --- a/libs/core/src/main/java/org/opensearch/core/internal/net/NetUtils.java +++ b/libs/core/src/main/java/org/opensearch/core/internal/net/NetUtils.java @@ -92,7 +92,8 @@ public static void tryEnsureReasonableKeepAliveConfig(NetworkChannel socketChann if (keepalive.booleanValue()) { for (SocketOption option : Arrays.asList( NetUtils.getTcpKeepIdleSocketOptionOrNull(), - NetUtils.getTcpKeepIntervalSocketOptionOrNull())) { + NetUtils.getTcpKeepIntervalSocketOptionOrNull() + )) { setMinValueForSocketOption(socketChannel, option, 300); } } diff --git a/libs/core/src/main/java11/org/opensearch/common/collect/Map.java b/libs/core/src/main/java11/org/opensearch/common/collect/Map.java index 45ba475de229b..21de546869390 100644 --- a/libs/core/src/main/java11/org/opensearch/common/collect/Map.java +++ b/libs/core/src/main/java11/org/opensearch/common/collect/Map.java @@ -79,40 +79,92 @@ public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6) { + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); } /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7) { + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7); } /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8 + ) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8); } /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8, + K k9, + V v9 + ) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9); } /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8, + K k9, + V v9, + K k10, + V v10 + ) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10); } diff --git a/libs/core/src/test/java/org/opensearch/bootstrap/JarHellTests.java b/libs/core/src/test/java/org/opensearch/bootstrap/JarHellTests.java index 446d8df47dac2..d0e411ae8e3c2 100644 --- a/libs/core/src/test/java/org/opensearch/bootstrap/JarHellTests.java +++ b/libs/core/src/test/java/org/opensearch/bootstrap/JarHellTests.java @@ -76,8 +76,7 @@ URL makeFile(Path dir, String name) throws IOException { public void testDifferentJars() throws Exception { Path dir = createTempDir(); - Set jars = asSet(makeJar(dir, "foo.jar", null, "DuplicateClass.class"), - makeJar(dir, "bar.jar", null, "DuplicateClass.class")); + Set jars = asSet(makeJar(dir, "foo.jar", null, "DuplicateClass.class"), makeJar(dir, "bar.jar", null, "DuplicateClass.class")); try { JarHell.checkJarHell(jars, logger::debug); fail("did not get expected exception"); @@ -92,10 +91,7 @@ public void testDifferentJars() throws Exception { public void testModuleInfo() throws Exception { Path dir = createTempDir(); JarHell.checkJarHell( - asSet( - makeJar(dir, "foo.jar", null, "module-info.class"), - makeJar(dir, "bar.jar", null, "module-info.class") - ), + asSet(makeJar(dir, "foo.jar", null, "module-info.class"), makeJar(dir, "bar.jar", null, "module-info.class")), logger::debug ); } @@ -103,10 +99,7 @@ public void testModuleInfo() throws Exception { public void testModuleInfoPackage() throws Exception { Path dir = createTempDir(); JarHell.checkJarHell( - asSet( - makeJar(dir, "foo.jar", null, "foo/bar/module-info.class"), - makeJar(dir, "bar.jar", null, "foo/bar/module-info.class") - ), + asSet(makeJar(dir, "foo.jar", null, "foo/bar/module-info.class"), makeJar(dir, "bar.jar", null, "foo/bar/module-info.class")), logger::debug ); } @@ -114,8 +107,7 @@ public void testModuleInfoPackage() throws Exception { public void testDirsOnClasspath() throws Exception { Path dir1 = createTempDir(); Path dir2 = createTempDir(); - Set dirs = asSet(makeFile(dir1, "DuplicateClass.class"), - makeFile(dir2, "DuplicateClass.class")); + Set dirs = asSet(makeFile(dir1, "DuplicateClass.class"), makeFile(dir2, "DuplicateClass.class")); try { JarHell.checkJarHell(dirs, logger::debug); fail("did not get expected exception"); @@ -130,8 +122,7 @@ public void testDirsOnClasspath() throws Exception { public void testDirAndJar() throws Exception { Path dir1 = createTempDir(); Path dir2 = createTempDir(); - Set dirs = asSet(makeJar(dir1, "foo.jar", null, "DuplicateClass.class"), - makeFile(dir2, "DuplicateClass.class")); + Set dirs = asSet(makeJar(dir1, "foo.jar", null, "DuplicateClass.class"), makeFile(dir2, "DuplicateClass.class")); try { JarHell.checkJarHell(dirs, logger::debug); fail("did not get expected exception"); @@ -172,7 +163,6 @@ public void testRequiredJDKVersionTooOld() throws Exception { } JavaVersion targetVersion = JavaVersion.parse(Strings.collectionToDelimitedString(target, ".")); - Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); @@ -198,8 +188,13 @@ public void testBadJDKVersionInJar() throws Exception { JarHell.checkJarHell(jars, logger::debug); fail("did not get expected exception"); } catch (IllegalStateException e) { - assertTrue(e.getMessage().equals("version string must be a sequence of nonnegative decimal integers separated " + - "by \".\"'s and may have leading zeros but was bogus")); + assertTrue( + e.getMessage() + .equals( + "version string must be a sequence of nonnegative decimal integers separated " + + "by \".\"'s and may have leading zeros but was bogus" + ) + ); } } @@ -214,7 +209,7 @@ public void testRequiredJDKVersionIsOK() throws Exception { } public void testValidVersions() { - String[] versions = new String[]{"1.7", "1.7.0", "0.1.7", "1.7.0.80"}; + String[] versions = new String[] { "1.7", "1.7.0", "0.1.7", "1.7.0.80" }; for (String version : versions) { try { JarHell.checkVersionFormat(version); @@ -225,13 +220,12 @@ public void testValidVersions() { } public void testInvalidVersions() { - String[] versions = new String[]{"", "1.7.0_80", "1.7."}; + String[] versions = new String[] { "", "1.7.0_80", "1.7." }; for (String version : versions) { try { JarHell.checkVersionFormat(version); fail("\"" + version + "\"" + " should be rejected as an invalid version format"); - } catch (IllegalStateException e) { - } + } catch (IllegalStateException e) {} } } diff --git a/libs/core/src/test/java/org/opensearch/common/collect/ListTests.java b/libs/core/src/test/java/org/opensearch/common/collect/ListTests.java index 68d99e86b4932..70841a102b783 100644 --- a/libs/core/src/test/java/org/opensearch/common/collect/ListTests.java +++ b/libs/core/src/test/java/org/opensearch/common/collect/ListTests.java @@ -50,7 +50,7 @@ public void testStringListOfZero() { } public void testStringListOfOne() { - final String[] strings = {"foo"}; + final String[] strings = { "foo" }; final java.util.List stringsList = List.of(strings); assertThat(stringsList.size(), equalTo(strings.length)); assertTrue(stringsList.containsAll(Arrays.asList(strings))); @@ -58,7 +58,7 @@ public void testStringListOfOne() { } public void testStringListOfTwo() { - final String[] strings = {"foo", "bar"}; + final String[] strings = { "foo", "bar" }; final java.util.List stringsList = List.of(strings); assertThat(stringsList.size(), equalTo(strings.length)); assertTrue(stringsList.containsAll(Arrays.asList(strings))); @@ -66,7 +66,7 @@ public void testStringListOfTwo() { } public void testStringListOfN() { - final String[] strings = {"foo", "bar", "baz"}; + final String[] strings = { "foo", "bar", "baz" }; final java.util.List stringsList = List.of(strings); assertThat(stringsList.size(), equalTo(strings.length)); assertTrue(stringsList.containsAll(Arrays.asList(strings))); diff --git a/libs/core/src/test/java/org/opensearch/common/collect/MapTests.java b/libs/core/src/test/java/org/opensearch/common/collect/MapTests.java index 72969fc0e201e..8d7ffa71df562 100644 --- a/libs/core/src/test/java/org/opensearch/common/collect/MapTests.java +++ b/libs/core/src/test/java/org/opensearch/common/collect/MapTests.java @@ -38,7 +38,7 @@ public class MapTests extends OpenSearchTestCase { - private static final String[] numbers = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; + private static final String[] numbers = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; public void testMapOfZero() { final java.util.Map map = Map.of(); @@ -66,38 +66,117 @@ public void testMapOfFour() { } public void testMapOfFive() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4); + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, numbers[4], 4); validateMapContents(map, 5); } public void testMapOfSix() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5 + ); validateMapContents(map, 6); } public void testMapOfSeven() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5, numbers[6], 6); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5, + numbers[6], + 6 + ); validateMapContents(map, 7); } public void testMapOfEight() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5, + numbers[6], + 6, + numbers[7], + 7 + ); validateMapContents(map, 8); } public void testMapOfNine() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7, numbers[8], 8); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5, + numbers[6], + 6, + numbers[7], + 7, + numbers[8], + 8 + ); validateMapContents(map, 9); } public void testMapOfTen() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7, numbers[8], 8, numbers[9], 9); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5, + numbers[6], + 6, + numbers[7], + 7, + numbers[8], + 8, + numbers[9], + 9 + ); validateMapContents(map, 10); } diff --git a/libs/core/src/test/java/org/opensearch/common/collect/SetTests.java b/libs/core/src/test/java/org/opensearch/common/collect/SetTests.java index b0c7ae06899ab..1b7f29ff0d6f1 100644 --- a/libs/core/src/test/java/org/opensearch/common/collect/SetTests.java +++ b/libs/core/src/test/java/org/opensearch/common/collect/SetTests.java @@ -50,7 +50,7 @@ public void testStringSetOfZero() { } public void testStringSetOfOne() { - final String[] strings = {"foo"}; + final String[] strings = { "foo" }; final java.util.Set stringsSet = Set.of(strings); assertThat(stringsSet.size(), equalTo(strings.length)); assertTrue(stringsSet.containsAll(Arrays.asList(strings))); @@ -58,7 +58,7 @@ public void testStringSetOfOne() { } public void testStringSetOfTwo() { - final String[] strings = {"foo", "bar"}; + final String[] strings = { "foo", "bar" }; final java.util.Set stringsSet = Set.of(strings); assertThat(stringsSet.size(), equalTo(strings.length)); assertTrue(stringsSet.containsAll(Arrays.asList(strings))); @@ -66,7 +66,7 @@ public void testStringSetOfTwo() { } public void testStringSetOfN() { - final String[] strings = {"foo", "bar", "baz"}; + final String[] strings = { "foo", "bar", "baz" }; final java.util.Set stringsSet = Set.of(strings); assertThat(stringsSet.size(), equalTo(strings.length)); assertTrue(stringsSet.containsAll(Arrays.asList(strings))); diff --git a/libs/core/src/test/java/org/opensearch/common/unit/TimeValueTests.java b/libs/core/src/test/java/org/opensearch/common/unit/TimeValueTests.java index 065cd784da5f6..250f1de1feeea 100644 --- a/libs/core/src/test/java/org/opensearch/common/unit/TimeValueTests.java +++ b/libs/core/src/test/java/org/opensearch/common/unit/TimeValueTests.java @@ -89,57 +89,35 @@ public void testMinusOne() { public void testParseTimeValue() { // Space is allowed before unit: - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10 ms", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10ms", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10 MS", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10MS", null, "test")); - - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10 s", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10s", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10 S", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10S", null, "test")); - - assertEquals(new TimeValue(10, TimeUnit.MINUTES), - TimeValue.parseTimeValue("10 m", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MINUTES), - TimeValue.parseTimeValue("10m", null, "test")); - - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10 h", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10h", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10 H", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10H", null, "test")); - - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10 d", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10d", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10 D", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10D", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10 ms", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10ms", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10 MS", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10MS", null, "test")); + + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10 s", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10s", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10 S", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10S", null, "test")); + + assertEquals(new TimeValue(10, TimeUnit.MINUTES), TimeValue.parseTimeValue("10 m", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MINUTES), TimeValue.parseTimeValue("10m", null, "test")); + + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10 h", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10h", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10 H", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10H", null, "test")); + + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10 d", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10d", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10 D", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10D", null, "test")); // Time values of months should throw an exception as months are not // supported. Note that this is the only unit that is not case sensitive // as `m` is the only character that is overloaded in terms of which // time unit is expected between the upper and lower case versions - expectThrows(IllegalArgumentException.class, () -> { - TimeValue.parseTimeValue("10 M", null, "test"); - }); - expectThrows(IllegalArgumentException.class, () -> { - TimeValue.parseTimeValue("10M", null, "test"); - }); + expectThrows(IllegalArgumentException.class, () -> { TimeValue.parseTimeValue("10 M", null, "test"); }); + expectThrows(IllegalArgumentException.class, () -> { TimeValue.parseTimeValue("10M", null, "test"); }); final int length = randomIntBetween(0, 8); final String zeros = new String(new char[length]).replace('\0', '0'); @@ -158,8 +136,7 @@ public void testRoundTrip() { public void testNonFractionalTimeValues() { final String s = randomAlphaOfLength(10) + randomTimeUnit(); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); assertThat(e, hasToString(containsString("failed to parse [" + s + "]"))); assertThat(e, not(hasToString(containsString(FRACTIONAL_TIME_VALUES_ARE_NOT_SUPPORTED)))); assertThat(e.getCause(), instanceOf(NumberFormatException.class)); @@ -171,8 +148,7 @@ public void testFractionalTimeValues() { value = randomDouble(); } while (value == 0); final String s = Double.toString(randomIntBetween(0, 128) + value) + randomTimeUnit(); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); assertThat(e, hasToString(containsString("failed to parse [" + s + "]"))); assertThat(e, hasToString(containsString(FRACTIONAL_TIME_VALUES_ARE_NOT_SUPPORTED))); assertThat(e.getCause(), instanceOf(NumberFormatException.class)); @@ -239,7 +215,7 @@ public void testCompareValue() { public void testCompareUnits() { long number = randomNonNegativeLong(); - TimeUnit randomUnit = randomValueOtherThan(TimeUnit.DAYS, ()->randomFrom(TimeUnit.values())); + TimeUnit randomUnit = randomValueOtherThan(TimeUnit.DAYS, () -> randomFrom(TimeUnit.values())); TimeValue firstValue = new TimeValue(number, randomUnit); TimeValue secondValue = new TimeValue(number, TimeUnit.DAYS); assertTrue(firstValue.compareTo(secondValue) < 0); @@ -256,11 +232,20 @@ public void testRejectsNegativeValuesDuringParsing() { final String settingName = "test-value"; final long negativeValue = randomLongBetween(Long.MIN_VALUE, -2); final String negativeTimeValueString = Long.toString(negativeValue) + randomTimeUnit(); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> TimeValue.parseTimeValue(negativeTimeValueString, settingName)); - assertThat(ex.getMessage(), - equalTo("failed to parse setting [" + settingName + "] with value [" + negativeTimeValueString + - "] as a time value: negative durations are not supported")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> TimeValue.parseTimeValue(negativeTimeValueString, settingName) + ); + assertThat( + ex.getMessage(), + equalTo( + "failed to parse setting [" + + settingName + + "] with value [" + + negativeTimeValueString + + "] as a time value: negative durations are not supported" + ) + ); } public void testRejectsNegativeValuesAtCreation() { @@ -270,7 +255,14 @@ public void testRejectsNegativeValuesAtCreation() { } private TimeUnit randomTimeUnitObject() { - return randomFrom(TimeUnit.NANOSECONDS, TimeUnit.MICROSECONDS, TimeUnit.MILLISECONDS, TimeUnit.SECONDS, - TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS); + return randomFrom( + TimeUnit.NANOSECONDS, + TimeUnit.MICROSECONDS, + TimeUnit.MILLISECONDS, + TimeUnit.SECONDS, + TimeUnit.MINUTES, + TimeUnit.HOURS, + TimeUnit.DAYS + ); } } diff --git a/libs/core/src/test/java/org/opensearch/common/util/OpenSearchSloppyMathTests.java b/libs/core/src/test/java/org/opensearch/common/util/OpenSearchSloppyMathTests.java index 196a51b3581ed..793b237329413 100644 --- a/libs/core/src/test/java/org/opensearch/common/util/OpenSearchSloppyMathTests.java +++ b/libs/core/src/test/java/org/opensearch/common/util/OpenSearchSloppyMathTests.java @@ -46,8 +46,8 @@ public class OpenSearchSloppyMathTests extends OpenSearchTestCase { public void testAtan() { assertTrue(Double.isNaN(atan(Double.NaN))); - assertEquals(-Math.PI/2, atan(Double.NEGATIVE_INFINITY), ATAN_DELTA); - assertEquals(Math.PI/2, atan(Double.POSITIVE_INFINITY), ATAN_DELTA); + assertEquals(-Math.PI / 2, atan(Double.NEGATIVE_INFINITY), ATAN_DELTA); + assertEquals(Math.PI / 2, atan(Double.POSITIVE_INFINITY), ATAN_DELTA); for (int i = 0; i < 10000; i++) { assertEquals(StrictMath.atan(i), atan(i), ATAN_DELTA); assertEquals(StrictMath.atan(-i), atan(-i), ATAN_DELTA); diff --git a/libs/core/src/test/java/org/opensearch/core/internal/io/IOUtilsTests.java b/libs/core/src/test/java/org/opensearch/core/internal/io/IOUtilsTests.java index 14d938354068d..85632ae406a1b 100644 --- a/libs/core/src/test/java/org/opensearch/core/internal/io/IOUtilsTests.java +++ b/libs/core/src/test/java/org/opensearch/core/internal/io/IOUtilsTests.java @@ -97,8 +97,8 @@ public void testCloseIterableWithIOExceptions() throws IOException { runTestCloseWithIOExceptions((Function>) Arrays::asList, IOUtils::close); } - private void runTestCloseWithIOExceptions( - final Function function, final CheckedConsumer close) throws IOException { + private void runTestCloseWithIOExceptions(final Function function, final CheckedConsumer close) + throws IOException { final int numberOfCloseables = randomIntBetween(1, 8); final Closeable[] closeables = new Closeable[numberOfCloseables]; final List indexesThatThrow = new ArrayList<>(numberOfCloseables); @@ -137,7 +137,9 @@ public void testDeleteFilesIgnoringExceptionsIterable() throws IOException { } private void runDeleteFilesIgnoringExceptionsTest( - final Function function, CheckedConsumer deleteFilesIgnoringExceptions) throws IOException { + final Function function, + CheckedConsumer deleteFilesIgnoringExceptions + ) throws IOException { final int numberOfFiles = randomIntBetween(0, 7); final Path[] files = new Path[numberOfFiles]; for (int i = 0; i < numberOfFiles; i++) { @@ -170,8 +172,9 @@ public void runTestRm(final boolean exception) throws IOException { for (int i = 0; i < numberOfLocations; i++) { if (exception && randomBoolean()) { final Path location = createTempDir(); - final FileSystem fs = - new AccessDeniedWhileDeletingFileSystem(location.getFileSystem()).getFileSystem(URI.create("file:///")); + final FileSystem fs = new AccessDeniedWhileDeletingFileSystem(location.getFileSystem()).getFileSystem( + URI.create("file:///") + ); final Path wrapped = new FilterPath(location, fs); locations[i] = wrapped.resolve(randomAlphaOfLength(8)); Files.createDirectory(locations[i]); @@ -241,10 +244,8 @@ private static final class AccessDeniedWhileOpeningDirectoryFileSystem extends F } @Override - public FileChannel newFileChannel( - final Path path, - final Set options, - final FileAttribute... attrs) throws IOException { + public FileChannel newFileChannel(final Path path, final Set options, final FileAttribute... attrs) + throws IOException { if (Files.isDirectory(path)) { throw new AccessDeniedException(path.toString()); } diff --git a/libs/dissect/src/main/java/org/opensearch/dissect/DissectKey.java b/libs/dissect/src/main/java/org/opensearch/dissect/DissectKey.java index bd5e7a1852fb3..a4322446422f9 100644 --- a/libs/dissect/src/main/java/org/opensearch/dissect/DissectKey.java +++ b/libs/dissect/src/main/java/org/opensearch/dissect/DissectKey.java @@ -124,7 +124,7 @@ public final class DissectKey { * @param key The key to copy (except for the modifier) * @param modifier the modifer to use for this copy */ - DissectKey(DissectKey key, DissectKey.Modifier modifier){ + DissectKey(DissectKey key, DissectKey.Modifier modifier) { this.modifier = modifier; this.skipRightPadding = key.skipRightPadding; this.skip = key.skip; @@ -152,19 +152,29 @@ String getName() { return name; } - //generated + // generated @Override public String toString() { - return "DissectKey{" + - "modifier=" + modifier + - ", skip=" + skip + - ", appendPosition=" + appendPosition + - ", name='" + name + '\'' + - '}'; + return "DissectKey{" + + "modifier=" + + modifier + + ", skip=" + + skip + + ", appendPosition=" + + appendPosition + + ", name='" + + name + + '\'' + + '}'; } public enum Modifier { - NONE(""), APPEND_WITH_ORDER("/"), APPEND("+"), FIELD_NAME("*"), FIELD_VALUE("&"), NAMED_SKIP("?"); + NONE(""), + APPEND_WITH_ORDER("/"), + APPEND("+"), + FIELD_NAME("*"), + FIELD_VALUE("&"), + NAMED_SKIP("?"); private static final Pattern MODIFIER_PATTERN = Pattern.compile("[/+*&?]"); @@ -179,10 +189,13 @@ public String toString() { this.modifier = modifier; } - //package private for testing + // package private for testing static Modifier fromString(String modifier) { - return EnumSet.allOf(Modifier.class).stream().filter(km -> km.modifier.equals(modifier)) - .findFirst().orElseThrow(() -> new IllegalArgumentException("Found invalid modifier.")); //throw should never happen + return EnumSet.allOf(Modifier.class) + .stream() + .filter(km -> km.modifier.equals(modifier)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Found invalid modifier.")); // throw should never happen } private static Modifier findModifier(String key) { diff --git a/libs/dissect/src/main/java/org/opensearch/dissect/DissectMatch.java b/libs/dissect/src/main/java/org/opensearch/dissect/DissectMatch.java index 98d47e4773ef0..72dce8f9bb407 100644 --- a/libs/dissect/src/main/java/org/opensearch/dissect/DissectMatch.java +++ b/libs/dissect/src/main/java/org/opensearch/dissect/DissectMatch.java @@ -59,7 +59,7 @@ final class DissectMatch { DissectMatch(String appendSeparator, int maxMatches, int maxResults, int appendCount, int referenceCount) { if (maxMatches <= 0 || maxResults <= 0) { - throw new IllegalArgumentException("Expected results are zero, can not construct DissectMatch");//should never happen + throw new IllegalArgumentException("Expected results are zero, can not construct DissectMatch");// should never happen } this.maxMatches = maxMatches; this.maxResults = maxResults; @@ -91,8 +91,8 @@ void add(DissectKey key, String value) { appendResults.computeIfAbsent(key.getName(), k -> new AppendResult(appendSeparator)).addValue(value, implicitAppendOrder++); break; case APPEND_WITH_ORDER: - appendResults.computeIfAbsent(key.getName(), - k -> new AppendResult(appendSeparator)).addValue(value, key.getAppendPosition()); + appendResults.computeIfAbsent(key.getName(), k -> new AppendResult(appendSeparator)) + .addValue(value, key.getAppendPosition()); break; case FIELD_NAME: referenceResults.computeIfAbsent(key.getName(), k -> new ReferenceResult()).setKey(value); diff --git a/libs/dissect/src/main/java/org/opensearch/dissect/DissectParser.java b/libs/dissect/src/main/java/org/opensearch/dissect/DissectParser.java index 212a65a8260ed..9861847c9e1ea 100644 --- a/libs/dissect/src/main/java/org/opensearch/dissect/DissectParser.java +++ b/libs/dissect/src/main/java/org/opensearch/dissect/DissectParser.java @@ -110,10 +110,12 @@ public final class DissectParser { private static final Pattern KEY_DELIMITER_FIELD_PATTERN = Pattern.compile("%\\{([^}]*?)}([^%]*)", Pattern.DOTALL); private static final EnumSet ASSOCIATE_MODIFIERS = EnumSet.of( DissectKey.Modifier.FIELD_NAME, - DissectKey.Modifier.FIELD_VALUE); + DissectKey.Modifier.FIELD_VALUE + ); private static final EnumSet APPEND_MODIFIERS = EnumSet.of( DissectKey.Modifier.APPEND, - DissectKey.Modifier.APPEND_WITH_ORDER); + DissectKey.Modifier.APPEND_WITH_ORDER + ); private static final Function KEY_NAME = val -> val.getKey().getName(); private final List matchPairs; private final String pattern; @@ -139,16 +141,19 @@ public DissectParser(String pattern, String appendSeparator) { matchPairs.add(new DissectPair(key, delimiter)); } this.maxMatches = matchPairs.size(); - this.maxResults = Long.valueOf(matchPairs.stream() - .filter(dissectPair -> !dissectPair.getKey().skip()).map(KEY_NAME).distinct().count()).intValue(); + this.maxResults = Long.valueOf( + matchPairs.stream().filter(dissectPair -> !dissectPair.getKey().skip()).map(KEY_NAME).distinct().count() + ).intValue(); if (this.maxMatches == 0 || maxResults == 0) { throw new DissectException.PatternParse(pattern, "Unable to find any keys or delimiters."); } - //append validation - look through all of the keys to see if there are any keys that need to participate in an append operation + // append validation - look through all of the keys to see if there are any keys that need to participate in an append operation // but don't have the '+' defined Set appendKeyNames = matchPairs.stream() .filter(dissectPair -> APPEND_MODIFIERS.contains(dissectPair.getKey().getModifier())) - .map(KEY_NAME).distinct().collect(Collectors.toSet()); + .map(KEY_NAME) + .distinct() + .collect(Collectors.toSet()); if (appendKeyNames.size() > 0) { List modifiedMatchPairs = new ArrayList<>(matchPairs.size()); for (DissectPair p : matchPairs) { @@ -162,15 +167,18 @@ public DissectParser(String pattern, String appendSeparator) { } appendCount = appendKeyNames.size(); - //reference validation - ensure that '*' and '&' come in pairs + // reference validation - ensure that '*' and '&' come in pairs Map> referenceGroupings = matchPairs.stream() .filter(dissectPair -> ASSOCIATE_MODIFIERS.contains(dissectPair.getKey().getModifier())) .collect(Collectors.groupingBy(KEY_NAME)); for (Map.Entry> entry : referenceGroupings.entrySet()) { if (entry.getValue().size() != 2) { - throw new DissectException.PatternParse(pattern, "Found invalid key/reference associations: '" - + entry.getValue().stream().map(KEY_NAME).collect(Collectors.joining(",")) + - "' Please ensure each '*' is matched with a matching '&"); + throw new DissectException.PatternParse( + pattern, + "Found invalid key/reference associations: '" + + entry.getValue().stream().map(KEY_NAME).collect(Collectors.joining(",")) + + "' Please ensure each '*' is matched with a matching '&" + ); } } @@ -178,7 +186,6 @@ public DissectParser(String pattern, String appendSeparator) { this.matchPairs = Collections.unmodifiableList(matchPairs); } - /** *

Entry point to dissect a string into it's parts.

* @@ -210,38 +217,39 @@ public Map parse(String inputString) { */ DissectMatch dissectMatch = new DissectMatch(appendSeparator, maxMatches, maxResults, appendCount, referenceCount); Iterator it = matchPairs.iterator(); - //ensure leading delimiter matches - if (inputString != null && inputString.length() > leadingDelimiter.length() + // ensure leading delimiter matches + if (inputString != null + && inputString.length() > leadingDelimiter.length() && leadingDelimiter.equals(inputString.substring(0, leadingDelimiter.length()))) { byte[] input = inputString.getBytes(StandardCharsets.UTF_8); - //grab the first key/delimiter pair + // grab the first key/delimiter pair DissectPair dissectPair = it.next(); DissectKey key = dissectPair.getKey(); byte[] delimiter = dissectPair.getDelimiter().getBytes(StandardCharsets.UTF_8); - //start dissection after the first delimiter + // start dissection after the first delimiter int i = leadingDelimiter.length(); int valueStart = i; int lookAheadMatches; - //start walking the input string byte by byte, look ahead for matches where needed - //if a match is found jump forward to the end of the match + // start walking the input string byte by byte, look ahead for matches where needed + // if a match is found jump forward to the end of the match for (; i < input.length; i++) { lookAheadMatches = 0; - //potential match between delimiter and input string + // potential match between delimiter and input string if (delimiter.length > 0 && input[i] == delimiter[0]) { - //look ahead to see if the entire delimiter matches the input string + // look ahead to see if the entire delimiter matches the input string for (int j = 0; j < delimiter.length; j++) { if (i + j < input.length && input[i + j] == delimiter[j]) { lookAheadMatches++; } } - //found a full delimiter match + // found a full delimiter match if (lookAheadMatches == delimiter.length) { - //record the key/value tuple + // record the key/value tuple byte[] value = Arrays.copyOfRange(input, valueStart, i); dissectMatch.add(key, new String(value, StandardCharsets.UTF_8)); - //jump to the end of the match + // jump to the end of the match i += lookAheadMatches; - //look for consecutive delimiters (e.g. a,,,,d,e) + // look for consecutive delimiters (e.g. a,,,,d,e) while (i < input.length) { lookAheadMatches = 0; for (int j = 0; j < delimiter.length; j++) { @@ -249,39 +257,39 @@ public Map parse(String inputString) { lookAheadMatches++; } } - //found consecutive delimiters + // found consecutive delimiters if (lookAheadMatches == delimiter.length) { - //jump to the end of the match + // jump to the end of the match i += lookAheadMatches; if (!key.skipRightPadding()) { - //progress the keys/delimiter if possible + // progress the keys/delimiter if possible if (!it.hasNext()) { - break; //the while loop + break; // the while loop } dissectPair = it.next(); key = dissectPair.getKey(); - //add the key with an empty value for the empty delimiter + // add the key with an empty value for the empty delimiter dissectMatch.add(key, ""); } } else { - break; //the while loop + break; // the while loop } } - //progress the keys/delimiter if possible + // progress the keys/delimiter if possible if (!it.hasNext()) { - break; //the for loop + break; // the for loop } dissectPair = it.next(); key = dissectPair.getKey(); delimiter = dissectPair.getDelimiter().getBytes(StandardCharsets.UTF_8); - //i is always one byte after the last found delimiter, aka the start of the next value + // i is always one byte after the last found delimiter, aka the start of the next value valueStart = i; } } } - //the last key, grab the rest of the input (unless consecutive delimiters already grabbed the last key) - //and there is no trailing delimiter - if (!dissectMatch.fullyMatched() && delimiter.length == 0 ) { + // the last key, grab the rest of the input (unless consecutive delimiters already grabbed the last key) + // and there is no trailing delimiter + if (!dissectMatch.fullyMatched() && delimiter.length == 0) { byte[] value = Arrays.copyOfRange(input, valueStart, input.length); String valueString = new String(value, StandardCharsets.UTF_8); dissectMatch.add(key, valueString); @@ -318,6 +326,3 @@ private String getDelimiter() { } } - - - diff --git a/libs/dissect/src/test/java/org/opensearch/dissect/DissectKeyTests.java b/libs/dissect/src/test/java/org/opensearch/dissect/DissectKeyTests.java index 185b294a72d1a..a8d282506a58d 100644 --- a/libs/dissect/src/test/java/org/opensearch/dissect/DissectKeyTests.java +++ b/libs/dissect/src/test/java/org/opensearch/dissect/DissectKeyTests.java @@ -135,7 +135,8 @@ public void testRightPaddingModifiers() { public void testMultipleLeftModifiers() { String keyName = randomAlphaOfLengthBetween(1, 10); - List validModifiers = EnumSet.allOf(DissectKey.Modifier.class).stream() + List validModifiers = EnumSet.allOf(DissectKey.Modifier.class) + .stream() .filter(m -> !m.equals(DissectKey.Modifier.NONE)) .map(DissectKey.Modifier::toString) .collect(Collectors.toList()); @@ -154,9 +155,10 @@ public void testSkipKey() { assertThat(dissectKey.getAppendPosition(), equalTo(0)); assertThat(dissectKey.getName(), equalTo(keyName)); } + public void testNamedSkipKey() { String keyName = "myname"; - DissectKey dissectKey = new DissectKey("?" +keyName); + DissectKey dissectKey = new DissectKey("?" + keyName); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NAMED_SKIP)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(false)); @@ -166,16 +168,17 @@ public void testNamedSkipKey() { public void testSkipKeyWithPadding() { String keyName = ""; - DissectKey dissectKey = new DissectKey(keyName + "->"); + DissectKey dissectKey = new DissectKey(keyName + "->"); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NONE)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(true)); assertThat(dissectKey.getAppendPosition(), equalTo(0)); assertThat(dissectKey.getName(), equalTo(keyName)); } + public void testNamedEmptySkipKeyWithPadding() { String keyName = ""; - DissectKey dissectKey = new DissectKey("?" +keyName + "->"); + DissectKey dissectKey = new DissectKey("?" + keyName + "->"); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NAMED_SKIP)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(true)); @@ -184,7 +187,7 @@ public void testNamedEmptySkipKeyWithPadding() { } public void testInvalidModifiers() { - //should never happen due to regex + // should never happen due to regex IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> DissectKey.Modifier.fromString("x")); assertThat(e.getMessage(), CoreMatchers.containsString("invalid modifier")); } diff --git a/libs/dissect/src/test/java/org/opensearch/dissect/DissectMatchTests.java b/libs/dissect/src/test/java/org/opensearch/dissect/DissectMatchTests.java index 289322ff6465a..0ec617b3d4894 100644 --- a/libs/dissect/src/test/java/org/opensearch/dissect/DissectMatchTests.java +++ b/libs/dissect/src/test/java/org/opensearch/dissect/DissectMatchTests.java @@ -51,8 +51,8 @@ public void testIllegalArgs() { public void testValidAndFullyMatched() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.fullyMatched(), equalTo(true)); assertThat(dissectMatch.isValid(dissectMatch.getResults()), equalTo(true)); } @@ -60,21 +60,21 @@ public void testValidAndFullyMatched() { public void testNotValidAndFullyMatched() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches - 1) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches - 1) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.fullyMatched(), equalTo(false)); assertThat(dissectMatch.isValid(dissectMatch.getResults()), equalTo(false)); } - public void testGetResultsIdempotent(){ + public void testGetResultsIdempotent() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.getResults(), equalTo(dissectMatch.getResults())); } - public void testAppend(){ + public void testAppend() { DissectMatch dissectMatch = new DissectMatch("-", 3, 1, 3, 0); dissectMatch.add(new DissectKey("+a"), "x"); dissectMatch.add(new DissectKey("+a"), "y"); @@ -84,7 +84,7 @@ public void testAppend(){ assertThat(results, equalTo(MapBuilder.newMapBuilder().put("a", "x-y-z").map())); } - public void testAppendWithOrder(){ + public void testAppendWithOrder() { DissectMatch dissectMatch = new DissectMatch("-", 3, 1, 3, 0); dissectMatch.add(new DissectKey("+a/3"), "x"); dissectMatch.add(new DissectKey("+a"), "y"); @@ -94,7 +94,7 @@ public void testAppendWithOrder(){ assertThat(results, equalTo(MapBuilder.newMapBuilder().put("a", "y-z-x").map())); } - public void testReference(){ + public void testReference() { DissectMatch dissectMatch = new DissectMatch("-", 2, 1, 0, 1); dissectMatch.add(new DissectKey("&a"), "x"); dissectMatch.add(new DissectKey("*a"), "y"); diff --git a/libs/dissect/src/test/java/org/opensearch/dissect/DissectParserTests.java b/libs/dissect/src/test/java/org/opensearch/dissect/DissectParserTests.java index a647a1cc6281f..fcd0dc8e248aa 100644 --- a/libs/dissect/src/test/java/org/opensearch/dissect/DissectParserTests.java +++ b/libs/dissect/src/test/java/org/opensearch/dissect/DissectParserTests.java @@ -77,46 +77,103 @@ public void testLogstashSpecs() { assertMatch("%{a} %{b} %{+b} %{z}", "foo bar baz quux", Arrays.asList("a", "b", "z"), Arrays.asList("foo", "bar baz", "quux"), " "); assertMatch("%{a}------->%{b}", "foo------->bar baz quux", Arrays.asList("a", "b"), Arrays.asList("foo", "bar baz quux")); assertMatch("%{a}------->%{}", "foo------->bar baz quux", Arrays.asList("a"), Arrays.asList("foo")); - assertMatch("%{a} » %{b}»%{c}€%{d}", "foo » bar»baz€quux", - Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "bar", "baz", "quux")); + assertMatch( + "%{a} » %{b}»%{c}€%{d}", + "foo » bar»baz€quux", + Arrays.asList("a", "b", "c", "d"), + Arrays.asList("foo", "bar", "baz", "quux") + ); assertMatch("%{a} %{b} %{+a}", "foo bar baz quux", Arrays.asList("a", "b"), Arrays.asList("foo baz quux", "bar"), " "); - //Logstash supports implicit ordering based anchored by the key without the '+' - //This implementation will only honor implicit ordering for appending right to left else explicit order (/N) is required. - //The results of this test differ from Logstash. - assertMatch("%{+a} %{a} %{+a} %{b}", "December 31 1999 quux", - Arrays.asList("a", "b"), Arrays.asList("December 31 1999", "quux"), " "); - //Same test as above, but with same result as Logstash using explicit ordering in the pattern - assertMatch("%{+a/1} %{a} %{+a/2} %{b}", "December 31 1999 quux", - Arrays.asList("a", "b"), Arrays.asList("31 December 1999", "quux"), " "); + // Logstash supports implicit ordering based anchored by the key without the '+' + // This implementation will only honor implicit ordering for appending right to left else explicit order (/N) is required. + // The results of this test differ from Logstash. + assertMatch( + "%{+a} %{a} %{+a} %{b}", + "December 31 1999 quux", + Arrays.asList("a", "b"), + Arrays.asList("December 31 1999", "quux"), + " " + ); + // Same test as above, but with same result as Logstash using explicit ordering in the pattern + assertMatch( + "%{+a/1} %{a} %{+a/2} %{b}", + "December 31 1999 quux", + Arrays.asList("a", "b"), + Arrays.asList("31 December 1999", "quux"), + " " + ); assertMatch("%{+a/2} %{+a/4} %{+a/1} %{+a/3}", "bar quux foo baz", Arrays.asList("a"), Arrays.asList("foo bar baz quux"), " "); assertMatch("%{+a} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - assertMatch("%{+a} %{b} %{+a} %{c}", "foo bar baz quux", - Arrays.asList("a", "b", "c"), Arrays.asList("foo baz", "bar", "quux"), " "); - assertMatch("%{} %{syslog_timestamp} %{hostname} %{rt}: %{reason} %{+reason} %{src_ip}/%{src_port}->%{dst_ip}/%{dst_port} " + - "%{polrt} %{+polrt} %{+polrt} %{from_zone} %{to_zone} %{rest}", - "42 2016-05-25T14:47:23Z host.name.com RT_FLOW - RT_FLOW_SESSION_DENY: session denied 2.2.2.20/60000->1.1.1.10/8090 None " + - "6(0) DEFAULT-DENY ZONE-UNTRUST ZONE-DMZ UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0", - Arrays.asList("syslog_timestamp", "hostname", "rt", "reason", "src_ip", "src_port", "dst_ip", "dst_port", "polrt" - , "from_zone", "to_zone", "rest"), - Arrays.asList("2016-05-25T14:47:23Z", "host.name.com", "RT_FLOW - RT_FLOW_SESSION_DENY", "session denied", "2.2.2.20", "60000" - , "1.1.1.10", "8090", "None 6(0) DEFAULT-DENY", "ZONE-UNTRUST", "ZONE-DMZ", "UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0"), " "); + assertMatch( + "%{+a} %{b} %{+a} %{c}", + "foo bar baz quux", + Arrays.asList("a", "b", "c"), + Arrays.asList("foo baz", "bar", "quux"), + " " + ); + assertMatch( + "%{} %{syslog_timestamp} %{hostname} %{rt}: %{reason} %{+reason} %{src_ip}/%{src_port}->%{dst_ip}/%{dst_port} " + + "%{polrt} %{+polrt} %{+polrt} %{from_zone} %{to_zone} %{rest}", + "42 2016-05-25T14:47:23Z host.name.com RT_FLOW - RT_FLOW_SESSION_DENY: session denied 2.2.2.20/60000->1.1.1.10/8090 None " + + "6(0) DEFAULT-DENY ZONE-UNTRUST ZONE-DMZ UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0", + Arrays.asList( + "syslog_timestamp", + "hostname", + "rt", + "reason", + "src_ip", + "src_port", + "dst_ip", + "dst_port", + "polrt", + "from_zone", + "to_zone", + "rest" + ), + Arrays.asList( + "2016-05-25T14:47:23Z", + "host.name.com", + "RT_FLOW - RT_FLOW_SESSION_DENY", + "session denied", + "2.2.2.20", + "60000", + "1.1.1.10", + "8090", + "None 6(0) DEFAULT-DENY", + "ZONE-UNTRUST", + "ZONE-DMZ", + "UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0" + ), + " " + ); assertBadKey("%{+/2}"); assertBadKey("%{&+a_field}"); - assertMatch("%{a->} %{b->}---%{c}", "foo bar------------baz", - Arrays.asList("a", "b", "c"), Arrays.asList("foo", "bar", "baz")); + assertMatch( + "%{a->} %{b->}---%{c}", + "foo bar------------baz", + Arrays.asList("a", "b", "c"), + Arrays.asList("foo", "bar", "baz") + ); assertMatch("%{->}-%{a}", "-----666", Arrays.asList("a"), Arrays.asList("666")); assertMatch("%{?skipme->}-%{a}", "-----666", Arrays.asList("a"), Arrays.asList("666")); - assertMatch("%{a},%{b},%{c},%{d},%{e},%{f}", "111,,333,,555,666", - Arrays.asList("a", "b", "c", "d", "e", "f"), Arrays.asList("111", "", "333", "", "555", "666")); + assertMatch( + "%{a},%{b},%{c},%{d},%{e},%{f}", + "111,,333,,555,666", + Arrays.asList("a", "b", "c", "d", "e", "f"), + Arrays.asList("111", "", "333", "", "555", "666") + ); assertMatch("%{a}.࿏.%{b}", "⟳༒.࿏.༒⟲", Arrays.asList("a", "b"), Arrays.asList("⟳༒", "༒⟲")); assertMatch("%{a}", "子", Arrays.asList("a"), Arrays.asList("子")); assertMatch("%{a}{\n}%{b}", "aaa{\n}bbb", Arrays.asList("a", "b"), Arrays.asList("aaa", "bbb")); assertMiss("MACHINE[%{a}] %{b}", "1234567890 MACHINE[foo] bar"); assertMiss("%{a} %{b} %{c}", "foo:bar:baz"); assertMatch("/var/%{key1}/log/%{key2}.log", "/var/foo/log/bar.log", Arrays.asList("key1", "key2"), Arrays.asList("foo", "bar")); - assertMatch("%{a->} %{b}-.-%{c}-%{d}-..-%{e}-%{f}-%{g}-%{h}", "foo bar-.-baz-1111-..-22-333-4444-55555", + assertMatch( + "%{a->} %{b}-.-%{c}-%{d}-..-%{e}-%{f}-%{g}-%{h}", + "foo bar-.-baz-1111-..-22-333-4444-55555", Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"), - Arrays.asList("foo", "bar", "baz", "1111", "22", "333", "4444", "55555")); + Arrays.asList("foo", "bar", "baz", "1111", "22", "333", "4444", "55555") + ); } public void testBasicMatch() { @@ -124,12 +181,13 @@ public void testBasicMatch() { String keyFirstPattern = ""; String delimiterFirstInput = ""; String delimiterFirstPattern = ""; - //parallel arrays + // parallel arrays List expectedKeys = new ArrayList<>(Sets.newSet(generateRandomStringArray(100, 10, false, false))); List expectedValues = new ArrayList<>(expectedKeys.size()); for (String key : expectedKeys) { String value = randomAsciiAlphanumOfLengthBetween(1, 100); - String delimiter = Integer.toString(randomInt()); //int to ensures values and delimiters don't overlap, else validation can fail + String delimiter = Integer.toString(randomInt()); // int to ensures values and delimiters don't overlap, else validation can + // fail keyFirstPattern += "%{" + key + "}" + delimiter; valueFirstInput += value + delimiter; delimiterFirstPattern += delimiter + "%{" + key + "}"; @@ -145,7 +203,7 @@ public void testBasicMatchUnicode() { String keyFirstPattern = ""; String delimiterFirstInput = ""; String delimiterFirstPattern = ""; - //parallel arrays + // parallel arrays List expectedKeys = new ArrayList<>(); List expectedValues = new ArrayList<>(); for (int i = 0; i < randomIntBetween(1, 100); i++) { @@ -154,7 +212,8 @@ public void testBasicMatchUnicode() { key = randomAsciiAlphanumOfLengthBetween(1, 100); } String value = randomRealisticUnicodeOfCodepointLengthBetween(1, 100); - String delimiter = Integer.toString(randomInt()); //int to ensures values and delimiters don't overlap, else validation can fail + String delimiter = Integer.toString(randomInt()); // int to ensures values and delimiters don't overlap, else validation can + // fail keyFirstPattern += "%{" + key + "}" + delimiter; valueFirstInput += value + delimiter; delimiterFirstPattern += delimiter + "%{" + key + "}"; @@ -196,8 +255,12 @@ public void testAssociate() { assertMatch("%{*a} %{&a}", "foo bar", Arrays.asList("foo"), Arrays.asList("bar")); assertMatch("%{&a} %{*a}", "foo bar", Arrays.asList("bar"), Arrays.asList("foo")); assertMatch("%{*a} %{&a} %{*b} %{&b}", "foo bar baz lol", Arrays.asList("foo", "baz"), Arrays.asList("bar", "lol")); - assertMatch("%{*a} %{&a} %{c} %{*b} %{&b}", "foo bar x baz lol", - Arrays.asList("foo", "baz", "c"), Arrays.asList("bar", "lol", "x")); + assertMatch( + "%{*a} %{&a} %{c} %{*b} %{&b}", + "foo bar x baz lol", + Arrays.asList("foo", "baz", "c"), + Arrays.asList("bar", "lol", "x") + ); assertBadPattern("%{*a} %{a}"); assertBadPattern("%{a} %{&a}"); assertMiss("%{*a} %{&a} {a} %{*b} %{&b}", "foo bar x baz lol"); @@ -205,8 +268,12 @@ public void testAssociate() { public void testAppendAndAssociate() { assertMatch("%{a} %{+a} %{*b} %{&b}", "foo bar baz lol", Arrays.asList("a", "baz"), Arrays.asList("foobar", "lol")); - assertMatch("%{a->} %{+a/2} %{+a/1} %{*b} %{&b}", "foo bar baz lol x", - Arrays.asList("a", "lol"), Arrays.asList("foobazbar", "x")); + assertMatch( + "%{a->} %{+a/2} %{+a/1} %{*b} %{&b}", + "foo bar baz lol x", + Arrays.asList("a", "lol"), + Arrays.asList("foobazbar", "x") + ); } public void testEmptyKey() { @@ -230,19 +297,19 @@ public void testNamedSkipKey() { } public void testConsecutiveDelimiters() { - //leading + // leading assertMatch("%{->},%{a}", ",,,,,foo", Arrays.asList("a"), Arrays.asList("foo")); assertMatch("%{a->},%{b}", ",,,,,foo", Arrays.asList("a", "b"), Arrays.asList("", "foo")); - //trailing + // trailing assertMatch("%{a->},", "foo,,,,,", Arrays.asList("a"), Arrays.asList("foo")); assertMatch("%{a} %{b},", "foo bar,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a} %{b->},", "foo bar,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //middle + // middle assertMatch("%{a->},%{b}", "foo,,,,,bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->}x%{b}", "fooxxxxxbar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} xyz%{b}", "foo xyz xyz xyz xyz xyzbar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //skipped with empty values + // skipped with empty values assertMatch("%{a},%{b},%{c},%{d}", "foo,,,", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "", "")); assertMatch("%{a},%{b},%{c},%{d}", "foo,,bar,baz", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "bar", "baz")); assertMatch("%{a},%{b},%{c},%{d}", "foo,,,baz", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "", "baz")); @@ -259,16 +326,20 @@ public void testSkipRightPadding() { assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{->} %{a}", "foo bar", Arrays.asList("a"), Arrays.asList("bar")); - assertMatch("%{a->} %{+a->} %{*b->} %{&b->} %{c}", "foo bar baz lol x", - Arrays.asList("a", "baz", "c"), Arrays.asList("foobar", "lol", "x")); + assertMatch( + "%{a->} %{+a->} %{*b->} %{&b->} %{c}", + "foo bar baz lol x", + Arrays.asList("a", "baz", "c"), + Arrays.asList("foobar", "lol", "x") + ); } public void testTrimmedEnd() { assertMatch("%{a} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a} %{b->} ", "foo bar ", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //only whitespace is trimmed in the absence of trailing characters + // only whitespace is trimmed in the absence of trailing characters assertMatch("%{a} %{b->}", "foo bar,,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar,,,,,,")); - //consecutive delimiters + right padding can be used to skip over the trailing delimiters + // consecutive delimiters + right padding can be used to skip over the trailing delimiters assertMatch("%{a} %{b->},", "foo bar,,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); } @@ -305,23 +376,50 @@ public void testBadPatternOrKey() { } public void testSyslog() { - assertMatch("%{timestamp} %{+timestamp} %{+timestamp} %{logsource} %{program}[%{pid}]: %{message}", + assertMatch( + "%{timestamp} %{+timestamp} %{+timestamp} %{logsource} %{program}[%{pid}]: %{message}", "Mar 16 00:01:25 evita postfix/smtpd[1713]: connect from camomile.cloud9.net[168.100.1.3]", Arrays.asList("timestamp", "logsource", "program", "pid", "message"), - Arrays.asList("Mar 16 00:01:25", "evita", "postfix/smtpd", "1713", "connect from camomile.cloud9.net[168.100.1.3]"), " "); + Arrays.asList("Mar 16 00:01:25", "evita", "postfix/smtpd", "1713", "connect from camomile.cloud9.net[168.100.1.3]"), + " " + ); } public void testApacheLog() { - assertMatch("%{clientip} %{ident} %{auth} [%{timestamp}] \"%{verb} %{request} HTTP/%{httpversion}\" %{response} %{bytes}" + - " \"%{referrer}\" \"%{agent}\" %{->}", - "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + - "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\"", - Arrays.asList("clientip", "ident", "auth", "timestamp", "verb", "request", "httpversion", "response", "bytes", - "referrer", "agent"), - Arrays.asList("31.184.238.164", "-", "-", "24/Jul/2014:05:35:37 +0530", "GET", "/logs/access.log", "1.0", "200", "69849", - "http://8rursodiol.enjin.com", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36" + - " (KHTML, like Gecko) Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36")); + assertMatch( + "%{clientip} %{ident} %{auth} [%{timestamp}] \"%{verb} %{request} HTTP/%{httpversion}\" %{response} %{bytes}" + + " \"%{referrer}\" \"%{agent}\" %{->}", + "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + + "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\"", + Arrays.asList( + "clientip", + "ident", + "auth", + "timestamp", + "verb", + "request", + "httpversion", + "response", + "bytes", + "referrer", + "agent" + ), + Arrays.asList( + "31.184.238.164", + "-", + "-", + "24/Jul/2014:05:35:37 +0530", + "GET", + "/logs/access.log", + "1.0", + "200", + "69849", + "http://8rursodiol.enjin.com", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36" + + " (KHTML, like Gecko) Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36" + ) + ); } /** @@ -357,7 +455,7 @@ public void testJsonSpecification() throws Exception { } } - private DissectException assertFail(String pattern, String input){ + private DissectException assertFail(String pattern, String input) { return expectThrows(DissectException.class, () -> new DissectParser(pattern, null).parse(input)); } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/Circle.java b/libs/geo/src/main/java/org/opensearch/geometry/Circle.java index 8669fd51fa131..6f8b0dc6929cc 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/Circle.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/Circle.java @@ -61,7 +61,7 @@ public Circle(final double x, final double y, final double z, final double radiu this.x = x; this.radiusMeters = radiusMeters; this.z = z; - if (radiusMeters < 0 ) { + if (radiusMeters < 0) { throw new IllegalArgumentException("Circle radius [" + radiusMeters + "] cannot be negative"); } } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/Line.java b/libs/geo/src/main/java/org/opensearch/geometry/Line.java index 14819c3f42547..7e6806eeba5cf 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/Line.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/Line.java @@ -156,8 +156,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Line line = (Line) o; - return Arrays.equals(y, line.y) && - Arrays.equals(x, line.x) && Arrays.equals(z, line.z); + return Arrays.equals(y, line.y) && Arrays.equals(x, line.x) && Arrays.equals(z, line.z); } @Override diff --git a/libs/geo/src/main/java/org/opensearch/geometry/LinearRing.java b/libs/geo/src/main/java/org/opensearch/geometry/LinearRing.java index 10ba0f97b749d..d9348495930cc 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/LinearRing.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/LinearRing.java @@ -42,8 +42,7 @@ public class LinearRing extends Line { public static final LinearRing EMPTY = new LinearRing(); - private LinearRing() { - } + private LinearRing() {} public LinearRing(double[] x, double[] y) { this(x, y, null); @@ -56,10 +55,22 @@ public LinearRing(double[] x, double[] y, double[] z) { } int last = x.length - 1; if (x[0] != x[last] || y[0] != y[last] || (z != null && z[0] != z[last])) { - throw new IllegalArgumentException("first and last points of the linear ring must be the same (it must close itself):" + - " x[0]=" + x[0] + " x[" + last + "]=" + x[last] + - " y[0]=" + y[0] + " y[" + last + "]=" + y[last] + - (z == null ? "" : " z[0]=" + z[0] + " z[" + last + "]=" + z[last] )); + throw new IllegalArgumentException( + "first and last points of the linear ring must be the same (it must close itself):" + + " x[0]=" + + x[0] + + " x[" + + last + + "]=" + + x[last] + + " y[0]=" + + y[0] + + " y[" + + last + + "]=" + + y[last] + + (z == null ? "" : " z[0]=" + z[0] + " z[" + last + "]=" + z[last]) + ); } } @@ -75,8 +86,11 @@ public T visit(GeometryVisitor visitor) throws E @Override public String toString() { - return "linearring(x=" + Arrays.toString(getX()) + - ", y=" + Arrays.toString(getY()) + - (hasZ() ? ", z=" + Arrays.toString(getZ()) : "") + ")"; + return "linearring(x=" + + Arrays.toString(getX()) + + ", y=" + + Arrays.toString(getY()) + + (hasZ() ? ", z=" + Arrays.toString(getZ()) : "") + + ")"; } } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/MultiLine.java b/libs/geo/src/main/java/org/opensearch/geometry/MultiLine.java index 37873488996ea..07d3e52a7c5b8 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/MultiLine.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/MultiLine.java @@ -40,8 +40,7 @@ public class MultiLine extends GeometryCollection { public static final MultiLine EMPTY = new MultiLine(); - private MultiLine() { - } + private MultiLine() {} public MultiLine(List lines) { super(lines); diff --git a/libs/geo/src/main/java/org/opensearch/geometry/MultiPoint.java b/libs/geo/src/main/java/org/opensearch/geometry/MultiPoint.java index 0b7f4dcb93038..6cbf2d0500463 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/MultiPoint.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/MultiPoint.java @@ -40,8 +40,7 @@ public class MultiPoint extends GeometryCollection { public static final MultiPoint EMPTY = new MultiPoint(); - private MultiPoint() { - } + private MultiPoint() {} public MultiPoint(List points) { super(points); diff --git a/libs/geo/src/main/java/org/opensearch/geometry/MultiPolygon.java b/libs/geo/src/main/java/org/opensearch/geometry/MultiPolygon.java index 35c140774e9ef..749b55d69ebbc 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/MultiPolygon.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/MultiPolygon.java @@ -40,8 +40,7 @@ public class MultiPolygon extends GeometryCollection { public static final MultiPolygon EMPTY = new MultiPolygon(); - private MultiPolygon() { - } + private MultiPolygon() {} public MultiPolygon(List polygons) { super(polygons); diff --git a/libs/geo/src/main/java/org/opensearch/geometry/Polygon.java b/libs/geo/src/main/java/org/opensearch/geometry/Polygon.java index f372d6ff17743..0fb5167c40309 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/Polygon.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/Polygon.java @@ -135,8 +135,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Polygon polygon1 = (Polygon) o; - return Objects.equals(polygon, polygon1.polygon) && - Objects.equals(holes, polygon1.holes); + return Objects.equals(polygon, polygon1.polygon) && Objects.equals(holes, polygon1.holes); } @Override diff --git a/libs/geo/src/main/java/org/opensearch/geometry/Rectangle.java b/libs/geo/src/main/java/org/opensearch/geometry/Rectangle.java index 699087a89fe6d..9ea4a3b30026f 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/Rectangle.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/Rectangle.java @@ -82,6 +82,7 @@ private Rectangle() { public Rectangle(double minX, double maxX, double maxY, double minY) { this(minX, maxX, maxY, minY, Double.NaN, Double.NaN); } + /** * Constructs a bounding box by first validating the provided latitude and longitude coordinates */ @@ -133,7 +134,6 @@ public double getMinLon() { return minX; } - public double getMinAlt() { return minZ; } @@ -160,7 +160,6 @@ public String toString() { return WellKnownText.INSTANCE.toWKT(this); } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/libs/geo/src/main/java/org/opensearch/geometry/package-info.java b/libs/geo/src/main/java/org/opensearch/geometry/package-info.java index 19dc6d8dcbd5e..ffab6e99ae9d7 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/package-info.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/package-info.java @@ -25,7 +25,6 @@ * under the License. */ - /** * Common Geometry classes */ diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/BitUtil.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/BitUtil.java index 8c742695d1ecb..664e7e68d96a5 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/BitUtil.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/BitUtil.java @@ -36,13 +36,15 @@ */ public class BitUtil { // magic numbers for bit interleaving private static final long MAGIC[] = { - 0x5555555555555555L, 0x3333333333333333L, - 0x0F0F0F0F0F0F0F0FL, 0x00FF00FF00FF00FFL, - 0x0000FFFF0000FFFFL, 0x00000000FFFFFFFFL, - 0xAAAAAAAAAAAAAAAAL - }; + 0x5555555555555555L, + 0x3333333333333333L, + 0x0F0F0F0F0F0F0F0FL, + 0x00FF00FF00FF00FFL, + 0x0000FFFF0000FFFFL, + 0x00000000FFFFFFFFL, + 0xAAAAAAAAAAAAAAAAL }; // shift values for bit interleaving - private static final short SHIFT[] = {1, 2, 4, 8, 16}; + private static final short SHIFT[] = { 1, 2, 4, 8, 16 }; /** * Interleaves the first 32 bits of each long value @@ -63,7 +65,7 @@ public static long interleave(int even, int odd) { v2 = (v2 | (v2 << SHIFT[1])) & MAGIC[1]; v2 = (v2 | (v2 << SHIFT[0])) & MAGIC[0]; - return (v2<<1) | v1; + return (v2 << 1) | v1; } /** @@ -83,6 +85,6 @@ public static long deinterleave(long b) { * flip flops odd with even bits */ public static final long flipFlop(final long b) { - return ((b & MAGIC[6]) >>> 1) | ((b & MAGIC[0]) << 1 ); + return ((b & MAGIC[6]) >>> 1) | ((b & MAGIC[0]) << 1); } } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/GeographyValidator.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/GeographyValidator.java index 2d9d2482b1012..bfde23c3da2c8 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/GeographyValidator.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/GeographyValidator.java @@ -83,7 +83,8 @@ public GeographyValidator(boolean ignoreZValue) { protected void checkLatitude(double latitude) { if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) { throw new IllegalArgumentException( - "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL); + "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL + ); } } @@ -93,14 +94,16 @@ protected void checkLatitude(double latitude) { protected void checkLongitude(double longitude) { if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) { throw new IllegalArgumentException( - "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL); + "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL + ); } } protected void checkAltitude(double zValue) { if (ignoreZValue == false && Double.isNaN(zValue) == false) { - throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] " - + "parameter is [" + ignoreZValue + "]"); + throw new IllegalArgumentException( + "found Z value [" + zValue + "] but [ignore_z_value] " + "parameter is [" + ignoreZValue + "]" + ); } } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/Geohash.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/Geohash.java index 5b98bc3b2f2f0..8b3b841e221e5 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/Geohash.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/Geohash.java @@ -48,21 +48,51 @@ * NOTE: this will replace {@code org.opensearch.common.geo.GeoHashUtils} */ public class Geohash { - private static final char[] BASE_32 = {'0', '1', '2', '3', '4', '5', '6', - '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + private static final char[] BASE_32 = { + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'j', + 'k', + 'm', + 'n', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z' }; private static final String BASE_32_STRING = new String(BASE_32); /** maximum precision for geohash strings */ public static final int PRECISION = 12; /** number of bits used for quantizing latitude and longitude values */ private static final short BITS = 32; - private static final double LAT_SCALE = (0x1L<<(BITS-1))/180.0D; - private static final double LAT_DECODE = 180.0D/(0x1L<>> 4) << shift); Point topRight = new Point(decodeLongitude(mortonHash), decodeLatitude(mortonHash)); return new Rectangle(bottomLeft.getX(), topRight.getX(), topRight.getY(), bottomLeft.getY()); } else { // We cannot go north of north pole, so just using 90 degrees instead of calculating it using // add 1 to lon to get lon of topRight, we are going to use 90 for lat - ghLong = BitUtil.interleave((int)lat, (int)(lon + 1)) << 4 | len; + ghLong = BitUtil.interleave((int) lat, (int) (lon + 1)) << 4 | len; final long mortonHash = BitUtil.flipFlop((ghLong >>> 4) << shift); Point topRight = new Point(decodeLongitude(mortonHash), decodeLatitude(mortonHash)); return new Rectangle(bottomLeft.getX(), topRight.getX(), 90D, bottomLeft.getY()); @@ -127,9 +156,9 @@ public static Rectangle toBoundingBox(final String geohash) { /** Array of geohashes one level below the baseGeohash. Sorted. */ public static String[] getSubGeohashes(String baseGeohash) { String[] hashes = new String[BASE_32.length]; - for (int i = 0; i < BASE_32.length; i++) {//note: already sorted + for (int i = 0; i < BASE_32.length; i++) {// note: already sorted char c = BASE_32[i]; - hashes[i] = baseGeohash+c; + hashes[i] = baseGeohash + c; } return hashes; } @@ -143,6 +172,7 @@ public static String[] getSubGeohashes(String baseGeohash) { public static Collection getNeighbors(String geohash) { return addNeighborsAtLevel(geohash, geohash.length(), new ArrayList(8)); } + /** * Add all geohashes of the cells next to a given geohash to a list. * @@ -162,8 +192,7 @@ public static final > E addNeighbors(String * @param neighbors list to add the neighbors to * @return the given list */ - public static final > E addNeighborsAtLevel(String geohash, - int level, E neighbors) { + public static final > E addNeighborsAtLevel(String geohash, int level, E neighbors) { String south = getNeighbor(geohash, level, 0, -1); String north = getNeighbor(geohash, level, 0, +1); if (north != null) { @@ -194,7 +223,7 @@ public static final > E addNeighborsAtLevel * @return geohash of the defined cell */ public static final String getNeighbor(String geohash, int level, int dx, int dy) { - int cell = BASE_32_STRING.indexOf(geohash.charAt(level -1)); + int cell = BASE_32_STRING.indexOf(geohash.charAt(level - 1)); // Decoding the Geohash bit pattern to determine grid coordinates int x0 = cell & 1; // first bit of x @@ -250,7 +279,7 @@ public static final long longEncode(String hash) { */ public static final long longEncode(final double lon, final double lat, final int level) { // shift to appropriate level - final short msf = (short)(((12 - level) * 5) + (MORTON_OFFSET - 2)); + final short msf = (short) (((12 - level) * 5) + (MORTON_OFFSET - 2)); return ((encodeLatLon(lat, lon) >>> msf) << 4) | level; } @@ -276,13 +305,13 @@ public static final String stringEncode(final double lon, final double lat, fina * Encode to a geohash string from the geohash based long format */ public static final String stringEncode(long geoHashLong) { - int level = (int)geoHashLong&15; + int level = (int) geoHashLong & 15; geoHashLong >>>= 4; char[] chars = new char[level]; do { - chars[--level] = BASE_32[(int) (geoHashLong&31L)]; - geoHashLong>>>=5; - } while(level > 0); + chars[--level] = BASE_32[(int) (geoHashLong & 31L)]; + geoHashLong >>>= 5; + } while (level > 0); return new String(chars); } @@ -299,9 +328,9 @@ private static long longEncode(final String hash, int length) { int level = length - 1; long b; long l = 0L; - for(char c : hash.toCharArray()) { - b = (long)(BASE_32_STRING.indexOf(c)); - l |= (b<<(level--*5)); + for (char c : hash.toCharArray()) { + b = (long) (BASE_32_STRING.indexOf(c)); + l |= (b << (level-- * 5)); if (level < 0) { // We cannot handle more than 12 levels break; @@ -320,12 +349,12 @@ public static long mortonEncode(final String hash) { int level = 11; long b; long l = 0L; - for(char c : hash.toCharArray()) { - b = (long)(BASE_32_STRING.indexOf(c)); + for (char c : hash.toCharArray()) { + b = (long) (BASE_32_STRING.indexOf(c)); if (b < 0) { throw new IllegalArgumentException("unsupported symbol [" + c + "] in geohash [" + hash + "]"); } - l |= (b<<((level--*5) + (MORTON_OFFSET - 2))); + l |= (b << ((level-- * 5) + (MORTON_OFFSET - 2))); if (level < 0) { // We cannot handle more than 12 levels break; @@ -351,7 +380,6 @@ private static long encodeLatLon(final double lat, final double lon) { return BitUtil.interleave(latEnc, lonEnc) >>> 2; } - /** encode latitude to integer */ public static int encodeLatitude(double latitude) { // the maximum possible value cannot be encoded without overflow diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/StandardValidator.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/StandardValidator.java index 748b9fb12bc81..a7482f1bcda5b 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/StandardValidator.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/StandardValidator.java @@ -53,13 +53,14 @@ public class StandardValidator implements GeometryValidator { private final boolean ignoreZValue; public StandardValidator(boolean ignoreZValue) { - this.ignoreZValue = ignoreZValue; + this.ignoreZValue = ignoreZValue; } protected void checkZ(double zValue) { if (ignoreZValue == false && Double.isNaN(zValue) == false) { - throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] " - + "parameter is [" + ignoreZValue + "]"); + throw new IllegalArgumentException( + "found Z value [" + zValue + "] but [ignore_z_value] " + "parameter is [" + ignoreZValue + "]" + ); } } @@ -138,4 +139,3 @@ public Void visit(Rectangle rectangle) throws RuntimeException { } } } - diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java index a92db5155dc6c..ed1d63e6d4fef 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java @@ -446,8 +446,9 @@ private Polygon parsePolygon(StreamTokenizer stream) throws IOException, ParseEx private void closeLinearRingIfCoerced(ArrayList lats, ArrayList lons, ArrayList alts) { if (coerce && lats.isEmpty() == false && lons.isEmpty() == false) { int last = lats.size() - 1; - if (!lats.get(0).equals(lats.get(last)) || !lons.get(0).equals(lons.get(last)) || - (alts.isEmpty() == false && !alts.get(0).equals(alts.get(last)))) { + if (!lats.get(0).equals(lats.get(last)) + || !lons.get(0).equals(lons.get(last)) + || (alts.isEmpty() == false && !alts.get(0).equals(alts.get(last)))) { lons.add(lons.get(0)); lats.add(lats.get(0)); if (alts.isEmpty() == false) { @@ -486,7 +487,6 @@ private Rectangle parseBBox(StreamTokenizer stream) throws IOException, ParseExc return new Rectangle(minLon, maxLon, maxLat, minLat); } - private Circle parseCircle(StreamTokenizer stream) throws IOException, ParseException { if (nextEmptyOrOpen(stream).equals(EMPTY)) { return Circle.EMPTY; @@ -561,8 +561,7 @@ private String nextEmptyOrOpen(StreamTokenizer stream) throws IOException, Parse if (next.equals(EMPTY) || next.equals(LPAREN)) { return next; } - throw new ParseException("expected " + EMPTY + " or " + LPAREN - + " but found: " + tokenString(stream), stream.lineno()); + throw new ParseException("expected " + EMPTY + " or " + LPAREN + " but found: " + tokenString(stream), stream.lineno()); } private String nextCloser(StreamTokenizer stream) throws IOException, ParseException { @@ -591,8 +590,7 @@ private String nextCloserOrComma(StreamTokenizer stream) throws IOException, Par if (token.equals(COMMA) || token.equals(RPAREN)) { return token; } - throw new ParseException("expected " + COMMA + " or " + RPAREN - + " but found: " + tokenString(stream), stream.lineno()); + throw new ParseException("expected " + COMMA + " or " + RPAREN + " but found: " + tokenString(stream), stream.lineno()); } private static String getWKTName(Geometry geometry) { diff --git a/libs/geo/src/test/java/org/opensearch/geometry/BaseGeometryTestCase.java b/libs/geo/src/test/java/org/opensearch/geometry/BaseGeometryTestCase.java index 4a20b96d70f5a..64edf0989d214 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/BaseGeometryTestCase.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/BaseGeometryTestCase.java @@ -87,7 +87,8 @@ public Object visit(Circle circle) { @Override public Object visit(GeometryCollection collection) { - return verify(collection, "GeometryCollection"); } + return verify(collection, "GeometryCollection"); + } @Override public Object visit(Line line) { diff --git a/libs/geo/src/test/java/org/opensearch/geometry/CircleTests.java b/libs/geo/src/test/java/org/opensearch/geometry/CircleTests.java index 4ddc286b922c5..1c7c86106236f 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/CircleTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/CircleTests.java @@ -44,9 +44,13 @@ public class CircleTests extends BaseGeometryTestCase { @Override protected Circle createTestInstance(boolean hasAlt) { if (hasAlt) { - return new Circle(randomDoubleBetween(-180, 180, true), randomDoubleBetween(-90, 90, true), randomDouble(), - randomDoubleBetween(0, 100, false)); - } else { + return new Circle( + randomDoubleBetween(-180, 180, true), + randomDoubleBetween(-90, 90, true), + randomDouble(), + randomDoubleBetween(0, 100, false) + ); + } else { return new Circle(randomDoubleBetween(-180, 180, true), randomDoubleBetween(-90, 90, true), randomDoubleBetween(0, 100, false)); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/GeometryCollectionTests.java b/libs/geo/src/test/java/org/opensearch/geometry/GeometryCollectionTests.java index 4ae66e83dd22e..631b6456a77da 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/GeometryCollectionTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/GeometryCollectionTests.java @@ -50,11 +50,15 @@ protected GeometryCollection createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)", - wkt.toWKT(new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY)))); + assertEquals( + "GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)", + wkt.toWKT(new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY))) + ); - assertEquals(new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY)), - wkt.fromWKT("GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)")); + assertEquals( + new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY)), + wkt.fromWKT("GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)") + ); assertEquals("GEOMETRYCOLLECTION EMPTY", wkt.toWKT(GeometryCollection.EMPTY)); assertEquals(GeometryCollection.EMPTY, wkt.fromWKT("GEOMETRYCOLLECTION EMPTY)")); @@ -68,12 +72,16 @@ public void testInitValidation() { ex = expectThrows(IllegalArgumentException.class, () -> new GeometryCollection<>(null)); assertEquals("the list of shapes cannot be null or empty", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new GeometryCollection<>( - Arrays.asList(new Point(20, 10), new Point(20, 10, 30)))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new GeometryCollection<>(Arrays.asList(new Point(20, 10), new Point(20, 10, 30))) + ); assertEquals("all elements of the collection should have the same number of dimension", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new GeometryCollection(Collections.singletonList(new Point(20, 10, 30))))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate(new GeometryCollection(Collections.singletonList(new Point(20, 10, 30)))) + ); assertEquals("found Z value [30.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate(new GeometryCollection(Collections.singletonList(new Point(20, 10, 30)))); diff --git a/libs/geo/src/test/java/org/opensearch/geometry/GeometryValidatorTests.java b/libs/geo/src/test/java/org/opensearch/geometry/GeometryValidatorTests.java index f2f76a966e3cb..6d21340e64b2b 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/GeometryValidatorTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/GeometryValidatorTests.java @@ -86,7 +86,8 @@ public OneValidator() { protected void checkLatitude(double latitude) { if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) { throw new IllegalArgumentException( - "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL); + "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL + ); } } @@ -94,7 +95,8 @@ protected void checkLatitude(double latitude) { protected void checkLongitude(double longitude) { if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) { throw new IllegalArgumentException( - "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL); + "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL + ); } } @@ -102,7 +104,8 @@ protected void checkLongitude(double longitude) { protected void checkAltitude(double zValue) { if (Double.isNaN(zValue) == false && (zValue < MIN_ALT_INCL || zValue > MAX_ALT_INCL)) { throw new IllegalArgumentException( - "invalid altitude " + zValue + "; must be between " + MIN_ALT_INCL + " and " + MAX_ALT_INCL); + "invalid altitude " + zValue + "; must be between " + MIN_ALT_INCL + " and " + MAX_ALT_INCL + ); } } } @@ -129,12 +132,13 @@ public void testOneValidator() throws Exception { assertEquals("invalid altitude 2.0; must be between -1.0 and 1.0", ex.getMessage()); ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("POLYGON ((0.3 0.1, 0.4 0.2, 5 0.3, 0.3 0.1))")); assertEquals("invalid longitude 5.0; must be between -1.0 and 1.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT( - "POLYGON ((0.3 0.1, 0.4 0.2, 0.5 0.3, 0.3 0.1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); + ex = expectThrows( + IllegalArgumentException.class, + () -> parser.fromWKT("POLYGON ((0.3 0.1, 0.4 0.2, 0.5 0.3, 0.3 0.1), (0.5 1.5, 2.5 1.5, 2.0 1.0))") + ); assertEquals("invalid latitude 1.5; must be between -1.0 and 1.0", ex.getMessage()); ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("MULTIPOINT (0 1, -2 1)")); assertEquals("invalid longitude -2.0; must be between -1.0 and 1.0", ex.getMessage()); } - } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/LineTests.java b/libs/geo/src/test/java/org/opensearch/geometry/LineTests.java index 7d695dbbb1472..846645a99ed73 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/LineTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/LineTests.java @@ -49,13 +49,17 @@ protected Line createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("LINESTRING (3.0 1.0, 4.0 2.0)", wkt.toWKT(new Line(new double[]{3, 4}, new double[]{1, 2}))); - assertEquals(new Line(new double[]{3, 4}, new double[]{1, 2}), wkt.fromWKT("LINESTRING (3 1, 4 2)")); + assertEquals("LINESTRING (3.0 1.0, 4.0 2.0)", wkt.toWKT(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }))); + assertEquals(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }), wkt.fromWKT("LINESTRING (3 1, 4 2)")); - assertEquals("LINESTRING (3.0 1.0 5.0, 4.0 2.0 6.0)", wkt.toWKT(new Line(new double[]{3, 4}, new double[]{1, 2}, - new double[]{5, 6}))); - assertEquals(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}), - wkt.fromWKT("LINESTRING (3 1 6, 4 2 5)")); + assertEquals( + "LINESTRING (3.0 1.0 5.0, 4.0 2.0 6.0)", + wkt.toWKT(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 5, 6 })) + ); + assertEquals( + new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }), + wkt.fromWKT("LINESTRING (3 1 6, 4 2 5)") + ); assertEquals("LINESTRING EMPTY", wkt.toWKT(Line.EMPTY)); assertEquals(Line.EMPTY, wkt.fromWKT("LINESTRING EMPTY)")); @@ -63,28 +67,38 @@ public void testBasicSerialization() throws IOException, ParseException { public void testInitValidation() { GeometryValidator validator = new GeographyValidator(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3}, new double[]{1}))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3 }, new double[] { 1 })) + ); assertEquals("at least two points in the line is required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3, 4, 500, 3}, new double[]{1, 2, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3, 4, 500, 3 }, new double[] { 1, 2, 3, 1 })) + ); assertEquals("invalid longitude 500.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3, 4, 5, 3}, new double[]{1, 100, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3, 4, 5, 3 }, new double[] { 1, 100, 3, 1 })) + ); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 })) + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - new StandardValidator(true).validate(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5})); + new StandardValidator(true).validate(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 })); } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("linestring (3 1 6, 4 2 5)")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("linestring (3 1 6, 4 2 5)") + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/LinearRingTests.java b/libs/geo/src/test/java/org/opensearch/geometry/LinearRingTests.java index 98d4b455aca09..338f470133f44 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/LinearRingTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/LinearRingTests.java @@ -41,46 +41,65 @@ public class LinearRingTests extends OpenSearchTestCase { public void testBasicSerialization() { - UnsupportedOperationException ex = expectThrows(UnsupportedOperationException.class, - () -> new WellKnownText(true, new GeographyValidator(true)) - .toWKT(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}))); + UnsupportedOperationException ex = expectThrows( + UnsupportedOperationException.class, + () -> new WellKnownText(true, new GeographyValidator(true)).toWKT( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }) + ) + ); assertEquals("line ring cannot be serialized using WKT", ex.getMessage()); } public void testInitValidation() { GeometryValidator validator = new GeographyValidator(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 5}, new double[]{1, 2, 3}))); - assertEquals("first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=5.0 y[0]=1.0 " + - "y[2]=3.0", - ex.getMessage()); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 5 }, new double[] { 1, 2, 3 })) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=5.0 y[0]=1.0 " + "y[2]=3.0", + ex.getMessage() + ); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 3}, new double[]{1, 2, 1}, new double[]{1, 2, 3}))); - assertEquals("first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=3.0 y[0]=1.0 " + - "y[2]=1.0 z[0]=1.0 z[2]=3.0", - ex.getMessage()); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 3 }, new double[] { 1, 2, 1 }, new double[] { 1, 2, 3 })) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=3.0 y[0]=1.0 " + + "y[2]=1.0 z[0]=1.0 z[2]=3.0", + ex.getMessage() + ); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3}, new double[]{1}))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new LinearRing(new double[] { 3 }, new double[] { 1 }))); assertEquals("at least two points in the line is required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 500, 3}, new double[]{1, 2, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 500, 3 }, new double[] { 1, 2, 3, 1 })) + ); assertEquals("invalid longitude 500.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 100, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 100, 3, 1 })) + ); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 1, 1, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 1, 1, 1 }) + ) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - new StandardValidator(true).validate(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 1, 1, 1})); + new StandardValidator(true).validate( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 1, 1, 1 }) + ); } public void testVisitor() { - BaseGeometryTestCase.testVisitor(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})); + BaseGeometryTestCase.testVisitor(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/MultiLineTests.java b/libs/geo/src/test/java/org/opensearch/geometry/MultiLineTests.java index ba045cd7e7846..d1979d2ddfa8a 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/MultiLineTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/MultiLineTests.java @@ -57,21 +57,30 @@ protected MultiLine createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("MULTILINESTRING ((3.0 1.0, 4.0 2.0))", wkt.toWKT( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}))))); - assertEquals(new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}))), - wkt.fromWKT("MULTILINESTRING ((3 1, 4 2))")); + assertEquals( + "MULTILINESTRING ((3.0 1.0, 4.0 2.0))", + wkt.toWKT(new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 })))) + ); + assertEquals( + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }))), + wkt.fromWKT("MULTILINESTRING ((3 1, 4 2))") + ); assertEquals("MULTILINESTRING EMPTY", wkt.toWKT(MultiLine.EMPTY)); assertEquals(MultiLine.EMPTY, wkt.fromWKT("MULTILINESTRING EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}))))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate( + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }))) + ) + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5})))); + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }))) + ); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/MultiPointTests.java b/libs/geo/src/test/java/org/opensearch/geometry/MultiPointTests.java index 4373bf3400db5..ed04823463ee4 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/MultiPointTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/MultiPointTests.java @@ -58,28 +58,27 @@ protected MultiPoint createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("MULTIPOINT (2.0 1.0)", wkt.toWKT( - new MultiPoint(Collections.singletonList(new Point(2, 1))))); - assertEquals(new MultiPoint(Collections.singletonList(new Point(2, 1))), - wkt.fromWKT("MULTIPOINT (2 1)")); + assertEquals("MULTIPOINT (2.0 1.0)", wkt.toWKT(new MultiPoint(Collections.singletonList(new Point(2, 1))))); + assertEquals(new MultiPoint(Collections.singletonList(new Point(2, 1))), wkt.fromWKT("MULTIPOINT (2 1)")); - assertEquals("MULTIPOINT (2.0 1.0, 3.0 4.0)", - wkt.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))))); - assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))), - wkt.fromWKT("MULTIPOINT (2 1, 3 4)")); + assertEquals("MULTIPOINT (2.0 1.0, 3.0 4.0)", wkt.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))))); + assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))), wkt.fromWKT("MULTIPOINT (2 1, 3 4)")); - assertEquals("MULTIPOINT (2.0 1.0 10.0, 3.0 4.0 20.0)", - wkt.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))))); - assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))), - wkt.fromWKT("MULTIPOINT (2 1 10, 3 4 20)")); + assertEquals( + "MULTIPOINT (2.0 1.0 10.0, 3.0 4.0 20.0)", + wkt.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20)))) + ); + assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))), wkt.fromWKT("MULTIPOINT (2 1 10, 3 4 20)")); assertEquals("MULTIPOINT EMPTY", wkt.toWKT(MultiPoint.EMPTY)); assertEquals(MultiPoint.EMPTY, wkt.fromWKT("MULTIPOINT EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new MultiPoint(Collections.singletonList(new Point(2, 1, 3))))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate(new MultiPoint(Collections.singletonList(new Point(2, 1, 3)))) + ); assertEquals("found Z value [3.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate(new MultiPoint(Collections.singletonList(new Point(2, 1, 3)))); diff --git a/libs/geo/src/test/java/org/opensearch/geometry/MultiPolygonTests.java b/libs/geo/src/test/java/org/opensearch/geometry/MultiPolygonTests.java index 4944ccbba74ac..7a72aa76d90e3 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/MultiPolygonTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/MultiPolygonTests.java @@ -57,26 +57,44 @@ protected MultiPolygon createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))", - wkt.toWKT(new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))))); - assertEquals(new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))), - wkt.fromWKT("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))")); + assertEquals( + "MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))", + wkt.toWKT( + new MultiPolygon( + Collections.singletonList(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ) + ) + ); + assertEquals( + new MultiPolygon( + Collections.singletonList(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ), + wkt.fromWKT("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))") + ); assertEquals("MULTIPOLYGON EMPTY", wkt.toWKT(MultiPolygon.EMPTY)); assertEquals(MultiPolygon.EMPTY, wkt.fromWKT("MULTIPOLYGON EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1})) - )))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate( + new MultiPolygon( + Collections.singletonList( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ) + ) + ) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate( - new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1}))))); + new MultiPolygon( + Collections.singletonList( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ) + ) + ); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/PointTests.java b/libs/geo/src/test/java/org/opensearch/geometry/PointTests.java index 4d783196fa7b9..f0e71890a7ae6 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/PointTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/PointTests.java @@ -74,8 +74,10 @@ public void testInitValidation() { } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("point (20.0 10.0 100.0)")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("point (20.0 10.0 100.0)") + ); assertEquals("found Z value [100.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/PolygonTests.java b/libs/geo/src/test/java/org/opensearch/geometry/PolygonTests.java index 8954a1ba47ff0..66a5310121a15 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/PolygonTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/PolygonTests.java @@ -49,65 +49,106 @@ protected Polygon createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("POLYGON ((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0))", - wkt.toWKT(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})), - wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1))")); - - assertEquals("POLYGON ((3.0 1.0 5.0, 4.0 2.0 4.0, 5.0 3.0 3.0, 3.0 1.0 5.0))", - wkt.toWKT(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})))); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})), - wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3, 3 1 5))")); + assertEquals( + "POLYGON ((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0))", + wkt.toWKT(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })), + wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1))") + ); + + assertEquals( + "POLYGON ((3.0 1.0 5.0, 4.0 2.0 4.0, 5.0 3.0 3.0, 3.0 1.0 5.0))", + wkt.toWKT(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 }))) + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })), + wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3, 3 1 5))") + ); // Auto closing in coerce mode - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})), - wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3))")); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})), - wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3))")); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}), - Collections.singletonList(new LinearRing(new double[]{0.5, 2.5, 2.0, 0.5}, new double[]{1.5, 1.5, 1.0, 1.5}))), - wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })), + wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3))") + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })), + wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3))") + ); + assertEquals( + new Polygon( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }), + Collections.singletonList(new LinearRing(new double[] { 0.5, 2.5, 2.0, 0.5 }, new double[] { 1.5, 1.5, 1.0, 1.5 })) + ), + wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))") + ); assertEquals("POLYGON EMPTY", wkt.toWKT(Polygon.EMPTY)); assertEquals(Polygon.EMPTY, wkt.fromWKT("POLYGON EMPTY)")); } public void testInitValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 3}, new double[]{1, 2, 1}))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon(new LinearRing(new double[] { 3, 4, 3 }, new double[] { 1, 2, 1 })) + ); assertEquals("at least 4 polygon points required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}), null)); + ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }), null) + ); assertEquals("holes must not be null", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5}), - Collections.singletonList(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 }), + Collections.singletonList(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })) + ) + ); assertEquals("holes must have the same number of dimensions as the polygon", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1})))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1}))); + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ); } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new WellKnownText(false, new GeographyValidator(true)).fromWKT("polygon ((3 1 5, 4 2 4, 5 3 3))")); - assertEquals("first and last points of the linear ring must be the same (it must close itself): " + - "x[0]=3.0 x[2]=5.0 y[0]=1.0 y[2]=3.0 z[0]=5.0 z[2]=3.0", ex.getMessage()); - - ex = expectThrows(IllegalArgumentException.class, - () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("polygon ((3 1 5, 4 2 4, 5 3 3, 3 1 5))")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new WellKnownText(false, new GeographyValidator(true)).fromWKT("polygon ((3 1 5, 4 2 4, 5 3 3))") + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): " + + "x[0]=3.0 x[2]=5.0 y[0]=1.0 y[2]=3.0 z[0]=5.0 z[2]=3.0", + ex.getMessage() + ); + + ex = expectThrows( + IllegalArgumentException.class, + () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("polygon ((3 1 5, 4 2 4, 5 3 3, 3 1 5))") + ); assertEquals("found Z value [5.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, + ex = expectThrows( + IllegalArgumentException.class, () -> new WellKnownText(false, new GeographyValidator(randomBoolean())).fromWKT( - "polygon ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); - assertEquals("first and last points of the linear ring must be the same (it must close itself): " + - "x[0]=0.5 x[2]=2.0 y[0]=1.5 y[2]=1.0", ex.getMessage()); + "polygon ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))" + ) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): " + "x[0]=0.5 x[2]=2.0 y[0]=1.5 y[2]=1.0", + ex.getMessage() + ); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/RectangleTests.java b/libs/geo/src/test/java/org/opensearch/geometry/RectangleTests.java index cb78ebda232db..fdc50fce15bd0 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/RectangleTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/RectangleTests.java @@ -59,24 +59,22 @@ public void testBasicSerialization() throws IOException, ParseException { public void testInitValidation() { GeometryValidator validator = new GeographyValidator(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 100, 1))); + IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 100, 1))); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(200, 3, 2, 1))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(200, 3, 2, 1))); assertEquals("invalid longitude 200.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 1, 2))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 1, 2))); assertEquals("max y cannot be less than min x", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 2, 1, 5, Double.NaN))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 2, 1, 5, Double.NaN))); assertEquals("only one z value is specified", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new Rectangle(50, 10, 40, 30, 20, 60))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate(new Rectangle(50, 10, 40, 30, 20, 60)) + ); assertEquals("found Z value [20.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate(new Rectangle(50, 10, 40, 30, 20, 60)); diff --git a/libs/geo/src/test/java/org/opensearch/geometry/utils/GeoHashTests.java b/libs/geo/src/test/java/org/opensearch/geometry/utils/GeoHashTests.java index 403ff67ff1fd5..a039574706a21 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/utils/GeoHashTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/utils/GeoHashTests.java @@ -42,15 +42,12 @@ public class GeoHashTests extends OpenSearchTestCase { public void testGeohashAsLongRoutines() { final GeoPoint expected = new GeoPoint(); final GeoPoint actual = new GeoPoint(); - //Ensure that for all points at all supported levels of precision + // Ensure that for all points at all supported levels of precision // that the long encoding of a geohash is compatible with its // String based counterpart - for (double lat=-90;lat<90;lat++) - { - for (double lng=-180;lng<180;lng++) - { - for(int p=1;p<=12;p++) - { + for (double lat = -90; lat < 90; lat++) { + for (double lng = -180; lng < 180; lng++) { + for (int p = 1; p <= 12; p++) { long geoAsLong = Geohash.longEncode(lng, lat, p); // string encode from geohashlong encoded location @@ -104,11 +101,11 @@ public void testLongGeohashes() { // Adding some random geohash characters at the end String extendedGeohash = geohash + randomGeohash(1, 10); GeoPoint actual = GeoPoint.fromGeohash(extendedGeohash); - assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]" , expected, actual); + assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]", expected, actual); Rectangle expectedBbox = Geohash.toBoundingBox(geohash); Rectangle actualBbox = Geohash.toBoundingBox(extendedGeohash); - assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]" , expectedBbox, actualBbox); + assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]", expectedBbox, actualBbox); } } diff --git a/libs/grok/src/main/java/org/opensearch/grok/Grok.java b/libs/grok/src/main/java/org/opensearch/grok/Grok.java index 3bae4b360e34b..e80bde02360ed 100644 --- a/libs/grok/src/main/java/org/opensearch/grok/Grok.java +++ b/libs/grok/src/main/java/org/opensearch/grok/Grok.java @@ -68,20 +68,26 @@ public final class Grok { private static final String SUBNAME_GROUP = "subname"; private static final String PATTERN_GROUP = "pattern"; private static final String DEFINITION_GROUP = "definition"; - private static final String GROK_PATTERN = - "%\\{" + - "(?" + - "(?[A-z0-9]+)" + - "(?::(?[[:alnum:]@\\[\\]_:.-]+))?" + - ")" + - "(?:=(?" + - "(?:[^{}]+|\\.+)+" + - ")" + - ")?" + "\\}"; - private static final Regex GROK_PATTERN_REGEX = new Regex(GROK_PATTERN.getBytes(StandardCharsets.UTF_8), 0, - GROK_PATTERN.getBytes(StandardCharsets.UTF_8).length, Option.NONE, UTF8Encoding.INSTANCE, Syntax.DEFAULT); - - private static final int MAX_TO_REGEX_ITERATIONS = 100_000; //sanity limit + private static final String GROK_PATTERN = "%\\{" + + "(?" + + "(?[A-z0-9]+)" + + "(?::(?[[:alnum:]@\\[\\]_:.-]+))?" + + ")" + + "(?:=(?" + + "(?:[^{}]+|\\.+)+" + + ")" + + ")?" + + "\\}"; + private static final Regex GROK_PATTERN_REGEX = new Regex( + GROK_PATTERN.getBytes(StandardCharsets.UTF_8), + 0, + GROK_PATTERN.getBytes(StandardCharsets.UTF_8).length, + Option.NONE, + UTF8Encoding.INSTANCE, + Syntax.DEFAULT + ); + + private static final int MAX_TO_REGEX_ITERATIONS = 100_000; // sanity limit private final Map patternBank; private final boolean namedCaptures; @@ -101,8 +107,13 @@ public Grok(Map patternBank, String grokPattern, MatcherWatchdog this(patternBank, grokPattern, namedCaptures, MatcherWatchdog.noop(), logCallBack); } - private Grok(Map patternBank, String grokPattern, boolean namedCaptures, MatcherWatchdog matcherWatchdog, - Consumer logCallBack) { + private Grok( + Map patternBank, + String grokPattern, + boolean namedCaptures, + MatcherWatchdog matcherWatchdog, + Consumer logCallBack + ) { this.patternBank = patternBank; this.namedCaptures = namedCaptures; this.matcherWatchdog = matcherWatchdog; @@ -111,8 +122,14 @@ private Grok(Map patternBank, String grokPattern, boolean namedC String expression = toRegex(grokPattern); byte[] expressionBytes = expression.getBytes(StandardCharsets.UTF_8); - this.compiledExpression = new Regex(expressionBytes, 0, expressionBytes.length, Option.DEFAULT, UTF8Encoding.INSTANCE, - message -> logCallBack.accept(message)); + this.compiledExpression = new Regex( + expressionBytes, + 0, + expressionBytes.length, + Option.DEFAULT, + UTF8Encoding.INSTANCE, + message -> logCallBack.accept(message) + ); List captureConfig = new ArrayList<>(); for (Iterator entry = compiledExpression.namedBackrefIterator(); entry.hasNext();) { @@ -141,8 +158,7 @@ private void validatePatternBank() { */ private void validatePatternBank(String patternName, Stack path) { String pattern = patternBank.get(patternName); - boolean isSelfReference = pattern.contains("%{" + patternName + "}") || - pattern.contains("%{" + patternName + ":"); + boolean isSelfReference = pattern.contains("%{" + patternName + "}") || pattern.contains("%{" + patternName + ":"); if (isSelfReference) { throwExceptionForCircularReference(patternName, pattern); } else if (path.contains(patternName)) { @@ -156,7 +172,7 @@ private void validatePatternBank(String patternName, Stack path) { int begin = i + 2; int syntaxEndIndex = pattern.indexOf('}', begin); if (syntaxEndIndex == -1) { - throw new IllegalArgumentException("Malformed pattern [" + patternName + "][" + pattern +"]"); + throw new IllegalArgumentException("Malformed pattern [" + patternName + "][" + pattern + "]"); } int semanticNameIndex = pattern.indexOf(':', begin); int end = syntaxEndIndex; @@ -173,8 +189,12 @@ private static void throwExceptionForCircularReference(String patternName, Strin throwExceptionForCircularReference(patternName, pattern, null, null); } - private static void throwExceptionForCircularReference(String patternName, String pattern, String originPatterName, - Stack path) { + private static void throwExceptionForCircularReference( + String patternName, + String pattern, + String originPatterName, + Stack path + ) { StringBuilder message = new StringBuilder("circular reference in pattern ["); message.append(patternName).append("][").append(pattern).append("]"); if (originPatterName != null) { @@ -188,8 +208,12 @@ private static void throwExceptionForCircularReference(String patternName, Strin private String groupMatch(String name, Region region, String pattern) { try { - int number = GROK_PATTERN_REGEX.nameToBackrefNumber(name.getBytes(StandardCharsets.UTF_8), 0, - name.getBytes(StandardCharsets.UTF_8).length, region); + int number = GROK_PATTERN_REGEX.nameToBackrefNumber( + name.getBytes(StandardCharsets.UTF_8), + 0, + name.getBytes(StandardCharsets.UTF_8).length, + region + ); int begin = region.beg[number]; int end = region.end[number]; return new String(pattern.getBytes(StandardCharsets.UTF_8), begin, end - begin, StandardCharsets.UTF_8); @@ -305,8 +329,9 @@ public boolean match(byte[] utf8Bytes, int offset, int length, GrokCaptureExtrac matcherWatchdog.unregister(matcher); } if (result == Matcher.INTERRUPTED) { - throw new RuntimeException("grok pattern matching was interrupted after [" + - matcherWatchdog.maxExecutionTimeInMillis() + "] ms"); + throw new RuntimeException( + "grok pattern matching was interrupted after [" + matcherWatchdog.maxExecutionTimeInMillis() + "] ms" + ); } if (result == Matcher.FAILED) { return false; @@ -327,14 +352,30 @@ public List captureConfig() { */ private static Map loadBuiltinPatterns() { String[] patternNames = new String[] { - "aws", "bacula", "bind", "bro", "exim", "firewalls", "grok-patterns", "haproxy", - "java", "junos", "linux-syslog", "maven", "mcollective-patterns", "mongodb", "nagios", - "postgresql", "rails", "redis", "ruby", "squid" - }; + "aws", + "bacula", + "bind", + "bro", + "exim", + "firewalls", + "grok-patterns", + "haproxy", + "java", + "junos", + "linux-syslog", + "maven", + "mcollective-patterns", + "mongodb", + "nagios", + "postgresql", + "rails", + "redis", + "ruby", + "squid" }; Map builtinPatterns = new LinkedHashMap<>(); for (String pattern : patternNames) { try { - try(InputStream is = Grok.class.getResourceAsStream("/patterns/" + pattern)) { + try (InputStream is = Grok.class.getResourceAsStream("/patterns/" + pattern)) { loadPatterns(builtinPatterns, is); } } catch (IOException e) { @@ -361,4 +402,3 @@ private static void loadPatterns(Map patternBank, InputStream in } } - diff --git a/libs/grok/src/main/java/org/opensearch/grok/MatcherWatchdog.java b/libs/grok/src/main/java/org/opensearch/grok/MatcherWatchdog.java index 9fc20d51feafc..70b4570ee69ad 100644 --- a/libs/grok/src/main/java/org/opensearch/grok/MatcherWatchdog.java +++ b/libs/grok/src/main/java/org/opensearch/grok/MatcherWatchdog.java @@ -83,10 +83,12 @@ public interface MatcherWatchdog { * @param relativeTimeSupplier A supplier that returns relative time * @param scheduler A scheduler that is able to execute a command for each fixed interval */ - static MatcherWatchdog newInstance(long interval, - long maxExecutionTime, - LongSupplier relativeTimeSupplier, - BiConsumer scheduler) { + static MatcherWatchdog newInstance( + long interval, + long maxExecutionTime, + LongSupplier relativeTimeSupplier, + BiConsumer scheduler + ) { return new Default(interval, maxExecutionTime, relativeTimeSupplier, scheduler); } @@ -101,12 +103,10 @@ class Noop implements MatcherWatchdog { private static final Noop INSTANCE = new Noop(); - private Noop() { - } + private Noop() {} @Override - public void register(Matcher matcher) { - } + public void register(Matcher matcher) {} @Override public long maxExecutionTimeInMillis() { @@ -114,8 +114,7 @@ public long maxExecutionTimeInMillis() { } @Override - public void unregister(Matcher matcher) { - } + public void unregister(Matcher matcher) {} } class Default implements MatcherWatchdog { @@ -128,10 +127,7 @@ class Default implements MatcherWatchdog { private final AtomicBoolean running = new AtomicBoolean(false); final ConcurrentHashMap registry = new ConcurrentHashMap<>(); - private Default(long interval, - long maxExecutionTime, - LongSupplier relativeTimeSupplier, - BiConsumer scheduler) { + private Default(long interval, long maxExecutionTime, LongSupplier relativeTimeSupplier, BiConsumer scheduler) { this.interval = interval; this.maxExecutionTime = maxExecutionTime; this.relativeTimeSupplier = relativeTimeSupplier; diff --git a/libs/grok/src/test/java/org/opensearch/grok/GrokTests.java b/libs/grok/src/test/java/org/opensearch/grok/GrokTests.java index a9c2d384b62a6..ed48585cc124a 100644 --- a/libs/grok/src/test/java/org/opensearch/grok/GrokTests.java +++ b/libs/grok/src/test/java/org/opensearch/grok/GrokTests.java @@ -62,7 +62,6 @@ import static org.opensearch.grok.GrokCaptureType.LONG; import static org.opensearch.grok.GrokCaptureType.STRING; - public class GrokTests extends OpenSearchTestCase { public void testMatchWithoutCaptures() { Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "value", logger::warn); @@ -130,8 +129,8 @@ public GrokCaptureExtracter forString(Function, GrokCaptureExtr } public void testSyslog5424Line() { - String line = "<191>1 2009-06-30T18:30:00+02:00 paxton.local grokdebug 4123 - [id1 foo=\\\"bar\\\"][id2 baz=\\\"something\\\"] " + - "Hello, syslog."; + String line = "<191>1 2009-06-30T18:30:00+02:00 paxton.local grokdebug 4123 - [id1 foo=\\\"bar\\\"][id2 baz=\\\"something\\\"] " + + "Hello, syslog."; Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "%{SYSLOG5424LINE}", logger::warn); assertCaptureConfig( grok, @@ -186,9 +185,13 @@ public void testNilWithNoCoercion() { } public void testUnicodeSyslog() { - Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "<%{POSINT:syslog_pri}>%{SPACE}%{SYSLOGTIMESTAMP:syslog_timestamp} " + - "%{SYSLOGHOST:syslog_hostname} %{PROG:syslog_program}(:?)(?:\\[%{GREEDYDATA:syslog_pid}\\])?(:?) " + - "%{GREEDYDATA:syslog_message}", logger::warn); + Grok grok = new Grok( + Grok.BUILTIN_PATTERNS, + "<%{POSINT:syslog_pri}>%{SPACE}%{SYSLOGTIMESTAMP:syslog_timestamp} " + + "%{SYSLOGHOST:syslog_hostname} %{PROG:syslog_program}(:?)(?:\\[%{GREEDYDATA:syslog_pid}\\])?(:?) " + + "%{GREEDYDATA:syslog_message}", + logger::warn + ); assertCaptureConfig( grok, org.opensearch.common.collect.Map.ofEntries( @@ -200,9 +203,11 @@ public void testUnicodeSyslog() { org.opensearch.common.collect.Map.entry("syslog_timestamp", STRING) ) ); - Map matches = grok.captures("<22>Jan 4 07:50:46 mailmaster postfix/policy-spf[9454]: : " + - "SPF permerror (Junk encountered in record 'v=spf1 mx a:mail.domain.no ip4:192.168.0.4 �all'): Envelope-from: " + - "email@domain.no"); + Map matches = grok.captures( + "<22>Jan 4 07:50:46 mailmaster postfix/policy-spf[9454]: : " + + "SPF permerror (Junk encountered in record 'v=spf1 mx a:mail.domain.no ip4:192.168.0.4 �all'): Envelope-from: " + + "email@domain.no" + ); assertThat(matches.get("syslog_pri"), equalTo("22")); assertThat(matches.get("syslog_program"), equalTo("postfix/policy-spf")); assertThat(matches.get("tags"), nullValue()); @@ -226,21 +231,21 @@ public void testISO8601() { Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "^%{TIMESTAMP_ISO8601}$", logger::warn); assertCaptureConfig(grok, org.opensearch.common.collect.Map.of()); List timeMessages = Arrays.asList( - "2001-01-01T00:00:00", - "1974-03-02T04:09:09", - "2010-05-03T08:18:18+00:00", - "2004-07-04T12:27:27-00:00", - "2001-09-05T16:36:36+0000", - "2001-11-06T20:45:45-0000", - "2001-12-07T23:54:54Z", - "2001-01-01T00:00:00.123456", - "1974-03-02T04:09:09.123456", - "2010-05-03T08:18:18.123456+00:00", - "2004-07-04T12:27:27.123456-00:00", - "2001-09-05T16:36:36.123456+0000", - "2001-11-06T20:45:45.123456-0000", - "2001-12-07T23:54:54.123456Z", - "2001-12-07T23:54:60.123456Z" // '60' second is a leap second. + "2001-01-01T00:00:00", + "1974-03-02T04:09:09", + "2010-05-03T08:18:18+00:00", + "2004-07-04T12:27:27-00:00", + "2001-09-05T16:36:36+0000", + "2001-11-06T20:45:45-0000", + "2001-12-07T23:54:54Z", + "2001-01-01T00:00:00.123456", + "1974-03-02T04:09:09.123456", + "2010-05-03T08:18:18.123456+00:00", + "2004-07-04T12:27:27.123456-00:00", + "2001-09-05T16:36:36.123456+0000", + "2001-11-06T20:45:45.123456-0000", + "2001-12-07T23:54:54.123456Z", + "2001-12-07T23:54:60.123456Z" // '60' second is a leap second. ); for (String msg : timeMessages) { assertThat(grok.match(msg), is(true)); @@ -251,28 +256,28 @@ public void testNotISO8601() { Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "^%{TIMESTAMP_ISO8601}$", logger::warn); assertCaptureConfig(grok, org.opensearch.common.collect.Map.of()); List timeMessages = Arrays.asList( - "2001-13-01T00:00:00", // invalid month - "2001-00-01T00:00:00", // invalid month - "2001-01-00T00:00:00", // invalid day - "2001-01-32T00:00:00", // invalid day - "2001-01-aT00:00:00", // invalid day - "2001-01-1aT00:00:00", // invalid day - "2001-01-01Ta0:00:00", // invalid hour - "2001-01-01T0:00:00", // invalid hour - "2001-01-01T25:00:00", // invalid hour - "2001-01-01T01:60:00", // invalid minute - "2001-01-01T00:aa:00", // invalid minute - "2001-01-01T00:00:aa", // invalid second - "2001-01-01T00:00:-1", // invalid second - "2001-01-01T00:00:61", // invalid second - "2001-01-01T00:00:00A", // invalid timezone - "2001-01-01T00:00:00+", // invalid timezone - "2001-01-01T00:00:00+25", // invalid timezone - "2001-01-01T00:00:00+2500", // invalid timezone - "2001-01-01T00:00:00+25:00", // invalid timezone - "2001-01-01T00:00:00-25", // invalid timezone - "2001-01-01T00:00:00-2500", // invalid timezone - "2001-01-01T00:00:00-00:61" // invalid timezone + "2001-13-01T00:00:00", // invalid month + "2001-00-01T00:00:00", // invalid month + "2001-01-00T00:00:00", // invalid day + "2001-01-32T00:00:00", // invalid day + "2001-01-aT00:00:00", // invalid day + "2001-01-1aT00:00:00", // invalid day + "2001-01-01Ta0:00:00", // invalid hour + "2001-01-01T0:00:00", // invalid hour + "2001-01-01T25:00:00", // invalid hour + "2001-01-01T01:60:00", // invalid minute + "2001-01-01T00:aa:00", // invalid minute + "2001-01-01T00:00:aa", // invalid second + "2001-01-01T00:00:-1", // invalid second + "2001-01-01T00:00:61", // invalid second + "2001-01-01T00:00:00A", // invalid timezone + "2001-01-01T00:00:00+", // invalid timezone + "2001-01-01T00:00:00+25", // invalid timezone + "2001-01-01T00:00:00+2500", // invalid timezone + "2001-01-01T00:00:00+25:00", // invalid timezone + "2001-01-01T00:00:00-25", // invalid timezone + "2001-01-01T00:00:00-2500", // invalid timezone + "2001-01-01T00:00:00-00:61" // invalid timezone ); for (String msg : timeMessages) { assertThat(grok.match(msg), is(false)); @@ -344,8 +349,10 @@ public void testCircularReference() { String pattern = "%{NAME1}"; new Grok(bank, pattern, false, logger::warn); }); - assertEquals("circular reference in pattern [NAME3][!!!%{NAME1}!!!] back to pattern [NAME1] via patterns [NAME1=>NAME2]", - e.getMessage()); + assertEquals( + "circular reference in pattern [NAME3][!!!%{NAME1}!!!] back to pattern [NAME1] via patterns [NAME1=>NAME2]", + e.getMessage() + ); e = expectThrows(IllegalArgumentException.class, () -> { Map bank = new TreeMap<>(); @@ -354,8 +361,7 @@ public void testCircularReference() { String pattern = "%{NAME1}"; new Grok(bank, pattern, false, logger::warn); }); - assertEquals("circular reference in pattern [NAME2][!!!%{NAME2}!!!]", - e.getMessage()); + assertEquals("circular reference in pattern [NAME2][!!!%{NAME2}!!!]", e.getMessage()); e = expectThrows(IllegalArgumentException.class, () -> { Map bank = new TreeMap<>(); @@ -365,10 +371,12 @@ public void testCircularReference() { bank.put("NAME4", "!!!%{NAME5}!!!"); bank.put("NAME5", "!!!%{NAME1}!!!"); String pattern = "%{NAME1}"; - new Grok(bank, pattern, false, logger::warn ); + new Grok(bank, pattern, false, logger::warn); }); - assertEquals("circular reference in pattern [NAME5][!!!%{NAME1}!!!] back to pattern [NAME1] " + - "via patterns [NAME1=>NAME2=>NAME3=>NAME4]", e.getMessage()); + assertEquals( + "circular reference in pattern [NAME5][!!!%{NAME1}!!!] back to pattern [NAME1] " + "via patterns [NAME1=>NAME2=>NAME3=>NAME4]", + e.getMessage() + ); } public void testMalformedPattern() { @@ -453,7 +461,7 @@ public GrokCaptureExtracter forLong(Function double[] rating = new double[1]; GrokCaptureExtracter ratingExtracter = namedConfig(g, "rating").nativeExtracter(new ThrowingNativeExtracterMap() { - public GrokCaptureExtracter forDouble(java.util.function.Function buildExtracter) { + public GrokCaptureExtracter forDouble(java.util.function.Function buildExtracter) { return buildExtracter.apply(d -> rating[0] = d); } }); @@ -491,9 +499,9 @@ public void testGarbageTypeNameBecomesString() { } public void testApacheLog() { - String logLine = "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + - "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\""; + String logLine = "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + + "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\""; Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "%{COMBINEDAPACHELOG}", logger::warn); assertCaptureConfig( grok, @@ -525,15 +533,21 @@ public void testApacheLog() { assertEquals("69849", matches.get("bytes")); assertEquals("\"http://8rursodiol.enjin.com\"", matches.get("referrer")); assertEquals(null, matches.get("port")); - assertEquals("\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.12785 " + - "YaBrowser/13.12.1599.12785 Safari/537.36\"", matches.get("agent")); + assertEquals( + "\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.12785 " + + "YaBrowser/13.12.1599.12785 Safari/537.36\"", + matches.get("agent") + ); } public void testComplete() { Map bank = new HashMap<>(); bank.put("MONTHDAY", "(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9])"); - bank.put("MONTH", "\\b(?:Jan(?:uary|uar)?|Feb(?:ruary|ruar)?|M(?:a|ä)?r(?:ch|z)?|Apr(?:il)?|Ma(?:y|i)?|Jun(?:e|i)" + - "?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|O(?:c|k)?t(?:ober)?|Nov(?:ember)?|De(?:c|z)(?:ember)?)\\b"); + bank.put( + "MONTH", + "\\b(?:Jan(?:uary|uar)?|Feb(?:ruary|ruar)?|M(?:a|ä)?r(?:ch|z)?|Apr(?:il)?|Ma(?:y|i)?|Jun(?:e|i)" + + "?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|O(?:c|k)?t(?:ober)?|Nov(?:ember)?|De(?:c|z)(?:ember)?)\\b" + ); bank.put("MINUTE", "(?:[0-5][0-9])"); bank.put("YEAR", "(?>\\d\\d){1,2}"); bank.put("HOUR", "(?:2[0123]|[01]?[0-9])"); @@ -544,19 +558,25 @@ public void testComplete() { bank.put("WORD", "\\b\\w+\\b"); bank.put("BASE10NUM", "(?[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))"); bank.put("NUMBER", "(?:%{BASE10NUM})"); - bank.put("IPV6", "((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]" + - "\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4})" + - "{1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:)" + - "{4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\" + - "d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]" + - "\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4})" + - "{1,5})" + - "|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))" + - "|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + - "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}" + - ":((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?"); - bank.put("IPV4", "(?(?\"(?>\\\\.|[^\\\\\"]+)+\"|\"\"|(?>'(?>\\\\.|[^\\\\']+)+')|''|(?>`(?>\\\\.|[^\\\\`]+)+`)|``))"); - String text = "83.149.9.216 - - [19/Jul/2015:08:13:42 +0000] \"GET /presentations/logstash-monitorama-2013/images/" + - "kibana-dashboard3.png HTTP/1.1\" 200 171717 \"http://semicomplete.com/presentations/logstash-monitorama-2013/\" " + - "\"Mozilla" + - "/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\""; - String pattern = "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request} " + - "HTTP/%{NUMBER:httpversion}\" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}"; + String text = "83.149.9.216 - - [19/Jul/2015:08:13:42 +0000] \"GET /presentations/logstash-monitorama-2013/images/" + + "kibana-dashboard3.png HTTP/1.1\" 200 171717 \"http://semicomplete.com/presentations/logstash-monitorama-2013/\" " + + "\"Mozilla" + + "/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\""; + String pattern = "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request} " + + "HTTP/%{NUMBER:httpversion}\" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}"; Grok grok = new Grok(bank, pattern, logger::warn); assertCaptureConfig( @@ -600,8 +620,11 @@ public void testComplete() { expected.put("response", 200); expected.put("bytes", 171717); expected.put("referrer", "\"http://semicomplete.com/presentations/logstash-monitorama-2013/\""); - expected.put("agent", "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/32.0.1700.77 Safari/537.36\""); + expected.put( + "agent", + "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/32.0.1700.77 Safari/537.36\"" + ); Map actual = grok.captures(text); @@ -629,10 +652,10 @@ public void testMultipleNamedCapturesWithSameName() { public void testExponentialExpressions() { AtomicBoolean run = new AtomicBoolean(true); // to avoid a lingering thread when test has completed - String grokPattern = "Bonsuche mit folgender Anfrage: Belegart->\\[%{WORD:param2},(?(\\s*%{NOTSPACE})*)\\] " + - "Zustand->ABGESCHLOSSEN Kassennummer->%{WORD:param9} Bonnummer->%{WORD:param10} Datum->%{DATESTAMP_OTHER:param11}"; - String logLine = "Bonsuche mit folgender Anfrage: Belegart->[EINGESCHRAENKTER_VERKAUF, VERKAUF, NACHERFASSUNG] " + - "Zustand->ABGESCHLOSSEN Kassennummer->2 Bonnummer->6362 Datum->Mon Jan 08 00:00:00 UTC 2018"; + String grokPattern = "Bonsuche mit folgender Anfrage: Belegart->\\[%{WORD:param2},(?(\\s*%{NOTSPACE})*)\\] " + + "Zustand->ABGESCHLOSSEN Kassennummer->%{WORD:param9} Bonnummer->%{WORD:param10} Datum->%{DATESTAMP_OTHER:param11}"; + String logLine = "Bonsuche mit folgender Anfrage: Belegart->[EINGESCHRAENKTER_VERKAUF, VERKAUF, NACHERFASSUNG] " + + "Zustand->ABGESCHLOSSEN Kassennummer->2 Bonnummer->6362 Datum->Mon Jan 08 00:00:00 UTC 2018"; BiConsumer scheduler = (delay, command) -> { try { Thread.sleep(delay); @@ -646,8 +669,12 @@ public void testExponentialExpressions() { }); t.start(); }; - Grok grok = new Grok(Grok.BUILTIN_PATTERNS, grokPattern, MatcherWatchdog.newInstance(10, 200, System::currentTimeMillis, scheduler), - logger::warn); + Grok grok = new Grok( + Grok.BUILTIN_PATTERNS, + grokPattern, + MatcherWatchdog.newInstance(10, 200, System::currentTimeMillis, scheduler), + logger::warn + ); Exception e = expectThrows(RuntimeException.class, () -> grok.captures(logLine)); run.set(false); assertThat(e.getMessage(), equalTo("grok pattern matching was interrupted after [200] ms")); @@ -702,11 +729,11 @@ public void testJavaFilePatternWithSpaces() { assertThat(grok.match("Test Class.java"), is(true)); } - public void testLogCallBack(){ + public void testLogCallBack() { AtomicReference message = new AtomicReference<>(); Grok grok = new Grok(Grok.BUILTIN_PATTERNS, ".*\\[.*%{SPACE}*\\].*", message::set); grok.match("[foo]"); - //this message comes from Joni, so updates to Joni may change the expectation + // this message comes from Joni, so updates to Joni may change the expectation assertThat(message.get(), containsString("regular expression has redundant nested repeat operator")); } diff --git a/libs/grok/src/test/java/org/opensearch/grok/MatcherWatchdogTests.java b/libs/grok/src/test/java/org/opensearch/grok/MatcherWatchdogTests.java index c3ad1ee0133b4..376db6226c165 100644 --- a/libs/grok/src/test/java/org/opensearch/grok/MatcherWatchdogTests.java +++ b/libs/grok/src/test/java/org/opensearch/grok/MatcherWatchdogTests.java @@ -77,7 +77,8 @@ public void testInterrupt() throws Exception { watchdog.register(matcher); verify(matcher, timeout(9999).atLeastOnce()).interrupt(); interrupted.set(true); - while (run.get()) {} // wait here so that the size of the registry can be asserted + while (run.get()) { + } // wait here so that the size of the registry can be asserted watchdog.unregister(matcher); }); thread.start(); @@ -86,16 +87,18 @@ public void testInterrupt() throws Exception { assertThat(registry.size(), is(1)); }); run.set(false); - assertBusy(() -> { - assertThat(registry.size(), is(0)); - }); + assertBusy(() -> { assertThat(registry.size(), is(0)); }); } public void testIdleIfNothingRegistered() throws Exception { long interval = 1L; ScheduledExecutorService threadPool = mock(ScheduledExecutorService.class); - MatcherWatchdog watchdog = MatcherWatchdog.newInstance(interval, Long.MAX_VALUE, System::currentTimeMillis, - (delay, command) -> threadPool.schedule(command, delay, TimeUnit.MILLISECONDS)); + MatcherWatchdog watchdog = MatcherWatchdog.newInstance( + interval, + Long.MAX_VALUE, + System::currentTimeMillis, + (delay, command) -> threadPool.schedule(command, delay, TimeUnit.MILLISECONDS) + ); // Periodic action is not scheduled because no thread is registered verifyZeroInteractions(threadPool); CompletableFuture commandFuture = new CompletableFuture<>(); @@ -103,9 +106,7 @@ public void testIdleIfNothingRegistered() throws Exception { doAnswer(invocationOnMock -> { commandFuture.complete((Runnable) invocationOnMock.getArguments()[0]); return null; - }).when(threadPool).schedule( - any(Runnable.class), eq(interval), eq(TimeUnit.MILLISECONDS) - ); + }).when(threadPool).schedule(any(Runnable.class), eq(interval), eq(TimeUnit.MILLISECONDS)); Matcher matcher = mock(Matcher.class); watchdog.register(matcher); // Registering the first thread should have caused the command to get scheduled again diff --git a/libs/nio/src/main/java/org/opensearch/nio/BytesChannelContext.java b/libs/nio/src/main/java/org/opensearch/nio/BytesChannelContext.java index 03a05d4b6d68e..14e027195bd8f 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/BytesChannelContext.java +++ b/libs/nio/src/main/java/org/opensearch/nio/BytesChannelContext.java @@ -37,8 +37,14 @@ public class BytesChannelContext extends SocketChannelContext { - public BytesChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, - Consumer exceptionHandler, NioChannelHandler handler, InboundChannelBuffer channelBuffer) { + public BytesChannelContext( + NioSocketChannel channel, + NioSelector selector, + Config.Socket socketConfig, + Consumer exceptionHandler, + NioChannelHandler handler, + InboundChannelBuffer channelBuffer + ) { super(channel, selector, socketConfig, exceptionHandler, handler, channelBuffer); } diff --git a/libs/nio/src/main/java/org/opensearch/nio/BytesWriteHandler.java b/libs/nio/src/main/java/org/opensearch/nio/BytesWriteHandler.java index a6136c15d3a0c..b4c3f5a147133 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/BytesWriteHandler.java +++ b/libs/nio/src/main/java/org/opensearch/nio/BytesWriteHandler.java @@ -42,8 +42,11 @@ public abstract class BytesWriteHandler implements NioChannelHandler { private static final List EMPTY_LIST = Collections.emptyList(); public WriteOperation createWriteOperation(SocketChannelContext context, Object message, BiConsumer listener) { - assert message instanceof ByteBuffer[] : "This channel only supports messages that are of type: " + ByteBuffer[].class - + ". Found type: " + message.getClass() + "."; + assert message instanceof ByteBuffer[] : "This channel only supports messages that are of type: " + + ByteBuffer[].class + + ". Found type: " + + message.getClass() + + "."; return new FlushReadyWrite(context, (ByteBuffer[]) message, listener); } diff --git a/libs/nio/src/main/java/org/opensearch/nio/ChannelFactory.java b/libs/nio/src/main/java/org/opensearch/nio/ChannelFactory.java index f87c19e5080b4..c7a0bb40b4206 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/ChannelFactory.java +++ b/libs/nio/src/main/java/org/opensearch/nio/ChannelFactory.java @@ -56,18 +56,43 @@ public abstract class ChannelFactory getListener() { } public boolean isFullyFlushed() { - assert length >= internalIndex : "Should never have an index that is greater than the length [length=" + length + ", index=" - + internalIndex + "]"; + assert length >= internalIndex : "Should never have an index that is greater than the length [length=" + + length + + ", index=" + + internalIndex + + "]"; return internalIndex == length; } public void incrementIndex(int delta) { internalIndex += delta; - assert length >= internalIndex : "Should never increment index past length [length=" + length + ", post-increment index=" - + internalIndex + ", delta=" + delta + "]"; + assert length >= internalIndex : "Should never increment index past length [length=" + + length + + ", post-increment index=" + + internalIndex + + ", delta=" + + delta + + "]"; } public ByteBuffer[] getBuffersToWrite() { diff --git a/libs/nio/src/main/java/org/opensearch/nio/InboundChannelBuffer.java b/libs/nio/src/main/java/org/opensearch/nio/InboundChannelBuffer.java index 6429fd410a0bf..38d9c35a22ae5 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/InboundChannelBuffer.java +++ b/libs/nio/src/main/java/org/opensearch/nio/InboundChannelBuffer.java @@ -135,8 +135,9 @@ public void release(long bytesToRelease) { */ public ByteBuffer[] sliceBuffersTo(long to) { if (to > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters to [" + to + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters to [" + to + "]" + ); } else if (to == 0) { return EMPTY_BYTE_BUFFER_ARRAY; } @@ -174,8 +175,9 @@ public ByteBuffer[] sliceBuffersTo(long to) { */ public Page[] sliceAndRetainPagesTo(long to) { if (to > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters to [" + to + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters to [" + to + "]" + ); } else if (to == 0) { return EMPTY_BYTE_PAGE_ARRAY; } @@ -213,8 +215,9 @@ public Page[] sliceAndRetainPagesTo(long to) { */ public ByteBuffer[] sliceBuffersFrom(long from) { if (from > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters from [" + from + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters from [" + from + "]" + ); } else if (from == capacity) { return EMPTY_BYTE_BUFFER_ARRAY; } @@ -242,8 +245,17 @@ public void incrementIndex(long delta) { long newIndex = delta + internalIndex; if (newIndex > capacity) { - throw new IllegalArgumentException("Cannot increment an index [" + internalIndex + "] with a delta [" + delta + - "] that will result in a new index [" + newIndex + "] that is greater than the capacity [" + capacity + "]."); + throw new IllegalArgumentException( + "Cannot increment an index [" + + internalIndex + + "] with a delta [" + + delta + + "] that will result in a new index [" + + newIndex + + "] that is greater than the capacity [" + + capacity + + "]." + ); } internalIndex = newIndex; } diff --git a/libs/nio/src/main/java/org/opensearch/nio/NioSelector.java b/libs/nio/src/main/java/org/opensearch/nio/NioSelector.java index cdd36b3f5648c..a38a33182afea 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/NioSelector.java +++ b/libs/nio/src/main/java/org/opensearch/nio/NioSelector.java @@ -128,8 +128,12 @@ public boolean isOnCurrentThread() { } public void assertOnSelectorThread() { - assert isOnCurrentThread() : "Must be on selector thread [" + thread.get().getName() + "} to perform this operation. " + - "Currently on thread [" + Thread.currentThread().getName() + "]."; + assert isOnCurrentThread() : "Must be on selector thread [" + + thread.get().getName() + + "} to perform this operation. " + + "Currently on thread [" + + Thread.currentThread().getName() + + "]."; } /** @@ -186,10 +190,10 @@ void singleLoop() { try { processKey(sk); } catch (CancelledKeyException cke) { - eventHandler.genericChannelException((ChannelContext) sk.attachment(), cke); + eventHandler.genericChannelException((ChannelContext) sk.attachment(), cke); } } else { - eventHandler.genericChannelException((ChannelContext) sk.attachment(), new CancelledKeyException()); + eventHandler.genericChannelException((ChannelContext) sk.attachment(), new CancelledKeyException()); } } } @@ -210,8 +214,9 @@ void cleanupAndCloseChannels() { cleanupPendingWrites(); channelsToClose.addAll(channelsToRegister); channelsToRegister.clear(); - channelsToClose.addAll(selector.keys().stream() - .map(sk -> (ChannelContext) sk.attachment()).filter(Objects::nonNull).collect(Collectors.toList())); + channelsToClose.addAll( + selector.keys().stream().map(sk -> (ChannelContext) sk.attachment()).filter(Objects::nonNull).collect(Collectors.toList()) + ); closePendingChannels(); } diff --git a/libs/nio/src/main/java/org/opensearch/nio/NioSelectorGroup.java b/libs/nio/src/main/java/org/opensearch/nio/NioSelectorGroup.java index 72b31d8c89706..47ba253a1bd61 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/NioSelectorGroup.java +++ b/libs/nio/src/main/java/org/opensearch/nio/NioSelectorGroup.java @@ -57,7 +57,6 @@ */ public class NioSelectorGroup implements NioGroup { - private final List dedicatedAcceptors; private final RoundRobinSupplier acceptorSupplier; @@ -75,8 +74,11 @@ public class NioSelectorGroup implements NioGroup { * @param eventHandlerFunction function for creating event handlers * @throws IOException occurs if there is a problem while opening a java.nio.Selector */ - public NioSelectorGroup(ThreadFactory threadFactory, int selectorCount, - Function, EventHandler> eventHandlerFunction) throws IOException { + public NioSelectorGroup( + ThreadFactory threadFactory, + int selectorCount, + Function, EventHandler> eventHandlerFunction + ) throws IOException { this(null, 0, threadFactory, selectorCount, eventHandlerFunction); } @@ -92,8 +94,13 @@ public NioSelectorGroup(ThreadFactory threadFactory, int selectorCount, * @param eventHandlerFunction function for creating event handlers * @throws IOException occurs if there is a problem while opening a java.nio.Selector */ - public NioSelectorGroup(ThreadFactory acceptorThreadFactory, int dedicatedAcceptorCount, ThreadFactory selectorThreadFactory, - int selectorCount, Function, EventHandler> eventHandlerFunction) throws IOException { + public NioSelectorGroup( + ThreadFactory acceptorThreadFactory, + int dedicatedAcceptorCount, + ThreadFactory selectorThreadFactory, + int selectorCount, + Function, EventHandler> eventHandlerFunction + ) throws IOException { dedicatedAcceptors = new ArrayList<>(dedicatedAcceptorCount); selectors = new ArrayList<>(selectorCount); diff --git a/libs/nio/src/main/java/org/opensearch/nio/NioServerSocketChannel.java b/libs/nio/src/main/java/org/opensearch/nio/NioServerSocketChannel.java index 38b08867d546b..72724b72afc78 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/NioServerSocketChannel.java +++ b/libs/nio/src/main/java/org/opensearch/nio/NioServerSocketChannel.java @@ -84,9 +84,7 @@ public ServerChannelContext getContext() { @Override public String toString() { - return "NioServerSocketChannel{" + - "localAddress=" + getLocalAddress() + - '}'; + return "NioServerSocketChannel{" + "localAddress=" + getLocalAddress() + '}'; } private void attemptToSetLocalAddress() { diff --git a/libs/nio/src/main/java/org/opensearch/nio/NioSocketChannel.java b/libs/nio/src/main/java/org/opensearch/nio/NioSocketChannel.java index b91963141a886..233b41a0a2130 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/NioSocketChannel.java +++ b/libs/nio/src/main/java/org/opensearch/nio/NioSocketChannel.java @@ -88,9 +88,6 @@ public void addConnectListener(BiConsumer listener) { @Override public String toString() { - return "NioSocketChannel{" + - "localAddress=" + getLocalAddress() + - ", remoteAddress=" + getRemoteAddress() + - '}'; + return "NioSocketChannel{" + "localAddress=" + getLocalAddress() + ", remoteAddress=" + getRemoteAddress() + '}'; } } diff --git a/libs/nio/src/main/java/org/opensearch/nio/SelectionKeyUtils.java b/libs/nio/src/main/java/org/opensearch/nio/SelectionKeyUtils.java index 708e0057089cf..c054ad1953788 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/SelectionKeyUtils.java +++ b/libs/nio/src/main/java/org/opensearch/nio/SelectionKeyUtils.java @@ -100,7 +100,6 @@ public static void setAcceptInterested(SelectionKey selectionKey) throws Cancell selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_ACCEPT); } - /** * Checks for an interest in writes for this selection key. * diff --git a/libs/nio/src/main/java/org/opensearch/nio/ServerChannelContext.java b/libs/nio/src/main/java/org/opensearch/nio/ServerChannelContext.java index d6cb708432c25..898ce7e4e913b 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/ServerChannelContext.java +++ b/libs/nio/src/main/java/org/opensearch/nio/ServerChannelContext.java @@ -58,9 +58,14 @@ public class ServerChannelContext extends ChannelContext { private final ChannelFactory channelFactory; private final CompletableContext bindContext = new CompletableContext<>(); - public ServerChannelContext(NioServerSocketChannel channel, ChannelFactory channelFactory, NioSelector selector, - Config.ServerSocket config, Consumer acceptor, - Consumer exceptionHandler) { + public ServerChannelContext( + NioServerSocketChannel channel, + ChannelFactory channelFactory, + NioSelector selector, + Config.ServerSocket config, + Consumer acceptor, + Consumer exceptionHandler + ) { super(channel.getRawChannel(), exceptionHandler); this.channel = channel; this.channelFactory = channelFactory; @@ -77,8 +82,7 @@ public void acceptChannels(Supplier selectorSupplier) throws IOExce acceptor.accept(nioChannel); } } finally { - if (acceptedChannel != null) - acceptedChannel.close(); + if (acceptedChannel != null) acceptedChannel.close(); } } diff --git a/libs/nio/src/main/java/org/opensearch/nio/SocketChannelContext.java b/libs/nio/src/main/java/org/opensearch/nio/SocketChannelContext.java index 208f0cf7ea883..9cf57910a2b96 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/SocketChannelContext.java +++ b/libs/nio/src/main/java/org/opensearch/nio/SocketChannelContext.java @@ -77,9 +77,14 @@ public abstract class SocketChannelContext extends ChannelContext private boolean socketOptionsSet; private Exception connectException; - protected SocketChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, - Consumer exceptionHandler, NioChannelHandler channelHandler, - InboundChannelBuffer channelBuffer) { + protected SocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Config.Socket socketConfig, + Consumer exceptionHandler, + NioChannelHandler channelHandler, + InboundChannelBuffer channelBuffer + ) { super(channel.getRawChannel(), exceptionHandler); this.selector = selector; this.channel = channel; diff --git a/libs/nio/src/test/java/org/opensearch/nio/BytesChannelContextTests.java b/libs/nio/src/test/java/org/opensearch/nio/BytesChannelContextTests.java index 7a62f2dfd7e11..11a9a26368052 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/BytesChannelContextTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/BytesChannelContextTests.java @@ -130,7 +130,6 @@ public void testPartialRead() throws IOException { return bytes.length; }); - when(readConsumer.apply(channelBuffer)).thenReturn(0); assertEquals(messageLength, context.read()); @@ -175,7 +174,7 @@ public void testReadLessThanZeroMeansReadyForClose() throws IOException { public void testQueuedWriteIsFlushedInFlushCall() throws Exception { assertFalse(context.readyForFlush()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); @@ -201,7 +200,7 @@ public void testPartialFlush() throws IOException { assertTrue(context.readyForFlush()); when(flushOperation.isFullyFlushed()).thenReturn(false); - when(flushOperation.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); + when(flushOperation.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); context.flushChannel(); verify(listener, times(0)).accept(null, null); @@ -215,8 +214,8 @@ public void testMultipleWritesPartialFlushes() throws IOException { BiConsumer listener2 = mock(BiConsumer.class); FlushReadyWrite flushOperation1 = mock(FlushReadyWrite.class); FlushReadyWrite flushOperation2 = mock(FlushReadyWrite.class); - when(flushOperation1.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); - when(flushOperation2.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); + when(flushOperation1.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); + when(flushOperation2.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); when(flushOperation1.getListener()).thenReturn(listener); when(flushOperation2.getListener()).thenReturn(listener2); @@ -244,7 +243,7 @@ public void testMultipleWritesPartialFlushes() throws IOException { public void testWhenIOExceptionThrownListenerIsCalled() throws IOException { assertFalse(context.readyForFlush()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); @@ -261,7 +260,7 @@ public void testWhenIOExceptionThrownListenerIsCalled() throws IOException { } public void testWriteIOExceptionMeansChannelReadyToClose() throws IOException { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); diff --git a/libs/nio/src/test/java/org/opensearch/nio/ChannelFactoryTests.java b/libs/nio/src/test/java/org/opensearch/nio/ChannelFactoryTests.java index 542512faccb7e..01837cd2a0662 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/ChannelFactoryTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/ChannelFactoryTests.java @@ -152,8 +152,11 @@ public NioSocketChannel createChannel(NioSelector selector, SocketChannel channe } @Override - public NioServerSocketChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioServerSocketChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { return new NioServerSocketChannel(channel); } diff --git a/libs/nio/src/test/java/org/opensearch/nio/EventHandlerTests.java b/libs/nio/src/test/java/org/opensearch/nio/EventHandlerTests.java index c84381b8ac088..8ee1439cc63b0 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/EventHandlerTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/EventHandlerTests.java @@ -234,7 +234,6 @@ public void testPostHandlingWillRemoveWriteIfNecessary() throws IOException { NioSocketChannel channel = mock(NioSocketChannel.class); when(channel.getContext()).thenReturn(context); - assertEquals(SelectionKey.OP_READ | SelectionKey.OP_WRITE, key.interestOps()); handler.postHandling(context); assertEquals(SelectionKey.OP_READ, key.interestOps()); @@ -254,9 +253,12 @@ public void testTaskExceptionWillCallExceptionHandler() throws Exception { private class DoNotRegisterSocketContext extends BytesChannelContext { - - DoNotRegisterSocketContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler handler) { + DoNotRegisterSocketContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler handler + ) { super(channel, selector, getSocketConfig(), exceptionHandler, handler, InboundChannelBuffer.allocatingInstance()); } @@ -270,7 +272,6 @@ public void register() { private class DoNotRegisterServerContext extends ServerChannelContext { - @SuppressWarnings("unchecked") DoNotRegisterServerContext(NioServerSocketChannel channel, NioSelector selector, Consumer acceptor) { super(channel, channelFactory, selector, getServerSocketConfig(), acceptor, mock(Consumer.class)); @@ -289,7 +290,17 @@ private static Config.ServerSocket getServerSocketConfig() { } private static Config.Socket getSocketConfig() { - return new Config.Socket(randomBoolean(), randomBoolean(), -1, -1, -1, randomBoolean(), -1, -1, mock(InetSocketAddress.class), - randomBoolean()); + return new Config.Socket( + randomBoolean(), + randomBoolean(), + -1, + -1, + -1, + randomBoolean(), + -1, + -1, + mock(InetSocketAddress.class), + randomBoolean() + ); } } diff --git a/libs/nio/src/test/java/org/opensearch/nio/FlushOperationTests.java b/libs/nio/src/test/java/org/opensearch/nio/FlushOperationTests.java index 276381401cdf4..2e19cab2cbd13 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/FlushOperationTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/FlushOperationTests.java @@ -52,7 +52,7 @@ public void setFields() { } public void testFullyFlushedMarker() { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(10); @@ -61,7 +61,7 @@ public void testFullyFlushedMarker() { } public void testPartiallyFlushedMarker() { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(5); @@ -70,7 +70,7 @@ public void testPartiallyFlushedMarker() { } public void testMultipleFlushesWithCompositeBuffer() throws IOException { - ByteBuffer[] buffers = {ByteBuffer.allocate(10), ByteBuffer.allocate(15), ByteBuffer.allocate(3)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10), ByteBuffer.allocate(15), ByteBuffer.allocate(3) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(5); diff --git a/libs/nio/src/test/java/org/opensearch/nio/InboundChannelBufferTests.java b/libs/nio/src/test/java/org/opensearch/nio/InboundChannelBufferTests.java index ed3a9318dd7d4..4d6ac99283a6c 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/InboundChannelBufferTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/InboundChannelBufferTests.java @@ -266,10 +266,10 @@ public void testAccessByteBuffers() { if (randomBoolean()) { long bytesToRelease = Math.min(randomInt(50), channelBuffer.getIndex()); channelBuffer.release(bytesToRelease); - bytesReleased += bytesToRelease; + bytesReleased += (int) bytesToRelease; } channelBuffer.incrementIndex(amountToInc); - indexIncremented += amountToInc; + indexIncremented += (int) amountToInc; } assertEquals(0, channelBuffer.sliceBuffersFrom(channelBuffer.getIndex()).length); diff --git a/libs/nio/src/test/java/org/opensearch/nio/NioSelectorGroupTests.java b/libs/nio/src/test/java/org/opensearch/nio/NioSelectorGroupTests.java index e04919db91dd3..154bc53696088 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/NioSelectorGroupTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/NioSelectorGroupTests.java @@ -51,8 +51,13 @@ public class NioSelectorGroupTests extends OpenSearchTestCase { @SuppressWarnings("unchecked") public void setUp() throws Exception { super.setUp(); - nioGroup = new NioSelectorGroup(daemonThreadFactory(Settings.EMPTY, "acceptor"), 1, - daemonThreadFactory(Settings.EMPTY, "selector"), 1, (s) -> new EventHandler(mock(Consumer.class), s)); + nioGroup = new NioSelectorGroup( + daemonThreadFactory(Settings.EMPTY, "acceptor"), + 1, + daemonThreadFactory(Settings.EMPTY, "selector"), + 1, + (s) -> new EventHandler(mock(Consumer.class), s) + ); } @Override @@ -71,11 +76,15 @@ public void testStartAndClose() throws IOException { public void testCannotOperateAfterClose() throws IOException { nioGroup.close(); - IllegalStateException ise = expectThrows(IllegalStateException.class, - () -> nioGroup.bindServerChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class))); + IllegalStateException ise = expectThrows( + IllegalStateException.class, + () -> nioGroup.bindServerChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class)) + ); assertEquals("NioGroup is closed.", ise.getMessage()); - ise = expectThrows(IllegalStateException.class, - () -> nioGroup.openChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class))); + ise = expectThrows( + IllegalStateException.class, + () -> nioGroup.openChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class)) + ); assertEquals("NioGroup is closed.", ise.getMessage()); } @@ -87,9 +96,13 @@ public void testCanCloseTwice() throws IOException { @SuppressWarnings("unchecked") public void testExceptionAtStartIsHandled() throws IOException { RuntimeException ex = new RuntimeException(); - CheckedRunnable ctor = () -> new NioSelectorGroup(r -> {throw ex;}, 1, + CheckedRunnable ctor = () -> new NioSelectorGroup( + r -> { throw ex; }, + 1, daemonThreadFactory(Settings.EMPTY, "selector"), - 1, (s) -> new EventHandler(mock(Consumer.class), s)); + 1, + (s) -> new EventHandler(mock(Consumer.class), s) + ); RuntimeException runtimeException = expectThrows(RuntimeException.class, ctor::run); assertSame(ex, runtimeException); // ctor starts threads. So we are testing that a failure to construct will stop threads. Our thread diff --git a/libs/nio/src/test/java/org/opensearch/nio/NioSelectorTests.java b/libs/nio/src/test/java/org/opensearch/nio/NioSelectorTests.java index 078c56fc94fb5..d4d63091d217c 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/NioSelectorTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/NioSelectorTests.java @@ -72,7 +72,7 @@ public class NioSelectorTests extends OpenSearchTestCase { private SocketChannelContext channelContext; private ServerChannelContext serverChannelContext; private BiConsumer listener; - private ByteBuffer[] buffers = {ByteBuffer.allocate(1)}; + private ByteBuffer[] buffers = { ByteBuffer.allocate(1) }; private Selector rawSelector; @Before @@ -107,7 +107,7 @@ public void setUp() throws Exception { }).when(eventHandler).handleTask(any()); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testQueueChannelForClosed() throws IOException { NioChannel channel = mock(NioChannel.class); ChannelContext context = mock(ChannelContext.class); @@ -121,7 +121,7 @@ public void testQueueChannelForClosed() throws IOException { verify(eventHandler).handleClose(context); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testCloseException() throws IOException, InterruptedException { IOException ioException = new IOException(); NioChannel channel = mock(NioChannel.class); @@ -152,9 +152,7 @@ public void testNioDelayedTasksAreExecuted() throws IOException { public void testTaskExceptionsAreHandled() { RuntimeException taskException = new RuntimeException(); long nanoTime = System.nanoTime() - 1; - Runnable task = () -> { - throw taskException; - }; + Runnable task = () -> { throw taskException; }; selector.getTaskScheduler().scheduleAtRelativeTime(task, nanoTime); doAnswer((a) -> { @@ -168,8 +166,7 @@ public void testTaskExceptionsAreHandled() { public void testDefaultSelectorTimeoutIsUsedIfNoTaskSooner() throws IOException { long delay = new TimeValue(15, TimeUnit.MINUTES).nanos(); - selector.getTaskScheduler().scheduleAtRelativeTime(() -> { - }, System.nanoTime() + delay); + selector.getTaskScheduler().scheduleAtRelativeTime(() -> {}, System.nanoTime() + delay); selector.singleLoop(); verify(rawSelector).select(300); @@ -181,8 +178,7 @@ public void testSelectorTimeoutWillBeReducedIfTaskSooner() throws Exception { assertBusy(() -> { ArgumentCaptor captor = ArgumentCaptor.forClass(Long.class); long delay = new TimeValue(50, TimeUnit.MILLISECONDS).nanos(); - selector.getTaskScheduler().scheduleAtRelativeTime(() -> { - }, System.nanoTime() + delay); + selector.getTaskScheduler().scheduleAtRelativeTime(() -> {}, System.nanoTime() + delay); selector.singleLoop(); verify(rawSelector).select(captor.capture()); assertTrue(captor.getValue() > 0); diff --git a/libs/nio/src/test/java/org/opensearch/nio/SocketChannelContextTests.java b/libs/nio/src/test/java/org/opensearch/nio/SocketChannelContextTests.java index 79b5a635042a2..093bd19ac4006 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/SocketChannelContextTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/SocketChannelContextTests.java @@ -136,8 +136,18 @@ public void testRegisterInitiatesConnect() throws IOException { boolean tcpReuseAddress = randomBoolean(); int tcpSendBufferSize = randomIntBetween(1000, 2000); int tcpReceiveBufferSize = randomIntBetween(1000, 2000); - config = new Config.Socket(tcpNoDelay, tcpKeepAlive, tcpKeepIdle, tcpKeepInterval, tcpKeepCount, tcpReuseAddress, tcpSendBufferSize, - tcpReceiveBufferSize, address, isAccepted); + config = new Config.Socket( + tcpNoDelay, + tcpKeepAlive, + tcpKeepIdle, + tcpKeepInterval, + tcpKeepCount, + tcpReuseAddress, + tcpSendBufferSize, + tcpReceiveBufferSize, + address, + isAccepted + ); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config); context.register(); @@ -205,8 +215,18 @@ public void testConnectCanSetSocketOptions() throws IOException { boolean tcpReuseAddress = randomBoolean(); int tcpSendBufferSize = randomIntBetween(1000, 2000); int tcpReceiveBufferSize = randomIntBetween(1000, 2000); - config = new Config.Socket(tcpNoDelay, tcpKeepAlive, tcpKeepIdle, tcpKeepInterval, tcpKeepCount, tcpReuseAddress, tcpSendBufferSize, - tcpReceiveBufferSize, address, false); + config = new Config.Socket( + tcpNoDelay, + tcpKeepAlive, + tcpKeepIdle, + tcpKeepInterval, + tcpKeepCount, + tcpReuseAddress, + tcpSendBufferSize, + tcpReceiveBufferSize, + address, + false + ); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config); doThrow(new SocketException()).doNothing().when(rawSocket).setReuseAddress(tcpReuseAddress); @@ -229,7 +249,7 @@ public void testChannelActiveCallsHandler() throws IOException { public void testWriteFailsIfClosing() { context.closeChannel(); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; context.sendMessage(buffers, listener); verify(listener).accept(isNull(Void.class), any(ClosedChannelException.class)); @@ -240,7 +260,7 @@ public void testSendMessageFromDifferentThreadIsQueuedWithSelector() throws Exce when(selector.isOnCurrentThread()).thenReturn(false); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; WriteOperation writeOperation = mock(WriteOperation.class); when(handler.createWriteOperation(context, buffers, listener)).thenReturn(writeOperation); context.sendMessage(buffers, listener); @@ -254,7 +274,7 @@ public void testSendMessageFromDifferentThreadIsQueuedWithSelector() throws Exce public void testSendMessageFromSameThreadIsQueuedInChannel() { ArgumentCaptor writeOpCaptor = ArgumentCaptor.forClass(WriteOperation.class); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; WriteOperation writeOperation = mock(WriteOperation.class); when(handler.createWriteOperation(context, buffers, listener)).thenReturn(writeOperation); context.sendMessage(buffers, listener); @@ -268,7 +288,7 @@ public void testSendMessageFromSameThreadIsQueuedInChannel() { public void testWriteIsQueuedInChannel() { assertFalse(context.readyForFlush()); - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; FlushReadyWrite writeOperation = new FlushReadyWrite(context, buffer, listener); when(handler.writeToBytes(writeOperation)).thenReturn(Collections.singletonList(writeOperation)); context.queueWriteOperation(writeOperation); @@ -284,7 +304,7 @@ public void testHandleReadBytesWillCheckForNewFlushOperations() throws IOExcepti assertTrue(context.readyForFlush()); } - @SuppressWarnings({"unchecked", "varargs"}) + @SuppressWarnings({ "unchecked", "varargs" }) public void testFlushOpsClearedOnClose() throws Exception { try (SocketChannel realChannel = SocketChannel.open()) { when(channel.getRawChannel()).thenReturn(realChannel); @@ -293,11 +313,12 @@ public void testFlushOpsClearedOnClose() throws Exception { assertFalse(context.readyForFlush()); - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; WriteOperation writeOperation = mock(WriteOperation.class); BiConsumer listener2 = mock(BiConsumer.class); - when(handler.writeToBytes(writeOperation)).thenReturn(Arrays.asList(new FlushOperation(buffer, listener), - new FlushOperation(buffer, listener2))); + when(handler.writeToBytes(writeOperation)).thenReturn( + Arrays.asList(new FlushOperation(buffer, listener), new FlushOperation(buffer, listener2)) + ); context.queueWriteOperation(writeOperation); assertTrue(context.readyForFlush()); @@ -312,21 +333,21 @@ public void testFlushOpsClearedOnClose() throws Exception { } } - @SuppressWarnings({"unchecked", "varargs"}) + @SuppressWarnings({ "unchecked", "varargs" }) public void testWillPollForFlushOpsToClose() throws Exception { try (SocketChannel realChannel = SocketChannel.open()) { when(channel.getRawChannel()).thenReturn(realChannel); InboundChannelBuffer channelBuffer = InboundChannelBuffer.allocatingInstance(); context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, channelBuffer); - - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; BiConsumer listener2 = mock(BiConsumer.class); assertFalse(context.readyForFlush()); when(channel.isOpen()).thenReturn(true); - when(handler.pollFlushOperations()).thenReturn(Arrays.asList(new FlushOperation(buffer, listener), - new FlushOperation(buffer, listener2))); + when(handler.pollFlushOperations()).thenReturn( + Arrays.asList(new FlushOperation(buffer, listener), new FlushOperation(buffer, listener2)) + ); context.closeFromSelector(); verify(selector, times(1)).executeFailedListener(same(listener), any(ClosedChannelException.class)); @@ -341,8 +362,14 @@ public void testCloseClosesWriteProducer() throws IOException { when(channel.getRawChannel()).thenReturn(realChannel); when(channel.isOpen()).thenReturn(true); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); - BytesChannelContext context = new BytesChannelContext(channel, selector, mock(Config.Socket.class), exceptionHandler, handler, - buffer); + BytesChannelContext context = new BytesChannelContext( + channel, + selector, + mock(Config.Socket.class), + exceptionHandler, + handler, + buffer + ); context.closeFromSelector(); verify(handler).close(); } @@ -371,7 +398,7 @@ public void testReadToChannelBufferWillReadAsMuchAsIOBufferAllows() throws IOExc assertEquals(ioBuffer.capacity(), channelBuffer.getIndex()); } - public void testReadToChannelBufferHandlesIOException() throws IOException { + public void testReadToChannelBufferHandlesIOException() throws IOException { when(rawChannel.read(any(ByteBuffer.class))).thenThrow(new IOException()); InboundChannelBuffer channelBuffer = InboundChannelBuffer.allocatingInstance(); @@ -392,7 +419,7 @@ public void testReadToChannelBufferHandlesEOF() throws IOException { public void testFlushBuffersHandlesZeroFlush() throws IOException { when(rawChannel.write(any(ByteBuffer.class))).thenAnswer(consumeBufferAnswer(0)); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertEquals(2, flushOperation.getBuffersToWrite().length); @@ -409,7 +436,7 @@ public void testFlushBuffersHandlesPartialFlush() throws IOException { } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertEquals(1, flushOperation.getBuffersToWrite().length); @@ -426,7 +453,7 @@ public void testFlushBuffersHandlesFullFlush() throws IOException { } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertTrue(flushOperation.isFullyFlushed()); @@ -435,7 +462,7 @@ public void testFlushBuffersHandlesFullFlush() throws IOException { public void testFlushBuffersHandlesIOException() throws IOException { when(rawChannel.write(any(ByteBuffer.class))).thenThrow(new IOException()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10), ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10), ByteBuffer.allocate(10) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); expectThrows(IOException.class, () -> context.flushToChannel(flushOperation)); assertTrue(context.closeNow()); @@ -451,7 +478,7 @@ public void testFlushBuffersHandlesIOExceptionSecondTimeThroughLoop() throws IOE } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); expectThrows(IOException.class, () -> context.flushToChannel(flushOperation)); assertTrue(context.closeNow()); @@ -460,19 +487,40 @@ public void testFlushBuffersHandlesIOExceptionSecondTimeThroughLoop() throws IOE } private static Config.Socket getSocketConfig() { - return new Config.Socket(randomBoolean(), randomBoolean(), -1, -1, -1, randomBoolean(), -1, -1, mock(InetSocketAddress.class), - randomBoolean()); + return new Config.Socket( + randomBoolean(), + randomBoolean(), + -1, + -1, + -1, + randomBoolean(), + -1, + -1, + mock(InetSocketAddress.class), + randomBoolean() + ); } private static class TestSocketChannelContext extends SocketChannelContext { - private TestSocketChannelContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler readWriteHandler, InboundChannelBuffer channelBuffer) { + private TestSocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler readWriteHandler, + InboundChannelBuffer channelBuffer + ) { this(channel, selector, exceptionHandler, readWriteHandler, channelBuffer, getSocketConfig()); } - private TestSocketChannelContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler readWriteHandler, InboundChannelBuffer channelBuffer, Config.Socket config) { + private TestSocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler readWriteHandler, + InboundChannelBuffer channelBuffer, + Config.Socket config + ) { super(channel, selector, config, exceptionHandler, readWriteHandler, channelBuffer); } @@ -484,7 +532,7 @@ public int read() throws IOException { @Override public void flushChannel() throws IOException { - ByteBuffer[] byteBuffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] byteBuffers = { ByteBuffer.allocate(10) }; flushToChannel(new FlushOperation(byteBuffers, (v, e) -> {})); } diff --git a/libs/plugin-classloader/src/main/java/org/opensearch/plugins/ExtendedPluginsClassLoader.java b/libs/plugin-classloader/src/main/java/org/opensearch/plugins/ExtendedPluginsClassLoader.java index 25b1f92b8da08..4a200a5dfa9bd 100644 --- a/libs/plugin-classloader/src/main/java/org/opensearch/plugins/ExtendedPluginsClassLoader.java +++ b/libs/plugin-classloader/src/main/java/org/opensearch/plugins/ExtendedPluginsClassLoader.java @@ -66,7 +66,8 @@ protected Class findClass(String name) throws ClassNotFoundException { * Return a new classloader across the parent and extended loaders. */ public static ExtendedPluginsClassLoader create(ClassLoader parent, List extendedLoaders) { - return AccessController.doPrivileged((PrivilegedAction) - () -> new ExtendedPluginsClassLoader(parent, extendedLoaders)); + return AccessController.doPrivileged( + (PrivilegedAction) () -> new ExtendedPluginsClassLoader(parent, extendedLoaders) + ); } } diff --git a/libs/secure-sm/src/main/java/org/opensearch/secure_sm/SecureSM.java b/libs/secure-sm/src/main/java/org/opensearch/secure_sm/SecureSM.java index ddb5f824781a9..08f1efd4dac45 100644 --- a/libs/secure-sm/src/main/java/org/opensearch/secure_sm/SecureSM.java +++ b/libs/secure-sm/src/main/java/org/opensearch/secure_sm/SecureSM.java @@ -106,10 +106,10 @@ public SecureSM(final String[] classesThatCanExit) { *
  • org.eclipse.internal.junit.runner.
  • *
  • com.intellij.rt.execution.junit.
  • * - * - * For testing purposes, the security manager grants network permissions "connect, accept" + * + * For testing purposes, the security manager grants network permissions "connect, accept" * to following classes, granted they only access local network interfaces. - * + * *