[PATCH v2 5/7] memtx: enter small delayed free mode from snapshot iterator
Vladimir Davydov
vdavydov.dev at gmail.com
Mon Aug 19 19:53:18 MSK 2019
We must enable SMALL_DELAYED_FREE_MODE to safely use a memtx snapshot
iterator. Currently, we do that in checkpoint related callbacks, but if
we want to reuse snapshot iterators for other purposes, e.g. feeding
a read view to a newly joined replica, we better hide this code behind
snapshot iterator constructors.
---
src/box/memtx_engine.c | 24 ++++++++++++++++--------
src/box/memtx_engine.h | 23 +++++++++++++++++++++++
src/box/memtx_hash.c | 3 +++
src/box/memtx_tree.c | 3 +++
4 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/src/box/memtx_engine.c b/src/box/memtx_engine.c
index c18177db..ea197cad 100644
--- a/src/box/memtx_engine.c
+++ b/src/box/memtx_engine.c
@@ -610,10 +610,6 @@ memtx_engine_begin_checkpoint(struct engine *engine)
memtx->checkpoint = NULL;
return -1;
}
-
- /* increment snapshot version; set tuple deletion to delayed mode */
- memtx->snapshot_version++;
- small_alloc_setopt(&memtx->alloc, SMALL_DELAYED_FREE_MODE, true);
return 0;
}
@@ -661,8 +657,6 @@ memtx_engine_commit_checkpoint(struct engine *engine,
/* waitCheckpoint() must have been done. */
assert(!memtx->checkpoint->waiting_for_snap_thread);
- small_alloc_setopt(&memtx->alloc, SMALL_DELAYED_FREE_MODE, false);
-
if (!memtx->checkpoint->touch) {
int64_t lsn = vclock_sum(&memtx->checkpoint->vclock);
struct xdir *dir = &memtx->checkpoint->dir;
@@ -703,8 +697,6 @@ memtx_engine_abort_checkpoint(struct engine *engine)
memtx->checkpoint->waiting_for_snap_thread = false;
}
- small_alloc_setopt(&memtx->alloc, SMALL_DELAYED_FREE_MODE, false);
-
/** Remove garbage .inprogress file. */
const char *filename =
xdir_format_filename(&memtx->checkpoint->dir,
@@ -1014,6 +1006,22 @@ memtx_engine_set_max_tuple_size(struct memtx_engine *memtx, size_t max_size)
memtx->max_tuple_size = max_size;
}
+void
+memtx_enter_delayed_free_mode(struct memtx_engine *memtx)
+{
+ memtx->snapshot_version++;
+ if (memtx->delayed_free_mode++ == 0)
+ small_alloc_setopt(&memtx->alloc, SMALL_DELAYED_FREE_MODE, true);
+}
+
+void
+memtx_leave_delayed_free_mode(struct memtx_engine *memtx)
+{
+ assert(memtx->delayed_free_mode > 0);
+ if (--memtx->delayed_free_mode == 0)
+ small_alloc_setopt(&memtx->alloc, SMALL_DELAYED_FREE_MODE, false);
+}
+
struct tuple *
memtx_tuple_new(struct tuple_format *format, const char *data, const char *end)
{
diff --git a/src/box/memtx_engine.h b/src/box/memtx_engine.h
index ccb51678..c092f5d8 100644
--- a/src/box/memtx_engine.h
+++ b/src/box/memtx_engine.h
@@ -137,6 +137,12 @@ struct memtx_engine {
size_t max_tuple_size;
/** Incremented with each next snapshot. */
uint32_t snapshot_version;
+ /**
+ * Unless zero, freeing of tuples allocated before the last
+ * call to memtx_enter_delayed_free_mode() is delayed until
+ * memtx_leave_delayed_free_mode() is called.
+ */
+ uint32_t delayed_free_mode;
/** Memory pool for rtree index iterator. */
struct mempool rtree_iterator_pool;
/**
@@ -205,6 +211,23 @@ memtx_engine_set_memory(struct memtx_engine *memtx, size_t size);
void
memtx_engine_set_max_tuple_size(struct memtx_engine *memtx, size_t max_size);
+/**
+ * Enter tuple delayed free mode: tuple allocated before the call
+ * won't be freed until memtx_leave_delayed_free_mode() is called.
+ * This function is reentrant, meaning it's okay to call it multiple
+ * times from the same or different fibers - one just has to leave
+ * the delayed free mode the same amount of times then.
+ */
+void
+memtx_enter_delayed_free_mode(struct memtx_engine *memtx);
+
+/**
+ * Leave tuple delayed free mode. This function undoes the effect
+ * of memtx_enter_delayed_free_mode().
+ */
+void
+memtx_leave_delayed_free_mode(struct memtx_engine *memtx);
+
/** Allocate a memtx tuple. @sa tuple_new(). */
struct tuple *
memtx_tuple_new(struct tuple_format *format, const char *data, const char *end);
diff --git a/src/box/memtx_hash.c b/src/box/memtx_hash.c
index 920f1032..cdd531cb 100644
--- a/src/box/memtx_hash.c
+++ b/src/box/memtx_hash.c
@@ -414,6 +414,8 @@ hash_snapshot_iterator_free(struct snapshot_iterator *iterator)
assert(iterator->free == hash_snapshot_iterator_free);
struct hash_snapshot_iterator *it =
(struct hash_snapshot_iterator *) iterator;
+ memtx_leave_delayed_free_mode((struct memtx_engine *)
+ it->index->base.engine);
light_index_iterator_destroy(&it->index->hash_table, &it->iterator);
index_unref(&it->index->base);
free(iterator);
@@ -465,6 +467,7 @@ memtx_hash_index_create_snapshot_iterator(struct index *base)
index_ref(base);
light_index_iterator_begin(&index->hash_table, &it->iterator);
light_index_iterator_freeze(&index->hash_table, &it->iterator);
+ memtx_enter_delayed_free_mode((struct memtx_engine *)base->engine);
return (struct snapshot_iterator *) it;
}
diff --git a/src/box/memtx_tree.c b/src/box/memtx_tree.c
index 831a2715..e155ecd6 100644
--- a/src/box/memtx_tree.c
+++ b/src/box/memtx_tree.c
@@ -1215,6 +1215,8 @@ tree_snapshot_iterator_free(struct snapshot_iterator *iterator)
assert(iterator->free == tree_snapshot_iterator_free);
struct tree_snapshot_iterator *it =
(struct tree_snapshot_iterator *)iterator;
+ memtx_leave_delayed_free_mode((struct memtx_engine *)
+ it->index->base.engine);
memtx_tree_iterator_destroy(&it->index->tree, &it->tree_iterator);
index_unref(&it->index->base);
free(iterator);
@@ -1262,6 +1264,7 @@ memtx_tree_index_create_snapshot_iterator(struct index *base)
index_ref(base);
it->tree_iterator = memtx_tree_iterator_first(&index->tree);
memtx_tree_iterator_freeze(&index->tree, &it->tree_iterator);
+ memtx_enter_delayed_free_mode((struct memtx_engine *)base->engine);
return (struct snapshot_iterator *) it;
}
--
2.20.1
More information about the Tarantool-patches
mailing list