Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding serialization support for InetSocketAddress #7821

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Allow mmap to use new JDK-19 preview APIs in Apache Lucene 9.4+ ([#5151](https://github.com/opensearch-project/OpenSearch/pull/5151))
- Add events correlation engine plugin ([#6854](https://github.com/opensearch-project/OpenSearch/issues/6854))
- Add support for ignoring missing Javadoc on generated code using annotation ([#7604](https://github.com/opensearch-project/OpenSearch/pull/7604))
- Add support for serializing `InetSocketAddress` in StreamOutput/StreamInput

### Dependencies
- Bump `log4j-core` from 2.18.0 to 2.19.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
import java.io.FilterInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.file.AccessDeniedException;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.DirectoryNotEmptyException;
Expand Down Expand Up @@ -346,6 +348,14 @@ public BigInteger readBigInteger() throws IOException {
return new BigInteger(readString());
}

public InetSocketAddress readInetSocketAddress() throws IOException {
String host = this.readString();
byte[] addressBytes = this.readByteArray();
InetAddress addr = InetAddress.getByAddress(host, addressBytes);
int port = this.readInt();
return new InetSocketAddress(addr, port);
}

@Nullable
public Text readOptionalText() throws IOException {
int length = readInt();
Expand Down Expand Up @@ -742,6 +752,8 @@ public Object readGenericValue() throws IOException {
return readCollection(StreamInput::readGenericValue, HashSet::new, Collections.emptySet());
case 26:
return readBigInteger();
case 27:
return readInetSocketAddress();
default:
throw new IOException("Can't read unknown type [" + type + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.nio.file.AccessDeniedException;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.DirectoryNotEmptyException;
Expand Down Expand Up @@ -791,6 +792,13 @@ public final void writeOptionalInstant(@Nullable Instant instant) throws IOExcep
o.writeByte((byte) 26);
o.writeString(v.toString());
});
writers.put(InetSocketAddress.class, (o, v) -> {
final InetSocketAddress inetSocketAddress = (InetSocketAddress) v;
o.writeByte((byte) 27);
o.writeString(inetSocketAddress.getHostString());
o.writeByteArray(inetSocketAddress.getAddress().getAddress());
o.writeInt(inetSocketAddress.getPort());
});
WRITERS = Collections.unmodifiableMap(writers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
Expand Down Expand Up @@ -513,6 +515,14 @@ public void testMapIsWriteable() throws IOException {
assertNotWriteable(Collections.singletonMap("a", new Unwriteable()), Unwriteable.class);
}

public void testInetSocketAddress() throws IOException {
InetSocketAddress toWrite = new InetSocketAddress(InetAddress.getLoopbackAddress(), 80);
BytesStreamOutput out = new BytesStreamOutput();
out.writeGenericValue(toWrite);
assertEquals(toWrite, getStreamInput(out.bytes()).readGenericValue());
out.close();
}

public void testObjectArrayIsWriteable() throws IOException {
StreamOutput.checkWriteable(new Object[] { "a", "b" });
assertNotWriteable(new Object[] { new Unwriteable() }, Unwriteable.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

import java.io.EOFException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -311,6 +313,7 @@ public void testSimpleStreams() throws Exception {
float[] floatArray = { 1.1f, 2.2f, 3.3f };
out.writeGenericValue(floatArray);
double[] doubleArray = { 1.1, 2.2, 3.3 };
InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 80);
out.writeGenericValue(doubleArray);
out.writeString("hello");
out.writeString("goodbye");
Expand All @@ -324,6 +327,7 @@ public void testSimpleStreams() throws Exception {
out.writeOptionalTimeZone(DateTimeZone.getDefault());
out.writeOptionalTimeZone(null);
out.writeGenericValue(new DateTime(123456, DateTimeZone.forID("America/Los_Angeles")));
out.writeGenericValue(inetSocketAddress);
final byte[] bytes = BytesReference.toBytes(out.bytes());
StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));
assertEquals(in.available(), bytes.length);
Expand Down Expand Up @@ -361,6 +365,7 @@ public void testSimpleStreams() throws Exception {
JodaCompatibleZonedDateTime jdt = (JodaCompatibleZonedDateTime) dt;
assertThat(jdt.getZonedDateTime().toInstant().toEpochMilli(), equalTo(123456L));
assertThat(jdt.getZonedDateTime().getZone(), equalTo(ZoneId.of("America/Los_Angeles")));
assertEquals(inetSocketAddress, in.readGenericValue());
assertEquals(0, in.available());
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> out.writeGenericValue(new Object() {
@Override
Expand Down