From 1acc064281c04ffa43f95cec7a5499d8506c65c3 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 27 Apr 2017 07:25:13 -0700 Subject: [PATCH] Issue #1507 - ROFS: Simplifying, Documenting, Testing --- .../jetty/util/RolloverFileOutputStream.java | 31 ++++++++++--------- .../util/RolloverFileOutputStreamTest.java | 27 ++++++++++++++-- 2 files changed, 42 insertions(+), 16 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 f4db2930bed..63e20d17cda 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 @@ -174,13 +174,12 @@ public class RolloverFileOutputStream extends FilterOutputStream { if (__rollover==null) __rollover=new Timer(RolloverFileOutputStream.class.getName(),true); + + // Calculate Today's Midnight, based on Configured TimeZone (will be in past, even if by a few milliseconds) + midnight = toMidnight(ZonedDateTime.now(zone.toZoneId()), zone.toZoneId()); - ZonedDateTime now = ZonedDateTime.now(zone.toZoneId()); - midnight = toMidnight(now, zone.toZoneId()); - while (midnight.isBefore(now)) - midnight = nextMidnight(midnight); - - scheduleNextRollover(now); + // This will schedule the rollover event to the next midnight + scheduleNextRollover(); } } @@ -204,15 +203,19 @@ 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) + private void scheduleNextRollover() { _rollTask = new RollTask(); + // Get tomorrow's midnight based on Configured TimeZone midnight = nextMidnight(midnight); - __rollover.schedule(_rollTask,midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli()); + // Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds + __rollover.schedule(_rollTask,midnight.toInstant().toEpochMilli() - System.currentTimeMillis()); } /* ------------------------------------------------------------ */ @@ -279,11 +282,12 @@ public class RolloverFileOutputStream extends FilterOutputStream } /* ------------------------------------------------------------ */ - private void removeOldFiles(ZonedDateTime now) + private void removeOldFiles() { if (_retainDays>0) { - now.minus(_retainDays, ChronoUnit.DAYS); + // Establish expiration time, based on configured TimeZone + ZonedDateTime now = ZonedDateTime.now(this.midnight.getZone()); long expired = now.toInstant().toEpochMilli(); File file= new File(_filename); @@ -356,10 +360,9 @@ public class RolloverFileOutputStream extends FilterOutputStream { try { - ZonedDateTime now = ZonedDateTime.now(midnight.getZone()); RolloverFileOutputStream.this.setFile(); - RolloverFileOutputStream.this.scheduleNextRollover(now); - RolloverFileOutputStream.this.removeOldFiles(now); + RolloverFileOutputStream.this.scheduleNextRollover(); + RolloverFileOutputStream.this.removeOldFiles(); } catch(Throwable t) { diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java index 829f255b527..834a27ec5af 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java @@ -57,14 +57,37 @@ public class RolloverFileOutputStreamTest for (int i = 0; i < expected.length; i++) { - long lastMs = nextEvent.toInstant().toEpochMilli(); + long currentMillis = nextEvent.toInstant().toEpochMilli(); nextEvent = RolloverFileOutputStream.nextMidnight(nextEvent); assertThat("Next Event", toString(nextEvent), is(expected[i][0])); - long duration = (nextEvent.toInstant().toEpochMilli() - lastMs); + long duration = (nextEvent.toInstant().toEpochMilli() - currentMillis); assertThat("Duration to next event", duration, is((long) expected[i][1])); } } + /** + * https://github.com/eclipse/jetty.project/issues/1507 + */ + @Test + public void testMidnightRolloverCalc_PDT_Issue1507() + { + ZoneId zone = toZoneId("PST"); + ZonedDateTime initialDate = toDateTime("2017.04.27-08:00:00.0 PM PDT", zone); + + ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone); + assertThat("Midnight", toString(midnight), is("2017.04.27-12:00:00.0 AM PDT")); + + Object expected[][] = { + {"2017.04.28-12:00:00.0 AM PDT", 14_400_000L}, // Ensure not negative + {"2017.04.29-12:00:00.0 AM PDT", 86_400_000L}, + {"2017.04.30-12:00:00.0 AM PDT", 86_400_000L}, + {"2017.05.01-12:00:00.0 AM PDT", 86_400_000L}, + {"2017.05.02-12:00:00.0 AM PDT", 86_400_000L}, + }; + + assertSequence(initialDate, expected); + } + @Test public void testMidnightRolloverCalc_PST_DST_Start() {