[watcher] Remove default timezone usage

Closes elastic/elasticsearch#387

Original commit: elastic/x-pack-elasticsearch@5382fecf10
This commit is contained in:
uboness 2015-08-05 00:20:05 +02:00
parent 87f8f6a0a6
commit 5f932952f2
15 changed files with 25 additions and 39 deletions

View File

@ -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)) {

View File

@ -18,8 +18,6 @@ public interface Clock {
long nanos();
DateTime now();
DateTime nowUTC();
DateTime now(DateTimeZone timeZone);

View File

@ -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);

View File

@ -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

View File

@ -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());

View File

@ -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:");

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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));

View File

@ -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<String, Object> map = ImmutableMap.<String, Object>builder()
.put("a", ImmutableMap.builder().put("a1", new int[] { 0, 1, 2 }).build())
.put("b", new String[] { "b0", "b1", "b2" })

View File

@ -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);

View File

@ -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)

View File

@ -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<TriggerEvent> 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<TriggerEvent> 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<TriggerEvent> 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<TriggerEvent> events) {
logger.info("triggered job on [{}]", SystemClock.INSTANCE.now());
logger.info("triggered job");
}
});

View File

@ -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);