Search in sources :

Example 1 with KillUnusedSegmentsTask

use of org.apache.druid.indexing.common.task.KillUnusedSegmentsTask in project druid by druid-io.

the class TaskLifecycleTest method testKillUnusedSegmentsTask.

@Test
public void testKillUnusedSegmentsTask() throws Exception {
    final File tmpSegmentDir = temporaryFolder.newFolder();
    List<DataSegment> expectedUnusedSegments = Lists.transform(ImmutableList.of("2011-04-01/2011-04-02", "2011-04-02/2011-04-03", "2011-04-04/2011-04-05"), new Function<String, DataSegment>() {

        @Override
        public DataSegment apply(String input) {
            final Interval interval = Intervals.of(input);
            try {
                return DataSegment.builder().dataSource("test_kill_task").interval(interval).loadSpec(ImmutableMap.of("type", "local", "path", tmpSegmentDir.getCanonicalPath() + "/druid/localStorage/wikipedia/" + interval.getStart() + "-" + interval.getEnd() + "/" + "2011-04-6T16:52:46.119-05:00" + "/0/index.zip")).version("2011-04-6T16:52:46.119-05:00").dimensions(ImmutableList.of()).metrics(ImmutableList.of()).shardSpec(NoneShardSpec.instance()).binaryVersion(9).size(0).build();
            } catch (IOException e) {
                throw new ISE(e, "Error creating segments");
            }
        }
    });
    mdc.setUnusedSegments(expectedUnusedSegments);
    // manually create local segments files
    List<File> segmentFiles = new ArrayList<>();
    for (DataSegment segment : mdc.retrieveUnusedSegmentsForInterval("test_kill_task", Intervals.of("2011-04-01/P4D"))) {
        File file = new File((String) segment.getLoadSpec().get("path"));
        FileUtils.mkdirp(file);
        segmentFiles.add(file);
    }
    final Task killUnusedSegmentsTask = new KillUnusedSegmentsTask(null, "test_kill_task", Intervals.of("2011-04-01/P4D"), null, false);
    final TaskStatus status = runTask(killUnusedSegmentsTask);
    Assert.assertEquals(taskLocation, status.getLocation());
    Assert.assertEquals("merged statusCode", TaskState.SUCCESS, status.getStatusCode());
    Assert.assertEquals("num segments published", 0, mdc.getPublished().size());
    Assert.assertEquals("num segments nuked", 3, mdc.getNuked().size());
    Assert.assertTrue("expected unused segments get killed", expectedUnusedSegments.containsAll(mdc.getNuked()) && mdc.getNuked().containsAll(expectedUnusedSegments));
    for (File file : segmentFiles) {
        Assert.assertFalse("unused segments files get deleted", file.exists());
    }
}
Also used : IndexTask(org.apache.druid.indexing.common.task.IndexTask) KillUnusedSegmentsTask(org.apache.druid.indexing.common.task.KillUnusedSegmentsTask) Task(org.apache.druid.indexing.common.task.Task) AbstractFixedIntervalTask(org.apache.druid.indexing.common.task.AbstractFixedIntervalTask) RealtimeIndexTask(org.apache.druid.indexing.common.task.RealtimeIndexTask) KillUnusedSegmentsTask(org.apache.druid.indexing.common.task.KillUnusedSegmentsTask) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TaskStatus(org.apache.druid.indexer.TaskStatus) DataSegment(org.apache.druid.timeline.DataSegment) ISE(org.apache.druid.java.util.common.ISE) File(java.io.File) Interval(org.joda.time.Interval) FireDepartmentTest(org.apache.druid.segment.realtime.FireDepartmentTest) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TaskStatus (org.apache.druid.indexer.TaskStatus)1 AbstractFixedIntervalTask (org.apache.druid.indexing.common.task.AbstractFixedIntervalTask)1 IndexTask (org.apache.druid.indexing.common.task.IndexTask)1 KillUnusedSegmentsTask (org.apache.druid.indexing.common.task.KillUnusedSegmentsTask)1 RealtimeIndexTask (org.apache.druid.indexing.common.task.RealtimeIndexTask)1 Task (org.apache.druid.indexing.common.task.Task)1 ISE (org.apache.druid.java.util.common.ISE)1 FireDepartmentTest (org.apache.druid.segment.realtime.FireDepartmentTest)1 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)1 DataSegment (org.apache.druid.timeline.DataSegment)1 Interval (org.joda.time.Interval)1 Test (org.junit.Test)1