hawq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From h...@apache.org
Subject incubator-hawq git commit: HAWQ-453. Do not allocate query resource in prepare stage for prepared statement
Date Fri, 26 Feb 2016 02:43:44 GMT
Repository: incubator-hawq
Updated Branches:
  refs/heads/master c070c7c8d -> 5069b7b0f


HAWQ-453. Do not allocate query resource in prepare stage for prepared statement


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

Branch: refs/heads/master
Commit: 5069b7b0fdbb4de9230a63e4793fb55ef254f586
Parents: c070c7c
Author: Ruilong Huo <rhuo@pivotal.io>
Authored: Thu Feb 25 00:28:37 2016 -0800
Committer: Ruilong Huo <rhuo@pivotal.io>
Committed: Thu Feb 25 18:42:37 2016 -0800

----------------------------------------------------------------------
 src/backend/cdb/cdbdatalocality.c    | 58 ++++++++++++++++++++++---
 src/backend/executor/spi.c           | 72 ++++++++++++++++++++++++++++---
 src/backend/optimizer/plan/planner.c |  3 +-
 src/include/cdb/cdbdatalocality.h    |  2 +-
 src/include/executor/spi.h           |  7 +++
 5 files changed, 127 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/5069b7b0/src/backend/cdb/cdbdatalocality.c
----------------------------------------------------------------------
diff --git a/src/backend/cdb/cdbdatalocality.c b/src/backend/cdb/cdbdatalocality.c
index 0561d2e..9a5bedb 100644
--- a/src/backend/cdb/cdbdatalocality.c
+++ b/src/backend/cdb/cdbdatalocality.c
@@ -41,6 +41,7 @@
 #include "utils/tqual.h"
 #include "utils/memutils.h"
 #include "executor/execdesc.h"
+#include "executor/spi.h"
 #include "nodes/nodes.h"
 #include "nodes/parsenodes.h"
 #include "optimizer/walkers.h"
