Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: full backups #2710

Merged
merged 30 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4c52c27
saving state
Oct 15, 2018
cd769f5
Merge branch 'master' of github.com:dgraph-io/dgraph into feature/roa…
Oct 16, 2018
0de29ea
saving changes
Oct 16, 2018
396c38a
saving state
Oct 17, 2018
7eb2172
added backup dir
Oct 17, 2018
f6b8b10
Merge branch 'master' of github.com:/dgraph-io/dgraph into feature/ro…
Oct 24, 2018
cd1b88d
renamed dgraphee tree to ee.
Oct 25, 2018
c4701a4
trying to get the handler and file writers working.
Oct 30, 2018
b5cfb77
added destination parameter. handler support to destination URI schem…
Oct 30, 2018
905f1b4
file handler rename on same volume. added more comments and logging.
Oct 30, 2018
d62e439
Merge branch 'master' of github.com:/dgraph-io/dgraph into feature/ro…
Oct 30, 2018
8ac55fb
changed worker to use stream pkg. updated protos for backup. fixed mi…
Oct 31, 2018
c8d9054
logging changes for debugging
Oct 31, 2018
6ea4688
added some error checks, tweaked comments.
Oct 31, 2018
00a1cd0
moved stream pkg out of worker.
Nov 1, 2018
2a317fb
removed unused const. format fixes.
Nov 1, 2018
2101284
saving state
Nov 3, 2018
255f8f4
Initial pass at simplifying things.
manishrjain Nov 3, 2018
90a394a
cleaned up redundant code.
Nov 6, 2018
1963b7c
unused const
Nov 6, 2018
b38ea24
missing space
Nov 6, 2018
03ae950
added progress monitoring. fixed issues found by CI
Nov 6, 2018
3606eaa
Small fixes here and there.
manishrjain Nov 7, 2018
7688e76
Rename handler files.
manishrjain Nov 7, 2018
a2af931
Both S3 uploads and file writes are tested to work.
manishrjain Nov 8, 2018
f8dc111
renamed writer.cleapup to writer.close
Nov 8, 2018
24c9258
Merge branch 'master' of github.com:/dgraph-io/dgraph into feature/ro…
Nov 8, 2018
4116b46
regenerated protos
Nov 8, 2018
1beefbf
Merge branch 'feature/roadmap-backups' of github.com:/dgraph-io/dgrap…
Nov 8, 2018
7da0cae
removed unneeded fallthrough
Nov 8, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions dgraph/cmd/alpha/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
)

