-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Fixes bug in kill task scheduling DruidCoordinatorSegmentKiller.findIntervalForKillTask() #2346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
server/src/main/java/io/druid/server/coordinator/helper/DruidCoordinatorSegmentKiller.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * 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.server.coordinator.helper; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Preconditions; | ||
| import com.metamx.common.logger.Logger; | ||
| import io.druid.client.indexing.IndexingServiceClient; | ||
| import io.druid.common.utils.JodaUtils; | ||
| import io.druid.metadata.MetadataSegmentManager; | ||
| import io.druid.server.coordinator.DruidCoordinatorRuntimeParams; | ||
| import org.joda.time.Duration; | ||
| import org.joda.time.Interval; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| */ | ||
| public class DruidCoordinatorSegmentKiller implements DruidCoordinatorHelper | ||
| { | ||
| private final static Logger log = new Logger(DruidCoordinatorSegmentKiller.class); | ||
|
|
||
| private final long period; | ||
| private final long retainDuration; | ||
| private final int maxSegmentsToKill; | ||
| private long lastKillTime = 0; | ||
|
|
||
|
|
||
| private final MetadataSegmentManager segmentManager; | ||
| private final IndexingServiceClient indexingServiceClient; | ||
|
|
||
| public DruidCoordinatorSegmentKiller( | ||
| MetadataSegmentManager segmentManager, | ||
| IndexingServiceClient indexingServiceClient, | ||
| Duration retainDuration, | ||
| Duration period, | ||
| int maxSegmentsToKill | ||
| ) | ||
| { | ||
| this.period = period.getMillis(); | ||
| Preconditions.checkArgument(this.period > 0, "coordinator kill period must be > 0"); | ||
|
|
||
| this.retainDuration = retainDuration.getMillis(); | ||
| Preconditions.checkArgument(this.retainDuration >= 0, "coordinator kill retainDuration must be >= 0"); | ||
|
|
||
| this.maxSegmentsToKill = maxSegmentsToKill; | ||
| Preconditions.checkArgument(this.maxSegmentsToKill > 0, "coordinator kill maxSegments must be > 0"); | ||
|
|
||
| log.info( | ||
| "Kill Task scheduling enabled with period [%s], retainDuration [%s], maxSegmentsToKill [%s]", | ||
| this.period, | ||
| this.retainDuration, | ||
| this.maxSegmentsToKill | ||
| ); | ||
|
|
||
| this.segmentManager = segmentManager; | ||
| this.indexingServiceClient = indexingServiceClient; | ||
| } | ||
|
|
||
| @Override | ||
| public DruidCoordinatorRuntimeParams run(DruidCoordinatorRuntimeParams params) | ||
| { | ||
| Set<String> whitelist = params.getCoordinatorDynamicConfig().getKillDataSourceWhitelist(); | ||
|
|
||
| if (whitelist != null && whitelist.size() > 0 && (lastKillTime + period) < System.currentTimeMillis()) { | ||
| lastKillTime = System.currentTimeMillis(); | ||
|
|
||
| for (String dataSource : whitelist) { | ||
| final Interval intervalToKill = findIntervalForKillTask(dataSource, maxSegmentsToKill); | ||
| if (intervalToKill != null) { | ||
| try { | ||
| indexingServiceClient.killSegments(dataSource, intervalToKill); | ||
| } | ||
| catch (Exception ex) { | ||
| log.error(ex, "Failed to submit kill task for dataSource [%s]", dataSource); | ||
| if (Thread.currentThread().isInterrupted()) { | ||
| log.warn("skipping kill task scheduling because thread is interrupted."); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return params; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| Interval findIntervalForKillTask(String dataSource, int limit) | ||
| { | ||
| List<Interval> unusedSegmentIntervals = segmentManager.getUnusedSegmentIntervals( | ||
| dataSource, | ||
| new Interval( | ||
| 0, | ||
| System.currentTimeMillis() | ||
| - retainDuration | ||
| ), | ||
| limit | ||
| ); | ||
|
|
||
| if (unusedSegmentIntervals != null && unusedSegmentIntervals.size() > 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could this be replaced with return JodaUtils.umbrellaInterval(usedSegmentIntervals) ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice tip, changed. |
||
| return JodaUtils.umbrellaInterval(unusedSegmentIntervals); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
113 changes: 113 additions & 0 deletions
113
...r/src/test/java/io/druid/server/coordinator/helper/DruidCoordinatorSegmentKillerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * 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.server.coordinator.helper; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import io.druid.client.indexing.IndexingServiceClient; | ||
| import io.druid.metadata.MetadataSegmentManager; | ||
| import org.easymock.EasyMock; | ||
| import org.joda.time.Duration; | ||
| import org.joda.time.Interval; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| */ | ||
| public class DruidCoordinatorSegmentKillerTest | ||
| { | ||
| @Test | ||
| public void testFindIntervalForKillTask() | ||
| { | ||
| testFindIntervalForKillTask(null, null); | ||
| testFindIntervalForKillTask(ImmutableList.<Interval>of(), null); | ||
|
|
||
| testFindIntervalForKillTask(ImmutableList.<Interval>of(Interval.parse("2014/2015")), Interval.parse("2014/2015")); | ||
|
|
||
| testFindIntervalForKillTask( | ||
| ImmutableList.<Interval>of(Interval.parse("2014/2015"), Interval.parse("2016/2017")), | ||
| Interval.parse("2014/2017") | ||
| ); | ||
|
|
||
| testFindIntervalForKillTask( | ||
| ImmutableList.<Interval>of(Interval.parse("2014/2015"), Interval.parse("2015/2016")), | ||
| Interval.parse("2014/2016") | ||
| ); | ||
|
|
||
| testFindIntervalForKillTask( | ||
| ImmutableList.<Interval>of(Interval.parse("2015/2016"), Interval.parse("2014/2015")), | ||
| Interval.parse("2014/2016") | ||
| ); | ||
|
|
||
| testFindIntervalForKillTask( | ||
| ImmutableList.<Interval>of(Interval.parse("2015/2017"), Interval.parse("2014/2016")), | ||
| Interval.parse("2014/2017") | ||
| ); | ||
|
|
||
| testFindIntervalForKillTask( | ||
| ImmutableList.<Interval>of( | ||
| Interval.parse("2015/2019"), | ||
| Interval.parse("2014/2016"), | ||
| Interval.parse("2018/2020") | ||
| ), | ||
| Interval.parse("2014/2020") | ||
| ); | ||
|
|
||
| testFindIntervalForKillTask( | ||
| ImmutableList.<Interval>of( | ||
| Interval.parse("2015/2019"), | ||
| Interval.parse("2014/2016"), | ||
| Interval.parse("2018/2020"), | ||
| Interval.parse("2021/2022") | ||
| ), | ||
| Interval.parse("2014/2022") | ||
| ); | ||
| } | ||
|
|
||
| private void testFindIntervalForKillTask(List<Interval> segmentManagerResult, Interval expected) | ||
| { | ||
| MetadataSegmentManager segmentManager = EasyMock.createMock(MetadataSegmentManager.class); | ||
| EasyMock.expect( | ||
| segmentManager.getUnusedSegmentIntervals( | ||
| EasyMock.anyString(), | ||
| EasyMock.anyObject(Interval.class), | ||
| EasyMock.anyInt() | ||
| ) | ||
| ).andReturn( | ||
| segmentManagerResult | ||
| ); | ||
| EasyMock.replay(segmentManager); | ||
| IndexingServiceClient indexingServiceClient = EasyMock.createMock(IndexingServiceClient.class); | ||
|
|
||
| DruidCoordinatorSegmentKiller coordinatorSegmentKiller = new DruidCoordinatorSegmentKiller( | ||
| segmentManager, | ||
| indexingServiceClient, | ||
| Duration.parse("PT86400S"), | ||
| Duration.parse("PT86400S"), | ||
| 1000 | ||
| ); | ||
|
|
||
| Assert.assertEquals( | ||
| expected, | ||
| coordinatorSegmentKiller.findIntervalForKillTask("test", 10000) | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return contract on params is ill-defined. Some other methods modify the values in the params before returning them. Can you add a method javadoc describing any intended changes to params, or lack of changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, added javadoc.