Skip to content

Commit

Permalink
[Refactor] Serverless common classes to libraries (#8065) (#8531)
Browse files Browse the repository at this point in the history
This commit refactors common classes from the server module common
package to the common and core library so they can be used across
serverless and cloud native implementations without depending on the
server module.

Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize authored Jul 7, 2023
1 parent e3762b4 commit 2065cdb
Show file tree
Hide file tree
Showing 217 changed files with 259 additions and 420 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Improved performance of parsing floating point numbers ([#8467](https://github.com/opensearch-project/OpenSearch/pull/8467))
- Move span actions to Scope ([#8411](https://github.com/opensearch-project/OpenSearch/pull/8411))
- [Refactor] OpenSearchException streamables to a registry ([#7646](https://github.com/opensearch-project/OpenSearch/pull/7646))
- [Refactor] Serverless common classes to libraries ([#8065](https://github.com/opensearch-project/OpenSearch/pull/8065))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;

import java.util.Random;
import java.util.concurrent.TimeUnit;
Expand Down
32 changes: 0 additions & 32 deletions libs/common/src/main/java/org/opensearch/common/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,6 @@ public final class Numbers {

private Numbers() {}

public static byte[] intToBytes(int val) {
byte[] arr = new byte[4];
arr[0] = (byte) (val >>> 24);
arr[1] = (byte) (val >>> 16);
arr[2] = (byte) (val >>> 8);
arr[3] = (byte) (val);
return arr;
}

/**
* Converts an int to a byte array.
*
* @param val The int to convert to a byte array
* @return The byte array converted
*/
public static byte[] shortToBytes(int val) {
byte[] arr = new byte[2];
arr[0] = (byte) (val >>> 8);
arr[1] = (byte) (val);
return arr;
}

/**
* Converts a long to a byte array.
*
Expand All @@ -93,16 +71,6 @@ public static byte[] longToBytes(long val) {
return arr;
}

/**
* Converts a double to a byte array.
*
* @param val The double to convert to a byte array
* @return The byte array converted
*/
public static byte[] doubleToBytes(double val) {
return longToBytes(Double.doubleToRawLongBits(val));
}

/** Returns true if value is neither NaN nor infinite. */
public static boolean isValidDouble(double value) {
if (Double.isNaN(value) || Double.isInfinite(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

package org.opensearch.common;

import org.opensearch.common.settings.SecureString;

import java.util.Arrays;
import java.util.Base64;
import java.util.Random;

Expand All @@ -54,27 +51,6 @@ public String getBase64UUID() {
return getBase64UUID(SecureRandomHolder.INSTANCE);
}

/**
* Returns a Base64 encoded {@link SecureString} of a Version 4.0 compatible UUID
* as defined here: http://www.ietf.org/rfc/rfc4122.txt
*/
public SecureString getBase64UUIDSecureString() {
byte[] uuidBytes = null;
byte[] encodedBytes = null;
try {
uuidBytes = getUUIDBytes(SecureRandomHolder.INSTANCE);
encodedBytes = Base64.getUrlEncoder().withoutPadding().encode(uuidBytes);
return new SecureString(CharArrays.utf8BytesToChars(encodedBytes));
} finally {
if (uuidBytes != null) {
Arrays.fill(uuidBytes, (byte) 0);
}
if (encodedBytes != null) {
Arrays.fill(encodedBytes, (byte) 0);
}
}
}

/**
* Returns a Base64 encoded version of a Version 4.0 compatible UUID
* randomly initialized by the given {@link java.util.Random} instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
package org.opensearch.common;

import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.lease.Releasable;

import java.text.NumberFormat;
import java.util.LinkedList;
Expand Down Expand Up @@ -155,7 +154,7 @@ public StopWatch stop() throws IllegalStateException {
return this;
}

public Releasable timing(String taskName) {
public TimingHandle timing(String taskName) {
start(taskName);
return this::stop;
}
Expand Down Expand Up @@ -268,4 +267,14 @@ public TimeValue getTime() {
}
}

/**
* Stops the watch and auto calls close in try-with-resources usage
*
* @opensearch.internal
*/
public interface TimingHandle extends AutoCloseable {
@Override
void close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

package org.opensearch.common;

import org.opensearch.common.settings.SecureString;

import java.util.Random;

/**
Expand All @@ -44,7 +42,7 @@
public class UUIDs {

private static final RandomBasedUUIDGenerator RANDOM_UUID_GENERATOR = new RandomBasedUUIDGenerator();
private static final UUIDGenerator LEGACY_TIME_UUID_GENERATOR = new LegacyTimeBasedUUIDGenerator();

private static final UUIDGenerator TIME_UUID_GENERATOR = new TimeBasedUUIDGenerator();

/** Generates a time-based UUID (similar to Flake IDs), which is preferred when generating an ID to be indexed into a Lucene index as
Expand All @@ -53,11 +51,6 @@ public static String base64UUID() {
return TIME_UUID_GENERATOR.getBase64UUID();
}

/** Legacy implementation of {@link #base64UUID()}, for pre 6.0 indices. */
public static String legacyBase64UUID() {
return LEGACY_TIME_UUID_GENERATOR.getBase64UUID();
}

/** Returns a Base64 encoded version of a Version 4.0 compatible UUID as defined here: http://www.ietf.org/rfc/rfc4122.txt, using the
* provided {@code Random} instance */
public static String randomBase64UUID(Random random) {
Expand All @@ -69,10 +62,4 @@ public static String randomBase64UUID(Random random) {
public static String randomBase64UUID() {
return RANDOM_UUID_GENERATOR.getBase64UUID();
}

/** Returns a Base64 encoded {@link SecureString} of a Version 4.0 compatible UUID as defined here: http://www.ietf.org/rfc/rfc4122.txt,
* using a private {@code SecureRandom} instance */
public static SecureString randomBase64UUIDSecureString() {
return RANDOM_UUID_GENERATOR.getBase64UUIDSecureString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
* GitHub history for details.
*/

package org.opensearch.core.common.lease;
package org.opensearch.common.lease;

import java.io.Closeable;

/**
* Specialization of {@link AutoCloseable} that may only throw an {@code BaseOpenSearchException}.
* Specialization of {@link AutoCloseable} for calls that might not throw a checked exception.
*
* @opensearch.internal
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

package org.opensearch.core.common.lease;
package org.opensearch.common.lease;

import org.opensearch.common.Nullable;
import org.opensearch.common.util.io.IOUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/

/** Base Releasables package. */
package org.opensearch.core.common.lease;
package org.opensearch.common.lease;
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.apache.lucene.search.ScoreMode;
import org.opensearch.common.util.BigArrays;
import org.opensearch.common.util.ObjectArray;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasables;
import org.opensearch.index.fielddata.NumericDoubleValues;
import org.opensearch.search.MultiValueMode;
import org.opensearch.search.aggregations.Aggregator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.ScoreMode;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasables;
import org.opensearch.search.aggregations.Aggregator;
import org.opensearch.search.aggregations.AggregatorFactories;
import org.opensearch.search.aggregations.CardinalityUpperBound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import org.opensearch.common.util.BigArrays;
import org.opensearch.common.util.DoubleArray;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasables;
import org.opensearch.search.aggregations.Aggregator;
import org.opensearch.search.aggregations.InternalAggregation;
import org.opensearch.search.aggregations.metrics.MetricsAggregator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
import org.opensearch.common.lucene.Lucene;
import org.opensearch.common.util.BigArrays;
import org.opensearch.common.util.BitArray;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasable;
import org.opensearch.common.lease.Releasables;
import org.opensearch.search.aggregations.Aggregator;
import org.opensearch.search.aggregations.AggregatorFactories;
import org.opensearch.search.aggregations.CardinalityUpperBound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.bytes.ReleasableBytesReference;
import org.opensearch.common.util.PageCacheRecycler;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasables;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.InboundPipeline;
import org.opensearch.transport.Transport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
import org.opensearch.common.util.PageCacheRecycler;
import org.opensearch.common.util.concurrent.OpenSearchExecutors;
import org.opensearch.common.util.net.NetUtils;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasables;
import org.opensearch.indices.breaker.CircuitBreakerService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.Netty4NioSocketChannel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import org.opensearch.common.bytes.ReleasableBytesReference;
import org.opensearch.common.util.PageCacheRecycler;
import org.opensearch.common.util.io.IOUtils;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasable;
import org.opensearch.common.lease.Releasables;
import org.opensearch.nio.BytesWriteHandler;
import org.opensearch.nio.InboundChannelBuffer;
import org.opensearch.nio.Page;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.opensearch.common.UUIDs;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchIntegTestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.opensearch.common.UUIDs;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.index.shard.IndexShardState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.opensearch.common.UUIDs;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
import org.opensearch.index.shard.ShardId;
import org.opensearch.indices.IndicesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.opensearch.common.UUIDs;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
import org.opensearch.index.shard.ShardId;
import org.opensearch.indices.IndicesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.opensearch.cluster.routing.ShardRouting;
import org.opensearch.common.Nullable;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.index.Index;
import org.opensearch.index.IndexModule;
import org.opensearch.index.IndexService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
import org.opensearch.common.lucene.index.OpenSearchDirectoryReader;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.lease.Releasable;
import org.opensearch.common.util.FeatureFlags;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.index.IndexModule;
import org.opensearch.index.SegmentReplicationPerGroupStats;
import org.opensearch.index.SegmentReplicationPressureService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.opensearch.common.Glob;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.RunOnce;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.core.common.Strings;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchIntegTestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.index.shard.ShardId;
import org.opensearch.indices.IndicesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.index.shard.ShardId;
import org.opensearch.indices.IndicesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import org.opensearch.common.Randomness;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasables;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.index.query.ParsedQuery;
import org.opensearch.index.query.QueryShardException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
import org.opensearch.common.inject.Inject;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.concurrent.AtomicArray;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.index.Index;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.index.IndexingPressureService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
import org.opensearch.common.util.concurrent.AbstractRunnable;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.common.lease.Releasable;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.index.IndexingPressureService;
import org.opensearch.index.SegmentReplicationPressureService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasables;
import org.opensearch.index.IndexService;
import org.opensearch.index.engine.Engine;
import org.opensearch.index.get.GetResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
import org.opensearch.common.SetOnce;
import org.opensearch.common.util.concurrent.AbstractRunnable;
import org.opensearch.common.util.concurrent.AtomicArray;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.core.common.lease.Releasables;
import org.opensearch.common.lease.Releasable;
import org.opensearch.common.lease.Releasables;
import org.opensearch.index.shard.ShardId;
import org.opensearch.search.SearchPhaseResult;
import org.opensearch.search.SearchShardTarget;
Expand Down
Loading

0 comments on commit 2065cdb

Please sign in to comment.