You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@quickstep.apache.org by hb...@apache.org on 2016/07/01 04:41:34 UTC

[04/18] incubator-quickstep git commit: Created unit test for Learner

Created unit test for Learner

- API changes for probability store.
- Check if there's a probability entry for the query to be removed.
- Bug fix in remove query.


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

Branch: refs/heads/scheduler++
Commit: 307289cdd8eeb87ca2904f1dcec79e1df8f601a5
Parents: 3fbda4a
Author: Harshad Deshmukh <hb...@apache.org>
Authored: Thu Jun 23 23:03:33 2016 -0500
Committer: Harshad Deshmukh <hb...@apache.org>
Committed: Thu Jun 30 23:41:05 2016 -0500

----------------------------------------------------------------------
 query_execution/CMakeLists.txt             |  9 ++++
 query_execution/Learner.cpp                |  6 ++-
 query_execution/Learner.hpp                | 33 ++++++++++-----
 query_execution/ProbabilityStore.hpp       |  5 +++
 query_execution/tests/Learner_unittest.cpp | 55 +++++++++++++++++++++++++
 5 files changed, 96 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/307289cd/query_execution/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/query_execution/CMakeLists.txt b/query_execution/CMakeLists.txt
index cb0f815..3904185 100644
--- a/query_execution/CMakeLists.txt
+++ b/query_execution/CMakeLists.txt
@@ -268,6 +268,15 @@ if (ENABLE_DISTRIBUTED)
   add_test(BlockLocator_unittest BlockLocator_unittest)
 endif()
 
