From 1738edcfed77f5cb709f3c200f1db2d2837d853d Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Wed, 3 Oct 2018 17:10:46 +0900
Subject: [PATCH 08/10] Allow delettion while dshash sequential scan

The previous version of dshash_seq_scan is not allow deletion while
scanning. This patch allows that in the same way with dynahash.
---
 src/backend/lib/dshash.c | 89 +++++++++++++++++++++++++++++++++++-------------
 src/include/lib/dshash.h |  4 ++-
 2 files changed, 68 insertions(+), 25 deletions(-)

diff --git a/src/backend/lib/dshash.c b/src/backend/lib/dshash.c
index 7584931515..e71d189ab6 100644
--- a/src/backend/lib/dshash.c
+++ b/src/backend/lib/dshash.c
@@ -112,6 +112,7 @@ struct dshash_table
 	size_t		size_log2;		/* log2(number of buckets) */
 	bool		find_locked;	/* Is any partition lock held by 'find'? */
 	bool		find_exclusively_locked;	/* ... exclusively? */
+	bool		seqscan_running;/* now under sequential scan */
 };
 
 /* Given a pointer to an item, find the entry (user data) it holds. */
@@ -153,6 +154,10 @@ struct dshash_table
 #define BUCKET_INDEX_FOR_PARTITION(partition, size_log2)	\
 	((partition) << NUM_SPLITS(size_log2))
 
+/* Choose partition based on bucket index. */
+#define PARTITION_FOR_BUCKET_INDEX(bucket_idx, size_log2)				\
+	((bucket_idx) >> NUM_SPLITS(size_log2))
+
 /* The head of the active bucket for a given hash value (lvalue). */
 #define BUCKET_FOR_HASH(hash_table, hash)								\
 	(hash_table->buckets[												\
@@ -228,6 +233,7 @@ dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
 
 	hash_table->find_locked = false;
 	hash_table->find_exclusively_locked = false;
+	hash_table->seqscan_running = false;
 
 	/*
 	 * Set up the initial array of buckets.  Our initial size is the same as
@@ -279,6 +285,7 @@ dshash_attach(dsa_area *area, const dshash_parameters *params,
 	hash_table->control = dsa_get_address(area, control);
 	hash_table->find_locked = false;
 	hash_table->find_exclusively_locked = false;
+	hash_table->seqscan_running = false;
 	Assert(hash_table->control->magic == DSHASH_MAGIC);
 
 	/*
@@ -599,9 +606,14 @@ dshash_delete_entry(dshash_table *hash_table, void *entry)
 								LW_EXCLUSIVE));
 
 	delete_item(hash_table, item);
-	hash_table->find_locked = false;
-	hash_table->find_exclusively_locked = false;
-	LWLockRelease(PARTITION_LOCK(hash_table, partition));
+
+	/* We need to keep partition lock while sequential scan */
+	if (!hash_table->seqscan_running)
+	{
+		hash_table->find_locked = false;
+		hash_table->find_exclusively_locked = false;
+		LWLockRelease(PARTITION_LOCK(hash_table, partition));
+	}
 }
 
 /*
@@ -618,6 +630,8 @@ dshash_release_lock(dshash_table *hash_table, void *entry)
 	Assert(LWLockHeldByMeInMode(PARTITION_LOCK(hash_table, partition_index),
 								hash_table->find_exclusively_locked
 								? LW_EXCLUSIVE : LW_SHARED));
+	/* don't allow during sequential scan */
+	Assert(!hash_table->seqscan_running);
 
 	hash_table->find_locked = false;
 	hash_table->find_exclusively_locked = false;
@@ -664,14 +678,20 @@ dshash_memhash(const void *v, size_t size, void *arg)
  */
 void
 dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table,
-				bool consistent)
+				bool consistent, bool exclusive)
 {
+	/* allowed at most one scan at once */
+	Assert(!hash_table->seqscan_running);
+
 	status->hash_table = hash_table;
 	status->curbucket = 0;
 	status->nbuckets = ((size_t) 1) << hash_table->control->size_log2;
 	status->curitem = NULL;
+	status->pnextitem = InvalidDsaPointer;
 	status->curpartition = -1;
 	status->consistent = consistent;
+	status->exclusive = exclusive;
+	hash_table->seqscan_running = true;
 
 	/*
 	 * Protect all partitions from modification if the caller wants a
@@ -685,7 +705,8 @@ dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table,
 		{
 			Assert(!LWLockHeldByMe(PARTITION_LOCK(hash_table, i)));
 
-			LWLockAcquire(PARTITION_LOCK(hash_table, i), LW_SHARED);
+			LWLockAcquire(PARTITION_LOCK(hash_table, i),
+						  exclusive ? LW_EXCLUSIVE : LW_SHARED);
 		}
 	}
 	ensure_valid_bucket_pointers(hash_table);
@@ -696,17 +717,29 @@ dshash_seq_next(dshash_seq_status *status)
 {
 	dsa_pointer next_item_pointer;
 
+	Assert(status->hash_table->seqscan_running);
 	if (status->curitem == NULL)
 	{
+		int partition;
+
 		Assert (status->curbucket == 0);
 		Assert(!status->hash_table->find_locked);
 
 		/* first shot. grab the first item. */
+		if (!status->consistent)
+		{
+			partition =
+				PARTITION_FOR_BUCKET_INDEX(status->curbucket,
+										   status->hash_table->size_log2);
+			LWLockAcquire(PARTITION_LOCK(status->hash_table, partition),
+						  status->exclusive ? LW_EXCLUSIVE : LW_SHARED);
+			status->curpartition = partition;
+		}
+		
 		next_item_pointer = status->hash_table->buckets[status->curbucket];
-		status->hash_table->find_locked = true;
 	}
 	else
