forked from apache/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EqualDistributionWithAffinityWorkerSelectStrategy which balance w… (
apache#3998) * Add EqualDistributionWithAffinityWorkerSelectStrategy which balance work load within affinity workers. * add docs to equalDistributionWithAffinity
- Loading branch information
Showing
8 changed files
with
290 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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
...a/io/druid/indexing/overlord/setup/EqualDistributionWithAffinityWorkerSelectStrategy.java
This file contains 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.indexing.overlord.setup; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.base.Optional; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.Sets; | ||
import io.druid.indexing.common.task.Task; | ||
import io.druid.indexing.overlord.ImmutableWorkerInfo; | ||
import io.druid.indexing.overlord.config.WorkerTaskRunnerConfig; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
*/ | ||
public class EqualDistributionWithAffinityWorkerSelectStrategy extends EqualDistributionWorkerSelectStrategy | ||
{ | ||
private final AffinityConfig affinityConfig; | ||
private final Set<String> affinityWorkerHosts = Sets.newHashSet(); | ||
|
||
@JsonCreator | ||
public EqualDistributionWithAffinityWorkerSelectStrategy( | ||
@JsonProperty("affinityConfig") AffinityConfig affinityConfig | ||
) | ||
{ | ||
this.affinityConfig = affinityConfig; | ||
for (List<String> affinityWorkers : affinityConfig.getAffinity().values()) { | ||
for (String affinityWorker : affinityWorkers) { | ||
this.affinityWorkerHosts.add(affinityWorker); | ||
} | ||
} | ||
} | ||
|
||
@JsonProperty | ||
public AffinityConfig getAffinityConfig() | ||
{ | ||
return affinityConfig; | ||
} | ||
|
||
@Override | ||
public Optional<ImmutableWorkerInfo> findWorkerForTask( | ||
final WorkerTaskRunnerConfig config, | ||
final ImmutableMap<String, ImmutableWorkerInfo> zkWorkers, | ||
final Task task | ||
) | ||
{ | ||
// don't run other datasources on affinity workers; we only want our configured datasources to run on them | ||
ImmutableMap.Builder<String, ImmutableWorkerInfo> builder = new ImmutableMap.Builder<>(); | ||
for (String workerHost : zkWorkers.keySet()) { | ||
if (!affinityWorkerHosts.contains(workerHost)) { | ||
builder.put(workerHost, zkWorkers.get(workerHost)); | ||
} | ||
} | ||
ImmutableMap<String, ImmutableWorkerInfo> eligibleWorkers = builder.build(); | ||
|
||
List<String> workerHosts = affinityConfig.getAffinity().get(task.getDataSource()); | ||
if (workerHosts == null) { | ||
return super.findWorkerForTask(config, eligibleWorkers, task); | ||
} | ||
|
||
ImmutableMap.Builder<String, ImmutableWorkerInfo> affinityBuilder = new ImmutableMap.Builder<>(); | ||
for (String workerHost : workerHosts) { | ||
ImmutableWorkerInfo zkWorker = zkWorkers.get(workerHost); | ||
if (zkWorker != null) { | ||
affinityBuilder.put(workerHost, zkWorker); | ||
} | ||
} | ||
ImmutableMap<String, ImmutableWorkerInfo> affinityWorkers = affinityBuilder.build(); | ||
|
||
if (!affinityWorkers.isEmpty()) { | ||
Optional<ImmutableWorkerInfo> retVal = super.findWorkerForTask(config, affinityWorkers, task); | ||
if (retVal.isPresent()) { | ||
return retVal; | ||
} | ||
} | ||
|
||
return super.findWorkerForTask(config, eligibleWorkers, task); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) | ||
{ | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
EqualDistributionWithAffinityWorkerSelectStrategy that = (EqualDistributionWithAffinityWorkerSelectStrategy) o; | ||
|
||
if (affinityConfig != null ? !affinityConfig.equals(that.affinityConfig) : that.affinityConfig != null) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return affinityConfig != null ? affinityConfig.hashCode() : 0; | ||
} | ||
} |
This file contains 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 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
142 changes: 142 additions & 0 deletions
142
.../druid/indexing/overlord/setup/EqualDistributionWithAffinityWorkerSelectStrategyTest.java
This file contains 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,142 @@ | ||
/* | ||
* 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.overlord.setup; | ||
|
||
import com.google.common.base.Optional; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.Sets; | ||
import io.druid.indexing.common.task.NoopTask; | ||
import io.druid.indexing.overlord.ImmutableWorkerInfo; | ||
import io.druid.indexing.overlord.config.RemoteTaskRunnerConfig; | ||
import io.druid.indexing.worker.Worker; | ||
import org.joda.time.DateTime; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
public class EqualDistributionWithAffinityWorkerSelectStrategyTest | ||
{ | ||
@Test | ||
public void testFindWorkerForTask() throws Exception | ||
{ | ||
EqualDistributionWorkerSelectStrategy strategy = new EqualDistributionWithAffinityWorkerSelectStrategy( | ||
new AffinityConfig(ImmutableMap.of("foo", Arrays.asList("localhost1", "localhost2", "localhost3"))) | ||
); | ||
|
||
Optional<ImmutableWorkerInfo> optional = strategy.findWorkerForTask( | ||
new RemoteTaskRunnerConfig(), | ||
ImmutableMap.of( | ||
"localhost0", | ||
new ImmutableWorkerInfo( | ||
new Worker("localhost0", "localhost0", 2, "v1"), 0, | ||
Sets.<String>newHashSet(), | ||
Sets.<String>newHashSet(), | ||
DateTime.now() | ||
), | ||
"localhost1", | ||
new ImmutableWorkerInfo( | ||
new Worker("localhost1", "localhost1", 2, "v1"), 0, | ||
Sets.<String>newHashSet(), | ||
Sets.<String>newHashSet(), | ||
DateTime.now() | ||
), | ||
"localhost2", | ||
new ImmutableWorkerInfo( | ||
new Worker("localhost2", "localhost2", 2, "v1"), 1, | ||
Sets.<String>newHashSet(), | ||
Sets.<String>newHashSet(), | ||
DateTime.now() | ||
), | ||
"localhost3", | ||
new ImmutableWorkerInfo( | ||
new Worker("localhost3", "localhost3", 2, "v1"), 1, | ||
Sets.<String>newHashSet(), | ||
Sets.<String>newHashSet(), | ||
DateTime.now() | ||
) | ||
), | ||
new NoopTask(null, 1, 0, null, null, null) | ||
{ | ||
@Override | ||
public String getDataSource() | ||
{ | ||
return "foo"; | ||
} | ||
} | ||
); | ||
ImmutableWorkerInfo worker = optional.get(); | ||
Assert.assertEquals("localhost1", worker.getWorker().getHost()); | ||
} | ||
|
||
@Test | ||
public void testFindWorkerForTaskWithNulls() throws Exception | ||
{ | ||
EqualDistributionWorkerSelectStrategy strategy = new EqualDistributionWithAffinityWorkerSelectStrategy( | ||
new AffinityConfig(ImmutableMap.of("foo", Arrays.asList("localhost"))) | ||
); | ||
|
||
Optional<ImmutableWorkerInfo> optional = strategy.findWorkerForTask( | ||
new RemoteTaskRunnerConfig(), | ||
ImmutableMap.of( | ||
"lhost", | ||
new ImmutableWorkerInfo( | ||
new Worker("lhost", "lhost", 1, "v1"), 0, | ||
Sets.<String>newHashSet(), | ||
Sets.<String>newHashSet(), | ||
DateTime.now() | ||
), | ||
"localhost", | ||
new ImmutableWorkerInfo( | ||
new Worker("localhost", "localhost", 1, "v1"), 0, | ||
Sets.<String>newHashSet(), | ||
Sets.<String>newHashSet(), | ||
DateTime.now() | ||
) | ||
), | ||
new NoopTask(null, 1, 0, null, null, null) | ||
); | ||
ImmutableWorkerInfo worker = optional.get(); | ||
Assert.assertEquals("lhost", worker.getWorker().getHost()); | ||
} | ||
|
||
@Test | ||
public void testIsolation() throws Exception | ||
{ | ||
EqualDistributionWorkerSelectStrategy strategy = new EqualDistributionWithAffinityWorkerSelectStrategy( | ||
new AffinityConfig(ImmutableMap.of("foo", Arrays.asList("localhost"))) | ||
); | ||
|
||
Optional<ImmutableWorkerInfo> optional = strategy.findWorkerForTask( | ||
new RemoteTaskRunnerConfig(), | ||
ImmutableMap.of( | ||
"localhost", | ||
new ImmutableWorkerInfo( | ||
new Worker("localhost", "localhost", 1, "v1"), 0, | ||
Sets.<String>newHashSet(), | ||
Sets.<String>newHashSet(), | ||
DateTime.now() | ||
) | ||
), | ||
new NoopTask(null, 1, 0, null, null, null) | ||
); | ||
Assert.assertFalse(optional.isPresent()); | ||
} | ||
} |
This file contains 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 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