// handlerInit does some standard checks. Returns false if something is wrong.
func handlerInit(w http.ResponseWriter, r *http.Request) bool {
if r.Method != http.MethodGet {
func handlerInit(w http.ResponseWriter, r *http.Request, method string) bool {
if r.Method != method {
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return false
}
Expand All @@ -47,7 +47,7 @@ func handlerInit(w http.ResponseWriter, r *http.Request) bool {
}

func shutDownHandler(w http.ResponseWriter, r *http.Request) {
if !handlerInit(w, r) {
if !handlerInit(w, r, http.MethodGet) {
return
}

Expand All @@ -57,19 +57,37 @@ func shutDownHandler(w http.ResponseWriter, r *http.Request) {
}

func exportHandler(w http.ResponseWriter, r *http.Request) {
if !handlerInit(w, r) {
if !handlerInit(w, r, http.MethodGet) {
return
}
ctx := context.Background()
// Export logic can be moved to dgraphzero.
if err := worker.ExportOverNetwork(ctx); err != nil {
if err := worker.ExportOverNetwork(context.Background()); err != nil {
x.SetStatus(w, err.Error(), "Export failed.")
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"code": "Success", "message": "Export completed."}`))
}

func backupHandler(w http.ResponseWriter, r *http.Request) {
if !handlerInit(w, r, http.MethodPost) {
return
}
target := r.FormValue("destination")
if target == "" {
err := x.Errorf("You must specify a 'destination' value")
x.SetStatus(w, err.Error(), "Backup failed.")
return
}
if err := worker.BackupOverNetwork(context.Background(), target); err != nil {
x.SetStatus(w, err.Error(), "Backup failed.")
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"code": "Success", "message": "Backup completed."}`))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error return value of w.Write is not checked


}

func memoryLimitHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
Expand Down
1 change: 1 addition & 0 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ func setupServer() {
http.HandleFunc("/share", shareHandler)
http.HandleFunc("/debug/store", storeStatsHandler)
http.HandleFunc("/admin/shutdown", shutDownHandler)
http.HandleFunc("/admin/backup", backupHandler)
http.HandleFunc("/admin/export", exportHandler)
http.HandleFunc("/admin/config/lru_mb", memoryLimitHandler)

Expand Down
4 changes: 4 additions & 0 deletions ee/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Dgraph Enterprise Edition (EE)

The files stored here correspond to the Dgraph Enterprise Edition features, which are _not_ under the Apache 2 License.

59 changes: 59 additions & 0 deletions ee/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Dgraph Labs, Inc. All rights reserved.
*
*/

package backup

import (
"context"

"github.com/dgraph-io/badger"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/worker/stream"
"github.com/golang/glog"
)

// Worker has all the information needed to perform a backup.
type Worker struct {
ReadTs uint64 // Timestamp to read at.
GroupId uint32 // The group ID of this node.
SeqTs string // Sequence data to label backup at the target.
TargetURI string // The intended location as URI.
DB *badger.DB // Badger pstore managed by this node.
}

// Process uses the worker values to create a stream writer then hand off the data
// retrieval to stream.Orchestrate. The writer will create all the fd's needed to
// collect the data and later move to the target.
// Returns errors on failure, nil on success.
func (w *Worker) Process(ctx context.Context) error {
glog.Infof("Backup process beginning ...")
c, err := newWriter(w)
if err != nil {
return err
}
sl := stream.Lists{Stream: c, DB: w.DB}
sl.ChooseKeyFunc = func(_ *badger.Item) bool { return true }
sl.ItemToKVFunc = func(key []byte, itr *badger.Iterator) (*pb.KV, error) {
item := itr.Item()
val, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
kv := &pb.KV{Key: item.KeyCopy(nil), Val: val, Version: item.Version()}
return kv, nil
}

if err = sl.Orchestrate(ctx, "Backup", w.ReadTs); err != nil {
return err
}

glog.Infof("Backup saving ...")
if err = c.save(); err != nil {
return err
}

glog.Infof("Backup done.")
return nil
}
65 changes: 65 additions & 0 deletions ee/backup/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2018 Dgraph Labs, Inc. All rights reserved.
*
*/

package backup

import (
"net/url"
"sync"

"github.com/dgraph-io/dgraph/x"
)

// handler interface is implemented by uri scheme handlers.
//
// Session() will read any supported environment variables and authenticate if needed.
// Copy() copies a local file to a new destination, possibly remote.
// Exists() tests if a file exists at destination.
type handler interface {
Copy(string, string) error
Session(string, string) error
}

// handlers map uri scheme to a handler
var handlers struct {
sync.Mutex
m map[string]handler
}

// getSchemeHandler takes a URI and picks the parts we need for creating a scheme handler.
// The scheme handler knows how to authenticate itself (using URI params), and how to copy
// itself to the destination target.
// Returns a new file handler on success, error otherwise.
func getSchemeHandler(uri string) (handler, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
// target might be just a dir like '/tmp/backup', then default to local file handler.
if u.Scheme == "" {
u.Scheme = "file"
}
handlers.Lock()
defer handlers.Unlock()
h, ok := handlers.m[u.Scheme]
if !ok {
return nil, x.Errorf("invalid scheme %q", u.Scheme)
}
if err := h.Session(u.Host, u.Path); err != nil {
return nil, err
}
return h, nil
}

func addSchemeHandler(scheme string, h handler) {
handlers.Lock()
defer handlers.Unlock()
if handlers.m == nil {
handlers.m = make(map[string]handler)
}
if _, ok := handlers.m[scheme]; !ok {
handlers.m[scheme] = h
}
}
92 changes: 92 additions & 0 deletions ee/backup/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2018 Dgraph Labs, Inc. All rights reserved.
*
*/

package backup

import (
"fmt"
"io/ioutil"
"os"

"github.com/dgraph-io/dgraph/protos/pb"
"github.com/golang/glog"
"github.com/matttproud/golang_protobuf_extensions/pbutil"
)

const dgraphBackupTempPrefix = "dgraph-backup-*"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dgraphBackupTempPrefix is unused

const dgraphBackupSuffix = ".dgraph-backup"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dgraphBackupSuffix is unused


type writer struct {
file string
dst handler
tmp *os.File
}

func (w *writer) save() error {
glog.Infof("Saving backup to: %q", w.file)
if err := w.dst.Copy(w.tmp.Name(), w.file); err != nil {
return err
}
glog.V(3).Infof("copied %q to %q on target ...", w.tmp.Name(), w.file)
// we are done done, cleanup.
return w.cleanup()
}

func (w *writer) cleanup() error {
// always remove the temp file
defer func() {
if err := os.Remove(w.tmp.Name()); err != nil {
// let the user know there's baggage left behind. they might have to delete by hand.
glog.Errorf("failed to remove temp file %q: %s", w.tmp.Name(), err)
}
}()

glog.V(3).Info("cleaning up ...")
if err := w.tmp.Close(); err != nil {
return err
}
return nil
}

func newWriter(worker *Worker) (*writer, error) {
var w writer
var err error

// dst is the final destination for data.
w.dst, err = getSchemeHandler(worker.TargetURI)
if err != nil {
return nil, err
}

// tmp file is our main working file.
// we will prepare this file and then copy to dst when done.
w.tmp, err = ioutil.TempFile("", dgraphBackupTempPrefix)
if err != nil {
glog.Errorf("could not create temp file: %s\n", err)
return nil, err
}
glog.V(3).Infof("temp file: %q", w.tmp.Name())

// file name: 1283719371922.12.3242423938.dgraph-backup
w.file = fmt.Sprintf("%s.%d.%d%s",
worker.SeqTs, worker.GroupId, worker.ReadTs, dgraphBackupSuffix)
glog.V(3).Infof("target file %q", w.file)

return &w, err
}

// Send implements the stream.kvStream interface.
// It writes the received KV into the temp file.
// Returns error if the writing fails, nil on success.
func (w *writer) Send(kvs *pb.KVS) error {
var err error
for _, kv := range kvs.Kv {
_, err = pbutil.WriteDelimited(w.tmp, kv)
if err != nil {
return err
}
}
return nil
}
75 changes: 75 additions & 0 deletions ee/backup/writer_local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2018 Dgraph Labs, Inc. All rights reserved.
*
*/

package backup

import (
"io"
"os"
"path/filepath"

"github.com/golang/glog"
)

// fileHandler is used for 'file:' URI scheme.
type fileHandler struct {
path string
}

// Session authenticates or prepares a handler session.
// Returns error on failure, nil on success.
func (h *fileHandler) Session(_, path string) error {
h.path = path
return os.Chdir(h.path)
}

// List returns a list of Dgraph backup files at target.
// Returns a list (might be empty) on success, error otherwise.
func (h *fileHandler) List() ([]string, error) {
return filepath.Glob(filepath.Join(h.path, "*"+dgraphBackupSuffix))
}

// Copy is called when we are ready to transmit a file to the target.
// Returns error on failure, nil on success.
func (h *fileHandler) Copy(in, out string) error {
if filepath.Base(out) == out {
out = filepath.Join(h.path, out)
}

if h.Exists(out) {
glog.Errorf("File already exists on target: %q", out)
return os.ErrExist
}

src, err := os.Open(in)
if err != nil {
return err
}
defer src.Close()

dst, err := os.Create(out)
if err != nil {
return err
}
defer dst.Close()

if _, err = io.Copy(dst, src); err != nil {
return err
}

return dst.Sync()
}

// Exists checks if a path (file or dir) is found at target.
// Returns true if found, false otherwise.
func (h *fileHandler) Exists(path string) bool {
_, err := os.Stat(path)
return os.IsExist(err)
}

// Register this handler
func init() {
addSchemeHandler("file", &fileHandler{})
}
2 changes: 1 addition & 1 deletion protos/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

PROTO_PATH := ${GOPATH}/src:.
PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/gogo/protobuf
PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/dgraph-io/dgraph
PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/dgraph-io/dgo/protos

.PHONY: help
Expand Down
Loading