You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by pr...@apache.org on 2015/08/11 14:21:08 UTC

[45/50] [abbrv] incubator-lens git commit: LENS-631: Using Query Cost instead of priority field for comparing queries in priority blocking queue

LENS-631: Using Query Cost instead of priority field for comparing queries in priority blocking queue


Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/cbdbea8f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/cbdbea8f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/cbdbea8f

Branch: refs/heads/current-release-line
Commit: cbdbea8f283f14679bcfc71f9ca4363029fcdfd0
Parents: 4ff8ac4
Author: Himanshu Gahlaut <ga...@gmail.com>
Authored: Mon Aug 3 17:56:11 2015 +0530
Committer: Himanshu Gahlaut <hi...@apache.org>
Committed: Mon Aug 10 17:28:36 2015 +0530

----------------------------------------------------------------------
 .../lens/server/api/query/QueryContext.java     | 17 +---
 .../query/QueryContextPriorityComparator.java   | 48 ++++++++++
 .../server/query/QueryExecutionServiceImpl.java |  3 +-
 .../server/util/FairPriorityBlockingQueue.java  | 12 ++-
 .../QueryContextPriorityComparatorTest.java     | 99 ++++++++++++++++++++
 5 files changed, 161 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/cbdbea8f/lens-server-api/src/main/java/org/apache/lens/server/api/query/QueryContext.java
----------------------------------------------------------------------
diff --git a/lens-server-api/src/main/java/org/apache/lens/server/api/query/QueryContext.java b/lens-server-api/src/main/java/org/apache/lens/server/api/query/QueryContext.java
index 86e3fa3..beaa72f 100644
--- a/lens-server-api/src/main/java/org/apache/lens/server/api/query/QueryContext.java
+++ b/lens-server-api/src/main/java/org/apache/lens/server/api/query/QueryContext.java
@@ -46,7 +46,7 @@ import lombok.Setter;
 /**
  * The Class QueryContext.
  */
