From 860d8658ca469a52e2042bdadd4d77cbfeffb757 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 28 Apr 2017 10:25:29 +0200 Subject: [PATCH 1/2] Fixed #1513 sync shared timer --- .../org/eclipse/jetty/util/RolloverFileOutputStream.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java index f4db2930bed..ef4d16fab3f 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java @@ -212,7 +212,10 @@ public class RolloverFileOutputStream extends FilterOutputStream { _rollTask = new RollTask(); midnight = nextMidnight(midnight); - __rollover.schedule(_rollTask,midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli()); + synchronized(RolloverFileOutputStream.class) + { + __rollover.schedule(_rollTask,midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli()); + } } /* ------------------------------------------------------------ */ From c87903f39d3d0063c460fe9b66b9e406d0e02bdb Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 28 Apr 2017 11:01:42 +0200 Subject: [PATCH 2/2] Fixed #1513 back ported some 9.4 changes --- .../jetty/util/RolloverFileOutputStream.java | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java index ef4d16fab3f..c7ea1cd9b16 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java @@ -55,7 +55,7 @@ public class RolloverFileOutputStream extends FilterOutputStream final static int ROLLOVER_FILE_RETAIN_DAYS = 31; private RollTask _rollTask; - private ZonedDateTime midnight; + private ZonedDateTime _midnight; private SimpleDateFormat _fileBackupFormat; private SimpleDateFormat _fileDateFormat; @@ -174,16 +174,16 @@ public class RolloverFileOutputStream extends FilterOutputStream { if (__rollover==null) __rollover=new Timer(RolloverFileOutputStream.class.getName(),true); - - ZonedDateTime now = ZonedDateTime.now(zone.toZoneId()); - midnight = toMidnight(now, zone.toZoneId()); - while (midnight.isBefore(now)) - midnight = nextMidnight(midnight); - + + // Calculate Today's Midnight, based on Configured TimeZone (will be in past, even if by a few milliseconds) + ZonedDateTime now = ZonedDateTime.now(zone.toZoneId()); + _midnight = toMidnight(now, zone.toZoneId()); + // This will schedule the rollover event to the next midnight scheduleNextRollover(now); } } - + + /* ------------------------------------------------------------ */ /** * Get the "start of day" for the provided DateTime at the zone specified. * @@ -195,7 +195,8 @@ public class RolloverFileOutputStream extends FilterOutputStream { return dateTime.toLocalDate().atStartOfDay(zone); } - + + /* ------------------------------------------------------------ */ /** * Get the next "start of day" for the provided date. * @@ -204,18 +205,23 @@ public class RolloverFileOutputStream extends FilterOutputStream */ public static ZonedDateTime nextMidnight(ZonedDateTime dateTime) { - // Increment to next day. + // Increment to next day, based on Configured TimeZone, then find start of that day. + // Will always be in the future, even if the Daylights Savings Time kicks in during + // the calculation. return dateTime.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(dateTime.getZone()); } - + + /* ------------------------------------------------------------ */ private void scheduleNextRollover(ZonedDateTime now) { _rollTask = new RollTask(); - midnight = nextMidnight(midnight); - synchronized(RolloverFileOutputStream.class) - { - __rollover.schedule(_rollTask,midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli()); - } + // Get tomorrow's midnight based on Configured TimeZone + while (_midnight.isBefore(now)) + _midnight = nextMidnight(_midnight); + + long delay = _midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli(); + // Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds + __rollover.schedule(_rollTask,delay); } /* ------------------------------------------------------------ */ @@ -286,7 +292,7 @@ public class RolloverFileOutputStream extends FilterOutputStream { if (_retainDays>0) { - now.minus(_retainDays, ChronoUnit.DAYS); + // Establish expiration time, based on configured TimeZone long expired = now.toInstant().toEpochMilli(); File file= new File(_filename); @@ -359,10 +365,13 @@ public class RolloverFileOutputStream extends FilterOutputStream { try { - ZonedDateTime now = ZonedDateTime.now(midnight.getZone()); - RolloverFileOutputStream.this.setFile(); - RolloverFileOutputStream.this.scheduleNextRollover(now); - RolloverFileOutputStream.this.removeOldFiles(now); + synchronized(RolloverFileOutputStream.class) + { + ZonedDateTime now = ZonedDateTime.now(_midnight.getZone()); + RolloverFileOutputStream.this.setFile(); + RolloverFileOutputStream.this.scheduleNextRollover(now); + RolloverFileOutputStream.this.removeOldFiles(now); + } } catch(Throwable t) {