@@ -3959,10 +3960,11 @@ static void cleanup_allocation_algorithm(
 
 /*
  * calculate_planner_segment_num
+ * fixedVsegNum is used by PBE, since all the execute should use the same number of vsegs.
  */
 SplitAllocResult *
 calculate_planner_segment_num(Query *query, QueryResourceLife resourceLife,
-		List *fullRangeTable, GpPolicy *intoPolicy, int sliceNum) {
+		List *fullRangeTable, GpPolicy *intoPolicy, int sliceNum, int fixedVsegNum) {
 	SplitAllocResult *result = NULL;
 	QueryResource *resource = NULL;
 	QueryResourceParameters *resource_parameters = NULL;
@@ -4046,7 +4048,15 @@ calculate_planner_segment_num(Query *query, QueryResourceLife resourceLife,
 
 		/*use inherit resource*/
 		if (resourceLife == QRL_INHERIT) {
-			resource = AllocateResource(resourceLife, sliceNum, 0, 0, 0, NULL, 0);
+
+			if ( SPI_IsInPrepare() && (GetActiveQueryResource() == NULL) )
+			{
+				resource = NULL;
+			}
+			else
+			{
+				resource = AllocateResource(resourceLife, sliceNum, 0, 0, 0, NULL, 0);
+			}
 
 			saveQueryResourceParameters(
 							resource_parameters,  /* resource_parameters */
@@ -4197,6 +4207,11 @@ calculate_planner_segment_num(Query *query, QueryResourceLife resourceLife,
 				maxTargetSegmentNumber = enforce_virtual_segment_number;
 				minTargetSegmentNumber = enforce_virtual_segment_number;
 			}
+			/* in PBE mode, the execute should use the same vseg number. */
+			if(fixedVsegNum > 0 ){
+				maxTargetSegmentNumber = fixedVsegNum;
+				minTargetSegmentNumber = fixedVsegNum;
+			}
 			uint64_t before_rm_allocate_resource = gettime_microsec();
 
 			/* cost is use by RM to balance workload between hosts. the cost is at least one block
size*/
@@ -4204,9 +4219,40 @@ calculate_planner_segment_num(Query *query, QueryResourceLife resourceLife,
 			mincost <<= 20;
 			int64 queryCost = context.total_size < mincost ? mincost : context.total_size;
 			if (QRL_NONE != resourceLife) {
-				resource = AllocateResource(QRL_ONCE, sliceNum, queryCost,
-						maxTargetSegmentNumber, minTargetSegmentNumber,
-						context.host_context.hostnameVolInfos, context.host_context.size);
+
+				if (SPI_IsInPrepare())
+				{
+					resource = NULL;
+					/*
+					 * prepare need to get resource quota from RM
+					 * and pass quota(planner_segments) to Orca or Planner to generate plan
+					 * the following executes(in PBE) should reallocate the same number
+					 * of resources.
+					 */
+					uint32 seg_num;
+					uint32 seg_num_min;
+					uint32 seg_memory_mb;
+					double seg_core;
+
+					GetResourceQuota(maxTargetSegmentNumber,
+					                 minTargetSegmentNumber,
+					                 &seg_num,
+					                 &seg_num_min,
+					                 &seg_memory_mb,
+					                 &seg_core);
+
+					planner_segments = seg_num;
+					minTargetSegmentNumber = planner_segments;
+					maxTargetSegmentNumber = planner_segments;
+				}
+				else
+				{
+					resource = AllocateResource(QRL_ONCE, sliceNum, queryCost,
+					                            maxTargetSegmentNumber,
+					                            minTargetSegmentNumber,
+					                            context.host_context.hostnameVolInfos,
+					                            context.host_context.size);
+				}
 
 				saveQueryResourceParameters(
 								resource_parameters,                   /* resource_parameters */
@@ -4236,7 +4282,7 @@ calculate_planner_segment_num(Query *query, QueryResourceLife resourceLife,
 
 			if (resource == NULL) {
 				result->resource = NULL;
-				result->resource_parameters = NULL;
+				result->resource_parameters = resource_parameters;
 				result->alloc_results = NIL;
 				result->relsType = NIL;
 				result->planner_segments = planner_segments;

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/5069b7b0/src/backend/executor/spi.c
----------------------------------------------------------------------
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index 07812aa..8e7645c 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -46,6 +46,7 @@
 #include "cdb/memquota.h"
 #include "executor/nodeFunctionscan.h"
 #include "nodes/stack.h"
+#include "cdb/cdbdatalocality.h"
 
 extern char *savedSeqServerHost;
 extern int savedSeqServerPort;
@@ -74,6 +75,7 @@ static int	_SPI_curid = -1;
 
 static PGconn *_QD_conn = NULL; /* To call back to the QD for SQL execution */
 static char *_QD_currently_prepared_stmt = NULL;
+static int SPI_prepare_depth = 0;
 
 static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan);
 
@@ -106,6 +108,32 @@ static bool _SPI_checktuples(void);
 
 /* =================== interface functions =================== */
 
+bool SPI_IsInPrepare(void)
+{
+	if (SPI_prepare_depth > 0)
+	{
+		return true;
+	}
+	else if (SPI_prepare_depth < 0)
+	{
+		elog(ERROR, "Invalid SPI_prepare_depth %d while getting SPI prepare depth",
+		            SPI_prepare_depth);
+	}
+
+	return false;
+}
+
+void SPI_IncreasePrepareDepth(void)
+{
+	SPI_prepare_depth++;
+}
+
+void SPI_DecreasePrepareDepth(void)
+{
+	SPI_prepare_depth--;
+}
+
+
 int
 SPI_connect(void)
 {
@@ -566,6 +594,8 @@ SPI_prepare(const char *src, int nargs, Oid *argtypes)
 	_SPI_plan	plan;
 	_SPI_plan  *result;
 
+	SPI_IncreasePrepareDepth();
+
 	if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL))
 	{
 		SPI_result = SPI_ERROR_ARGUMENT;
@@ -591,9 +621,13 @@ SPI_prepare(const char *src, int nargs, Oid *argtypes)
 
 		/* copy plan to procedure context */
 		result = _SPI_copy_plan(&plan, _SPI_CPLAN_PROCXT);
+
+		SPI_DecreasePrepareDepth();
 	}
 	PG_CATCH();
 	{
+		SPI_DecreasePrepareDepth();
+
 		_SPI_end_call(true);
 		PG_RE_THROW();
 	}
@@ -1819,13 +1853,37 @@ _SPI_execute_plan(_SPI_plan * plan, Datum *Values, const char *Nulls,
 					     (stmt->resource == NULL) &&
 					     (stmt->resource_parameters != NULL) )
 					{
-						stmt->resource = AllocateResource(stmt->resource_parameters->life,
-						                        stmt->resource_parameters->slice_size,
-						                        stmt->resource_parameters->iobytes,
-						                        stmt->resource_parameters->max_target_segment_num,
-						                        stmt->resource_parameters->min_target_segment_num,
-						                        stmt->resource_parameters->vol_info,
-						                        stmt->resource_parameters->vol_info_size);
+						SplitAllocResult *allocResult = NULL;
+
+						/* If this is a parallel plan. */
+						if (stmt->planTree->dispatch == DISPATCH_PARALLEL)
+						{
+							/*
+							 * Now, we want to allocate resource.
+							 */
+							allocResult = calculate_planner_segment_num(queryTree,
+							                                            stmt->resource_parameters->life,
+							                                            stmt->rtable,
+							                                            stmt->intoPolicy,
+							                                            stmt->nMotionNodes + stmt->nInitPlans
+ 1,
+							                                            stmt->resource_parameters->min_target_segment_num);
+
+							Assert(allocResult);
+
+							if(stmt->resource !=NULL)
+							{
+								pfree(stmt->resource);
+							}
+							stmt->resource = allocResult->resource;
+							if(stmt->scantable_splits !=NULL)
+							{
+								list_free_deep(stmt->scantable_splits);
+							}
+							stmt->scantable_splits = allocResult->alloc_results;
+							stmt->planner_segments = allocResult->planner_segments;
+							stmt->datalocalityInfo = allocResult->datalocalityInfo;
+							pfree(allocResult);
+						}
 					}
 
 					originalStmt->resource = NULL;

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/5069b7b0/src/backend/optimizer/plan/planner.c
----------------------------------------------------------------------
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 824558b..c98ba49 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -495,7 +495,8 @@ resource_negotiator(Query *parse, int cursorOptions, ParamListInfo boundParams,
        */
       allocResult = calculate_planner_segment_num(my_parse, resourceLife,
                                           plannedstmt->rtable, plannedstmt->intoPolicy,
-                                          plannedstmt->nMotionNodes + plannedstmt->nInitPlans
+ 1);
+                                          plannedstmt->nMotionNodes + plannedstmt->nInitPlans
+ 1,
+                                          -1);
 
       Assert(allocResult);
 

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/5069b7b0/src/include/cdb/cdbdatalocality.h
----------------------------------------------------------------------
diff --git a/src/include/cdb/cdbdatalocality.h b/src/include/cdb/cdbdatalocality.h
index 95f2bd5..0cb93d4 100644
--- a/src/include/cdb/cdbdatalocality.h
+++ b/src/include/cdb/cdbdatalocality.h
@@ -86,7 +86,7 @@ void saveQueryResourceParameters(
  * we calculate the appropriate planner segment_num.
  */
 SplitAllocResult * calculate_planner_segment_num(Query *query, QueryResourceLife resourceLife,
-                                                List *rtable, GpPolicy *intoPolicy, int sliceNum);
+                                                List *rtable, GpPolicy *intoPolicy, int sliceNum,
int fixedVsegNum);
 
 FILE *fp;
 FILE *fpaoseg;

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/5069b7b0/src/include/executor/spi.h
----------------------------------------------------------------------
diff --git a/src/include/executor/spi.h b/src/include/executor/spi.h
index 867daf5..7b4ba1b 100644
--- a/src/include/executor/spi.h
+++ b/src/include/executor/spi.h
@@ -155,4 +155,11 @@ extern uint64 SPI_GetMemoryReservation(void);
 extern void SPI_ReserveMemory(uint64 mem_reserved);
 extern bool SPI_IsMemoryReserved(void);
 
+/**
+ * Query resource related routines.
+ */
+extern bool SPI_IsInPrepare(void);
+extern void SPI_IncreasePrepareCounter(void);
+extern void SPI_DecreasePrepareCounter(void);
+
 #endif   /* SPI_H */


Mime
View raw message