Skip to content

Commit

Permalink
[Refactor] Stream Reader and Write Generics
Browse files Browse the repository at this point in the history
StreamInput and StreamOutput provide the core functionality for
marshalling / unmarshalling objects over the transport wire. The class
is intended to be generic but it is tightly coupled to the types defined
in the server module. Because of this tight coupling, the classes cannot
be refactored to the core library, thus all new types are required to be
hard coded in the server module.

To decouple this logic and make it more generic across opensearch
modules and plugins, this commit introduces a reader and writer registry
in a new BaseWriteable interface. The StreamInput and StreamOutput also
now inherits from new BaseStreamInput and BaseStreamOutput classes,
respectively, located in the core library. This will be the new home for
streaming primitives in a follow up commit.

Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed May 8, 2023
1 parent c2c5730 commit 49de842
Show file tree
Hide file tree
Showing 20 changed files with 305 additions and 90 deletions.
5 changes: 5 additions & 0 deletions libs/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@ tasks.named("dependencyLicenses").configure {
mapping from: /jackson-.*/, to: 'jackson'
mapping from: /lucene-.*/, to: 'lucene'
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs -= '-Xlint:rawtypes'
options.compilerArgs -= '-Xlint:unchecked'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/
package org.opensearch.core.common.io.stream;

import java.io.InputStream;

/**
* Foundation class for reading core types off the transport stream
*
* todo: refactor {@code StreamInput} primitive readers to this class
*
* @opensearch.internal
*/
public abstract class BaseStreamInput extends InputStream {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/
package org.opensearch.core.common.io.stream;

import java.io.OutputStream;

/**
* Foundation class for writing core types over the transport stream
*
* todo: refactor {@code StreamOutput} primitive writers to this class
*
* @opensearch.internal
*/
public abstract class BaseStreamOutput extends OutputStream {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.
*/
package org.opensearch.core.common.io.stream;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* Implementers can be written to a {@code StreamOutput} and read from a {@code StreamInput}. This allows them to be "thrown
* across the wire" using OpenSearch's internal protocol. If the implementer also implements equals and hashCode then a copy made by
* serializing and deserializing must be equal and have the same hashCode. It isn't required that such a copy be entirely unchanged.
*
* @opensearch.internal
*/
public interface BaseWriteable<S extends BaseStreamOutput> {
/**
* A WriteableRegistry registers {@link Writer} methods for writing data types over a
* {@link BaseStreamOutput} channel and {@link Reader} methods for reading data from a
* {@link BaseStreamInput} channel.
*
* @opensearch.internal
*/
class WriteableRegistry {
private static final Map<Class<?>, Writer> WRITER_REGISTRY = new HashMap<>();
private static final Map<Byte, Reader> READER_REGISTRY = new HashMap<>();

/**
* registers a streamable writer
*
* @opensearch.internal
*/
public static <W extends Writer> void registerWriter(final Class<?> clazz, final W writer) {
if (WRITER_REGISTRY.containsKey(clazz)) {
throw new IllegalArgumentException("Streamable writer already registered for type [" + clazz.getName() + "]");
}
WRITER_REGISTRY.put(clazz, writer);
}

/**
* registers a streamable reader
*
* @opensearch.internal
*/
public static <R extends Reader> void registerReader(final byte ordinal, final R reader) {
if (READER_REGISTRY.containsKey(ordinal)) {
throw new IllegalArgumentException("Streamable reader already registered for ordinal [" + (int) ordinal + "]");
}
READER_REGISTRY.put(ordinal, reader);
}

public static <W extends Writer> W getWriter(final Class<?> clazz) {
return (W) WRITER_REGISTRY.get(clazz);
}

public static <R extends Reader> R getReader(final byte b) {
return (R) READER_REGISTRY.get(b);
}
}

/**
* Write this into the {@linkplain BaseStreamOutput}.
*/
void writeTo(final S out) throws IOException;

/**
* Reference to a method that can write some object to a {@link BaseStreamOutput}.
* <p>
* By convention this is a method from {@link BaseStreamOutput} itself (e.g., {@code StreamOutput#writeString}). If the value can be
* {@code null}, then the "optional" variant of methods should be used!
* <p>
* Most classes should implement {@code Writeable} and the {@code Writeable#writeTo(BaseStreamOutput)} method should <em>use</em>
* {@link BaseStreamOutput} methods directly or this indirectly:
* <pre><code>
* public void writeTo(StreamOutput out) throws IOException {
* out.writeVInt(someValue);
* out.writeMapOfLists(someMap, StreamOutput::writeString, StreamOutput::writeString);
* }
* </code></pre>
*/
@FunctionalInterface
interface Writer<S extends BaseStreamOutput, V> {

/**
* Write {@code V}-type {@code value} to the {@code out}put stream.
*
* @param out Output to write the {@code value} too
* @param value The value to add
*/
void write(final S out, V value) throws IOException;
}

/**
* Reference to a method that can read some object from a stream. By convention this is a constructor that takes
* {@linkplain BaseStreamInput} as an argument for most classes and a static method for things like enums. Returning null from one of these
* is always wrong - for that we use methods like {@code StreamInput#readOptionalWriteable(Reader)}.
* <p>
* As most classes will implement this via a constructor (or a static method in the case of enumerations), it's something that should
* look like:
* <pre><code>
* public MyClass(final StreamInput in) throws IOException {
* this.someValue = in.readVInt();
* this.someMap = in.readMapOfLists(StreamInput::readString, StreamInput::readString);
* }
* </code></pre>
*/
@FunctionalInterface
interface Reader<S extends BaseStreamInput, V> {

/**
* Read {@code V}-type value from a stream.
*
* @param in Input to read the value from
*/
V read(final S in) throws IOException;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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.
*/
/** Core transport stream classes */
package org.opensearch.core.common.io.stream;
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.common.util.LongObjectPagedHashMap;
import org.opensearch.core.common.io.stream.BaseWriteable;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.search.aggregations.InternalAggregation;
import org.opensearch.search.aggregations.InternalAggregations;
Expand Down Expand Up @@ -69,15 +70,15 @@ protected BaseGeoGrid(String name, int requiredSize, List<BaseGeoGridBucket> buc
this.buckets = buckets;
}

protected abstract Writeable.Reader<B> getBucketReader();
protected abstract BaseWriteable.Reader<StreamInput, B> getBucketReader();

/**
* Read from a stream.
*/
public BaseGeoGrid(StreamInput in) throws IOException {
super(in);
requiredSize = readSize(in);
buckets = (List<BaseGeoGridBucket>) in.readList(getBucketReader());
buckets = (List<BaseGeoGridBucket>) in.readList((Writeable.Reader<B>) getBucketReader());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.opensearch.common.geo.GeoPoint;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.BaseStreamInput;
import org.opensearch.geometry.utils.Geohash;
import org.opensearch.search.aggregations.InternalAggregations;

Expand All @@ -51,8 +52,8 @@ class InternalGeoHashGridBucket extends BaseGeoGridBucket<InternalGeoHashGridBuc
/**
* Read from a stream.
*/
InternalGeoHashGridBucket(StreamInput in) throws IOException {
super(in);
InternalGeoHashGridBucket(BaseStreamInput in) throws IOException {
super((StreamInput) in);
}

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

import org.opensearch.common.geo.GeoPoint;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.BaseStreamInput;
import org.opensearch.search.aggregations.InternalAggregations;
import org.opensearch.search.aggregations.bucket.GeoTileUtils;

Expand All @@ -52,8 +53,8 @@ class InternalGeoTileGridBucket extends BaseGeoGridBucket<InternalGeoTileGridBuc
/**
* Read from a stream.
*/
InternalGeoTileGridBucket(StreamInput in) throws IOException {
super(in);
InternalGeoTileGridBucket(BaseStreamInput in) throws IOException {
super((StreamInput) in);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public GeoBoundingBox(GeoPoint topLeft, GeoPoint bottomRight) {
}

public GeoBoundingBox(StreamInput input) throws IOException {
this.topLeft = input.readGeoPoint();
this.bottomRight = input.readGeoPoint();
this.topLeft = new GeoPoint(input);
this.bottomRight = new GeoPoint(input);
}

public boolean isUnbounded() {
Expand Down Expand Up @@ -164,8 +164,8 @@ public boolean pointInBounds(double lon, double lat) {

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeGeoPoint(topLeft);
out.writeGeoPoint(bottomRight);
topLeft.writeTo(out);
bottomRight.writeTo(out);
}

@Override
Expand Down
26 changes: 26 additions & 0 deletions server/src/main/java/org/opensearch/common/geo/GeoPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
import org.apache.lucene.util.BytesRef;
import org.opensearch.OpenSearchParseException;
import org.opensearch.common.geo.GeoUtils.EffectivePoint;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.BaseWriteable.Reader;
import org.opensearch.core.common.io.stream.BaseWriteable.Writer;
import org.opensearch.core.common.io.stream.BaseWriteable.WriteableRegistry;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.geometry.Geometry;
Expand Down Expand Up @@ -87,6 +92,22 @@ public GeoPoint(GeoPoint template) {
this(template.getLat(), template.getLon());
}

public GeoPoint(final StreamInput in) throws IOException {
this.lat = in.readDouble();
this.lon = in.readDouble();
}

/**
* Register this type as a streamable so it can be serialized over the wire
*/
public static void registerStreamables() {
WriteableRegistry.<Writer<StreamOutput, ?>>registerWriter(GeoPoint.class, (o, v) -> {
o.writeByte((byte) 22);
((GeoPoint) v).writeTo(o);
});
WriteableRegistry.<Reader<StreamInput, ?>>registerReader(Byte.valueOf((byte) 22), GeoPoint::new);
}

public GeoPoint reset(double lat, double lon) {
this.lat = lat;
this.lon = lon;
Expand Down Expand Up @@ -210,6 +231,11 @@ public GeoPoint resetFromGeoHash(long geohashLong) {
return this.resetFromIndexHash(BitUtil.flipFlop((geohashLong >>> 4) << ((level * 5) + 2)));
}

public void writeTo(final StreamOutput out) throws IOException {
out.writeDouble(this.lat);
out.writeDouble(this.lon);
}

public double lat() {
return this.lat;
}
Expand Down
Loading

0 comments on commit 49de842

Please sign in to comment.