You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2020/04/21 20:31:05 UTC

[GitHub] [druid] clintropolis commented on a change in pull request #9733: Introduce CompositeQueryLaningStrategy

clintropolis commented on a change in pull request #9733:
URL: https://github.com/apache/druid/pull/9733#discussion_r412432952



##########
File path: server/src/main/java/org/apache/druid/server/scheduling/CompositeQueryLaningStrategy.java
##########
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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 org.apache.druid.server.scheduling;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
+import it.unimi.dsi.fastutil.objects.Object2IntMap;
+import org.apache.druid.client.SegmentServerSelector;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryPlus;
+import org.apache.druid.server.QueryLaningStrategy;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * A laning strategy that allows you to combine multiple {@link QueryLaningStrategy}. To use this strategy,
+ * an admin must configure lane groups and their associated strategy.
+ *
+ * For example:
+ * druid.query.scheduler.laning.strategy=composite
+ * druid.query.scheduler.laning.strategies={"manualLanes": {"strategy":"manual", "lanes":{"one": 1}},"hiLoLanes": {"strategy":"hilo", "maxLowPercent":1}}
+ *
+ * This strategy does *not* support nesting a composite strategy within this strategy.
+ */
+public class CompositeQueryLaningStrategy implements QueryLaningStrategy
+{
+  @JsonProperty
+  private final Map<String, QueryLaningStrategy> strategies;
+
+  @JsonCreator
+  public CompositeQueryLaningStrategy(@JsonProperty("strategies") Map<String, QueryLaningStrategy> strategies)
+  {
+    this.strategies = Preconditions.checkNotNull(strategies, "strategies must be set.");
+    Preconditions.checkArgument(!strategies.isEmpty(), "strategies must define at least one strategy.");
+    Preconditions.checkArgument(
+        strategies.values().stream().noneMatch(s -> s instanceof CompositeQueryLaningStrategy),
+        "strategies can not contain a composite strategy."
+    );
+  }
+
+  @Override
+  public Object2IntMap<String> getLaneLimits(int totalLimit)
+  {
+    Object2IntArrayMap<String> laneLimits = new Object2IntArrayMap<>();
+    for (QueryLaningStrategy strategy : strategies.values()) {
+      laneLimits.putAll(strategy.getLaneLimits(totalLimit));
+    }
+    return laneLimits;
+  }
+
+  @Override
+  public <T> Optional<String> computeLane(QueryPlus<T> query, Set<SegmentServerSelector> segments)
+  {
+    String laneStrategyKey = QueryContexts.getCompositeLaneStrategy(query.getQuery());

Review comment:
       I think this laning strategy would be more practically useful, perhaps even beyond integration tests, if it just went through all of the available strategies and checked if any apply, calling `computeLane` until one one spits up a non-empty lane, or empty if none match. This would require having some sense of order to the available strategies though so it is controllable by the operator, so probably passing a list of strategies instead of a map.
   
   I think as this currently is it probably won't be very useful beyond integration tests, because its entirely manual. That said i guess it does give a bit more direct control over what exactly happens in an integration test, but I think maybe the flexibility of the other approach and possible utility beyond tests makes the trade-offs worth it. What do you think?

##########
File path: server/src/main/java/org/apache/druid/server/scheduling/CompositeQueryLaningStrategy.java
##########
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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 org.apache.druid.server.scheduling;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
+import it.unimi.dsi.fastutil.objects.Object2IntMap;
+import org.apache.druid.client.SegmentServerSelector;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryPlus;
+import org.apache.druid.server.QueryLaningStrategy;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * A laning strategy that allows you to combine multiple {@link QueryLaningStrategy}. To use this strategy,
+ * an admin must configure lane groups and their associated strategy.
+ *
+ * For example:
+ * druid.query.scheduler.laning.strategy=composite
+ * druid.query.scheduler.laning.strategies={"manualLanes": {"strategy":"manual", "lanes":{"one": 1}},"hiLoLanes": {"strategy":"hilo", "maxLowPercent":1}}
+ *
+ * This strategy does *not* support nesting a composite strategy within this strategy.
+ */
+public class CompositeQueryLaningStrategy implements QueryLaningStrategy
+{
+  @JsonProperty
+  private final Map<String, QueryLaningStrategy> strategies;
+
+  @JsonCreator
+  public CompositeQueryLaningStrategy(@JsonProperty("strategies") Map<String, QueryLaningStrategy> strategies)
+  {
+    this.strategies = Preconditions.checkNotNull(strategies, "strategies must be set.");
+    Preconditions.checkArgument(!strategies.isEmpty(), "strategies must define at least one strategy.");
+    Preconditions.checkArgument(
+        strategies.values().stream().noneMatch(s -> s instanceof CompositeQueryLaningStrategy),
+        "strategies can not contain a composite strategy."
+    );
+  }
+
+  @Override
+  public Object2IntMap<String> getLaneLimits(int totalLimit)
+  {
+    Object2IntArrayMap<String> laneLimits = new Object2IntArrayMap<>();
+    for (QueryLaningStrategy strategy : strategies.values()) {
+      laneLimits.putAll(strategy.getLaneLimits(totalLimit));

Review comment:
       you should maybe check for and fail on conflicting lane names so it doesn't end up with unexpected runtime behavior

##########
File path: server/src/test/java/org/apache/druid/server/scheduling/CompositeQueryLaningStrategyTest.java
##########
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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 org.apache.druid.server.scheduling;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
+import it.unimi.dsi.fastutil.objects.Object2IntMap;
+import org.apache.druid.client.SegmentServerSelector;
+import org.apache.druid.query.Query;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryPlus;
+import org.apache.druid.server.QueryLaningStrategy;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+@RunWith(MockitoJUnitRunner.class)
+public class CompositeQueryLaningStrategyTest
+{
+  private static final int TOTAL_LIMIT = 1000;
+  private static final String STRATEGY_ONE_LANE_ONE = "s1-l1";
+  private static final String STRATEGY_ONE_LANE_TWO = "s1-l2";
+  private static final String STRATEGY_ONE_LANE_THREE = "s1-l3";
+  private static final String STRATEGY_TWO_LANE_ONE = "s2-l1";
+  private static final String STRATEGY_TWO_LANE_TWO = "s2-l2";
+  private static final List<String> ALL_LANES = ImmutableList.of(
+      STRATEGY_ONE_LANE_ONE,
+      STRATEGY_ONE_LANE_TWO,
+      STRATEGY_ONE_LANE_THREE,
+      STRATEGY_TWO_LANE_ONE,
+      STRATEGY_TWO_LANE_TWO
+  );
+  private static final Object2IntMap<String> STRATEGY_ONE_LIMITS = new Object2IntArrayMap<>();
+  private static final Object2IntMap<String> STRATEGY_TWO_LIMITS = new Object2IntArrayMap<>();
+  private static final Optional<String> STRATEGY_ONE_COMPUTED_LANE = Optional.of("STRATEGY_ONE_COMPUTED_LANE");
+  private static final Optional<String> STRATEGY_TWO_COMPUTED_LANE = Optional.of("STRATEGY_TWO_COMPUTED_LANE");
+
+  static {
+    STRATEGY_ONE_LIMITS.put(STRATEGY_ONE_LANE_ONE, 2);
+    STRATEGY_ONE_LIMITS.put(STRATEGY_ONE_LANE_TWO, 3);
+    STRATEGY_ONE_LIMITS.put(STRATEGY_ONE_LANE_THREE, 4);
+    STRATEGY_TWO_LIMITS.put(STRATEGY_TWO_LANE_ONE, 30);
+    STRATEGY_TWO_LIMITS.put(STRATEGY_TWO_LANE_TWO, 40);
+  }
+
+  @Mock
+  private CompositeQueryLaningStrategy subCompositeStrategy;
+  @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+  private QueryLaningStrategy strategyOne;
+  @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+  private QueryLaningStrategy strategyTwo;
+  @Mock
+  private Query<String> query;
+  @Mock
+  private Set<SegmentServerSelector> segments;
+
+  private QueryPlus<String> queryPlus;
+  private CompositeQueryLaningStrategy target;
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Before
+  public void setup()
+  {
+    queryPlus = QueryPlus.wrap(query);
+    Mockito.doReturn(STRATEGY_ONE_COMPUTED_LANE).when(strategyOne).computeLane(queryPlus, segments);
+    Mockito.doReturn(STRATEGY_TWO_COMPUTED_LANE).when(strategyTwo).computeLane(queryPlus, segments);
+    mockLimits();
+    target = new CompositeQueryLaningStrategy(
+        ImmutableMap.of(
+            "one", strategyOne,
+            "two", strategyTwo
+        )
+    );
+  }
+
+  @Test
+  public void initStrategiesMustBeSet()
+  {
+    expectedException.expect(NullPointerException.class);
+    expectedException.expectMessage("strategies must be set.");
+    target = new CompositeQueryLaningStrategy(null);
+  }
+
+  @Test
+  public void initMinimumNumberOfExpectedStrategies()
+  {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("strategies must define at least one strategy.");
+    target = new CompositeQueryLaningStrategy(Collections.emptyMap());
+  }
+
+  @Test
+  public void initNestedCompositeQueryLaningStrategiesAreNotAllowed()
+  {
+    expectedException.expect(IllegalArgumentException.class);

Review comment:
       if you make the change i suggested and accept a list and exhaust all possible laning strategies in an attempt to find one, then you can relax this restriction as well, allowing a truly mad operator to have any number of nested strategies.

##########
File path: processing/src/main/java/org/apache/druid/query/QueryContexts.java
##########
@@ -221,6 +222,15 @@ public String toString()
     return (String) query.getContextValue(LANE_KEY);
   }
 
+  /**
+   * Returns the laning strategy specified in the query context.
+   * This strategy only applies if Druid is running with CompositeQueryLaningStrategy.
+   */
+  public static <T> String getCompositeLaneStrategy(Query<T> query)
+  {
+    return (String) query.getContextValue(COMPOSITE_LANE_STRATEGY_KEY);

Review comment:
       If this is only for integration tests, I don't think it is worth adding to `QueryContexts`, and these constants should belong in `CompositeLaningStrategy` and just use `query.getContextValue` directly there. Or, if you do the other suggestion of exhausting all strategies to find a lane, then this can be removed entirely.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org