-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Feature: full backups #2710
Changes from 18 commits
4c52c27
cd769f5
0de29ea
396c38a
7eb2172
f6b8b10
cd1b88d
c4701a4
b5cfb77
905f1b4
d62e439
8ac55fb
c8d9054
6ea4688
00a1cd0
2a317fb
2101284
255f8f4
90a394a
1963b7c
b38ea24
03ae950
3606eaa
7688e76
a2af931
f8dc111
24c9258
4116b46
1beefbf
7da0cae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2018 Dgraph Labs, Inc. All rights reserved. | ||
* | ||
*/ | ||
|
||
package backup | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/dgraph-io/badger" | ||
"github.com/dgraph-io/dgraph/posting" | ||
"github.com/dgraph-io/dgraph/protos/pb" | ||
"github.com/dgraph-io/dgraph/stream" | ||
"github.com/dgraph-io/dgraph/x" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File is not |
||
"github.com/golang/glog" | ||
) | ||
|
||
// Request has all the information needed to perform a backup. | ||
type Request struct { | ||
DB *badger.DB // Badger pstore managed by this node. | ||
Sizex int64 // approximate upload size | ||
Backup *pb.BackupRequest | ||
} | ||
|
||
// Process uses the request 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 (r *Request) Process(ctx context.Context) error { | ||
w, err := r.newWriter() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sl := stream.Lists{Stream: w, DB: r.DB} | ||
sl.ChooseKeyFunc = nil | ||
// sl.ChooseKeyFunc = func(_ *badger.Item) bool { return true } | ||
sl.ItemToKVFunc = func(key []byte, itr *badger.Iterator) (*pb.KV, error) { | ||
item := itr.Item() | ||
pk := x.Parse(key) | ||
if pk.IsSchema() { | ||
val, err := item.ValueCopy(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
kv := &pb.KV{ | ||
Key: key, | ||
Val: val, | ||
UserMeta: []byte{item.UserMeta()}, | ||
Version: item.Version(), | ||
} | ||
return kv, nil | ||
} | ||
l, err := posting.ReadPostingList(key, itr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return l.MarshalToKv() | ||
} | ||
|
||
br := r.Backup | ||
glog.V(2).Infof("Backup started ...") | ||
if err = sl.Orchestrate(ctx, "Backup", br.ReadTs); err != nil { | ||
return err | ||
} | ||
if err = w.cleanup(); err != nil { | ||
return err | ||
} | ||
glog.Infof("Backup complete: group %d at %d", br.GroupId, br.ReadTs) | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2018 Dgraph Labs, Inc. All rights reserved. | ||
* | ||
*/ | ||
|
||
package backup | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/dgraph-io/dgraph/x" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File is not |
||
"github.com/golang/glog" | ||
) | ||
|
||
// fileHandler is used for 'file:' URI scheme. | ||
type fileHandler struct { | ||
fp *os.File | ||
} | ||
|
||
// Open authenticates or prepares a handler session. | ||
// Returns error on failure, nil on success. | ||
func (h *fileHandler) Open(uri *url.URL, req *Request) error { | ||
// check that this path exists and we can access it. | ||
if !h.exists(uri.Path) { | ||
return x.Errorf("The path %q does not exist or it is inaccessible.", uri.Path) | ||
} | ||
|
||
dir := filepath.Join(uri.Path, fmt.Sprintf("dgraph.%s", req.Backup.UnixTs)) | ||
if err := os.Mkdir(dir, 0700); err != nil { | ||
return err | ||
} | ||
|
||
path := filepath.Join(dir, | ||
fmt.Sprintf("r%d-g%d.backup", req.Backup.ReadTs, req.Backup.GroupId)) | ||
fp, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
glog.V(2).Infof("Using file path: %q", path) | ||
h.fp = fp | ||
return nil | ||
} | ||
|
||
func (h *fileHandler) Close() error { | ||
if err := h.fp.Sync(); err != nil { | ||
return err | ||
} | ||
return h.fp.Close() | ||
} | ||
|
||
func (h *fileHandler) Write(b []byte) (int, error) { | ||
return h.fp.Write(b) | ||
} | ||
|
||
// 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) | ||
if err == nil { | ||
return true | ||
} | ||
return !os.IsNotExist(err) && !os.IsPermission(err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright 2018 Dgraph Labs, Inc. All rights reserved. | ||
* | ||
*/ | ||
|
||
package backup | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync/atomic" | ||
|
||
"github.com/dgraph-io/dgraph/x" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File is not |
||
"github.com/golang/glog" | ||
minio "github.com/minio/minio-go" | ||
) | ||
|
||
const ( | ||
s3defaultEndpoint = "s3.amazonaws.com" | ||
s3accelerateHost = "s3-accelerate" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) | ||
|
||
// s3Handler is used for 's3:' URI scheme. | ||
type s3Handler struct { | ||
bucket string | ||
path string | ||
w io.Writer | ||
n uint64 | ||
} | ||
|
||
// Open creates an AWS session and sends our data stream to an S3 blob. | ||
// URI formats: | ||
// s3://<s3 region endpoint>/bucket/folder1.../folderN?secure=true|false | ||
// s3:///bucket/folder1.../folderN?secure=true|false (use default S3 endpoint) | ||
func (h *s3Handler) Open(uri *url.URL, req *Request) error { | ||
accessKeyID := os.Getenv("AWS_ACCESS_KEY_ID") | ||
secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY") | ||
if accessKeyID == "" || secretAccessKey == "" { | ||
return x.Errorf("Env vars AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY not set.") | ||
} | ||
|
||
glog.V(2).Infof("S3Handler got uri: %+v\n", uri) | ||
var host, path string | ||
// s3:///bucket/folder | ||
if !strings.Contains(uri.Host, ".") { | ||
path, host = uri.Host, s3defaultEndpoint | ||
} | ||
glog.V(2).Infof("Backup using S3 host: %s, path: %s", host, path) | ||
|
||
if len(path) < 1 { | ||
return x.Errorf("The S3 bucket %q is invalid", path) | ||
} | ||
|
||
parts := strings.Split(path[1:], "/") | ||
h.bucket = parts[0] // bucket | ||
|
||
// split path into bucket and blob | ||
h.path = filepath.Join(parts[1:]...) | ||
h.path = filepath.Join(h.path, fmt.Sprintf("dgraph.%s", req.Backup.UnixTs), | ||
fmt.Sprintf("r%d-g%d.backup", req.Backup.ReadTs, req.Backup.GroupId)) | ||
|
||
// if len(parts) > 1 { | ||
// parts = append(parts, s.file) | ||
// // blob: folder1/...folderN/file1 | ||
// s.file = filepath.Join(parts[1:]...) | ||
// } | ||
glog.V(3).Infof("Sending to S3. Path=%s", h.path) | ||
|
||
// secure by default | ||
// uri.Query() | ||
// TODO: Do the arg query here. | ||
// secure := s.Getarg("secure") != "false" | ||
secure := true | ||
|
||
mc, err := minio.New(host, accessKeyID, secretAccessKey, secure) | ||
if err != nil { | ||
return err | ||
} | ||
loc, err := mc.GetBucketLocation(h.bucket) | ||
if err != nil { | ||
return err | ||
} | ||
if err := mc.MakeBucket(path, loc); err != nil { | ||
return err | ||
} | ||
|
||
// S3 transfer acceleration support. | ||
if strings.Contains(host, "s3-accelerate") { | ||
mc.SetS3TransferAccelerate(host) | ||
} | ||
mc.TraceOn(os.Stderr) | ||
|
||
// TODO: Fix this up. | ||
// go h.send(c, s) | ||
|
||
return nil | ||
} | ||
|
||
// func (h *s3Handler) send(c *minio.Client, s *session) { | ||
// glog.Infof("Backup streaming in background, est. %d bytes", s.size) | ||
// start := time.Now() | ||
// pr, pw := io.Pipe() | ||
// buf := bufio.NewWriterSize(pw, 5<<20) | ||
// h.w = buf | ||
|
||
// // block until done or pipe closed. | ||
// for { | ||
// time.Sleep(10 * time.Millisecond) // wait for some buffered data | ||
// n, err := c.PutObject(s.path, s.file, pr, s.size, | ||
// minio.PutObjectOptions{}) | ||
// if err != nil { | ||
// glog.Errorf("Failure while uploading backup: %s", err) | ||
// } | ||
// glog.V(3).Infof("sent %d bytes, actual %d bytes, time elapsed %s", n, h.n, time.Since(start)) | ||
// break | ||
// } | ||
// pr.CloseWithError(nil) // EOF | ||
// } | ||
|
||
func (h *s3Handler) Close() error { | ||
return nil | ||
} | ||
|
||
func (h *s3Handler) Write(b []byte) (int, error) { | ||
n, err := h.w.Write(b) | ||
atomic.AddUint64(&h.n, uint64(n)) | ||
return n, err | ||
} |
There was a problem hiding this comment.
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