-public class QueryContext extends AbstractQueryContext implements Comparable<QueryContext> {
+public class QueryContext extends AbstractQueryContext {
 
   /**
    * The Constant serialVersionUID.
@@ -270,21 +270,6 @@ public class QueryContext extends AbstractQueryContext implements Comparable<Que
     return conf;
   }
 
-  /*
-   * (non-Javadoc)
-   *
-   * @see java.lang.Comparable#compareTo(java.lang.Object)
-   */
-  @Override
-  public int compareTo(QueryContext other) {
-    int pcomp = this.priority.compareTo(other.priority);
-    if (pcomp == 0) {
-      return (int) (this.submissionTime - other.submissionTime);
-    } else {
-      return pcomp;
-    }
-  }
-
   /**
    * Update conf.
    *

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/cbdbea8f/lens-server/src/main/java/org/apache/lens/server/query/QueryContextPriorityComparator.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/query/QueryContextPriorityComparator.java b/lens-server/src/main/java/org/apache/lens/server/query/QueryContextPriorityComparator.java
new file mode 100644
index 0000000..b25ad79
--- /dev/null
+++ b/lens-server/src/main/java/org/apache/lens/server/query/QueryContextPriorityComparator.java
@@ -0,0 +1,48 @@
+/*
+ * 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.lens.server.query;
+
+import java.util.Comparator;
+
+import org.apache.lens.server.api.query.QueryContext;
+import org.apache.lens.server.api.query.cost.QueryCost;
+
+public class QueryContextPriorityComparator implements Comparator<QueryContext> {
+
+  @Override
+  public int compare(final QueryContext o1, final QueryContext o2) {
+
+    /* Lowest Query Cost First */
+
+    QueryCost qcO1 = o1.getSelectedDriverQueryCost();
+    QueryCost qcO2 = o2.getSelectedDriverQueryCost();
+
+    int result = qcO1.compareTo(qcO2);
+    if (result != 0) {
+      return result;
+    }
+
+    /* FIFO on submissionTime when query cost is same */
+
+    Long submitTimeO1 = new Long(o1.getSubmissionTime());
+    Long submitTimeO2 = new Long(o2.getSubmissionTime());
+    return submitTimeO1.compareTo(submitTimeO2);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/cbdbea8f/lens-server/src/main/java/org/apache/lens/server/query/QueryExecutionServiceImpl.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/query/QueryExecutionServiceImpl.java b/lens-server/src/main/java/org/apache/lens/server/query/QueryExecutionServiceImpl.java
index 2e96451..c773534 100644
--- a/lens-server/src/main/java/org/apache/lens/server/query/QueryExecutionServiceImpl.java
+++ b/lens-server/src/main/java/org/apache/lens/server/query/QueryExecutionServiceImpl.java
@@ -143,7 +143,8 @@ public class QueryExecutionServiceImpl extends BaseLensService implements QueryE
   /**
    * The accepted queries.
    */
-  private FairPriorityBlockingQueue<QueryContext> queuedQueries = new FairPriorityBlockingQueue<QueryContext>();
+  private FairPriorityBlockingQueue<QueryContext> queuedQueries
+    = new FairPriorityBlockingQueue<QueryContext>(new QueryContextPriorityComparator());
 
   /**
    * The launched queries.

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/cbdbea8f/lens-server/src/main/java/org/apache/lens/server/util/FairPriorityBlockingQueue.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/util/FairPriorityBlockingQueue.java b/lens-server/src/main/java/org/apache/lens/server/util/FairPriorityBlockingQueue.java
index 1175f65..b2ebf5b 100644
--- a/lens-server/src/main/java/org/apache/lens/server/util/FairPriorityBlockingQueue.java
+++ b/lens-server/src/main/java/org/apache/lens/server/util/FairPriorityBlockingQueue.java
@@ -20,10 +20,13 @@
 package org.apache.lens.server.util;
 
 import java.util.Collection;
+import java.util.Comparator;
 import java.util.concurrent.PriorityBlockingQueue;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.ReentrantLock;
 
+import lombok.NonNull;
+
 /**
  *
  * A priority blocking queue in which an element is not allowed to be removed from head while a bulk addAll operation
@@ -32,11 +35,18 @@ import java.util.concurrent.locks.ReentrantLock;
  */
 public class FairPriorityBlockingQueue<E> {
 
-  private final PriorityBlockingQueue<E> priorityBlockingQueue = new PriorityBlockingQueue<E>();
+  /* PriorityBlockingQueue#DEFAULT_INITIAL_CAPACITY is 11 */
+
+  private static final int DEFAULT_INITIAL_CAPACITY = 11;
+
+  private final PriorityBlockingQueue<E> priorityBlockingQueue;
   private final Object fairnessLock = new Object();
   private final ReentrantLock conditionalWaitLock = new ReentrantLock();
   private final Condition notEmpty = conditionalWaitLock.newCondition();
 
+  public FairPriorityBlockingQueue(@NonNull final Comparator<? super E> comparator) {
+    this.priorityBlockingQueue = new PriorityBlockingQueue<E>(DEFAULT_INITIAL_CAPACITY, comparator);
+  }
   /**
    *
    * take is implemented by using poll and a fairnessLock to synchronize removal from head of queue with bulk addAll

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/cbdbea8f/lens-server/src/test/java/org/apache/lens/server/query/QueryContextPriorityComparatorTest.java
----------------------------------------------------------------------
diff --git a/lens-server/src/test/java/org/apache/lens/server/query/QueryContextPriorityComparatorTest.java b/lens-server/src/test/java/org/apache/lens/server/query/QueryContextPriorityComparatorTest.java
new file mode 100644
index 0000000..5ab9144
--- /dev/null
+++ b/lens-server/src/test/java/org/apache/lens/server/query/QueryContextPriorityComparatorTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.lens.server.query;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+
+import java.util.Comparator;
+
+import org.apache.lens.server.api.query.QueryContext;
+import org.apache.lens.server.api.query.cost.QueryCost;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+public class QueryContextPriorityComparatorTest {
+
+  private final Comparator<QueryContext> pqComparator = new QueryContextPriorityComparator();
+
+  @DataProvider
+  public Object[][] dpQueryCostCompare() {
+    return new Object[][] {
+      /* Query cost of query1 is less than query cost of query2 */
+      {-1, -1},
+      /* Query cost of query1 is more than query cost of query2 */
+      {1, 1},
+    };
+  }
+
+  @Test(dataProvider = "dpQueryCostCompare")
+  public void testCompareOnQueryCost(final int resultOfQueryCostCompare, final int expectedResult) {
+
+    QueryContext query1 = mock(QueryContext.class);
+    QueryCost qcO1 = mock(QueryCost.class);
+    when(query1.getSelectedDriverQueryCost()).thenReturn(qcO1);
+
+    QueryContext query2 = mock(QueryContext.class);
+    QueryCost qcO2 = mock(QueryCost.class);
+    when(query2.getSelectedDriverQueryCost()).thenReturn(qcO2);
+
+    when(qcO1.compareTo(qcO2)).thenReturn(resultOfQueryCostCompare);
+    assertEquals(pqComparator.compare(query1, query2), expectedResult);
+  }
+
+  @DataProvider
+  public Object[][] dpSubmitTimeCompare() {
+    return new Object[][] {
+      /* Submission Time of query1 is less than Submission Time of query2 */
+      {123, 125, -1},
+      /* Submission Time of query1 is more than Submission Time of query2 */
+      {125, 123, 1},
+      /* Submission Time of query1 is equal to Submission Time of query2 */
+      {123, 123, 0},
+      /* Boundary case: Submission Time of query1 is Long.MIN_VALUE and submission time of query2 Long.MAX_VALUE */
+      {Long.MIN_VALUE, Long.MAX_VALUE, -1},
+      /* Boundary case: Submission Time of query1 is Long.MAX_VALUE and submission time of query2 Long.MIN_VALUE */
+      {Long.MAX_VALUE, Long.MIN_VALUE, 1},
+      /* Submission Time of query1 and query2 is 0 */
+      {0, 0, 0},
+    };
+  }
+
+  @Test(dataProvider = "dpSubmitTimeCompare")
+  public void testCompareOnQuerySubmitTime(final long submitTimeQuery1, final long submitTimeQuery2,
+      final int expectedResult) {
+
+    QueryContext query1 = mock(QueryContext.class);
+    QueryCost qcO1 = mock(QueryCost.class);
+    when(query1.getSelectedDriverQueryCost()).thenReturn(qcO1);
+
+    QueryContext query2 = mock(QueryContext.class);
+    QueryCost qcO2 = mock(QueryCost.class);
+    when(query2.getSelectedDriverQueryCost()).thenReturn(qcO2);
+
+    when(qcO1.compareTo(qcO2)).thenReturn(0);
+    when(query1.getSubmissionTime()).thenReturn(submitTimeQuery1);
+    when(query2.getSubmissionTime()).thenReturn(submitTimeQuery2);
+
+    assertEquals(pqComparator.compare(query1, query2), expectedResult);
+  }
+}