-		next_item_pointer = status->curitem->next;
+		next_item_pointer = status->pnextitem;
 
 	/* Move to the next bucket if we finished the current bucket */
 	while (!DsaPointerIsValid(next_item_pointer))
@@ -717,42 +750,50 @@ dshash_seq_next(dshash_seq_status *status)
 			dshash_seq_term(status);
 			return NULL;
 		}
-		Assert(status->hash_table->find_locked);
 
-		next_item_pointer = status->hash_table->buckets[status->curbucket];
-
-		/*
-		 * we need a lock on the scanning partition even if the caller don't
-		 * requested a consistent snapshot.
-		 */
-		if (!status->consistent && DsaPointerIsValid(next_item_pointer))
+		/* Also move parititon lock if needed */
+		if (!status->consistent)
 		{
-			dshash_table_item  *item = dsa_get_address(status->hash_table->area,
-													   next_item_pointer);
-			int next_partition = PARTITION_FOR_HASH(item->hash);
+			int next_partition =
+				PARTITION_FOR_BUCKET_INDEX(status->curbucket,
+										   status->hash_table->size_log2);
+
+			/* Move lock along with partition for the bucket */
 			if (status->curpartition != next_partition)
 			{
-				if (status->curpartition >= 0)
-					LWLockRelease(PARTITION_LOCK(status->hash_table,
-												 status->curpartition));
+				LWLockRelease(PARTITION_LOCK(status->hash_table,
+											 status->curpartition));
 				LWLockAcquire(PARTITION_LOCK(status->hash_table,
 											 next_partition),
-							  LW_SHARED);
+							  status->exclusive ? LW_EXCLUSIVE : LW_SHARED);
 				status->curpartition = next_partition;
 			}
 		}
+
+		next_item_pointer = status->hash_table->buckets[status->curbucket];
 	}
 
 	status->curitem =
 		dsa_get_address(status->hash_table->area, next_item_pointer);
+	status->hash_table->find_locked = true;
+	status->hash_table->find_exclusively_locked = status->exclusive;
+
+	/*
+	 * This item can be deleted by the caller. Store the next item for the
+	 * next iteration for the occasion.
+	 */
+	status->pnextitem = status->curitem->next;
+
 	return ENTRY_FROM_ITEM(status->curitem);
 }
 
 void
 dshash_seq_term(dshash_seq_status *status)
 {
-	Assert(status->hash_table->find_locked);
+	Assert(status->hash_table->seqscan_running);
 	status->hash_table->find_locked = false;
+	status->hash_table->find_exclusively_locked = false;
+	status->hash_table->seqscan_running = false;
 
 	if (status->consistent)
 	{
@@ -773,7 +814,7 @@ dshash_get_num_entries(dshash_table *hash_table)
 	void *p;
 	int n = 0;
 
-	dshash_seq_init(&s, hash_table, false);
+	dshash_seq_init(&s, hash_table, false, false);
 	while ((p = dshash_seq_next(&s)) != NULL)
 		n++;
 
diff --git a/src/include/lib/dshash.h b/src/include/lib/dshash.h
index b207585eeb..1b3e01000b 100644
--- a/src/include/lib/dshash.h
+++ b/src/include/lib/dshash.h
@@ -71,8 +71,10 @@ typedef struct dshash_seq_status
 	int					curbucket;
 	int					nbuckets;
 	dshash_table_item  *curitem;
+	dsa_pointer			pnextitem;
 	int					curpartition;
 	bool				consistent;
+	bool				exclusive;
 } dshash_seq_status;
 
 /* Creating, sharing and destroying from hash tables. */
@@ -101,7 +103,7 @@ extern void dshash_release_lock(dshash_table *hash_table, void *entry);
 
 /* seq scan support */
 extern void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table,
-							bool exclusive);
+							bool consistent, bool exclusive);
 extern void *dshash_seq_next(dshash_seq_status *status);
 extern void dshash_seq_term(dshash_seq_status *status);
 extern int dshash_get_num_entries(dshash_table *hash_table);
-- 
2.16.3

