From 887accdba9f83a264d91a9cc6e5078f44547ab26 Mon Sep 17 00:00:00 2001
From: Roman Tsisyk <roman@tsisyk.com>
Date: Thu, 21 Apr 2016 18:07:09 +0300
Subject: [PATCH] Move recovery_bootstrap() to MemtxEngine

---
 src/box/box.cc          |  5 +----
 src/box/engine.cc       |  9 +++++++++
 src/box/engine.h        | 10 ++++++++++
 src/box/memtx_engine.cc | 24 ++++++++++++++++++++++++
 src/box/memtx_engine.h  |  1 +
 src/box/recovery.cc     | 20 --------------------
 src/box/recovery.h      |  3 ---
 7 files changed, 45 insertions(+), 27 deletions(-)

diff --git a/src/box/box.cc b/src/box/box.cc
index 6be2207c39..34f85014c3 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -1224,10 +1224,7 @@ bootstrap_cluster(void)
 	/* Add a surrogate server id for snapshot rows */
 	vclock_add_server(&recovery->vclock, 0);
 
-	/* Process bootstrap.bin */
-	struct xstream bootstrap_stream;
-	xstream_create(&bootstrap_stream, apply_row);
-	recovery_bootstrap(recovery, &bootstrap_stream);
+	engine_bootstrap();
 
 	uint32_t server_id = 1;
 
diff --git a/src/box/engine.cc b/src/box/engine.cc
index 866e529c51..6d68993ab1 100644
--- a/src/box/engine.cc
+++ b/src/box/engine.cc
@@ -268,6 +268,15 @@ engine_recover_to_checkpoint(int64_t checkpoint_id)
 	}
 }
 
+void
+engine_bootstrap()
+{
+	Engine *engine;
+	engine_foreach(engine) {
+		engine->bootstrap();
+	}
+}
+
 void
 engine_begin_join()
 {
diff --git a/src/box/engine.h b/src/box/engine.h
index 69b2f8b1bd..a54d0d99cd 100644
--- a/src/box/engine.h
+++ b/src/box/engine.h
@@ -134,6 +134,10 @@ class Engine {
 	 * binary log.
 	 */
 	virtual void endRecovery();
+	/**
+	 * Bootstrap an empty data directory
+	 */
+	virtual void bootstrap() {}
 	/**
 	 * Notify engine about a JOIN start (slave-side)
 	 */
@@ -240,6 +244,12 @@ engine_id(Handler *space)
 void
 engine_recover_to_checkpoint(int64_t checkpoint_id);
 
+/**
+ * Initialize an empty data directory
+ */
+void
+engine_bootstrap();
+
 /**
  * Called at the start of JOIN routine
  * on the replica.
diff --git a/src/box/memtx_engine.cc b/src/box/memtx_engine.cc
index 16ae1df7f5..009de80047 100644
--- a/src/box/memtx_engine.cc
+++ b/src/box/memtx_engine.cc
@@ -44,6 +44,7 @@
 #include "iproto_constants.h"
 #include "xrow.h"
 #include "xstream.h"
+#include "bootstrap.h"
 #include "cluster.h"
 #include "relay.h"
 #include "schema.h"
@@ -997,6 +998,29 @@ MemtxEngine::commit(struct txn *txn, int64_t signature)
 	}
 }
 
+void
+MemtxEngine::bootstrap()
+{
+	/* Recover from bootstrap.snap */
+	say_info("initializing an empty data directory");
+	struct xdir dir;
+	xdir_create(&dir, "", SNAP, &uuid_nil);
+	FILE *f = fmemopen((void *) &bootstrap_bin,
+			   sizeof(bootstrap_bin), "r");
+	struct xlog *snap = xlog_open_stream_xc(&dir, 0, f, "bootstrap.snap");
+	struct xlog_cursor cursor;
+	xlog_cursor_open(&cursor, snap);
+	auto guard = make_scoped_guard([&]{
+		xlog_cursor_close(&cursor);
+		xlog_close(snap);
+		xdir_destroy(&dir);
+	});
+
+	struct xrow_header row;
+	while (xlog_cursor_next_xc(&cursor, &row) == 0)
+		recoverSnapshotRow(&row);
+}
+
 void
 MemtxEngine::beginJoin()
 {
diff --git a/src/box/memtx_engine.h b/src/box/memtx_engine.h
index 6ca9201794..b8a2a4386f 100644
--- a/src/box/memtx_engine.h
+++ b/src/box/memtx_engine.h
@@ -59,6 +59,7 @@ struct MemtxEngine: public Engine {
 	virtual void rollback(struct txn *txn) override;
 	virtual void prepare(struct txn *txn) override;
 	virtual void commit(struct txn *txn, int64_t signature) override;
+	virtual void bootstrap() override;
 	virtual void beginJoin() override;
 	virtual void recoverToCheckpoint(int64_t lsn) override;
 	virtual void endRecovery() override;
diff --git a/src/box/recovery.cc b/src/box/recovery.cc
index f5faba506f..2bf68c3764 100644
--- a/src/box/recovery.cc
+++ b/src/box/recovery.cc
@@ -32,7 +32,6 @@
 
 #include "scoped_guard.h"
 #include "fiber.h"
-#include "bootstrap.h"
 #include "xlog.h"
 #include "xrow.h"
 #include "xstream.h"
@@ -247,25 +246,6 @@ recover_xlog(struct recovery *r, struct xstream *stream, struct xlog *l,
 	}
 }
 
-void
-recovery_bootstrap(struct recovery *r, struct xstream *stream)
-{
-	/* Recover from bootstrap.snap */
-	say_info("initializing an empty data directory");
-	struct xdir dir;
-	xdir_create(&dir, "", SNAP, &uuid_nil);
-	const char *filename = "bootstrap.snap";
-	FILE *f = fmemopen((void *) &bootstrap_bin,
-			   sizeof(bootstrap_bin), "r");
-	struct xlog *snap = xlog_open_stream_xc(&dir, 0, f, filename);
-	auto guard = make_scoped_guard([&]{
-		xlog_close(snap);
-		xdir_destroy(&dir);
-	});
-	/** The snapshot must have a EOF marker. */
-	recover_xlog(r, stream, snap, NULL);
-}
-
 /**
  * Find out if there are new .xlog files since the current
  * LSN, and read them all up.
diff --git a/src/box/recovery.h b/src/box/recovery.h
index 61a7b3c598..7efda2a35c 100644
--- a/src/box/recovery.h
+++ b/src/box/recovery.h
@@ -72,9 +72,6 @@ recovery_delete(struct recovery *r);
 void
 recovery_exit(struct recovery *r);
 
-void
-recovery_bootstrap(struct recovery *r, struct xstream *stream);
-
 void
 recovery_follow_local(struct recovery *r, struct xstream *stream,
 		      const char *name, ev_tstamp wal_dir_rescan_delay);
-- 
GitLab