component: Add 'Snapshottable' component

This commit is contained in:
Tom Wiesing 2022-10-01 20:11:01 +02:00
parent 1dac09bc03
commit b6c36b5488
No known key found for this signature in database
7 changed files with 28 additions and 13 deletions

View file

@ -3,6 +3,7 @@ package component
import ( import (
"io" "io"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/tkw1536/goprogram/stream" "github.com/tkw1536/goprogram/stream"
) )
@ -14,11 +15,11 @@ type Backupable interface {
BackupName() string BackupName() string
// Backup backs up this component into the destination path path // Backup backs up this component into the destination path path
Backup(context BackupContext) error Backup(context StagingContext) error
} }
// BackupContext is the context for backups // StagingContext represents a context for [Backupable] and [Snapshotable]
type BackupContext interface { type StagingContext interface {
// IO returns the input output stream belonging to this backup file // IO returns the input output stream belonging to this backup file
IO() stream.IOStream IO() stream.IOStream
@ -41,3 +42,17 @@ type BackupContext interface {
// AddFile will not return before op has returned. // AddFile will not return before op has returned.
AddFile(path string, op func(file io.Writer) error) error AddFile(path string, op func(file io.Writer) error) error
} }
// Snapshotable represents a component with a Snapshot method.
type Snapshotable interface {
Component
// SnapshotNeedsRunning returns if this Snapshotable requires a running instance.
SnapshotNeedsRunning() bool
// SnapshotName returns a new name to be used as an argument for path.
SnapshotName() string
// Snapshot snapshots a part of the instance
Snapshot(wisski models.Instance, context StagingContext) error
}

View file

@ -11,7 +11,7 @@ func (*Control) BackupName() string {
} }
// Backup backups all control plane configuration files into dest // Backup backups all control plane configuration files into dest
func (control *Control) Backup(context component.BackupContext) error { func (control *Control) Backup(context component.StagingContext) error {
files := control.backupFiles() files := control.backupFiles()
return context.AddDirectory("", func() error { return context.AddDirectory("", func() error {

View file

@ -212,7 +212,7 @@ func (snapshot *Snapshot) makeBlackbox(io stream.IOStream, snapshots *Manager, i
defer nquads.Close() defer nquads.Close()
// directly store the result // directly store the result
_, err = snapshots.TS.Snapshot(nquads, instance.GraphDBRepository) _, err = snapshots.TS.SnapshotDB(nquads, instance.GraphDBRepository)
return err return err
}, &snapshot.ErrTriplestore) }, &snapshot.ErrTriplestore)
@ -233,7 +233,7 @@ func (snapshot *Snapshot) makeBlackbox(io stream.IOStream, snapshots *Manager, i
defer sql.Close() defer sql.Close()
// directly store the result // directly store the result
return snapshots.SQL.Snapshot(io, sql, instance.SqlDatabase) return snapshots.SQL.SnapshotDB(io, sql, instance.SqlDatabase)
}, &snapshot.ErrSQL) }, &snapshot.ErrSQL)
// wait for the group! // wait for the group!

View file

@ -14,7 +14,7 @@ func (*SQL) BackupName() string {
} }
// Backup makes a backup of all SQL databases into the path dest. // Backup makes a backup of all SQL databases into the path dest.
func (sql *SQL) Backup(context component.BackupContext) error { func (sql *SQL) Backup(context component.StagingContext) error {
return context.AddFile("", func(file io.Writer) error { return context.AddFile("", func(file io.Writer) error {
io := context.IO().Streams(file, nil, nil, 0).NonInteractive() io := context.IO().Streams(file, nil, nil, 0).NonInteractive()
code, err := sql.Stack(sql.Environment).Exec(io, "sql", "mysqldump", "--all-databases") code, err := sql.Stack(sql.Environment).Exec(io, "sql", "mysqldump", "--all-databases")

View file

@ -6,8 +6,8 @@ import (
"github.com/tkw1536/goprogram/stream" "github.com/tkw1536/goprogram/stream"
) )
// Snapshot makes a backup of the sql database into dest. // SnapshotDB makes a backup of the sql database into dest.
func (sql SQL) Snapshot(io stream.IOStream, dest io.Writer, database string) error { func (sql SQL) SnapshotDB(io stream.IOStream, dest io.Writer, database string) error {
io = io.Streams(dest, nil, nil, 0).NonInteractive() io = io.Streams(dest, nil, nil, 0).NonInteractive()
code, err := sql.Stack(sql.Environment).Exec(io, "sql", "mysqldump", "--databases", database) code, err := sql.Stack(sql.Environment).Exec(io, "sql", "mysqldump", "--databases", database)

View file

@ -10,7 +10,7 @@ import (
func (ts *Triplestore) BackupName() string { return "triplestore" } func (ts *Triplestore) BackupName() string { return "triplestore" }
// Backup makes a backup of all Triplestore repositories databases into the path dest. // Backup makes a backup of all Triplestore repositories databases into the path dest.
func (ts *Triplestore) Backup(context component.BackupContext) error { func (ts *Triplestore) Backup(context component.StagingContext) error {
// list all the directories // list all the directories
repos, err := ts.listRepositories() repos, err := ts.listRepositories()
@ -22,7 +22,7 @@ func (ts *Triplestore) Backup(context component.BackupContext) error {
return context.AddDirectory("", func() error { return context.AddDirectory("", func() error {
for _, repo := range repos { for _, repo := range repos {
if err := context.AddFile(repo.ID+".nq", func(file io.Writer) error { if err := context.AddFile(repo.ID+".nq", func(file io.Writer) error {
_, err := ts.Snapshot(file, repo.ID) _, err := ts.SnapshotDB(file, repo.ID)
return err return err
}); err != nil { }); err != nil {
return err return err

View file

@ -124,8 +124,8 @@ func (ts Triplestore) PurgeRepo(repo string) error {
var errTSBackupWrongStatusCode = errors.New("Distillery.Backup: Wrong status code") var errTSBackupWrongStatusCode = errors.New("Distillery.Backup: Wrong status code")
// Snapshot snapshots the provided repository into dst // SnapshotDB snapshots the provided repository into dst
func (ts Triplestore) Snapshot(dst io.Writer, repo string) (int64, error) { func (ts Triplestore) SnapshotDB(dst io.Writer, repo string) (int64, error) {
res, err := ts.OpenRaw("GET", "/repositories/"+repo+"/statements?infer=false", nil, "", "application/n-quads") res, err := ts.OpenRaw("GET", "/repositories/"+repo+"/statements?infer=false", nil, "", "application/n-quads")
if err != nil { if err != nil {
return 0, err return 0, err