Skip to content

Commit

Permalink
Allow realtime nodes to have multiple shards of the same datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
cheddar authored and Bingkun Guo committed Feb 29, 2016
1 parent 14a306c commit 68631d8
Showing 1 changed file with 71 additions and 26 deletions.
97 changes: 71 additions & 26 deletions server/src/main/java/io/druid/segment/realtime/RealtimeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.util.concurrent.MoreExecutors;
import com.google.inject.Inject;
import com.metamx.common.guava.CloseQuietly;
import com.metamx.common.guava.FunctionalIterable;
import com.metamx.common.lifecycle.LifecycleStart;
import com.metamx.common.lifecycle.LifecycleStop;
import com.metamx.emitter.EmittingLogger;
Expand All @@ -51,9 +52,12 @@
import io.druid.segment.realtime.plumber.Plumbers;
import org.joda.time.Interval;

import javax.annotation.Nullable;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -69,7 +73,7 @@ public class RealtimeManager implements QuerySegmentWalker
/**
* key=data source name,value=FireChiefs of all partition of that data source
*/
private final Map<String, List<FireChief>> chiefs;
private final Map<String, Map<Integer, FireChief>> chiefs;

@Inject
public RealtimeManager(
Expand All @@ -90,12 +94,12 @@ public void start() throws IOException
DataSchema schema = fireDepartment.getDataSchema();

final FireChief chief = new FireChief(fireDepartment);
List<FireChief> chiefs = this.chiefs.get(schema.getDataSource());
Map<Integer, FireChief> chiefs = this.chiefs.get(schema.getDataSource());
if (chiefs == null) {
chiefs = new ArrayList<FireChief>();
chiefs = new HashMap<Integer, FireChief>();
this.chiefs.put(schema.getDataSource(), chiefs);
}
chiefs.add(chief);
chiefs.put(fireDepartment.getTuningConfig().getShardSpec().getPartitionNum(), chief);

chief.setName(
String.format(
Expand All @@ -112,21 +116,21 @@ public void start() throws IOException
@LifecycleStop
public void stop()
{
for (Iterable<FireChief> chiefs : this.chiefs.values()) {
for (FireChief chief : chiefs) {
for (Map<Integer, FireChief> chiefs : this.chiefs.values()) {
for (FireChief chief : chiefs.values()) {
CloseQuietly.close(chief);
}
}
}

public FireDepartmentMetrics getMetrics(String datasource)
{
List<FireChief> chiefs = this.chiefs.get(datasource);
Map<Integer, FireChief> chiefs = this.chiefs.get(datasource);
if (chiefs == null) {
return null;
}
FireDepartmentMetrics snapshot = null;
for (FireChief chief : chiefs) {
for (FireChief chief : chiefs.values()) {
if (snapshot == null) {
snapshot = chief.getMetrics().snapshot();
} else {
Expand All @@ -139,30 +143,71 @@ public FireDepartmentMetrics getMetrics(String datasource)
@Override
public <T> QueryRunner<T> getQueryRunnerForIntervals(final Query<T> query, Iterable<Interval> intervals)
{
return getQueryRunnerForSegments(query, null);
final QueryRunnerFactory<T, Query<T>> factory = conglomerate.findFactory(query);
final Iterable<QueryRunner> runners;
final List<String> names = query.getDataSource().getNames();
runners = Iterables.transform(
names, new Function<String, QueryRunner>()
{
@Override
public QueryRunner<T> apply(String input)
{
Map<Integer, FireChief> chiefsOfDataSource = chiefs.get(input);
return chiefsOfDataSource == null ? new NoopQueryRunner() : factory.getToolchest().mergeResults(
factory.mergeRunners(
MoreExecutors.sameThreadExecutor(),
// Chaining query runners which wait on submitted chain query runners can make executor pools deadlock
Iterables.transform(
chiefsOfDataSource.values(), new Function<FireChief, QueryRunner<T>>()
{
@Override
public QueryRunner<T> apply(FireChief input)
{
return input.getQueryRunner(query);
}
}
)
)
);
}
}
);
return new UnionQueryRunner<>(
runners, conglomerate.findFactory(query).getToolchest()
);
}

@Override
public <T> QueryRunner<T> getQueryRunnerForSegments(final Query<T> query, Iterable<SegmentDescriptor> specs)
public <T> QueryRunner<T> getQueryRunnerForSegments(final Query<T> query, final Iterable<SegmentDescriptor> specs)
{
final QueryRunnerFactory<T, Query<T>> factory = conglomerate.findFactory(query);
List<QueryRunner> runners = new ArrayList();
for (String dataSource : query.getDataSource().getNames()) {
final Map<Integer, FireChief> dataSourceChiefs = RealtimeManager.this.chiefs.get(dataSource);
if (dataSourceChiefs == null) {
continue;
}

Iterable<FireChief> chiefsOfDataSource = chiefs.get(Iterables.getOnlyElement(query.getDataSource().getNames()));
return chiefsOfDataSource == null ? new NoopQueryRunner() : factory.getToolchest().mergeResults(
factory.mergeRunners(
MoreExecutors.sameThreadExecutor(),
// Chaining query runners which wait on submitted chain query runners can make executor pools deadlock
Iterables.transform(
chiefsOfDataSource, new Function<FireChief, QueryRunner<T>>()
{
@Override
public QueryRunner<T> apply(FireChief input)
{
return input.getQueryRunner(query);
}
}
)
)
QueryToolChest<T, Query<T>> toolchest = factory.getToolchest();
Iterable<QueryRunner<T>> subRunners = Iterables.transform(
specs,
new Function<SegmentDescriptor, QueryRunner<T>>()
{
@Nullable
@Override
public QueryRunner<T> apply(SegmentDescriptor spec)
{
FireChief retVal = dataSourceChiefs.get(spec.getPartitionNumber());
return retVal == null ? new NoopQueryRunner<T>() : retVal.getQueryRunner(query);
}
}
);
runners.add(
toolchest.mergeResults(factory.mergeRunners(MoreExecutors.sameThreadExecutor(), subRunners))
);
}
return new UnionQueryRunner<>(
runners, conglomerate.findFactory(query).getToolchest()
);
}

Expand Down

0 comments on commit 68631d8

Please sign in to comment.