From 983d44a0afe67e067dd1e8fd79d60956ed7b7aa9 Mon Sep 17 00:00:00 2001 From: Nicholas Walter Knize Date: Tue, 15 Aug 2023 10:47:06 -0500 Subject: [PATCH 1/2] [Remove] Joda from Serialization In Legacy 7.0 joda time was removed in favor of java time to support nanoseconds properly. This commit removes joda from the serialization including ignoring the joda flag sent over the wire for DocValueFormat. Signed-off-by: Nicholas Walter Knize --- build.gradle | 4 +- .../core/common/io/stream/StreamInput.java | 2 + .../common/io/stream/Streamables.java | 21 ---- .../xcontent/XContentOpenSearchExtension.java | 29 +---- .../action/cat/RestCatRecoveryAction.java | 5 +- .../cat/RestCatSegmentReplicationAction.java | 5 +- .../org/opensearch/search/DocValueFormat.java | 17 +-- .../cluster/metadata/IndexGraveyardTests.java | 10 +- .../common/io/stream/BytesStreamsTests.java | 16 +-- .../common/xcontent/BaseXContentTestCase.java | 103 +++--------------- .../builder/XContentBuilderTests.java | 4 +- .../action/cat/RestRecoveryActionTests.java | 5 +- 12 files changed, 57 insertions(+), 164 deletions(-) diff --git a/build.gradle b/build.gradle index eef5d6d5b442c..45d07f75e0e54 100644 --- a/build.gradle +++ b/build.gradle @@ -225,10 +225,10 @@ tasks.register("verifyVersions") { * after the backport of the backcompat code is complete. */ -boolean bwc_tests_enabled = true +boolean bwc_tests_enabled = false /* place an issue link here when committing bwc changes */ -String bwc_tests_disabled_issue = "" +String bwc_tests_disabled_issue = "https://github.com/opensearch-project/OpenSearch/issues/9349" /* there's no existing MacOS release, therefore disable bcw tests */ if (Os.isFamily(Os.FAMILY_MAC)) { diff --git a/libs/core/src/main/java/org/opensearch/core/common/io/stream/StreamInput.java b/libs/core/src/main/java/org/opensearch/core/common/io/stream/StreamInput.java index b6d4fcf21ca66..6681bc6035d7a 100644 --- a/libs/core/src/main/java/org/opensearch/core/common/io/stream/StreamInput.java +++ b/libs/core/src/main/java/org/opensearch/core/common/io/stream/StreamInput.java @@ -722,6 +722,8 @@ public Object readGenericValue() throws IOException { return readByte(); case 12: return readDate(); + case 13: + return readZonedDateTime(); case 14: return readBytesReference(); case 15: diff --git a/server/src/main/java/org/opensearch/common/io/stream/Streamables.java b/server/src/main/java/org/opensearch/common/io/stream/Streamables.java index c6a03cf0475b7..fd9ed6e5b18fe 100644 --- a/server/src/main/java/org/opensearch/common/io/stream/Streamables.java +++ b/server/src/main/java/org/opensearch/common/io/stream/Streamables.java @@ -9,16 +9,9 @@ package org.opensearch.common.io.stream; import org.opensearch.common.geo.GeoPoint; -import org.opensearch.common.time.DateUtils; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.io.stream.Writeable.WriteableRegistry; -import org.joda.time.DateTimeZone; -import org.joda.time.ReadableInstant; - -import java.time.Instant; -import java.time.ZoneId; -import java.time.ZonedDateTime; /** * This utility class registers generic types for streaming over the wire using @@ -47,14 +40,6 @@ public static void registerStreamables() { * Registers writers by class type */ private static void registerWriters() { - /** {@link ReadableInstant} */ - WriteableRegistry.registerWriter(ReadableInstant.class, (o, v) -> { - o.writeByte((byte) 13); - final ReadableInstant instant = (ReadableInstant) v; - o.writeString(instant.getZone().getID()); - o.writeLong(instant.getMillis()); - }); - WriteableRegistry.registerClassAlias(ReadableInstant.class, ReadableInstant.class); /** {@link GeoPoint} */ WriteableRegistry.registerWriter(GeoPoint.class, (o, v) -> { o.writeByte((byte) 22); @@ -68,12 +53,6 @@ private static void registerWriters() { * NOTE: see {@code StreamOutput#WRITERS} for all registered ordinals */ private static void registerReaders() { - /** {@link ZonedDateTime} */ - WriteableRegistry.registerReader(Byte.valueOf((byte) 13), (i) -> { - final ZoneId zoneId = DateUtils.dateTimeZoneToZoneId(DateTimeZone.forID(i.readString())); - long millis = i.readLong(); - return ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), zoneId); - }); /** {@link GeoPoint} */ WriteableRegistry.registerReader(Byte.valueOf((byte) 22), GeoPoint::new); } diff --git a/server/src/main/java/org/opensearch/common/xcontent/XContentOpenSearchExtension.java b/server/src/main/java/org/opensearch/common/xcontent/XContentOpenSearchExtension.java index 17de403fbc157..0ce6e3da515d8 100644 --- a/server/src/main/java/org/opensearch/common/xcontent/XContentOpenSearchExtension.java +++ b/server/src/main/java/org/opensearch/common/xcontent/XContentOpenSearchExtension.java @@ -39,18 +39,10 @@ import org.opensearch.core.common.unit.ByteSizeValue; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentBuilderExtension; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.Instant; -import org.joda.time.MutableDateTime; -import org.joda.time.ReadableInstant; -import org.joda.time.format.DateTimeFormatter; -import org.joda.time.format.ISODateTimeFormat; -import org.joda.time.tz.CachedDateTimeZone; -import org.joda.time.tz.FixedDateTimeZone; import java.time.DayOfWeek; import java.time.Duration; +import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; @@ -79,7 +71,6 @@ */ public class XContentOpenSearchExtension implements XContentBuilderExtension { - public static final DateTimeFormatter DEFAULT_DATE_PRINTER = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); public static final DateFormatter DEFAULT_FORMATTER = DateFormatter.forPattern("strict_date_optional_time_nanos"); public static final DateFormatter LOCAL_TIME_FORMATTER = DateFormatter.forPattern("HH:mm:ss.SSS"); public static final DateFormatter OFFSET_TIME_FORMATTER = DateFormatter.forPattern("HH:mm:ss.SSSZZZZZ"); @@ -90,11 +81,6 @@ public Map, XContentBuilder.Writer> getXContentWriters() { // Fully-qualified here to reduce ambiguity around our (OpenSearch') Version class writers.put(org.apache.lucene.util.Version.class, (b, v) -> b.value(Objects.toString(v))); - writers.put(DateTimeZone.class, (b, v) -> b.value(Objects.toString(v))); - writers.put(CachedDateTimeZone.class, (b, v) -> b.value(Objects.toString(v))); - writers.put(FixedDateTimeZone.class, (b, v) -> b.value(Objects.toString(v))); - writers.put(MutableDateTime.class, XContentBuilder::timeValue); - writers.put(DateTime.class, XContentBuilder::timeValue); writers.put(TimeValue.class, (b, v) -> b.value(v.toString())); writers.put(ZonedDateTime.class, XContentBuilder::timeValue); writers.put(OffsetDateTime.class, XContentBuilder::timeValue); @@ -141,14 +127,11 @@ public Map, XContentBuilder.HumanReadableTransformer> getXContentHumanR @Override public Map, Function> getDateTransformers() { Map, Function> transformers = new HashMap<>(); - transformers.put(Date.class, d -> DEFAULT_DATE_PRINTER.print(((Date) d).getTime())); - transformers.put(DateTime.class, d -> DEFAULT_DATE_PRINTER.print((DateTime) d)); - transformers.put(MutableDateTime.class, d -> DEFAULT_DATE_PRINTER.print((MutableDateTime) d)); - transformers.put(ReadableInstant.class, d -> DEFAULT_DATE_PRINTER.print((ReadableInstant) d)); - transformers.put(Long.class, d -> DEFAULT_DATE_PRINTER.print((long) d)); - transformers.put(Calendar.class, d -> DEFAULT_DATE_PRINTER.print(((Calendar) d).getTimeInMillis())); - transformers.put(GregorianCalendar.class, d -> DEFAULT_DATE_PRINTER.print(((Calendar) d).getTimeInMillis())); - transformers.put(Instant.class, d -> DEFAULT_DATE_PRINTER.print((Instant) d)); + transformers.put(Date.class, d -> DEFAULT_FORMATTER.format(((Date) d).toInstant())); + transformers.put(Long.class, d -> DEFAULT_FORMATTER.format(Instant.ofEpochMilli((long) d))); + transformers.put(Calendar.class, d -> DEFAULT_FORMATTER.format(((Calendar) d).toInstant())); + transformers.put(GregorianCalendar.class, d -> DEFAULT_FORMATTER.format(((Calendar) d).toInstant())); + transformers.put(Instant.class, d -> DEFAULT_FORMATTER.format((Instant) d)); transformers.put(ZonedDateTime.class, d -> DEFAULT_FORMATTER.format((ZonedDateTime) d)); transformers.put(OffsetDateTime.class, d -> DEFAULT_FORMATTER.format((OffsetDateTime) d)); transformers.put(OffsetTime.class, d -> OFFSET_TIME_FORMATTER.format((OffsetTime) d)); diff --git a/server/src/main/java/org/opensearch/rest/action/cat/RestCatRecoveryAction.java b/server/src/main/java/org/opensearch/rest/action/cat/RestCatRecoveryAction.java index 4f95e10ae3622..26efd9929afea 100644 --- a/server/src/main/java/org/opensearch/rest/action/cat/RestCatRecoveryAction.java +++ b/server/src/main/java/org/opensearch/rest/action/cat/RestCatRecoveryAction.java @@ -48,6 +48,7 @@ import org.opensearch.rest.RestResponse; import org.opensearch.rest.action.RestResponseListener; +import java.time.Instant; import java.util.Comparator; import java.util.List; import java.util.Locale; @@ -170,9 +171,9 @@ public int compare(RecoveryState o1, RecoveryState o2) { t.startRow(); t.addCell(index); t.addCell(state.getShardId().id()); - t.addCell(XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(state.getTimer().startTime())); + t.addCell(XContentOpenSearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(state.getTimer().startTime()))); t.addCell(state.getTimer().startTime()); - t.addCell(XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(state.getTimer().stopTime())); + t.addCell(XContentOpenSearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(state.getTimer().stopTime()))); t.addCell(state.getTimer().stopTime()); t.addCell(new TimeValue(state.getTimer().time())); t.addCell(state.getRecoverySource().getType().toString().toLowerCase(Locale.ROOT)); diff --git a/server/src/main/java/org/opensearch/rest/action/cat/RestCatSegmentReplicationAction.java b/server/src/main/java/org/opensearch/rest/action/cat/RestCatSegmentReplicationAction.java index ae00344bce446..1716289ca4c20 100644 --- a/server/src/main/java/org/opensearch/rest/action/cat/RestCatSegmentReplicationAction.java +++ b/server/src/main/java/org/opensearch/rest/action/cat/RestCatSegmentReplicationAction.java @@ -27,6 +27,7 @@ import org.opensearch.rest.RestResponse; import org.opensearch.rest.action.RestResponseListener; +import java.time.Instant; import java.util.List; import java.util.Locale; import java.util.Map; @@ -180,8 +181,8 @@ public Table buildSegmentReplicationTable(RestRequest request, SegmentReplicatio t.addCell(String.format(Locale.ROOT, "%1.1f%%", state.getIndex().recoveredFilesPercent())); t.addCell(state.getIndex().recoveredBytes()); t.addCell(String.format(Locale.ROOT, "%1.1f%%", state.getIndex().recoveredBytesPercent())); - t.addCell(XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(state.getTimer().startTime())); - t.addCell(XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(state.getTimer().stopTime())); + t.addCell(XContentOpenSearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(state.getTimer().startTime()))); + t.addCell(XContentOpenSearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(state.getTimer().stopTime()))); t.addCell(state.getIndex().totalRecoverFiles()); t.addCell(state.getIndex().totalFileCount()); t.addCell(new ByteSizeValue(state.getIndex().totalRecoverBytes())); diff --git a/server/src/main/java/org/opensearch/search/DocValueFormat.java b/server/src/main/java/org/opensearch/search/DocValueFormat.java index 07fa53e9c0315..12debec51ce64 100644 --- a/server/src/main/java/org/opensearch/search/DocValueFormat.java +++ b/server/src/main/java/org/opensearch/search/DocValueFormat.java @@ -34,9 +34,8 @@ import org.apache.lucene.document.InetAddressPoint; import org.apache.lucene.util.BytesRef; +import org.opensearch.Version; import org.opensearch.common.Numbers; -import org.opensearch.common.joda.Joda; -import org.opensearch.common.joda.JodaDateFormatter; import org.opensearch.common.network.InetAddresses; import org.opensearch.common.network.NetworkAddress; import org.opensearch.common.time.DateFormatter; @@ -244,13 +243,14 @@ public DateTime(DateFormatter formatter, ZoneId timeZone, DateFieldMapper.Resolu } public DateTime(StreamInput in) throws IOException { - String datePattern = in.readString(); + this.formatter = DateFormatter.forPattern(in.readString()); + this.parser = formatter.toDateMathParser(); String zoneId = in.readString(); this.timeZone = ZoneId.of(zoneId); this.resolution = DateFieldMapper.Resolution.ofOrdinal(in.readVInt()); - final boolean isJoda = in.readBoolean(); - this.formatter = isJoda ? Joda.forPattern(datePattern) : DateFormatter.forPattern(datePattern); - this.parser = formatter.toDateMathParser(); + if (in.getVersion().before(Version.V_2_10_0)) { + in.readBoolean(); // ignore deprecated joda + } } @Override @@ -263,8 +263,9 @@ public void writeTo(StreamOutput out) throws IOException { out.writeString(formatter.pattern()); out.writeString(timeZone.getId()); out.writeVInt(resolution.ordinal()); - // in order not to loose information if the formatter is a joda we send a flag - out.writeBoolean(formatter instanceof JodaDateFormatter);// todo pg consider refactor to isJoda method.. + if (out.getVersion().before(Version.V_2_10_0)) { + out.writeBoolean(false); // ignore deprecated joda flag + } } public DateMathParser getDateMathParser() { diff --git a/server/src/test/java/org/opensearch/cluster/metadata/IndexGraveyardTests.java b/server/src/test/java/org/opensearch/cluster/metadata/IndexGraveyardTests.java index d33048afef4fc..770145a0ebf37 100644 --- a/server/src/test/java/org/opensearch/cluster/metadata/IndexGraveyardTests.java +++ b/server/src/test/java/org/opensearch/cluster/metadata/IndexGraveyardTests.java @@ -36,7 +36,6 @@ import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.settings.Settings; import org.opensearch.common.xcontent.XContentOpenSearchExtension; -import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.core.common.Strings; import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.core.index.Index; @@ -47,6 +46,7 @@ import org.opensearch.test.OpenSearchTestCase; import java.io.IOException; +import java.time.Instant; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -80,7 +80,7 @@ public void testSerialization() throws IOException { public void testXContent() throws IOException { final IndexGraveyard graveyard = createRandom(); - final XContentBuilder builder = JsonXContent.contentBuilder(); + final XContentBuilder builder = MediaTypeRegistry.JSON.contentBuilder(); builder.startObject(); graveyard.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); @@ -89,11 +89,13 @@ public void testXContent() throws IOException { assertThat( Strings.toString(MediaTypeRegistry.JSON, graveyard, false, true), containsString( - XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(graveyard.getTombstones().get(0).getDeleteDateInMillis()) + XContentOpenSearchExtension.DEFAULT_FORMATTER.format( + Instant.ofEpochMilli(graveyard.getTombstones().get(0).getDeleteDateInMillis()) + ) ) ); } - XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); + XContentParser parser = createParser(MediaTypeRegistry.JSON.xContent(), BytesReference.bytes(builder)); parser.nextToken(); // the beginning of the parser assertThat(IndexGraveyard.fromXContent(parser), equalTo(graveyard)); } diff --git a/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java b/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java index d560593caaacb..370c691daf401 100644 --- a/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java +++ b/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java @@ -36,7 +36,6 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.Constants; import org.opensearch.common.geo.GeoPoint; -import org.opensearch.common.joda.Joda; import org.opensearch.common.lucene.BytesRefs; import org.opensearch.common.unit.TimeValue; import org.opensearch.common.util.PageCacheRecycler; @@ -49,11 +48,10 @@ import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.io.stream.Writeable; import org.opensearch.test.OpenSearchTestCase; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import java.io.EOFException; import java.io.IOException; +import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; @@ -327,10 +325,9 @@ public void testSimpleStreams() throws Exception { out.writeOptionalBytesReference(new BytesArray("test")); out.writeOptionalDouble(null); out.writeOptionalDouble(1.2); - Joda.writeTimeZone(out, DateTimeZone.forID("CET")); - Joda.writeOptionalTimeZone(out, DateTimeZone.getDefault()); - Joda.writeOptionalTimeZone(out, null); - out.writeGenericValue(new DateTime(123456, DateTimeZone.forID("America/Los_Angeles"))); + out.writeZoneId(ZoneId.of("CET")); + out.writeOptionalZoneId(ZoneId.systemDefault()); + out.writeGenericValue(ZonedDateTime.ofInstant(Instant.ofEpochMilli(123456), ZoneId.of("America/Los_Angeles"))); final byte[] bytes = BytesReference.toBytes(out.bytes()); StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes())); assertEquals(in.available(), bytes.length); @@ -360,9 +357,8 @@ public void testSimpleStreams() throws Exception { assertThat(in.readOptionalBytesReference(), equalTo(new BytesArray("test"))); assertNull(in.readOptionalDouble()); assertThat(in.readOptionalDouble(), closeTo(1.2, 0.0001)); - assertEquals(DateTimeZone.forID("CET"), Joda.readTimeZone(in)); - assertEquals(DateTimeZone.getDefault(), Joda.readOptionalTimeZone(in)); - assertNull(Joda.readOptionalTimeZone(in)); + assertEquals(ZoneId.of("CET"), in.readZoneId()); + assertEquals(ZoneId.systemDefault(), in.readOptionalZoneId()); Object dt = in.readGenericValue(); assertThat(dt, instanceOf(ZonedDateTime.class)); ZonedDateTime zdt = (ZonedDateTime) dt; diff --git a/server/src/test/java/org/opensearch/common/xcontent/BaseXContentTestCase.java b/server/src/test/java/org/opensearch/common/xcontent/BaseXContentTestCase.java index 779825e20b3f6..930c3415168a7 100644 --- a/server/src/test/java/org/opensearch/common/xcontent/BaseXContentTestCase.java +++ b/server/src/test/java/org/opensearch/common/xcontent/BaseXContentTestCase.java @@ -61,12 +61,6 @@ import org.opensearch.core.xcontent.XContentParser; import org.opensearch.core.xcontent.XContentParser.Token; import org.opensearch.test.OpenSearchTestCase; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.Instant; -import org.joda.time.ReadableInstant; -import org.joda.time.format.DateTimeFormatter; -import org.joda.time.format.ISODateTimeFormat; import org.hamcrest.Matcher; import org.hamcrest.Matchers; @@ -89,14 +83,15 @@ import java.time.Year; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Date; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -421,78 +416,21 @@ public void testText() throws Exception { } } - public void testReadableInstant() throws Exception { - assertResult("{'instant':null}", () -> builder().startObject().timeField("instant", (ReadableInstant) null).endObject()); - assertResult("{'instant':null}", () -> builder().startObject().field("instant").timeValue((ReadableInstant) null).endObject()); - - final DateTime t1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC); - - String expected = "{'t1':'2016-01-01T00:00:00.000Z'}"; - assertResult(expected, () -> builder().startObject().timeField("t1", t1).endObject()); - assertResult(expected, () -> builder().startObject().field("t1").timeValue(t1).endObject()); - - final DateTime t2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC); - - expected = "{'t2':'2016-12-25T07:59:42.213Z'}"; - assertResult(expected, () -> builder().startObject().timeField("t2", t2).endObject()); - assertResult(expected, () -> builder().startObject().field("t2").timeValue(t2).endObject()); - - final DateTimeFormatter formatter = randomFrom(ISODateTimeFormat.basicDate(), ISODateTimeFormat.dateTimeNoMillis()); - final DateTime t3 = DateTime.now(); - - expected = "{'t3':'" + formatter.print(t3) + "'}"; - assertResult(expected, () -> builder().startObject().timeField("t3", formatter.print(t3)).endObject()); - assertResult(expected, () -> builder().startObject().field("t3").value(formatter.print(t3)).endObject()); - - final DateTime t4 = new DateTime(randomDateTimeZone()); - - expected = "{'t4':'" + formatter.print(t4) + "'}"; - assertResult(expected, () -> builder().startObject().timeField("t4", formatter.print(t4)).endObject()); - assertResult(expected, () -> builder().startObject().field("t4").value(formatter.print(t4)).endObject()); - - long date = Math.abs(randomLong() % (2 * (long) 10e11)); // 1970-01-01T00:00:00Z - 2033-05-18T05:33:20.000+02:00 - final DateTime t5 = new DateTime(date, randomDateTimeZone()); - - expected = "{'t5':'" + XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(t5) + "'}"; - assertResult(expected, () -> builder().startObject().timeField("t5", t5).endObject()); - assertResult(expected, () -> builder().startObject().field("t5").timeValue(t5).endObject()); - - expected = "{'t5':'" + formatter.print(t5) + "'}"; - assertResult(expected, () -> builder().startObject().timeField("t5", formatter.print(t5)).endObject()); - assertResult(expected, () -> builder().startObject().field("t5").value(formatter.print(t5)).endObject()); - - Instant i1 = new Instant(1451606400000L); // 2016-01-01T00:00:00.000Z - expected = "{'i1':'2016-01-01T00:00:00.000Z'}"; - assertResult(expected, () -> builder().startObject().timeField("i1", i1).endObject()); - assertResult(expected, () -> builder().startObject().field("i1").timeValue(i1).endObject()); - - Instant i2 = new Instant(1482652782213L); // 2016-12-25T07:59:42.213Z - expected = "{'i2':'" + formatter.print(i2) + "'}"; - assertResult(expected, () -> builder().startObject().timeField("i2", formatter.print(i2)).endObject()); - assertResult(expected, () -> builder().startObject().field("i2").value(formatter.print(i2)).endObject()); - } - public void testDate() throws Exception { assertResult("{'date':null}", () -> builder().startObject().timeField("date", (Date) null).endObject()); assertResult("{'date':null}", () -> builder().startObject().field("date").timeValue((Date) null).endObject()); - final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate(); + final Date d1 = Date.from(ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()); assertResult("{'d1':'2016-01-01T00:00:00.000Z'}", () -> builder().startObject().timeField("d1", d1).endObject()); assertResult("{'d1':'2016-01-01T00:00:00.000Z'}", () -> builder().startObject().field("d1").timeValue(d1).endObject()); - final Date d2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC).toDate(); + final Date d2 = Date.from(ZonedDateTime.of(2016, 12, 25, 7, 59, 42, 213000000, ZoneOffset.UTC).toInstant()); assertResult("{'d2':'2016-12-25T07:59:42.213Z'}", () -> builder().startObject().timeField("d2", d2).endObject()); assertResult("{'d2':'2016-12-25T07:59:42.213Z'}", () -> builder().startObject().field("d2").timeValue(d2).endObject()); - - final DateTimeFormatter formatter = randomFrom(ISODateTimeFormat.basicDate(), ISODateTimeFormat.dateTimeNoMillis()); - final Date d3 = DateTime.now().toDate(); - - String expected = "{'d3':'" + formatter.print(d3.getTime()) + "'}"; - assertResult(expected, () -> builder().startObject().field("d3").value(formatter.print(d3.getTime())).endObject()); } public void testDateField() throws Exception { - final Date d = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate(); + final Date d = Date.from(ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()); assertResult( "{'date_in_millis':1451606400000}", @@ -505,7 +443,7 @@ public void testDateField() throws Exception { } public void testCalendar() throws Exception { - Calendar calendar = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toCalendar(Locale.ROOT); + Calendar calendar = GregorianCalendar.from(ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)); assertResult( "{'calendar':'2016-01-01T00:00:00.000Z'}", () -> builder().startObject().field("calendar").timeValue(calendar).endObject() @@ -673,20 +611,13 @@ public void testObjects() throws Exception { final String paths = Constants.WINDOWS ? "{'objects':['a\\\\b\\\\c','d\\\\e']}" : "{'objects':['a/b/c','d/e']}"; objects.put(paths, new Object[] { PathUtils.get("a", "b", "c"), PathUtils.get("d", "e") }); - final DateTimeFormatter formatter = XContentOpenSearchExtension.DEFAULT_DATE_PRINTER; - final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate(); - final Date d2 = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).toDate(); - objects.put( - "{'objects':['" + formatter.print(d1.getTime()) + "','" + formatter.print(d2.getTime()) + "']}", - new Object[] { d1, d2 } - ); + final DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT; + final Date d1 = Date.from(ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()); + final Date d2 = Date.from(ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()); + objects.put("{'objects':['2016-01-01T00:00:00.000Z','2015-01-01T00:00:00.000Z']}", new Object[] { d1, d2 }); - final DateTime dt1 = DateTime.now(); - final DateTime dt2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC); - objects.put("{'objects':['" + formatter.print(dt1) + "','2016-12-25T07:59:42.213Z']}", new Object[] { dt1, dt2 }); - - final Calendar c1 = new DateTime(2012, 7, 7, 10, 23, DateTimeZone.UTC).toCalendar(Locale.ROOT); - final Calendar c2 = new DateTime(2014, 11, 16, 19, 36, DateTimeZone.UTC).toCalendar(Locale.ROOT); + final Calendar c1 = GregorianCalendar.from(ZonedDateTime.of(2012, 7, 7, 10, 23, 0, 0, ZoneOffset.UTC)); + final Calendar c2 = GregorianCalendar.from(ZonedDateTime.of(2014, 11, 16, 19, 36, 0, 0, ZoneOffset.UTC)); objects.put("{'objects':['2012-07-07T10:23:00.000Z','2014-11-16T19:36:00.000Z']}", new Object[] { c1, c2 }); final ToXContent x1 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", 2).array("f3", 3, 4, 5).endObject(); @@ -727,14 +658,10 @@ public void testObject() throws Exception { final String path = Constants.WINDOWS ? "{'object':'a\\\\b\\\\c'}" : "{'object':'a/b/c'}"; object.put(path, PathUtils.get("a", "b", "c")); - final DateTimeFormatter formatter = XContentOpenSearchExtension.DEFAULT_DATE_PRINTER; - final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate(); - object.put("{'object':'" + formatter.print(d1.getTime()) + "'}", d1); - - final DateTime d2 = DateTime.now(); - object.put("{'object':'" + formatter.print(d2) + "'}", d2); + final Date d1 = Date.from(ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()); + object.put("{'object':'" + "2016-01-01T00:00:00.000Z" + "'}", d1); - final Calendar c1 = new DateTime(2010, 1, 1, 0, 0, DateTimeZone.UTC).toCalendar(Locale.ROOT); + final Calendar c1 = GregorianCalendar.from(ZonedDateTime.of(2010, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)); object.put("{'object':'2010-01-01T00:00:00.000Z'}", c1); final ToXContent x1 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", 2).array("f3", 3, 4, 5).endObject(); diff --git a/server/src/test/java/org/opensearch/common/xcontent/builder/XContentBuilderTests.java b/server/src/test/java/org/opensearch/common/xcontent/builder/XContentBuilderTests.java index 184f90df785c7..36e61a32525c1 100644 --- a/server/src/test/java/org/opensearch/common/xcontent/builder/XContentBuilderTests.java +++ b/server/src/test/java/org/opensearch/common/xcontent/builder/XContentBuilderTests.java @@ -192,9 +192,9 @@ public void testByteConversion() throws Exception { public void testDateTypesConversion() throws Exception { Date date = new Date(); - String expectedDate = XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(date.getTime()); + String expectedDate = XContentOpenSearchExtension.DEFAULT_FORMATTER.format(date.toInstant()); Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.ROOT); - String expectedCalendar = XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(calendar.getTimeInMillis()); + String expectedCalendar = XContentOpenSearchExtension.DEFAULT_FORMATTER.format(calendar.toInstant()); XContentBuilder builder = MediaTypeRegistry.contentBuilder(MediaTypeRegistry.JSON); builder.startObject().timeField("date", date).endObject(); assertThat(builder.toString(), equalTo("{\"date\":\"" + expectedDate + "\"}")); diff --git a/server/src/test/java/org/opensearch/rest/action/cat/RestRecoveryActionTests.java b/server/src/test/java/org/opensearch/rest/action/cat/RestRecoveryActionTests.java index e0b3847cda571..20fcac1089bc3 100644 --- a/server/src/test/java/org/opensearch/rest/action/cat/RestRecoveryActionTests.java +++ b/server/src/test/java/org/opensearch/rest/action/cat/RestRecoveryActionTests.java @@ -49,6 +49,7 @@ import org.opensearch.indices.replication.common.ReplicationTimer; import org.opensearch.test.OpenSearchTestCase; +import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -164,9 +165,9 @@ public void testRestRecoveryAction() { final List expectedValues = Arrays.asList( "index", i, - XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(state.getTimer().startTime()), + XContentOpenSearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(state.getTimer().startTime())), state.getTimer().startTime(), - XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(state.getTimer().stopTime()), + XContentOpenSearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(state.getTimer().stopTime())), state.getTimer().stopTime(), new TimeValue(state.getTimer().time()), state.getRecoverySource().getType().name().toLowerCase(Locale.ROOT), From 25c9a3bfdefb49b08418823ca9bf6cb057b4f348 Mon Sep 17 00:00:00 2001 From: Nicholas Walter Knize Date: Thu, 17 Aug 2023 08:00:31 -0500 Subject: [PATCH 2/2] re-enable bwc testing and change version to prevent fail Signed-off-by: Nicholas Walter Knize --- build.gradle | 4 ++-- .../src/main/java/org/opensearch/search/DocValueFormat.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 45d07f75e0e54..eef5d6d5b442c 100644 --- a/build.gradle +++ b/build.gradle @@ -225,10 +225,10 @@ tasks.register("verifyVersions") { * after the backport of the backcompat code is complete. */ -boolean bwc_tests_enabled = false +boolean bwc_tests_enabled = true /* place an issue link here when committing bwc changes */ -String bwc_tests_disabled_issue = "https://github.com/opensearch-project/OpenSearch/issues/9349" +String bwc_tests_disabled_issue = "" /* there's no existing MacOS release, therefore disable bcw tests */ if (Os.isFamily(Os.FAMILY_MAC)) { diff --git a/server/src/main/java/org/opensearch/search/DocValueFormat.java b/server/src/main/java/org/opensearch/search/DocValueFormat.java index 12debec51ce64..c6187e5949035 100644 --- a/server/src/main/java/org/opensearch/search/DocValueFormat.java +++ b/server/src/main/java/org/opensearch/search/DocValueFormat.java @@ -248,7 +248,7 @@ public DateTime(StreamInput in) throws IOException { String zoneId = in.readString(); this.timeZone = ZoneId.of(zoneId); this.resolution = DateFieldMapper.Resolution.ofOrdinal(in.readVInt()); - if (in.getVersion().before(Version.V_2_10_0)) { + if (in.getVersion().before(Version.V_3_0_0)) { in.readBoolean(); // ignore deprecated joda } } @@ -263,7 +263,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeString(formatter.pattern()); out.writeString(timeZone.getId()); out.writeVInt(resolution.ordinal()); - if (out.getVersion().before(Version.V_2_10_0)) { + if (out.getVersion().before(Version.V_3_0_0)) { out.writeBoolean(false); // ignore deprecated joda flag } }