From ddfafa071198bbdc34c1983a9a7e0bed218747f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20L=C3=A9aut=C3=A9?= Date: Thu, 12 Mar 2015 18:06:59 -0700 Subject: [PATCH] randomize task ID to fix spurious test failure --- .../common/task/RealtimeIndexTask.java | 25 +++++++++++-- .../common/task/RealtimeIndexTaskTest.java | 37 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 indexing-service/src/test/java/io/druid/indexing/common/task/RealtimeIndexTaskTest.java diff --git a/indexing-service/src/main/java/io/druid/indexing/common/task/RealtimeIndexTask.java b/indexing-service/src/main/java/io/druid/indexing/common/task/RealtimeIndexTask.java index 1432bc6085b..2fdbe98a769 100644 --- a/indexing-service/src/main/java/io/druid/indexing/common/task/RealtimeIndexTask.java +++ b/indexing-service/src/main/java/io/druid/indexing/common/task/RealtimeIndexTask.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; +import com.google.common.primitives.Ints; import com.metamx.common.guava.CloseQuietly; import com.metamx.common.parsers.ParseException; import com.metamx.emitter.EmittingLogger; @@ -57,18 +58,36 @@ import org.joda.time.Period; import java.io.File; import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Random; public class RealtimeIndexTask extends AbstractTask { private static final EmittingLogger log = new EmittingLogger(RealtimeIndexTask.class); + private final static Random random = new Random(); private static String makeTaskId(FireDepartment fireDepartment) { - return String.format( - "index_realtime_%s_%d_%s", + return makeTaskId( fireDepartment.getDataSchema().getDataSource(), fireDepartment.getTuningConfig().getShardSpec().getPartitionNum(), - new DateTime().toString() + new DateTime(), + random.nextInt() + ); + } + + static String makeTaskId(String dataSource, int partitionNumber, DateTime timestamp, int randomBits) + { + final StringBuilder suffix = new StringBuilder(8); + for(int i = 0; i < Ints.BYTES * 2; ++i) { + suffix.append((char)('a' + ((randomBits >>> (i * 4)) & 0x0F))); + } + return String.format( + "index_realtime_%s_%d_%s_%s", + dataSource, + partitionNumber, + timestamp, + suffix ); } diff --git a/indexing-service/src/test/java/io/druid/indexing/common/task/RealtimeIndexTaskTest.java b/indexing-service/src/test/java/io/druid/indexing/common/task/RealtimeIndexTaskTest.java new file mode 100644 index 00000000000..67e0f81b853 --- /dev/null +++ b/indexing-service/src/test/java/io/druid/indexing/common/task/RealtimeIndexTaskTest.java @@ -0,0 +1,37 @@ +/* + * Licensed to Metamarkets Group Inc. (Metamarkets) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Metamarkets licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.druid.indexing.common.task; + +import org.joda.time.DateTime; +import org.junit.Assert; +import org.junit.Test; + +public class RealtimeIndexTaskTest +{ + + @Test + public void testMakeTaskId() throws Exception + { + Assert.assertEquals( + "index_realtime_test_0_2015-01-02T00:00:00.000Z_abcdefgh", + RealtimeIndexTask.makeTaskId("test", 0, new DateTime("2015-01-02"), 0x76543210) + ); + } +}