+add_executable(Learner_unittest
+  "${CMAKE_CURRENT_SOURCE_DIR}/tests/Learner_unittest.cpp")
+target_link_libraries(Learner_unittest
+                      gtest
+                      gtest_main
+                      quickstep_queryexecution_Learner
+                      quickstep_queryoptimizer_QueryHandle)
+add_test(Learner_unittest Learner_unittest) 
+
 add_executable(ProbabilityStore_unittest
   "${CMAKE_CURRENT_SOURCE_DIR}/tests/ProbabilityStore_unittest.cpp")
 target_link_libraries(ProbabilityStore_unittest

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/307289cd/query_execution/Learner.cpp
----------------------------------------------------------------------
diff --git a/query_execution/Learner.cpp b/query_execution/Learner.cpp
index 72c68f0..5d877b4 100644
--- a/query_execution/Learner.cpp
+++ b/query_execution/Learner.cpp
@@ -188,8 +188,10 @@ void Learner::initializeDefaultProbabilitiesForPriorityLevels() {
     priority_levels.emplace_back(priority_iter->first);
     numerators.emplace_back(priority_iter->first);
   }
-  probabilities_of_priority_levels_->addOrUpdateObjectsNewDenominator(
-      priority_levels, numerators, sum_priority_levels);
+  if (sum_priority_levels > 0) {
+    probabilities_of_priority_levels_->addOrUpdateObjectsNewDenominator(
+        priority_levels, numerators, sum_priority_levels);
+  }
 }
 
 }  // namespace quickstep

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/307289cd/query_execution/Learner.hpp
----------------------------------------------------------------------
diff --git a/query_execution/Learner.hpp b/query_execution/Learner.hpp
index 64120a7..9d51877 100644
--- a/query_execution/Learner.hpp
+++ b/query_execution/Learner.hpp
@@ -35,10 +35,10 @@
 
 namespace quickstep {
 
-DEFINE_int32(max_past_entries_learner,
+/*DECLARE_int32(max_past_entries_learner,
               10,
               "The maximum number of past WorkOrder execution statistics"
-              " entries for a query");
+              " entries for a query");*/
 /** \addtogroup QueryExecution
  *  @{
  */
@@ -49,6 +49,7 @@ class Learner {
    * @brief Constructor.
    **/
   Learner() {
+    probabilities_of_priority_levels_.reset(new ProbabilityStore());
   }
 
   void addCompletionFeedback(
@@ -67,7 +68,15 @@ class Learner {
     const std::size_t priority_level = getQueryPriority(query_id);
     auto stats_iter_mutable = getExecutionStatsIterMutable(query_id);
     execution_stats_[priority_level].erase(stats_iter_mutable);
-    current_probabilities_[priority_level]->removeObject(query_id);
+    DCHECK(current_probabilities_.find(priority_level) !=
+           current_probabilities_.end());
+    if (current_probabilities_[priority_level]->hasObject(query_id)) {
+      // We may have cases when a query doesn't produce any feedback message,
+      // therefore we may not have an entry for this query in the
+      // current_probabilities_[priority_level] ProbabilityStore.
+      current_probabilities_[priority_level]->removeObject(query_id);
+    }
+    query_id_to_priority_lookup_.erase(query_id);
     checkAndRemovePriorityLevel(priority_level);
     relearn();
   }
@@ -93,6 +102,10 @@ class Learner {
   // at this point.
   void updateProbabilitiesOfAllPriorityLevels(const std::size_t priority_level);
 
+  inline const std::size_t hasActiveQueries() const {
+    return !query_id_to_priority_lookup_.empty();
+  }
+
  private:
   /**
    * @brief Initialize the default probabilities for the queries.
@@ -111,12 +124,15 @@ class Learner {
    **/
   inline void initializePriorityLevelIfNotPresent(
       const std::size_t priority_level) {
-    if (isPriorityLevelPresent(priority_level)) {
+    if (!isPriorityLevelPresent(priority_level)) {
       current_probabilities_[priority_level].reset(new ProbabilityStore());
       // Calculate the default probability for the priority level here and use
       // it instead of 0.5 here.
       // TODO(harshad) - Correct this.
-      probabilities_of_priority_levels_->addOrUpdateObject(priority_level, 0);
+      /*const float new_denominator =
+          probabilities_of_priority_levels_[priority_level]->getDenominator();
+      probabilities_of_priority_levels_->addOrUpdateObjectNewDenominator(
+          priority_level, priority_level, new_denominator);*/
       execution_stats_[priority_level];
     }
   }
@@ -168,7 +184,8 @@ class Learner {
     execution_stats_[priority_level].emplace_back(
         query_id,
         std::unique_ptr<ExecutionStats>(
-            new ExecutionStats(FLAGS_max_past_entries_learner)));
+            // new ExecutionStats(FLAGS_max_past_entries_learner)));
+            new ExecutionStats(10)));
     // As we are initializing the query, we obviously haven't gotten any
     // feedback message for this query. Hence mark the following field as false.
     has_feedback_from_all_queries_[priority_level] = false;
@@ -251,10 +268,6 @@ class Learner {
     has_feedback_from_all_queries_[priority_level] = true;
   }
 
-  inline const std::size_t hasActiveQueries() const {
-    return !query_id_to_priority_lookup_.empty();
-  }
-
   /**
    * @brief Get the mean work order execution times for all the queries in
    *        a given priority level.

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/307289cd/query_execution/ProbabilityStore.hpp
----------------------------------------------------------------------
diff --git a/query_execution/ProbabilityStore.hpp b/query_execution/ProbabilityStore.hpp
index d31caa6..347df89 100644
--- a/query_execution/ProbabilityStore.hpp
+++ b/query_execution/ProbabilityStore.hpp
@@ -55,6 +55,11 @@ class ProbabilityStore {
     return common_denominator_;
   }
 
+  inline bool hasObject(const std::size_t property) const {
+    auto it = individual_probabilities_.find(property);
+    return (it != individual_probabilities_.end());
+  }
+
   /**
    * @brief Add individual (not cumulative) probability for a given object.
    *

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/307289cd/query_execution/tests/Learner_unittest.cpp
----------------------------------------------------------------------
diff --git a/query_execution/tests/Learner_unittest.cpp b/query_execution/tests/Learner_unittest.cpp
new file mode 100644
index 0000000..cab241a
--- /dev/null
+++ b/query_execution/tests/Learner_unittest.cpp
@@ -0,0 +1,55 @@
+/**
+ *   Copyright 2016, Quickstep Research Group, Computer Sciences Department,
+ *     University of Wisconsin\u2014Madison.
+ *
+ *   Licensed 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.
+ **/
+
+#include <memory>
+
+#include "gtest/gtest.h"
+
+#include "query_execution/Learner.hpp"
+#include "query_optimizer/QueryHandle.hpp"
+
+namespace quickstep {
+
+TEST(LearnerTest, AddQueryTest) {
+  Learner learner;
+  std::unique_ptr<QueryHandle> handle;
+  handle.reset(new QueryHandle(1, 1));
+
+  EXPECT_FALSE(learner.hasActiveQueries());
+  learner.addQuery(*handle);
+  EXPECT_TRUE(learner.hasActiveQueries());
+}
+
+TEST(LearnerTest, RemoveQueryTest) {
+  Learner learner;
+  std::unique_ptr<QueryHandle> handle;
+  handle.reset(new QueryHandle(1, 1));
+
+  EXPECT_FALSE(learner.hasActiveQueries());
+  learner.addQuery(*handle);
+  EXPECT_TRUE(learner.hasActiveQueries());
+  learner.removeQuery(handle->query_id());
+  EXPECT_FALSE(learner.hasActiveQueries());
+
+  handle.reset(new QueryHandle(2, 1));
+  learner.addQuery(*handle);
+  EXPECT_TRUE(learner.hasActiveQueries());
+  learner.removeQuery(handle->query_id());
+  EXPECT_FALSE(learner.hasActiveQueries());
+}
+
+}  // namespace quickstep