From f5c9c45384ec43734c0890dd875101defe6590bc Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 1 Dec 2017 14:34:47 +0900
Subject: [PATCH 1/4] Simple implement of local shapshot of dshash.

Add snapshot feature to DSHASH. This makes palloc'ed copy of
underlying DSA and returns unmodifiable DSHASH using the copied DSA.
---
 src/backend/lib/dshash.c     | 74 +++++++++++++++++++++++++++++++++++++++-----
 src/backend/utils/mmgr/dsa.c | 57 +++++++++++++++++++++++++++++++++-
 src/include/lib/dshash.h     |  1 +
 src/include/utils/dsa.h      |  1 +
 4 files changed, 124 insertions(+), 9 deletions(-)

diff --git a/src/backend/lib/dshash.c b/src/backend/lib/dshash.c
index dd87573..973a826 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		is_snapshot;	/* Is this hash is a local snapshot?*/
 };
 
 /* Given a pointer to an item, find the entry (user data) it holds. */
@@ -228,6 +229,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->is_snapshot = false;
 
 	/*
 	 * Set up the initial array of buckets.  Our initial size is the same as
@@ -279,6 +281,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->is_snapshot = false;
 	Assert(hash_table->control->magic == DSHASH_MAGIC);
 
 	/*
@@ -321,6 +324,15 @@ dshash_destroy(dshash_table *hash_table)
 	size_t		i;
 
 	Assert(hash_table->control->magic == DSHASH_MAGIC);
+
+	/* this is a local copy */
+	if (hash_table->is_snapshot)
+	{
+		pfree(hash_table->area);
+		pfree(hash_table);
+		return;
+	}
+
 	ensure_valid_bucket_pointers(hash_table);
 
 	/* Free all the entries. */
@@ -355,6 +367,29 @@ dshash_destroy(dshash_table *hash_table)
 }
 
 /*
+ * take a local snapshot of a dshash table
+ */
+dshash_table *
+dshash_take_snapshot(dshash_table *org_table, dsa_area *new_area)
+{
+	dshash_table *new_table;
+
+	if (org_table->is_snapshot)
+		elog(ERROR, "cannot make local copy of local dshash");
+
+	new_table = palloc(sizeof(dshash_table));
+
+	new_table->area = new_area;
+	new_table->params = org_table->params;
+	new_table->control = dsa_get_address(new_table->area,
+										 org_table->control->handle);
+	/* mark this dshash as a local copy */
+	new_table->is_snapshot = true;
+
+	return new_table;
+}
+
+/*
  * Get a handle that can be used by other processes to attach to this hash
  * table.
  */
@@ -392,15 +427,22 @@ dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
 	partition = PARTITION_FOR_HASH(hash);
 
 	Assert(hash_table->control->magic == DSHASH_MAGIC);
-	Assert(!hash_table->find_locked);
 
-	LWLockAcquire(PARTITION_LOCK(hash_table, partition),
-				  exclusive ? LW_EXCLUSIVE : LW_SHARED);
+	if (!hash_table->is_snapshot)
+	{
+		Assert(!hash_table->find_locked);
+		LWLockAcquire(PARTITION_LOCK(hash_table, partition),
+					  exclusive ? LW_EXCLUSIVE : LW_SHARED);
+	}
 	ensure_valid_bucket_pointers(hash_table);
 
 	/* Search the active bucket. */
 	item = find_in_bucket(hash_table, key, BUCKET_FOR_HASH(hash_table, hash));
 
