From: Robert Varga Date: Sun, 14 Nov 2021 05:36:34 +0000 (+0100) Subject: Use DateTimeFormatter in ShardStats X-Git-Tag: v4.0.7~3 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=9a64168236b2fa2e835d9b581c38530fb67a8f50 Use DateTimeFormatter in ShardStats This conversion allows us to drop a global lock. Also clean up the associated test. Change-Id: Ie91e99dd0d1ba4726b802c1c8953cfe1226714e1 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardStats.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardStats.java index 71cbf128a3..f377ff7100 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardStats.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardStats.java @@ -10,11 +10,11 @@ package org.opendaylight.controller.cluster.datastore; import akka.actor.ActorRef; import com.google.common.base.Joiner; import com.google.common.base.Joiner.MapJoiner; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.List; import java.util.concurrent.atomic.AtomicLong; -import org.checkerframework.checker.lock.qual.GuardedBy; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStatsMXBean; @@ -31,9 +31,8 @@ import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean; final class ShardStats extends AbstractMXBean implements ShardStatsMXBean { public static final String JMX_CATEGORY_SHARD = "Shards"; - // FIXME: migrate this to Java 8 thread-safe time - @GuardedBy("DATE_FORMAT") - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS") + .withZone(ZoneId.systemDefault()); private static final MapJoiner MAP_JOINER = Joiner.on(", ").withKeyValueSeparator(": "); @@ -89,9 +88,7 @@ final class ShardStats extends AbstractMXBean implements ShardStatsMXBean { } private static String formatMillis(final long timeMillis) { - synchronized (DATE_FORMAT) { - return DATE_FORMAT.format(new Date(timeMillis)); - } + return DATE_FORMATTER.format(Instant.ofEpochMilli(timeMillis)); } @Override diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardStatsTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardStatsTest.java index c4399558a0..03fb3ae8ea 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardStatsTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardStatsTest.java @@ -7,13 +7,15 @@ */ package org.opendaylight.controller.cluster.datastore; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + import java.lang.management.ManagementFactory; import java.text.SimpleDateFormat; import java.util.Date; import javax.management.MBeanServer; import javax.management.ObjectName; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean; @@ -25,7 +27,6 @@ public class ShardStatsTest { @Before public void setUp() throws Exception { - shardStats = new ShardStats("shard-1", "DataStore", null); shardStats.registerMBean(); mbeanServer = ManagementFactory.getPlatformMBeanServer(); @@ -41,10 +42,7 @@ public class ShardStatsTest { @Test public void testGetShardName() throws Exception { - - Object attribute = mbeanServer.getAttribute(testMBeanName, "ShardName"); - Assert.assertEquals(attribute, "shard-1"); - + assertEquals("shard-1", mbeanServer.getAttribute(testMBeanName, "ShardName")); } @Test @@ -55,28 +53,21 @@ public class ShardStatsTest { shardStats.incrementCommittedTransactionCount(); //now let us get from MBeanServer what is the transaction count. - Object attribute = mbeanServer.getAttribute(testMBeanName, - "CommittedTransactionsCount"); - Assert.assertEquals(attribute, 3L); - - + assertEquals(3L, mbeanServer.getAttribute(testMBeanName, "CommittedTransactionsCount")); } @Test public void testGetLastCommittedTransactionTime() throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); - Assert.assertEquals(shardStats.getLastCommittedTransactionTime(), - sdf.format(new Date(0L))); + assertEquals(sdf.format(new Date(0L)), shardStats.getLastCommittedTransactionTime()); long millis = System.currentTimeMillis(); shardStats.setLastCommittedTransactionTime(millis); //now let us get from MBeanServer what is the transaction count. Object attribute = mbeanServer.getAttribute(testMBeanName, "LastCommittedTransactionTime"); - Assert.assertEquals(attribute, sdf.format(new Date(millis))); - Assert.assertNotEquals(attribute, - sdf.format(new Date(millis - 1))); - + assertEquals(sdf.format(new Date(millis)), attribute); + assertNotEquals(attribute, sdf.format(new Date(millis - 1))); } @Test @@ -85,11 +76,8 @@ public class ShardStatsTest { shardStats.incrementFailedTransactionsCount(); shardStats.incrementFailedTransactionsCount(); - //now let us get from MBeanServer what is the transaction count. - Object attribute = - mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount"); - Assert.assertEquals(attribute, 2L); + assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount")); } @Test @@ -98,11 +86,8 @@ public class ShardStatsTest { shardStats.incrementAbortTransactionsCount(); shardStats.incrementAbortTransactionsCount(); - //now let us get from MBeanServer what is the transaction count. - Object attribute = - mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount"); - Assert.assertEquals(attribute, 2L); + assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount")); } @Test @@ -111,49 +96,32 @@ public class ShardStatsTest { shardStats.incrementFailedReadTransactionsCount(); shardStats.incrementFailedReadTransactionsCount(); - //now let us get from MBeanServer what is the transaction count. - Object attribute = - mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount"); - Assert.assertEquals(attribute, 2L); + assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount")); } @Test public void testResetTransactionCounters() throws Exception { - //let us increment committed transactions count and then check shardStats.incrementCommittedTransactionCount(); shardStats.incrementCommittedTransactionCount(); shardStats.incrementCommittedTransactionCount(); //now let us get from MBeanServer what is the transaction count. - Object attribute = mbeanServer.getAttribute(testMBeanName, - "CommittedTransactionsCount"); - Assert.assertEquals(attribute, 3L); + assertEquals(3L, mbeanServer.getAttribute(testMBeanName, "CommittedTransactionsCount")); //let us increment FailedReadTransactions count and then check shardStats.incrementFailedReadTransactionsCount(); shardStats.incrementFailedReadTransactionsCount(); - //now let us get from MBeanServer what is the transaction count. - attribute = - mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount"); - Assert.assertEquals(attribute, 2L); - + assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount")); //here we will reset the counters and check the above ones are 0 after reset mbeanServer.invoke(testMBeanName, "resetTransactionCounters", null, null); //now let us get from MBeanServer what is the transaction count. - attribute = mbeanServer.getAttribute(testMBeanName, - "CommittedTransactionsCount"); - Assert.assertEquals(attribute, 0L); - - attribute = - mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount"); - Assert.assertEquals(attribute, 0L); - - + assertEquals(0L, mbeanServer.getAttribute(testMBeanName, "CommittedTransactionsCount")); + assertEquals(0L, mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount")); } }