From 5f932952f2a92c8488197f11e58c3c6b181b0301 Mon Sep 17 00:00:00 2001 From: uboness Date: Wed, 5 Aug 2015 00:20:05 +0200 Subject: [PATCH] [watcher] Remove default timezone usage Closes elastic/elasticsearch#387 Original commit: elastic/x-pack-elasticsearch@5382fecf108ed24f7b2034028b658b1e567d85fb --- .../watcher/actions/email/service/Email.java | 3 +-- .../watcher/support/clock/Clock.java | 2 -- .../watcher/support/clock/SystemClock.java | 5 ----- .../SchedulerScheduleTriggerEngine.java | 2 +- .../engine/TickerScheduleTriggerEngine.java | 2 +- .../trigger/schedule/tool/CronEvalTool.java | 2 +- .../actions/email/service/EmailTest.java | 2 +- .../actions/throttler/AckThrottlerTests.java | 6 +++--- .../throttler/PeriodThrottlerTests.java | 4 ++-- .../compare/CompareConditionTests.java | 2 +- .../watcher/support/WatcherUtilsTests.java | 2 +- .../watcher/support/clock/ClockMock.java | 7 +------ .../test/integration/BasicWatcherTests.java | 4 ++-- .../engine/BaseTriggerEngineTestCase.java | 19 +++++++++---------- .../watcher/watch/WatchServiceTests.java | 2 +- 15 files changed, 25 insertions(+), 39 deletions(-) diff --git a/watcher/src/main/java/org/elasticsearch/watcher/actions/email/service/Email.java b/watcher/src/main/java/org/elasticsearch/watcher/actions/email/service/Email.java index deabb77375a..17feb4f1d3b 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/actions/email/service/Email.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/actions/email/service/Email.java @@ -10,7 +10,6 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -197,7 +196,7 @@ public class Email implements ToXContent { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.PRIORITY)) { email.priority(Email.Priority.resolve(parser.text())); } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.SENT_DATE)) { - email.sentDate(new DateTime(parser.text())); + email.sentDate(new DateTime(parser.text(), DateTimeZone.UTC)); } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.SUBJECT)) { email.subject(parser.text()); } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.BODY)) { diff --git a/watcher/src/main/java/org/elasticsearch/watcher/support/clock/Clock.java b/watcher/src/main/java/org/elasticsearch/watcher/support/clock/Clock.java index 101387da3c2..439140386aa 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/support/clock/Clock.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/support/clock/Clock.java @@ -18,8 +18,6 @@ public interface Clock { long nanos(); - DateTime now(); - DateTime nowUTC(); DateTime now(DateTimeZone timeZone); diff --git a/watcher/src/main/java/org/elasticsearch/watcher/support/clock/SystemClock.java b/watcher/src/main/java/org/elasticsearch/watcher/support/clock/SystemClock.java index 225cb71b63f..8de10532a39 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/support/clock/SystemClock.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/support/clock/SystemClock.java @@ -29,11 +29,6 @@ public final class SystemClock implements Clock { return System.nanoTime(); } - @Override - public DateTime now() { - return now(DateTimeZone.getDefault()); - } - @Override public DateTime nowUTC() { return now(DateTimeZone.UTC); diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/SchedulerScheduleTriggerEngine.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/SchedulerScheduleTriggerEngine.java index 4239f34d777..b686236cf72 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/SchedulerScheduleTriggerEngine.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/SchedulerScheduleTriggerEngine.java @@ -51,7 +51,7 @@ public class SchedulerScheduleTriggerEngine extends ScheduleTriggerEngine { } } this.schedules = new Schedules(schedules); - logger.debug("schedule engine started at [{}]", clock.now()); + logger.debug("schedule engine started at [{}]", clock.nowUTC()); } @Override diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java index 62e1a0f9a27..880d9d88cf8 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java @@ -142,7 +142,7 @@ public class TickerScheduleTriggerEngine extends ScheduleTriggerEngine { while (clock.millis() % 1000 > 15) { } while (active) { - logger.trace("checking jobs [{}]", clock.now()); + logger.trace("checking jobs [{}]", clock.nowUTC()); checkJobs(); try { sleep(tickInterval.millis()); diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/tool/CronEvalTool.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/tool/CronEvalTool.java index 05d73c3cc59..68cf846fa44 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/tool/CronEvalTool.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/tool/CronEvalTool.java @@ -83,7 +83,7 @@ public class CronEvalTool extends CliTool { terminal.println("Valid!"); - DateTime date = DateTime.now(DateTimeZone.getDefault()); + DateTime date = DateTime.now(DateTimeZone.UTC); terminal.println("Now is [" + formatter.print(date) + "]"); terminal.println("Here are the next " + count + " times this cron expression will trigger:"); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/actions/email/service/EmailTest.java b/watcher/src/test/java/org/elasticsearch/watcher/actions/email/service/EmailTest.java index 1c10339b033..5b8d70b0e52 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/actions/email/service/EmailTest.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/actions/email/service/EmailTest.java @@ -36,7 +36,7 @@ public class EmailTest extends ESTestCase { Email.AddressList possibleList = new Email.AddressList(addresses); Email.AddressList replyTo = randomFrom(possibleList, null); Email.Priority priority = randomFrom(Email.Priority.values()); - DateTime sentDate = new DateTime(randomInt(), DateTimeZone.getDefault()); + DateTime sentDate = new DateTime(randomInt(), DateTimeZone.UTC); Email.AddressList to = randomFrom(possibleList, null); Email.AddressList cc = randomFrom(possibleList, null); Email.AddressList bcc = randomFrom(possibleList, null); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/actions/throttler/AckThrottlerTests.java b/watcher/src/test/java/org/elasticsearch/watcher/actions/throttler/AckThrottlerTests.java index 7359b94d020..3d03a15a0e2 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/actions/throttler/AckThrottlerTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/actions/throttler/AckThrottlerTests.java @@ -29,7 +29,7 @@ public class AckThrottlerTests extends ESTestCase { @Test public void testWhenAcked() throws Exception { - DateTime timestamp = SystemClock.INSTANCE.now(); + DateTime timestamp = SystemClock.INSTANCE.nowUTC(); WatchExecutionContext ctx = mockExecutionContext("_watch", EMPTY_PAYLOAD); Watch watch = ctx.watch(); ActionStatus actionStatus = mock(ActionStatus.class); @@ -45,7 +45,7 @@ public class AckThrottlerTests extends ESTestCase { @Test public void testThrottle_When_AwaitsSuccessfulExecution() throws Exception { - DateTime timestamp = SystemClock.INSTANCE.now(); + DateTime timestamp = SystemClock.INSTANCE.nowUTC(); WatchExecutionContext ctx = mockExecutionContext("_watch", EMPTY_PAYLOAD); Watch watch = ctx.watch(); ActionStatus actionStatus = mock(ActionStatus.class); @@ -61,7 +61,7 @@ public class AckThrottlerTests extends ESTestCase { @Test public void testThrottle_When_Ackable() throws Exception { - DateTime timestamp = SystemClock.INSTANCE.now(); + DateTime timestamp = SystemClock.INSTANCE.nowUTC(); WatchExecutionContext ctx = mockExecutionContext("_watch", EMPTY_PAYLOAD); Watch watch = ctx.watch(); ActionStatus actionStatus = mock(ActionStatus.class); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/actions/throttler/PeriodThrottlerTests.java b/watcher/src/test/java/org/elasticsearch/watcher/actions/throttler/PeriodThrottlerTests.java index a9844897357..ddfd12bde0a 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/actions/throttler/PeriodThrottlerTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/actions/throttler/PeriodThrottlerTests.java @@ -35,7 +35,7 @@ public class PeriodThrottlerTests extends ESTestCase { WatchExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD); ActionStatus actionStatus = mock(ActionStatus.class); - when(actionStatus.lastSuccessfulExecution()).thenReturn(ActionStatus.Execution.successful(SystemClock.INSTANCE.now().minusSeconds((int) period.seconds() - 1))); + when(actionStatus.lastSuccessfulExecution()).thenReturn(ActionStatus.Execution.successful(SystemClock.INSTANCE.nowUTC().minusSeconds((int) period.seconds() - 1))); WatchStatus status = mock(WatchStatus.class); when(status.actionStatus("_action")).thenReturn(actionStatus); when(ctx.watch().status()).thenReturn(status); @@ -55,7 +55,7 @@ public class PeriodThrottlerTests extends ESTestCase { WatchExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD); ActionStatus actionStatus = mock(ActionStatus.class); - when(actionStatus.lastSuccessfulExecution()).thenReturn(ActionStatus.Execution.successful(SystemClock.INSTANCE.now().minusSeconds((int) period.seconds() + 1))); + when(actionStatus.lastSuccessfulExecution()).thenReturn(ActionStatus.Execution.successful(SystemClock.INSTANCE.nowUTC().minusSeconds((int) period.seconds() + 1))); WatchStatus status = mock(WatchStatus.class); when(status.actionStatus("_action")).thenReturn(actionStatus); when(ctx.watch().status()).thenReturn(status); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/condition/compare/CompareConditionTests.java b/watcher/src/test/java/org/elasticsearch/watcher/condition/compare/CompareConditionTests.java index dde2f6ba2e5..4866265a94e 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/condition/compare/CompareConditionTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/condition/compare/CompareConditionTests.java @@ -150,7 +150,7 @@ public class CompareConditionTests extends ESTestCase { boolean met = randomBoolean(); Op op = met ? randomFrom(Op.GT, Op.GTE, Op.NOT_EQ) : randomFrom(Op.LT, Op.LTE, Op.EQ); String value = "<{now-1d}>"; - DateTime payloadValue = clock.now(); + DateTime payloadValue = clock.nowUTC(); ExecutableCompareCondition condition = new ExecutableCompareCondition(new CompareCondition("ctx.payload.value", op, value), logger, clock); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.Simple("value", payloadValue)); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherUtilsTests.java b/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherUtilsTests.java index 19329486cdf..3e61f057f99 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherUtilsTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherUtilsTests.java @@ -44,7 +44,7 @@ public class WatcherUtilsTests extends ESTestCase { @Test public void testFlattenModel() throws Exception { - DateTime now = SystemClock.INSTANCE.now(); + DateTime now = SystemClock.INSTANCE.nowUTC(); Map map = ImmutableMap.builder() .put("a", ImmutableMap.builder().put("a1", new int[] { 0, 1, 2 }).build()) .put("b", new String[] { "b0", "b1", "b2" }) diff --git a/watcher/src/test/java/org/elasticsearch/watcher/support/clock/ClockMock.java b/watcher/src/test/java/org/elasticsearch/watcher/support/clock/ClockMock.java index ac625918dd5..8a084524940 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/support/clock/ClockMock.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/support/clock/ClockMock.java @@ -16,7 +16,7 @@ import java.util.concurrent.TimeUnit; */ public class ClockMock implements Clock { - private DateTime now = DateTime.now(DateTimeZone.getDefault()); + private DateTime now = DateTime.now(DateTimeZone.UTC); @Override public long millis() { @@ -28,11 +28,6 @@ public class ClockMock implements Clock { return TimeUnit.MILLISECONDS.toNanos(now.getMillis()); } - @Override - public DateTime now() { - return now; - } - @Override public DateTime nowUTC() { return now(DateTimeZone.UTC); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java b/watcher/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java index a90dc5c8f5b..d465172e678 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java @@ -437,7 +437,7 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests { private void testConditionSearch(SearchRequest request) throws Exception { if (timeWarped()) { // reset, so we don't miss event docs when we filter over the _timestamp field. - timeWarp().clock().setTime(SystemClock.INSTANCE.now()); + timeWarp().clock().setTime(SystemClock.INSTANCE.nowUTC()); } String watchName = "_name"; @@ -450,7 +450,7 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests { .condition(ConditionBuilders.scriptCondition("return ctx.payload.hits.total >= 3"))) .get(); - logger.info("created watch [{}] at [{}]", watchName, SystemClock.INSTANCE.now()); + logger.info("created watch [{}] at [{}]", watchName, SystemClock.INSTANCE.nowUTC()); client().prepareIndex("events", "event") .setCreate(true) diff --git a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/engine/BaseTriggerEngineTestCase.java b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/engine/BaseTriggerEngineTestCase.java index a197170fa2c..ff2534bab93 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/engine/BaseTriggerEngineTestCase.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/engine/BaseTriggerEngineTestCase.java @@ -5,9 +5,8 @@ */ package org.elasticsearch.watcher.trigger.schedule.engine; -import org.elasticsearch.watcher.support.clock.SystemClock; -import org.joda.time.DateTime; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.watcher.support.clock.SystemClock; import org.elasticsearch.watcher.trigger.Trigger; import org.elasticsearch.watcher.trigger.TriggerEngine; import org.elasticsearch.watcher.trigger.TriggerEvent; @@ -15,9 +14,9 @@ import org.elasticsearch.watcher.trigger.schedule.Schedule; import org.elasticsearch.watcher.trigger.schedule.ScheduleTrigger; import org.elasticsearch.watcher.trigger.schedule.support.DayOfWeek; import org.elasticsearch.watcher.trigger.schedule.support.WeekTimes; +import org.joda.time.DateTime; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import java.util.ArrayList; @@ -27,9 +26,9 @@ import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import static org.joda.time.DateTimeZone.UTC; import static org.elasticsearch.watcher.trigger.schedule.Schedules.*; import static org.hamcrest.Matchers.is; +import static org.joda.time.DateTimeZone.UTC; public abstract class BaseTriggerEngineTestCase extends ESTestCase { @@ -63,11 +62,11 @@ public abstract class BaseTriggerEngineTestCase extends ESTestCase { for (TriggerEvent event : events) { int index = Integer.parseInt(event.jobName()); if (!bits.get(index)) { - logger.info("job [{}] first fire: {}", index, SystemClock.INSTANCE.now()); + logger.info("job [{}] first fire", index); bits.set(index); } else { latch.countDown(); - logger.info("job [{}] second fire: {}", index, SystemClock.INSTANCE.now()); + logger.info("job [{}] second fire", index); } } } @@ -91,7 +90,7 @@ public abstract class BaseTriggerEngineTestCase extends ESTestCase { public void triggered(Iterable events) { for (TriggerEvent event : events) { assertThat(event.jobName(), is(name)); - logger.info("triggered job on [{}]", SystemClock.INSTANCE.now()); + logger.info("triggered job on [{}]", SystemClock.INSTANCE.nowUTC()); } latch.countDown(); } @@ -125,7 +124,7 @@ public abstract class BaseTriggerEngineTestCase extends ESTestCase { public void triggered(Iterable events) { for (TriggerEvent event : events) { assertThat(event.jobName(), is(name)); - logger.info("triggered job on [{}]", SystemClock.INSTANCE.now()); + logger.info("triggered job on [{}]", SystemClock.INSTANCE.nowUTC()); latch.countDown(); } } @@ -161,7 +160,7 @@ public abstract class BaseTriggerEngineTestCase extends ESTestCase { public void triggered(Iterable events) { for (TriggerEvent event : events) { assertThat(event.jobName(), is(name)); - logger.info("triggered job on [{}]", SystemClock.INSTANCE.now()); + logger.info("triggered job"); } latch.countDown(); } @@ -195,7 +194,7 @@ public abstract class BaseTriggerEngineTestCase extends ESTestCase { @Override public void triggered(Iterable events) { - logger.info("triggered job on [{}]", SystemClock.INSTANCE.now()); + logger.info("triggered job"); } }); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/watch/WatchServiceTests.java b/watcher/src/test/java/org/elasticsearch/watcher/watch/WatchServiceTests.java index 00ad4af3374..7ef5b91f938 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/watch/WatchServiceTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/watch/WatchServiceTests.java @@ -205,7 +205,7 @@ public class WatchServiceTests extends ESTestCase { @Test public void testAckWatch_NotAck() throws Exception { - DateTime now = SystemClock.INSTANCE.now(); + DateTime now = SystemClock.INSTANCE.nowUTC(); TimeValue timeout = TimeValue.timeValueSeconds(5); WatchLockService.Lock lock = mock(WatchLockService.Lock.class); when(watchLockService.tryAcquire("_id", timeout)).thenReturn(lock);