From 6eadd7228b81aee06439464519eb42d6d8c3bf76 Mon Sep 17 00:00:00 2001 From: Bela Ban Date: Fri, 13 Oct 2023 09:27:25 +0200 Subject: [PATCH] JGRP-2736 Use LocalDateTime w/UTC instead of Date * Avoids triggering of the JDK TZ rule caching --- src/org/jgroups/blocks/Cache.java | 2 +- src/org/jgroups/blocks/GridFile.java | 5 ++--- src/org/jgroups/protocols/FD_HOST.java | 2 +- src/org/jgroups/protocols/FD_SOCK.java | 4 ++-- src/org/jgroups/protocols/FD_SOCK2.java | 2 +- src/org/jgroups/protocols/FailureDetection.java | 2 +- src/org/jgroups/protocols/SOS.java | 2 +- src/org/jgroups/protocols/pbcast/GMS.java | 2 +- src/org/jgroups/protocols/pbcast/ViewHandler.java | 4 ++-- src/org/jgroups/stack/GossipRouter.java | 2 +- src/org/jgroups/util/Util.java | 13 +++++++++++++ 11 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/org/jgroups/blocks/Cache.java b/src/org/jgroups/blocks/Cache.java index b6e2b1e22c1..19fb17ff436 100644 --- a/src/org/jgroups/blocks/Cache.java +++ b/src/org/jgroups/blocks/Cache.java @@ -202,7 +202,7 @@ public String toString() { if(expiration_time <= 0) sb.append(expiration_time); else { - sb.append(new Date(expiration_time)); + sb.append(Util.utcEpoch(expiration_time)); } sb.append(")\n"); } diff --git a/src/org/jgroups/blocks/GridFile.java b/src/org/jgroups/blocks/GridFile.java index 8520ee4fe19..e2fc2fd019d 100644 --- a/src/org/jgroups/blocks/GridFile.java +++ b/src/org/jgroups/blocks/GridFile.java @@ -7,7 +7,6 @@ import java.io.*; import java.util.ArrayList; import java.util.Collection; -import java.util.Date; import java.util.Set; /** @@ -360,8 +359,8 @@ public String toString() { StringBuilder sb=new StringBuilder(); sb.append(getType()); if(is_file) - sb.append(", len=" + Util.printBytes(length) + ", chunk_size=" + chunk_size); - sb.append(", mod_time=" + new Date(modification_time)); + sb.append(", len=").append(Util.printBytes(length)).append(", chunk_size=").append(chunk_size); + sb.append(", mod_time=").append(Util.utcEpoch(modification_time)); return sb.toString(); } diff --git a/src/org/jgroups/protocols/FD_HOST.java b/src/org/jgroups/protocols/FD_HOST.java index ffcbd3abce4..9711b1add98 100644 --- a/src/org/jgroups/protocols/FD_HOST.java +++ b/src/org/jgroups/protocols/FD_HOST.java @@ -108,7 +108,7 @@ public FD_HOST setCommand(String command) { public String printSuspectHistory() { StringBuilder sb=new StringBuilder(); for(Tuple tmp: suspect_history) { - sb.append(new Date(tmp.getVal2())).append(": ").append(tmp.getVal1()).append("\n"); + sb.append(Util.utcEpoch(tmp.getVal2())).append(": ").append(tmp.getVal1()).append("\n"); } return sb.toString(); } diff --git a/src/org/jgroups/protocols/FD_SOCK.java b/src/org/jgroups/protocols/FD_SOCK.java index fd89beed7ab..15fdb201e36 100644 --- a/src/org/jgroups/protocols/FD_SOCK.java +++ b/src/org/jgroups/protocols/FD_SOCK.java @@ -525,7 +525,7 @@ protected void suspect(Set
suspects) { return; suspects.remove(local_addr); - suspects.forEach(suspect -> suspect_history.add(String.format("%s: %s", new Date(), suspect))); + suspects.forEach(suspect -> suspect_history.add(String.format("%s: %s", Util.utcNow(), suspect))); suspected_mbrs.addAll(suspects); List
eligible_mbrs=new ArrayList<>(this.members); @@ -752,7 +752,7 @@ protected void broadcastSuspectMessage(Address suspected_mbr) { bcast_task.addSuspectedMember(suspected_mbr); if(stats) { num_suspect_events++; - suspect_history.add(String.format("%s: %s", new Date(), suspected_mbr)); + suspect_history.add(String.format("%s: %s", Util.utcNow(), suspected_mbr)); } } diff --git a/src/org/jgroups/protocols/FD_SOCK2.java b/src/org/jgroups/protocols/FD_SOCK2.java index 459cf857689..21638fd6adc 100644 --- a/src/org/jgroups/protocols/FD_SOCK2.java +++ b/src/org/jgroups/protocols/FD_SOCK2.java @@ -473,7 +473,7 @@ protected void suspect(Collection
suspects) { return; suspects.remove(local_addr); - suspects.forEach(suspect -> suspect_history.add(String.format("%s: %s", new Date(), suspect))); + suspects.forEach(suspect -> suspect_history.add(String.format("%s: %s", Util.utcNow(), suspect))); suspected_mbrs.add(suspects); Collection
suspects_copy=suspected_mbrs.getMembers(); // returns a copy if(suspects_copy.isEmpty()) diff --git a/src/org/jgroups/protocols/FailureDetection.java b/src/org/jgroups/protocols/FailureDetection.java index f721001e776..ff3d9bacaa4 100644 --- a/src/org/jgroups/protocols/FailureDetection.java +++ b/src/org/jgroups/protocols/FailureDetection.java @@ -118,7 +118,7 @@ public void stopFailureDetection() { public String printSuspectHistory() { StringBuilder sb=new StringBuilder(); for(Tuple tmp: suspect_history) { - sb.append(new Date(tmp.getVal2())).append(": ").append(tmp.getVal1()).append("\n"); + sb.append(Util.utcEpoch(tmp.getVal2())).append(": ").append(tmp.getVal1()).append("\n"); } return sb.toString(); } diff --git a/src/org/jgroups/protocols/SOS.java b/src/org/jgroups/protocols/SOS.java index 37342017a89..e3f5ea182e3 100644 --- a/src/org/jgroups/protocols/SOS.java +++ b/src/org/jgroups/protocols/SOS.java @@ -107,7 +107,7 @@ public String exec() { protected String getMetadata() { TP tp=stack.getTransport(); return String.format("\nDate: %s, member: %s (%s), version: %s\nview: %s\n", - new Date(), tp.getAddress(), tp.getPhysicalAddress(), + Util.utcNow(), tp.getAddress(), tp.getPhysicalAddress(), Version.printVersion(), tp.view()); } diff --git a/src/org/jgroups/protocols/pbcast/GMS.java b/src/org/jgroups/protocols/pbcast/GMS.java index f57f78b2bbe..9a74904029f 100644 --- a/src/org/jgroups/protocols/pbcast/GMS.java +++ b/src/org/jgroups/protocols/pbcast/GMS.java @@ -692,7 +692,7 @@ public synchronized void installView(View new_view, Digest digest) { if(stats) { num_views++; - prev_views.add(new Date() + ": " + new_view); + prev_views.add(Util.utcNow() + ": " + new_view); } } diff --git a/src/org/jgroups/protocols/pbcast/ViewHandler.java b/src/org/jgroups/protocols/pbcast/ViewHandler.java index 86047b226b1..1866ee8ca63 100644 --- a/src/org/jgroups/protocols/pbcast/ViewHandler.java +++ b/src/org/jgroups/protocols/pbcast/ViewHandler.java @@ -177,7 +177,7 @@ protected boolean _add(R req) { log().trace("%s: queue is suspended; request %s is discarded", gms.getAddress(), req); return false; } - String log=new Date() + ": " + req; + String log= Util.utcNow() + ": " + req; lock.lock(); try { if(!requests.contains(req)) { // non-null check already performed (above) @@ -212,7 +212,7 @@ protected boolean _add(Collection reqs) { for(R req: reqs) { if(req != null && !requests.contains(req)) { requests.add(req); - history.add(new Date() + ": " + req); + history.add(Util.utcNow() + ": " + req); } } return processing.compareAndSet(false, true); diff --git a/src/org/jgroups/stack/GossipRouter.java b/src/org/jgroups/stack/GossipRouter.java index d7ea39ffdd2..4aabc7fdd2f 100644 --- a/src/org/jgroups/stack/GossipRouter.java +++ b/src/org/jgroups/stack/GossipRouter.java @@ -752,7 +752,7 @@ public Entry(Address client_addr, PhysicalAddress phys_addr, String logical_name * Prints startup information. */ private void printStartupInfo() { - System.out.println("GossipRouter started at " + new Date()); + System.out.println("GossipRouter started at " + Util.utcNow()); System.out.print("Listening on port " + port); System.out.println(" bound on address " + server.localAddress()); diff --git a/src/org/jgroups/util/Util.java b/src/org/jgroups/util/Util.java index f46ecb39b33..b838083c03a 100644 --- a/src/org/jgroups/util/Util.java +++ b/src/org/jgroups/util/Util.java @@ -32,6 +32,10 @@ import java.nio.channels.SocketChannel; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -110,6 +114,7 @@ public enum AddressScope {GLOBAL,SITE_LOCAL,LINK_LOCAL,LOOPBACK,NON_LOOPBACK} private static final StackType ip_stack_type=_getIpStackType(); public static final boolean can_bind_to_mcast_addr; protected static ResourceBundle resource_bundle; + static DateTimeFormatter UTF_FORMAT = DateTimeFormatter.ofPattern("E MMM d H:m:s 'UTC' y"); static { String tmp; @@ -4996,4 +5001,12 @@ public static Collection otherSites(View bridge_view, String excluding_s return ret; } + public static String utcNow() { + return UTF_FORMAT.format(LocalDateTime.now(ZoneOffset.UTC)); + } + + public static String utcEpoch(long milliseconds) { + return UTF_FORMAT.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneOffset.UTC)); + } + }