This is an automated email from the ASF dual-hosted git repository. stoty pushed a commit to branch 5.1 in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/5.1 by this push: new aeb59448cf PHOENIX-6914 Refactor phoenix-pherf to use java.time instead of Joda-Time aeb59448cf is described below commit aeb59448cfa757594e2bafd09c6e0418d521f733 Author: Istvan Toth <st...@apache.org> AuthorDate: Tue Mar 21 09:05:52 2023 +0100 PHOENIX-6914 Refactor phoenix-pherf to use java.time instead of Joda-Time --- phoenix-pherf/pom.xml | 5 -- .../org/apache/phoenix/pherf/PherfConstants.java | 5 +- .../apache/phoenix/pherf/rules/RulesApplier.java | 74 ++++++++++++++-------- .../pherf/rules/SequentialDateDataGenerator.java | 25 +++++--- .../apache/phoenix/pherf/RuleGeneratorTest.java | 16 ++--- .../rules/SequentialDateDataGeneratorTest.java | 27 ++++---- .../rules/SequentialListDataGeneratorTest.java | 23 ++++--- 7 files changed, 97 insertions(+), 78 deletions(-) diff --git a/phoenix-pherf/pom.xml b/phoenix-pherf/pom.xml index c1cc21c597..a3b8691808 100644 --- a/phoenix-pherf/pom.xml +++ b/phoenix-pherf/pom.xml @@ -136,10 +136,6 @@ <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> </dependency> - <dependency> - <groupId>joda-time</groupId> - <artifactId>joda-time</artifactId> - </dependency> <dependency> <groupId>stax</groupId> <artifactId>stax-api</artifactId> @@ -358,7 +354,6 @@ <include>org.apache.commons:commons-lang3</include> <include>org.apache.commons:commons-math3</include> <include>org.apache.phoenix.thirdparty:phoenix-shaded-commons-cli</include> - <include>joda-time:joda-time</include> <include>org.apache.commons:commons-csv</include> <include>commons-lang:commons-lang</include> <include>commons-io:commons-io</include> diff --git a/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/PherfConstants.java b/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/PherfConstants.java index c749d15f31..caba0d1e4a 100644 --- a/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/PherfConstants.java +++ b/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/PherfConstants.java @@ -18,10 +18,9 @@ package org.apache.phoenix.pherf; -import org.joda.time.DateTimeZone; - import java.io.IOException; import java.io.InputStream; +import java.time.ZoneId; import java.util.Properties; public class PherfConstants { @@ -41,7 +40,7 @@ public class PherfConstants { public static final int DEFAULT_THREAD_POOL_SIZE = 10; public static final int DEFAULT_BATCH_SIZE = 1000; public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; - public static final DateTimeZone DEFAULT_TIME_ZONE = DateTimeZone.UTC; + public static final ZoneId DEFAULT_TIME_ZONE = ZoneId.of("UTC"); public static final String RESOURCE_SCENARIO = "/scenario"; public static final String SCENARIO_ROOT_PATTERN = diff --git a/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/rules/RulesApplier.java b/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/rules/RulesApplier.java index 15d40c3181..a75e99fcac 100644 --- a/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/rules/RulesApplier.java +++ b/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/rules/RulesApplier.java @@ -18,26 +18,38 @@ package org.apache.phoenix.pherf.rules; -import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions; -import org.apache.phoenix.thirdparty.com.google.common.collect.Lists; -import org.apache.commons.math3.random.RandomDataGenerator; -import org.apache.phoenix.pherf.PherfConstants; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadLocalRandom; + import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.phoenix.pherf.configuration.*; +import org.apache.commons.math3.random.RandomDataGenerator; +import org.apache.phoenix.pherf.PherfConstants; +import org.apache.phoenix.pherf.configuration.Column; +import org.apache.phoenix.pherf.configuration.DataModel; +import org.apache.phoenix.pherf.configuration.DataSequence; +import org.apache.phoenix.pherf.configuration.DataTypeMapping; +import org.apache.phoenix.pherf.configuration.Scenario; +import org.apache.phoenix.pherf.configuration.XMLConfigParser; +import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions; +import org.apache.phoenix.thirdparty.com.google.common.collect.Lists; import org.apache.phoenix.util.EnvironmentEdgeManager; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ThreadLocalRandom; - public class RulesApplier { private static final Logger LOGGER = LoggerFactory.getLogger(RulesApplier.class); @@ -325,28 +337,32 @@ public class RulesApplier { } public String generateRandomDate(String min, String max) throws Exception { - DateTimeFormatter fmtr = DateTimeFormat.forPattern(PherfConstants.DEFAULT_DATE_PATTERN).withZone(DateTimeZone.UTC); - DateTime minDt; - DateTime maxDt; - DateTime dt; + DateTimeFormatter fmtr = + DateTimeFormatter.ofPattern(PherfConstants.DEFAULT_DATE_PATTERN) + .withZone(ZoneId.of("UTC")); + Instant minDt; + Instant maxDt; + Instant dt; - minDt = fmtr.parseDateTime(checkDatePattern(min)); - maxDt = fmtr.parseDateTime(checkDatePattern(max)); + minDt = ZonedDateTime.parse(checkDatePattern(min), fmtr).toInstant(); + maxDt = ZonedDateTime.parse(checkDatePattern(max), fmtr).toInstant(); // Get Ms Date between min and max synchronized (randomDataGenerator) { //Make sure date generated is exactly between the passed limits - long rndLong = randomDataGenerator.nextLong(minDt.getMillis()+1, maxDt.getMillis()-1); - dt = new DateTime(rndLong, PherfConstants.DEFAULT_TIME_ZONE); + long rndLong = randomDataGenerator.nextLong(minDt.toEpochMilli()+1, maxDt.toEpochMilli()-1); + dt = Instant.ofEpochMilli(rndLong); } - return fmtr.print(dt); + return fmtr.format(dt); } public String getCurrentDate() { - DateTimeFormatter fmtr = DateTimeFormat.forPattern(PherfConstants.DEFAULT_DATE_PATTERN).withZone(DateTimeZone.UTC);; - DateTime dt = new DateTime(PherfConstants.DEFAULT_TIME_ZONE); - return fmtr.print(dt); + DateTimeFormatter fmtr = + DateTimeFormatter.ofPattern(PherfConstants.DEFAULT_DATE_PATTERN) + .withZone(ZoneId.of("UTC")); + LocalDateTime dt = LocalDateTime.now(); + return fmtr.format(dt); } /** @@ -422,9 +438,11 @@ public class RulesApplier { // Checks if date is in defult pattern public String checkDatePattern(String date) { - DateTimeFormatter fmtr = DateTimeFormat.forPattern(PherfConstants.DEFAULT_DATE_PATTERN).withZone(DateTimeZone.UTC);; - DateTime parsedDate = fmtr.parseDateTime(date); - return fmtr.print(parsedDate); + DateTimeFormatter fmtr = + DateTimeFormatter.ofPattern(PherfConstants.DEFAULT_DATE_PATTERN) + .withZone(ZoneId.of("UTC")); + Instant parsedDate = ZonedDateTime.parse(date, fmtr).toInstant(); + return fmtr.format(parsedDate); } /** diff --git a/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/rules/SequentialDateDataGenerator.java b/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/rules/SequentialDateDataGenerator.java index 63328d0a4c..9a398c1c84 100644 --- a/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/rules/SequentialDateDataGenerator.java +++ b/phoenix-pherf/src/main/java/org/apache/phoenix/pherf/rules/SequentialDateDataGenerator.java @@ -18,25 +18,27 @@ package org.apache.phoenix.pherf.rules; -import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.concurrent.atomic.AtomicInteger; + import org.apache.phoenix.pherf.configuration.Column; import org.apache.phoenix.pherf.configuration.DataSequence; import org.apache.phoenix.pherf.configuration.DataTypeMapping; - -import org.joda.time.LocalDateTime; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; -import java.util.concurrent.atomic.AtomicInteger; +import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting; +import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions; /** * A generator for sequentially increasing dates. * For now the increments are fixed at 1 second. */ public class SequentialDateDataGenerator implements RuleBasedDataGenerator { - private static DateTimeFormatter FMT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); + private static DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); private final Column columnRule; private final AtomicInteger counter; - private final LocalDateTime startDateTime = new LocalDateTime(); + //Make sure we truncate to milliseconds + private final LocalDateTime startDateTime = + LocalDateTime.parse(LocalDateTime.now().format(FMT), FMT); public SequentialDateDataGenerator(Column columnRule) { Preconditions.checkArgument(columnRule.getDataSequence() == DataSequence.SEQUENTIAL); @@ -45,6 +47,11 @@ public class SequentialDateDataGenerator implements RuleBasedDataGenerator { counter = new AtomicInteger(0); } + @VisibleForTesting + public LocalDateTime getStartDateTime() { + return startDateTime; + } + /** * Note that this method rolls over for attempts to get larger than maxValue * @return new DataValue @@ -52,7 +59,7 @@ public class SequentialDateDataGenerator implements RuleBasedDataGenerator { @Override public DataValue getDataValue() { LocalDateTime newDateTime = startDateTime.plusSeconds(counter.getAndIncrement()); - String formattedDateTime = newDateTime.toString(FMT); + String formattedDateTime = newDateTime.format(FMT); return new DataValue(columnRule.getType(), formattedDateTime); } diff --git a/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/RuleGeneratorTest.java b/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/RuleGeneratorTest.java index c62922b402..eb3ee18cd1 100644 --- a/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/RuleGeneratorTest.java +++ b/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/RuleGeneratorTest.java @@ -25,6 +25,10 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -42,10 +46,6 @@ import org.apache.phoenix.pherf.configuration.XMLConfigParser; import org.apache.phoenix.pherf.rules.DataValue; import org.apache.phoenix.pherf.rules.RulesApplier; import org.apache.phoenix.pherf.workload.WriteWorkload; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; import org.junit.Test; public class RuleGeneratorTest { @@ -438,11 +438,11 @@ public class RuleGeneratorTest { * @param value */ private void assertDateBetween(DataValue value) { - DateTimeFormatter fmtr = DateTimeFormat.forPattern(PherfConstants.DEFAULT_DATE_PATTERN).withZone(DateTimeZone.UTC); + DateTimeFormatter fmtr = DateTimeFormatter.ofPattern(PherfConstants.DEFAULT_DATE_PATTERN).withZone(ZoneId.of("UTC")); - DateTime dt = fmtr.parseDateTime(value.getValue()); - DateTime min = fmtr.parseDateTime(value.getMinValue()); - DateTime max = fmtr.parseDateTime(value.getMaxValue()); + Instant dt = ZonedDateTime.parse(value.getValue(), fmtr).toInstant(); + Instant min = ZonedDateTime.parse(value.getMinValue(), fmtr).toInstant(); + Instant max = ZonedDateTime.parse(value.getMaxValue(), fmtr).toInstant(); assertTrue("Value " + dt + " is not after minValue", dt.isAfter(min)); assertTrue("Value " + dt + " is not before maxValue", dt.isBefore(max)); diff --git a/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/rules/SequentialDateDataGeneratorTest.java b/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/rules/SequentialDateDataGeneratorTest.java index 3cc75e4761..1c351fbe11 100644 --- a/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/rules/SequentialDateDataGeneratorTest.java +++ b/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/rules/SequentialDateDataGeneratorTest.java @@ -17,18 +17,18 @@ */ package org.apache.phoenix.pherf.rules; -import org.apache.phoenix.pherf.configuration.Column; -import org.apache.phoenix.pherf.configuration.DataSequence; -import org.joda.time.LocalDateTime; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; -import org.junit.Test; - import static org.apache.phoenix.pherf.configuration.DataTypeMapping.DATE; import static org.apache.phoenix.pherf.configuration.DataTypeMapping.VARCHAR; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.apache.phoenix.pherf.configuration.Column; +import org.apache.phoenix.pherf.configuration.DataSequence; +import org.junit.Test; + public class SequentialDateDataGeneratorTest { SequentialDateDataGenerator generator; @@ -54,25 +54,26 @@ public class SequentialDateDataGeneratorTest { @Test public void testGetDataValue() { - DateTimeFormatter FMT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); + DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); Column columnA = new Column(); columnA.setType(DATE); columnA.setDataSequence(DataSequence.SEQUENTIAL); - LocalDateTime startDateTime = new LocalDateTime(); // The increments are the of 1 sec units generator = new SequentialDateDataGenerator(columnA); + LocalDateTime startDateTime = generator.getStartDateTime(); + DataValue result1 = generator.getDataValue(); - LocalDateTime result1LocalTime = FMT.parseDateTime(result1.getValue()).toLocalDateTime(); + LocalDateTime result1LocalTime = LocalDateTime.parse(result1.getValue(), FMT); assertFalse(result1LocalTime.isBefore(startDateTime)); DataValue result2 = generator.getDataValue(); - LocalDateTime result2LocalTime = FMT.parseDateTime(result2.getValue()).toLocalDateTime(); + LocalDateTime result2LocalTime = LocalDateTime.parse(result2.getValue(), FMT); assertEquals(result2LocalTime.minusSeconds(1), result1LocalTime); DataValue result3 = generator.getDataValue(); - LocalDateTime result3LocalTime = FMT.parseDateTime(result3.getValue()).toLocalDateTime(); + LocalDateTime result3LocalTime = LocalDateTime.parse(result3.getValue(), FMT); assertEquals(result3LocalTime.minusSeconds(1), result2LocalTime); DataValue result4 = generator.getDataValue(); - LocalDateTime result4LocalTime = FMT.parseDateTime(result4.getValue()).toLocalDateTime(); + LocalDateTime result4LocalTime = LocalDateTime.parse(result4.getValue(), FMT); assertEquals(result4LocalTime.minusSeconds(1), result3LocalTime); } } diff --git a/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/rules/SequentialListDataGeneratorTest.java b/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/rules/SequentialListDataGeneratorTest.java index d590167d76..a258b83a2a 100644 --- a/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/rules/SequentialListDataGeneratorTest.java +++ b/phoenix-pherf/src/test/java/org/apache/phoenix/pherf/rules/SequentialListDataGeneratorTest.java @@ -17,19 +17,18 @@ */ package org.apache.phoenix.pherf.rules; -import org.apache.phoenix.pherf.configuration.Column; -import org.apache.phoenix.pherf.configuration.DataSequence; -import org.joda.time.LocalDateTime; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; -import org.junit.Test; +import static org.apache.phoenix.pherf.configuration.DataTypeMapping.DATE; +import static org.apache.phoenix.pherf.configuration.DataTypeMapping.VARCHAR; +import static org.junit.Assert.assertEquals; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; -import static org.apache.phoenix.pherf.configuration.DataTypeMapping.DATE; -import static org.apache.phoenix.pherf.configuration.DataTypeMapping.VARCHAR; -import static org.junit.Assert.assertEquals; +import org.apache.phoenix.pherf.configuration.Column; +import org.apache.phoenix.pherf.configuration.DataSequence; +import org.junit.Test; public class SequentialListDataGeneratorTest { SequentialListDataGenerator generator; @@ -46,9 +45,9 @@ public class SequentialListDataGeneratorTest { @Test(expected = IllegalArgumentException.class) public void testRejectsNonVarchar() { - DateTimeFormatter FMT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); - LocalDateTime startDateTime = new LocalDateTime(); - String formattedDateTime = startDateTime.toString(FMT); + DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); + LocalDateTime startDateTime = LocalDateTime.now(); + String formattedDateTime = startDateTime.format(FMT); Column columnA = new Column(); columnA.setType(DATE); columnA.setDataSequence(DataSequence.SEQUENTIAL);