From e208d9c2c584a80d109950feb8419a095d8bab05 Mon Sep 17 00:00:00 2001
From: Jonas Theis <mail@jonastheis.de>
Date: Mon, 16 Mar 2020 15:47:44 +0100
Subject: [PATCH] Add database plugin to call garbage collection and close DB
(#289)
* Add database plugin to call garbage collection and close DB on shutdown #284
* Adjust comment
---
packages/shutdown/order.go | 3 ++-
pluginmgr/core/plugins.go | 2 ++
plugins/database/plugin.go | 45 ++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+), 1 deletion(-)
create mode 100644 plugins/database/plugin.go
diff --git a/packages/shutdown/order.go b/packages/shutdown/order.go
index 199c0350..9815548b 100644
--- a/packages/shutdown/order.go
+++ b/packages/shutdown/order.go
@@ -1,7 +1,8 @@
package shutdown
const (
- ShutdownPriorityTangle = iota
+ ShutdownPriorityDatabase = iota
+ ShutdownPriorityTangle
ShutdownPriorityRemoteLog
ShutdownPriorityAnalysis
ShutdownPriorityMetrics
diff --git a/pluginmgr/core/plugins.go b/pluginmgr/core/plugins.go
index 6f4a5100..c7faaaf7 100644
--- a/pluginmgr/core/plugins.go
+++ b/pluginmgr/core/plugins.go
@@ -5,6 +5,7 @@ import (
"github.com/iotaledger/goshimmer/plugins/banner"
"github.com/iotaledger/goshimmer/plugins/cli"
"github.com/iotaledger/goshimmer/plugins/config"
+ "github.com/iotaledger/goshimmer/plugins/database"
"github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/gracefulshutdown"
"github.com/iotaledger/goshimmer/plugins/logger"
@@ -20,6 +21,7 @@ var PLUGINS = node.Plugins(
logger.PLUGIN,
cli.PLUGIN,
portcheck.PLUGIN,
+ database.PLUGIN,
autopeering.PLUGIN,
tangle.PLUGIN,
gossip.PLUGIN,
diff --git a/plugins/database/plugin.go b/plugins/database/plugin.go
new file mode 100644
index 00000000..3762d8f0
--- /dev/null
+++ b/plugins/database/plugin.go
@@ -0,0 +1,45 @@
+// database is a plugin that manages the badger database (e.g. garbage collection).
+package database
+
+import (
+ "time"
+
+ "github.com/iotaledger/hive.go/timeutil"
+
+ "github.com/iotaledger/goshimmer/packages/database"
+ "github.com/iotaledger/goshimmer/packages/shutdown"
+
+ "github.com/iotaledger/hive.go/daemon"
+ "github.com/iotaledger/hive.go/logger"
+ "github.com/iotaledger/hive.go/node"
+)
+
+const (
+ PLUGIN_NAME = "Database"
+)
+
+var (
+ PLUGIN = node.NewPlugin(PLUGIN_NAME, node.Enabled, configure, run)
+ log *logger.Logger
+)
+
+func configure(plugin *node.Plugin) {
+ log = logger.NewLogger(PLUGIN_NAME)
+
+ _ = database.GetBadgerInstance()
+}
+
+func run(plugin *node.Plugin) {
+ daemon.BackgroundWorker(PLUGIN_NAME+"_GC", func(shutdownSignal <-chan struct{}) {
+ timeutil.Ticker(func() {
+ database.CleanupBadgerInstance(log)
+ }, 5*time.Minute, shutdownSignal)
+ }, shutdown.ShutdownPriorityBadgerGarbageCollection)
+
+ daemon.BackgroundWorker(PLUGIN_NAME, func(shutdownSignal <-chan struct{}) {
+ <-shutdownSignal
+ log.Infof("Syncing database to disk...")
+ database.GetBadgerInstance().Close()
+ log.Infof("Syncing database to disk... done")
+ }, shutdown.ShutdownPriorityDatabase)
+}
--
GitLab