Skip to content

Commit

Permalink
[Refactor] core.common to new opensearch-common library (#5976)
Browse files Browse the repository at this point in the history
Refactors all of o.opensearch.common classes in the core library to a new
opensearch-commons library (inspired by apache commons). The intent is to
refactor any o.opensearch.common.* classes in :server module to this new
library. This will be done with care such that the dependencies are carefully
managed to avoid any cyclic or unnecessarily complicated dependencies.

Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize authored Jan 31, 2023
1 parent 7aea7d2 commit c557f27
Show file tree
Hide file tree
Showing 66 changed files with 247 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Use ReplicationFailedException instead of OpensearchException in ReplicationTarget ([#4725](https://github.com/opensearch-project/OpenSearch/pull/4725))
- [Refactor] Use local opensearch.common.SetOnce instead of lucene's utility class ([#5947](https://github.com/opensearch-project/OpenSearch/pull/5947))
- Cluster health call to throw decommissioned exception for local decommissioned node([#6008](https://github.com/opensearch-project/OpenSearch/pull/6008))
- [Refactor] core.common to new opensearch-common library ([#5976](https://github.com/opensearch-project/OpenSearch/pull/5976))

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ if (project != rootProject) {
distribution project(':distribution:archives:linux-tar')
distribution project(':distribution:archives:windows-zip')

integTestRuntimeOnly(project(":libs:opensearch-common"))
integTestRuntimeOnly(project(":libs:opensearch-core"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public class JarHellPrecommitPlugin extends PrecommitPlugin {
@Override
public TaskProvider<? extends Task> createTask(Project project) {
Configuration jarHellConfig = project.getConfigurations().create("jarHell");
if (BuildParams.isInternal() && project.getPath().equals(":libs:opensearch-core") == false) {
if (BuildParams.isInternal() && project.getPath().equals(":libs:opensearch-common") == false) {
// External plugins will depend on this already via transitive dependencies.
// Internal projects are not all plugins, so make sure the check is available
// we are not doing this for this project itself to avoid jar hell with itself
project.getDependencies().add("jarHell", project.project(":libs:opensearch-core"));
project.getDependencies().add("jarHell", project.project(":libs:opensearch-common"));
}

TaskProvider<JarHellTask> jarHell = project.getTasks().register("jarHell", JarHellTask.class);
Expand Down
7 changes: 5 additions & 2 deletions gradle/missing-javadoc.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ configure([
project(":client:rest-high-level"),
project(":client:test"),
project(":libs:opensearch-cli"),
project(":libs:opensearch-core"),
project(":libs:opensearch-dissect"),
project(":libs:opensearch-geo"),
project(":libs:opensearch-grok"),
Expand Down Expand Up @@ -162,7 +161,11 @@ configure([
}
}

configure(project(":server")) {
configure([
project(":libs:opensearch-common"),
project(":libs:opensearch-core"),
project(":server")
]) {
project.tasks.withType(MissingJavadocTask) {
// TODO: bump to variable missing level after increasing javadoc coverage
javadocMissingLevel = "class"
Expand Down
5 changes: 3 additions & 2 deletions libs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ subprojects {
dependencies.matching { it instanceof ProjectDependency }.all { ProjectDependency dep ->
Project depProject = dep.dependencyProject
if (depProject != null
&& false == depProject.path.equals(':libs:opensearch-core')
&& (false == depProject.path.equals(':libs:opensearch-core') &&
false == depProject.path.equals(':libs:opensearch-common'))
&& depProject.path.startsWith(':libs')) {
throw new InvalidUserDataException("projects in :libs "
+ "may not depend on other projects libs except "
+ ":libs:opensearch-core but "
+ ":libs:opensearch-core or :libs:opensearch-common but "
+ "${project.path} depends on ${depProject.path}")
}
}
Expand Down
1 change: 1 addition & 0 deletions libs/cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ apply plugin: 'opensearch.publish'

dependencies {
api 'net.sf.jopt-simple:jopt-simple:5.0.4'
api project(':libs:opensearch-common')
api project(':libs:opensearch-core')
}

Expand Down
35 changes: 35 additions & 0 deletions libs/common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import org.opensearch.gradle.info.BuildParams

apply plugin: 'opensearch.publish'

archivesBaseName = 'opensearch-common'

dependencies {
// This dependency is used only by :libs:core for null-checking interop with other tools
compileOnly "com.google.code.findbugs:jsr305:3.0.2"

testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
testImplementation "junit:junit:${versions.junit}"
testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}"

testImplementation(project(":test:framework")) {
exclude group: 'org.opensearch', module: 'opensearch-common'
}
}

tasks.named('forbiddenApisMain').configure {
// :libs:opensearch-common does not depend on server
// TODO: Need to decide how we want to handle for forbidden signatures with the changes to server
replaceSignatureFiles 'jdk-signatures'
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
* <li>Checks any {@code X-Compile-OpenSearch-Version} value in
* the jar manifest is compatible with the current ES</li>
* </ul>
*
* @opensearch.internal
*/
public class JarHell {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
import java.util.HashSet;
import java.util.Set;

/**
* Checks for jdk jar hell
*
* @opensearch.internal
*/
public class JdkJarHellCheck {

private Set<String> detected = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

/** Contains JarHell Classes */
package org.opensearch.bootstrap;
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

package org.opensearch.common;

/**
* Boolean class utilities
*
* @opensearch.api
*/
public final class Booleans {
private Booleans() {
throw new AssertionError("No instances intended");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

/**
* Helper class similar to Arrays to handle conversions for Char arrays
*
* @opensearch.api
*/
public final class CharArrays {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

/**
* A {@link Consumer}-like interface which allows throwing checked exceptions.
*
* @opensearch.api
*/
@FunctionalInterface
public interface CheckedConsumer<T, E extends Exception> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

/**
* A {@link Function}-like interface which allows throwing checked exceptions.
*
* @opensearch.api
*/
@FunctionalInterface
public interface CheckedFunction<T, R, E extends Exception> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

/**
* A {@link Runnable}-like interface which allows throwing checked exceptions.
*
* @opensearch.api
*/
@FunctionalInterface
public interface CheckedRunnable<E extends Exception> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

/**
* Utility class for glob-like matching
*
* @opensearch.api
*/
public class Glob {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@

import java.util.function.Supplier;

/**
* A base supplier using memoization optimization technique
*
* @opensearch.api
*/
public class MemoizedSupplier<T> implements Supplier<T> {
private Supplier<T> supplier;
private T value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* {@code null} is an acceptable value for that parameter. It should not be
* used for parameters of primitive types.
*
*
* @opensearch.api
*/
@Documented
@TypeQualifierNickname
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

/**
* Annotation to suppress forbidden-apis errors inside a whole class, a method, or a field.
*
* @opensearch.api
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@

import java.util.Collection;

/**
* Java 9 List
*
* todo: deprecate and remove w/ min jdk upgrade to 11?
*
* @opensearch.internal
*/
public class List {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@

package org.opensearch.common.collect;

/**
* Java 9 Map
*
* todo: deprecate and remove w/ min jdk upgrade to 11?
*
* @opensearch.internal
*/
public class Map {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@

import java.util.Collection;

/**
* Java 9 Set
*
* todo: deprecate and remove w/ min jdk upgrade to 11?
*
* @opensearch.internal
*/
public class Set {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@

package org.opensearch.common.collect;

/**
* Java 9 Tuple
*
* todo: deprecate and remove w/ min jdk upgrade to 11?
*
* @opensearch.internal
*/
public class Tuple<V1, V2> {

public static <V1, V2> Tuple<V1, V2> tuple(V1 v1, V2 v2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

/** Common collections classes used across opensearch. */
package org.opensearch.common.collect;
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
* an exceptional result. This allows attaching listeners that only handle {@link Exception}.
*
* @param <T> the result type
*
* @opensearch.api
*/
public class CompletableContext<T> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

/** Common concurrency utilities used across opensearch. */
package org.opensearch.common.concurrent;
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
* <p>
* This class allows the default filesystem to
* be changed during tests.
*
* @opensearch.internal
*/
@SuppressForbidden(reason = "accesses the default filesystem by design")
// TODO: can we move this to the .env package and make it package-private?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

/** Common i/o utilities used across opensearch. */
package org.opensearch.common.io;
10 changes: 10 additions & 0 deletions libs/common/src/main/java/org/opensearch/common/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

/** Common Library of Utilties and Data Structures used across OpenSearch. */
package org.opensearch.common;
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* Time value unit of measurement
*
* @opensearch.api
*/
public class TimeValue implements Comparable<TimeValue> {

/** How many nano-seconds in one milli-second */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

/** Common units of measurement used across opensearch. */
package org.opensearch.common.unit;
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

/**
* Additions or modifications to this class should only come from the original org.math.plot.utils.FastMath source
*
* @opensearch.api
*/
final class FastMath {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

/**
* Similar to Lucene's SloppyMath, but for additional math functions.
*
* @opensearch.api
*/
public class OpenSearchSloppyMath {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
* A basic RefCounted implementation that is initialized with a
* ref count of 1 and calls {@link #closeInternal()} once it reaches
* a 0 ref count
*
* @opensearch.api
*/
public abstract class AbstractRefCounted implements RefCounted {
private final AtomicInteger refCount = new AtomicInteger(1);
Expand Down
Loading

0 comments on commit c557f27

Please sign in to comment.