+	/* don't lock if this is a local copy */
+	if (hash_table->is_snapshot)
+		return item ? ENTRY_FROM_ITEM(item) : NULL;
+
 	if (!item)
 	{
 		/* Not found. */
@@ -436,6 +478,9 @@ dshash_find_or_insert(dshash_table *hash_table,
 	dshash_partition *partition;
 	dshash_table_item *item;
 
+	if (hash_table->is_snapshot)
+		elog(ERROR, "cannot insert into local dshash");
+
 	hash = hash_key(hash_table, key);
 	partition_index = PARTITION_FOR_HASH(hash);
 	partition = &hash_table->control->partitions[partition_index];
@@ -505,6 +550,9 @@ dshash_delete_key(dshash_table *hash_table, const void *key)
 	size_t		partition;
 	bool		found;
 
+	if (hash_table->is_snapshot)
+		elog(ERROR, "cannot delete from a snapshot");
+
 	Assert(hash_table->control->magic == DSHASH_MAGIC);
 	Assert(!hash_table->find_locked);
 
@@ -545,6 +593,7 @@ dshash_delete_entry(dshash_table *hash_table, void *entry)
 	Assert(hash_table->control->magic == DSHASH_MAGIC);
 	Assert(hash_table->find_locked);
 	Assert(hash_table->find_exclusively_locked);
+	Assert(!hash_table->is_snapshot);
 	Assert(LWLockHeldByMeInMode(PARTITION_LOCK(hash_table, partition),
 								LW_EXCLUSIVE));
 
@@ -563,6 +612,9 @@ dshash_release_lock(dshash_table *hash_table, void *entry)
 	dshash_table_item *item = ITEM_FROM_ENTRY(entry);
 	size_t		partition_index = PARTITION_FOR_HASH(item->hash);
 
+	if (hash_table->is_snapshot)
+		return;
+
 	Assert(hash_table->control->magic == DSHASH_MAGIC);
 	Assert(hash_table->find_locked);
 	Assert(LWLockHeldByMeInMode(PARTITION_LOCK(hash_table, partition_index),
@@ -605,10 +657,13 @@ dshash_dump(dshash_table *hash_table)
 	Assert(hash_table->control->magic == DSHASH_MAGIC);
 	Assert(!hash_table->find_locked);
 
-	for (i = 0; i < DSHASH_NUM_PARTITIONS; ++i)
+	if (!hash_table->is_snapshot)
 	{
-		Assert(!LWLockHeldByMe(PARTITION_LOCK(hash_table, i)));
-		LWLockAcquire(PARTITION_LOCK(hash_table, i), LW_SHARED);
+		for (i = 0; i < DSHASH_NUM_PARTITIONS; ++i)
+		{
+			Assert(!LWLockHeldByMe(PARTITION_LOCK(hash_table, i)));
+			LWLockAcquire(PARTITION_LOCK(hash_table, i), LW_SHARED);
+		}
 	}
 
 	ensure_valid_bucket_pointers(hash_table);
@@ -643,8 +698,11 @@ dshash_dump(dshash_table *hash_table)
 		}
 	}
 
-	for (i = 0; i < DSHASH_NUM_PARTITIONS; ++i)
-		LWLockRelease(PARTITION_LOCK(hash_table, i));
+	if (!hash_table->is_snapshot)
+	{
+		for (i = 0; i < DSHASH_NUM_PARTITIONS; ++i)
+			LWLockRelease(PARTITION_LOCK(hash_table, i));
+	}
 }
 
 /*
diff --git a/src/backend/utils/mmgr/dsa.c b/src/backend/utils/mmgr/dsa.c
index fe62788..dd02147 100644
--- a/src/backend/utils/mmgr/dsa.c
+++ b/src/backend/utils/mmgr/dsa.c
@@ -319,6 +319,7 @@ typedef struct
 	bool		pinned;
 	/* The number of times that segments have been freed. */
 	Size		freed_segment_counter;
+	bool		is_snapshot;
 	/* The LWLock tranche ID. */
 	int			lwlock_tranche_id;
 	/* The general lock (protects everything except object pools). */
@@ -931,7 +932,8 @@ dsa_get_address(dsa_area *area, dsa_pointer dp)
 		return NULL;
 
 	/* Process any requests to detach from freed segments. */
-	check_for_freed_segments(area);
+	if (!area->control->is_snapshot)
+		check_for_freed_segments(area);
 
 	/* Break the dsa_pointer into its components. */
 	index = DSA_EXTRACT_SEGMENT_NUMBER(dp);
@@ -1232,6 +1234,7 @@ create_internal(void *place, size_t size,
 	control->high_segment_index = 0;
 	control->refcnt = 1;
 	control->freed_segment_counter = 0;
+	control->is_snapshot = false;
 	control->lwlock_tranche_id = tranche_id;
 
 	/*
@@ -2239,3 +2242,55 @@ check_for_freed_segments(dsa_area *area)
 		area->freed_segment_counter = freed_segment_counter;
 	}
 }
+
+/*
+ * Make a static local copy of this dsa area.
+ */
+dsa_area *
+dsa_take_snapshot(dsa_area *source_area)
+{
+	dsa_area   *area;
+	Size		size;
+	int i;
+	char 	   *mem;
+
+	/* allocate required size of memory */
+	size = sizeof(dsa_area);
+	size += sizeof(dsa_area_control);
+
+	LWLockAcquire(DSA_AREA_LOCK(source_area), LW_SHARED);
+	for (i = 0 ; i <= source_area->high_segment_index ; i++)
+		size += source_area->segment_maps[i].header->size;
+	mem = palloc(size);
+
+	area = (dsa_area *)mem;
+	mem += sizeof(dsa_area);
+	area->control = (dsa_area_control *)mem;
+	mem += sizeof(dsa_area_control);
+	memcpy(area->control, source_area->control, sizeof(dsa_area_control));
+	area->control->is_snapshot = true;
+	area->mapping_pinned = false;
+
+	/* Copy and connect the all segments */
+	for (i = 0 ; i <= source_area->high_segment_index ; i++)
+	{
+		dsa_segment_map *smap = &source_area->segment_maps[i];
+		dsa_segment_map *dmap = &area->segment_maps[i];
+
+		dmap->mapped_address = mem;
+		memcpy(dmap->mapped_address, smap->mapped_address, smap->header->size);
+		mem += smap->header->size;
+		dmap->header = (dsa_segment_header*) dmap->mapped_address;
+		dmap->header->magic = 0;
+		dmap->fpm = NULL;
+		dmap->pagemap = (dsa_pointer *)
+			(dmap->mapped_address + MAXALIGN(sizeof(dsa_area_control)) +
+			 MAXALIGN(sizeof(FreePageManager)));
+	}
+
+	area->high_segment_index = source_area->high_segment_index;
+	LWLockRelease(DSA_AREA_LOCK(source_area));
+
+	elog(LOG, "dsa_take_snapshot copied %lu bytes", size);
+	return area;
+}
diff --git a/src/include/lib/dshash.h b/src/include/lib/dshash.h
index 220553c..d8f48ed 100644
--- a/src/include/lib/dshash.h
+++ b/src/include/lib/dshash.h
@@ -70,6 +70,7 @@ extern dshash_table *dshash_attach(dsa_area *area,
 extern void dshash_detach(dshash_table *hash_table);
 extern dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table);
 extern void dshash_destroy(dshash_table *hash_table);
+extern dshash_table *dshash_take_snapshot(dshash_table *org_table, dsa_area *new_area);
 
 /* Finding, creating, deleting entries. */
 extern void *dshash_find(dshash_table *hash_table,
diff --git a/src/include/utils/dsa.h b/src/include/utils/dsa.h
index 516ef61..c10fb4a 100644
--- a/src/include/utils/dsa.h
+++ b/src/include/utils/dsa.h
@@ -121,5 +121,6 @@ extern void dsa_free(dsa_area *area, dsa_pointer dp);
 extern void *dsa_get_address(dsa_area *area, dsa_pointer dp);
 extern void dsa_trim(dsa_area *area);
 extern void dsa_dump(dsa_area *area);
+extern dsa_area *dsa_take_snapshot(dsa_area *source_area);
 
 #endif							/* DSA_H */
-- 
2.9.2

