Repository: lens Updated Branches: refs/heads/master 5faaf11b4 -> b36a02774
LENS-995 : QueryContextPriorityComparator should compare the priorities instead of cost Project: http://git-wip-us.apache.org/repos/asf/lens/repo Commit: http://git-wip-us.apache.org/repos/asf/lens/commit/b36a0277 Tree: http://git-wip-us.apache.org/repos/asf/lens/tree/b36a0277 Diff: http://git-wip-us.apache.org/repos/asf/lens/diff/b36a0277 Branch: refs/heads/master Commit: b36a027748413db07099837bbd7c62207b95c896 Parents: 5faaf11 Author: sushilmohanty <sushilmoha...@apache.org> Authored: Fri Apr 1 11:33:18 2016 +0530 Committer: sushilmohanty <sushilmoha...@apache.org> Committed: Fri Apr 1 11:33:18 2016 +0530 ---------------------------------------------------------------------- .../lens/server/api/LensConfConstants.java | 2 + .../lens/server/query/FIFOQueryComparator.java | 33 +++++ .../lens/server/query/QueryComparator.java | 28 +++++ .../query/QueryContextPriorityComparator.java | 48 -------- .../lens/server/query/QueryCostComparator.java | 39 ++++++ .../server/query/QueryExecutionServiceImpl.java | 31 ++++- .../server/query/QueryPriorityComparator.java | 38 ++++++ .../query/QueryContextComparatorTest.java | 121 +++++++++++++++++++ .../QueryContextPriorityComparatorTest.java | 99 --------------- .../collect/DefaultQueryCollectionTest.java | 34 +++++- .../server/query/collect/QueryCollectUtil.java | 25 +++- 11 files changed, 345 insertions(+), 153 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/lens-server-api/src/main/java/org/apache/lens/server/api/LensConfConstants.java ---------------------------------------------------------------------- diff --git a/lens-server-api/src/main/java/org/apache/lens/server/api/LensConfConstants.java b/lens-server-api/src/main/java/org/apache/lens/server/api/LensConfConstants.java index 724c868..23537cb 100644 --- a/lens-server-api/src/main/java/org/apache/lens/server/api/LensConfConstants.java +++ b/lens-server-api/src/main/java/org/apache/lens/server/api/LensConfConstants.java @@ -91,6 +91,8 @@ public final class LensConfConstants { public static final String MAX_SESSIONS_PER_USER = SERVER_PFX + "max.sessions.per.user"; + public static final String QUERY_COMPARATOR_CLASS = SERVER_PFX + "query.comparator.class"; + public static final Integer DEFAULT_MAX_SESSIONS_PER_USER = 10; /** http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/lens-server/src/main/java/org/apache/lens/server/query/FIFOQueryComparator.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/query/FIFOQueryComparator.java b/lens-server/src/main/java/org/apache/lens/server/query/FIFOQueryComparator.java new file mode 100644 index 0000000..75c1146 --- /dev/null +++ b/lens-server/src/main/java/org/apache/lens/server/query/FIFOQueryComparator.java @@ -0,0 +1,33 @@ +/* + * 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 org.apache.lens.server.api.query.QueryContext; + +public class FIFOQueryComparator implements QueryComparator { + + @Override + public int compare(QueryContext o1, QueryContext o2) { + + Long submitTimeO1 = o1.getSubmissionTime(); + Long submitTimeO2 = o2.getSubmissionTime(); + + return submitTimeO1.compareTo(submitTimeO2); + } +} http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/lens-server/src/main/java/org/apache/lens/server/query/QueryComparator.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/query/QueryComparator.java b/lens-server/src/main/java/org/apache/lens/server/query/QueryComparator.java new file mode 100644 index 0000000..67dda6b --- /dev/null +++ b/lens-server/src/main/java/org/apache/lens/server/query/QueryComparator.java @@ -0,0 +1,28 @@ +/* + * 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; + +public interface QueryComparator extends Comparator<QueryContext> { +} http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/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 deleted file mode 100644 index b25ad79..0000000 --- a/lens-server/src/main/java/org/apache/lens/server/query/QueryContextPriorityComparator.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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/lens/blob/b36a0277/lens-server/src/main/java/org/apache/lens/server/query/QueryCostComparator.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/query/QueryCostComparator.java b/lens-server/src/main/java/org/apache/lens/server/query/QueryCostComparator.java new file mode 100644 index 0000000..2702581 --- /dev/null +++ b/lens-server/src/main/java/org/apache/lens/server/query/QueryCostComparator.java @@ -0,0 +1,39 @@ +/* + * 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 org.apache.lens.server.api.query.QueryContext; +import org.apache.lens.server.api.query.cost.QueryCost; + +public class QueryCostComparator extends FIFOQueryComparator { + + @Override + public int compare(final QueryContext o1, final QueryContext o2) { + + QueryCost qcO1 = o1.getSelectedDriverQueryCost(); + QueryCost qcO2 = o2.getSelectedDriverQueryCost(); + + int result = qcO1.compareTo(qcO2); + if (result == 0) { + return super.compare(o1, o2); + } + return result; + } +} + http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/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 cf8a5f7..ad39028 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 @@ -137,8 +137,7 @@ public class QueryExecutionServiceImpl extends BaseLensService implements QueryE /** * The accepted queries. */ - private FairPriorityBlockingQueue<QueryContext> queuedQueries - = new FairPriorityBlockingQueue<QueryContext>(new QueryContextPriorityComparator()); + private FairPriorityBlockingQueue<QueryContext> queuedQueries; /** * The launched queries. @@ -223,6 +222,10 @@ public class QueryExecutionServiceImpl extends BaseLensService implements QueryE private DriverSelector driverSelector; /** + * The query comparator + */ + private QueryComparator queryComparator; + /** * The result sets. */ private Map<QueryHandle, LensResultSet> resultSets = new HashMap<QueryHandle, LensResultSet>(); @@ -366,6 +369,18 @@ public class QueryExecutionServiceImpl extends BaseLensService implements QueryE + DRIVER_SELECTOR_CLASS); } } + private void loadQueryComparator() throws LensException { + try { + Class<? extends QueryComparator> queryComparatorClass = conf.getClass(QUERY_COMPARATOR_CLASS, + QueryPriorityComparator.class, QueryComparator.class); + log.info("Using query comparator class: {}", queryComparatorClass.getCanonicalName()); + queryComparator = queryComparatorClass.newInstance(); + } catch (Exception e) { + throw new LensException("Couldn't instantiate query comparator class. Class name: " + + conf.get(QUERY_COMPARATOR_CLASS) + ". Please supply a valid value for " + + QUERY_COMPARATOR_CLASS); + } + } /** * Loads drivers for the configured Driver types in lens-site.xml @@ -1114,10 +1129,20 @@ public class QueryExecutionServiceImpl extends BaseLensService implements QueryE super.init(hiveConf); this.conf = hiveConf; + try { + loadQueryComparator(); + } catch (LensException e) { + log.error("Error while loading query comparator class", e); + throw new IllegalStateException("Could not load query comparator class", e); + } + this.launchedQueries = new ThreadSafeEstimatedQueryCollection(new DefaultEstimatedQueryCollection(new DefaultQueryCollection())); + this.queuedQueries + = new FairPriorityBlockingQueue<QueryContext>(queryComparator); + this.waitingQueries = new ThreadSafeEstimatedQueryCollection(new DefaultEstimatedQueryCollection( - new DefaultQueryCollection(new TreeSet<QueryContext>(new QueryContextPriorityComparator())))); + new DefaultQueryCollection(new TreeSet<QueryContext>(queryComparator)))); ImmutableSet<QueryLaunchingConstraint> queryConstraints = getImplementations( QUERY_LAUNCHING_CONSTRAINT_FACTORIES_KEY, hiveConf); http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/lens-server/src/main/java/org/apache/lens/server/query/QueryPriorityComparator.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/query/QueryPriorityComparator.java b/lens-server/src/main/java/org/apache/lens/server/query/QueryPriorityComparator.java new file mode 100644 index 0000000..2c6d904 --- /dev/null +++ b/lens-server/src/main/java/org/apache/lens/server/query/QueryPriorityComparator.java @@ -0,0 +1,38 @@ +/* + * 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 org.apache.lens.api.Priority; +import org.apache.lens.server.api.query.QueryContext; + +public class QueryPriorityComparator extends FIFOQueryComparator { + + @Override + public int compare(final QueryContext o1, final QueryContext o2) { + + Priority pO1 = o1.getPriority(); + Priority pO2 = o2.getPriority(); + + int result = pO1.compareTo(pO2); + if (result == 0) { + return super.compare(o1, o2); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/lens-server/src/test/java/org/apache/lens/server/query/QueryContextComparatorTest.java ---------------------------------------------------------------------- diff --git a/lens-server/src/test/java/org/apache/lens/server/query/QueryContextComparatorTest.java b/lens-server/src/test/java/org/apache/lens/server/query/QueryContextComparatorTest.java new file mode 100644 index 0000000..46adb7b --- /dev/null +++ b/lens-server/src/test/java/org/apache/lens/server/query/QueryContextComparatorTest.java @@ -0,0 +1,121 @@ +/* + * 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.api.Priority; +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 QueryContextComparatorTest { + + private final Comparator<QueryContext> priorityComparator = new QueryPriorityComparator(); + private final Comparator<QueryContext> costComparator = new QueryCostComparator(); + + + + @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(costComparator.compare(query1, query2), expectedResult); + } + + @Test + public void testCompareOnQueryPriority() { + + QueryContext query1 = mock(QueryContext.class); + when(query1.getPriority()).thenReturn(Priority.HIGH); // Ordinal = 1 + + QueryContext query2 = mock(QueryContext.class); + when(query2.getPriority()).thenReturn(Priority.LOW); // Ordinal = 3 + + assertEquals(priorityComparator.compare(query1, query2), -2); + } + + + @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); + when(query1.getPriority()).thenReturn(Priority.HIGH); + QueryCost qcO1 = mock(QueryCost.class); + when(query1.getSelectedDriverQueryCost()).thenReturn(qcO1); + + QueryContext query2 = mock(QueryContext.class); + when(query2.getPriority()).thenReturn(Priority.HIGH); + QueryCost qcO2 = mock(QueryCost.class); + when(query2.getSelectedDriverQueryCost()).thenReturn(qcO2); + + when(query1.getSubmissionTime()).thenReturn(submitTimeQuery1); + when(query2.getSubmissionTime()).thenReturn(submitTimeQuery2); + + // Cost and Priority both are same, hence the comparison should happen + // on query submission time + assertEquals(priorityComparator.compare(query1, query2), expectedResult); + assertEquals(costComparator.compare(query1, query2), expectedResult); + + } +} http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/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 deleted file mode 100644 index 5ab9144..0000000 --- a/lens-server/src/test/java/org/apache/lens/server/query/QueryContextPriorityComparatorTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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); - } -} http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/lens-server/src/test/java/org/apache/lens/server/query/collect/DefaultQueryCollectionTest.java ---------------------------------------------------------------------- diff --git a/lens-server/src/test/java/org/apache/lens/server/query/collect/DefaultQueryCollectionTest.java b/lens-server/src/test/java/org/apache/lens/server/query/collect/DefaultQueryCollectionTest.java index d4b6bdf..69baec9 100644 --- a/lens-server/src/test/java/org/apache/lens/server/query/collect/DefaultQueryCollectionTest.java +++ b/lens-server/src/test/java/org/apache/lens/server/query/collect/DefaultQueryCollectionTest.java @@ -27,6 +27,7 @@ import static org.testng.Assert.assertEquals; import java.util.Set; +import org.apache.lens.api.Priority; import org.apache.lens.server.api.query.QueryContext; import org.testng.annotations.DataProvider; @@ -84,7 +85,7 @@ public class DefaultQueryCollectionTest { } @Test(dataProvider = "dpQueryCosts") - public void testRemoveMethodMustChangeQueryIndices(final double[] queryCosts) { + public void testRemoveMethodMustChangeQueryCostIndices(final double[] queryCosts) { /* Initialization */ int numberOfQueries = queryCosts.length; @@ -108,6 +109,37 @@ public class DefaultQueryCollectionTest { } @Test + public void testRemoveMethodMustChangeQueryPriorityIndices() { + + Priority[] priorities = Priority.values(); + + /* Initialization */ + int numberOfQueries = priorities.length; + QueryCollection collection = createQueriesTreeSetWithQueryHandleAndPriorityStubbing(priorities, MOCK_HANDLE); + + QueryContext completedQuery = getMockedQueryFromQueries(collection.getQueries(), MOCK_HANDLE, 1); + QueryContext queuedQuery = getMockedQueryFromQueries(collection.getQueries(), MOCK_HANDLE, 5); + + /* Verification 1: Verifies that all queries were added into the collection*/ + assertEquals(collection.getQueriesCount(), numberOfQueries); + + /* Execution */ + collection.remove(completedQuery); + + /* Verification 2: Verifies that queries were removed from the collection */ + assertEquals(collection.getQueriesCount(), numberOfQueries - 1); + + /* Verification 3: Verifies that query index is decreased after removal of queries which were present before + them in the queries list */ + assertEquals(collection.getQueryIndex(queuedQuery).intValue(), 4); + + /* Verification 4: Verifies that query index is increasing when query with existing priority added to list */ + completedQuery.setPriority(Priority.NORMAL); + collection.add(completedQuery); + assertEquals(collection.getQueryIndex(queuedQuery).intValue(), 5); + } + + @Test public void testGetQueriesMustReturnCopyOfUnderlyingCollection() { /* Initialization */ http://git-wip-us.apache.org/repos/asf/lens/blob/b36a0277/lens-server/src/test/java/org/apache/lens/server/query/collect/QueryCollectUtil.java ---------------------------------------------------------------------- diff --git a/lens-server/src/test/java/org/apache/lens/server/query/collect/QueryCollectUtil.java b/lens-server/src/test/java/org/apache/lens/server/query/collect/QueryCollectUtil.java index 7789a4c..8b72c81 100644 --- a/lens-server/src/test/java/org/apache/lens/server/query/collect/QueryCollectUtil.java +++ b/lens-server/src/test/java/org/apache/lens/server/query/collect/QueryCollectUtil.java @@ -22,10 +22,12 @@ package org.apache.lens.server.query.collect; import static java.lang.reflect.Modifier.isPublic; import static java.lang.reflect.Modifier.isSynchronized; +import org.apache.lens.api.Priority; import org.apache.lens.api.query.QueryHandle; import org.apache.lens.server.api.query.QueryContext; import org.apache.lens.server.api.query.cost.FactPartitionBasedQueryCost; -import org.apache.lens.server.query.QueryContextPriorityComparator; +import org.apache.lens.server.query.QueryCostComparator; +import org.apache.lens.server.query.QueryPriorityComparator; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -85,7 +87,7 @@ public class QueryCollectUtil { public static QueryCollection createQueriesTreeSetWithQueryHandleAndCostStubbing(final double[] queryCosts, final String handlePrefix) { - TreeSet<QueryContext> mockQueries = new TreeSet<>(new QueryContextPriorityComparator()); + TreeSet<QueryContext> mockQueries = new TreeSet<>(new QueryCostComparator()); for (int index = 1; index <= queryCosts.length; ++index) { mockQueries.add(createQueryInstanceWithQueryHandleAndCostStubbing(handlePrefix, index, queryCosts[index - 1])); @@ -101,6 +103,25 @@ public class QueryCollectUtil { return mockQuery; } + public static QueryCollection createQueriesTreeSetWithQueryHandleAndPriorityStubbing(Priority[] priorities, + final String handlePrefix) { + TreeSet<QueryContext> mockQueries = new TreeSet<>(new QueryPriorityComparator()); + + for (int index = 1; index <= priorities.length; ++index) { + mockQueries.add(createQueryInstanceWithQueryHandleAndPriorityStubbing(handlePrefix, index, + priorities[index -1])); + } + return new DefaultQueryCollection(mockQueries); + } + + public static QueryContext createQueryInstanceWithQueryHandleAndPriorityStubbing(String handlePrefix, int index, + Priority priority) { + QueryContext mockQuery = mock(QueryContext.class); + when(mockQuery.getQueryHandle()).thenReturn(QueryHandle.fromString(handlePrefix + index)); + when(mockQuery.getPriority()).thenReturn(priority); + return mockQuery; + } + public static QueryContext getMockedQueryFromQueries(Set<QueryContext> queries, String mockHandle, int index) { Iterator iterator = queries.iterator(); while (iterator.hasNext()) {