Merge branch 'replication_rewrite' (in fact it's a 90% rewrite)
This commit is contained in:
+235
-11
@@ -14,6 +14,8 @@ import (
|
||||
"github.com/problame/go-rwccmd"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/zrepl/zrepl/util"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DatasetPath struct {
|
||||
@@ -140,7 +142,7 @@ type ZFSError struct {
|
||||
}
|
||||
|
||||
func (e ZFSError) Error() string {
|
||||
return fmt.Sprintf("zfs exited with error: %s", e.WaitErr.Error())
|
||||
return fmt.Sprintf("zfs exited with error: %s\nstderr:\n%s", e.WaitErr.Error(), e.Stderr)
|
||||
}
|
||||
|
||||
var ZFS_BINARY string = "zfs"
|
||||
@@ -195,13 +197,13 @@ func ZFSList(properties []string, zfsArgs ...string) (res [][]string, err error)
|
||||
}
|
||||
|
||||
type ZFSListResult struct {
|
||||
fields []string
|
||||
err error
|
||||
Fields []string
|
||||
Err error
|
||||
}
|
||||
|
||||
// ZFSListChan executes `zfs list` and sends the results to the `out` channel.
|
||||
// The `out` channel is always closed by ZFSListChan:
|
||||
// If an error occurs, it is closed after sending a result with the err field set.
|
||||
// If an error occurs, it is closed after sending a result with the Err field set.
|
||||
// If no error occurs, it is just closed.
|
||||
// If the operation is cancelled via context, the channel is just closed.
|
||||
//
|
||||
@@ -256,15 +258,56 @@ func ZFSListChan(ctx context.Context, out chan ZFSListResult, properties []strin
|
||||
return
|
||||
}
|
||||
|
||||
func ZFSSend(fs *DatasetPath, from, to *FilesystemVersion) (stream io.Reader, err error) {
|
||||
func validateRelativeZFSVersion(s string) error {
|
||||
if len(s) <= 1 {
|
||||
return errors.New("version must start with a delimiter char followed by at least one character")
|
||||
}
|
||||
if !(s[0] == '#' || s[0] == '@') {
|
||||
return errors.New("version name starts with invalid delimiter char")
|
||||
}
|
||||
// FIXME whitespace check...
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateZFSFilesystem(fs string) error {
|
||||
if len(fs) < 1 {
|
||||
return errors.New("filesystem path must have length > 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func absVersion(fs, v string) (full string, err error) {
|
||||
if err := validateZFSFilesystem(fs); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := validateRelativeZFSVersion(v); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s%s", fs, v), nil
|
||||
}
|
||||
|
||||
func ZFSSend(fs string, from, to string) (stream io.ReadCloser, err error) {
|
||||
|
||||
fromV, err := absVersion(fs, from)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
toV := ""
|
||||
if to != "" {
|
||||
toV, err = absVersion(fs, to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
args = append(args, "send")
|
||||
|
||||
if to == nil { // Initial
|
||||
args = append(args, from.ToAbsPath(fs))
|
||||
if toV == "" { // Initial
|
||||
args = append(args, fromV)
|
||||
} else {
|
||||
args = append(args, "-i", from.ToAbsPath(fs), to.ToAbsPath(fs))
|
||||
args = append(args, "-i", fromV, toV)
|
||||
}
|
||||
|
||||
stream, err = util.RunIOCommand(ZFS_BINARY, args...)
|
||||
@@ -272,14 +315,74 @@ func ZFSSend(fs *DatasetPath, from, to *FilesystemVersion) (stream io.Reader, er
|
||||
return
|
||||
}
|
||||
|
||||
func ZFSRecv(fs *DatasetPath, stream io.Reader, additionalArgs ...string) (err error) {
|
||||
var BookmarkSizeEstimationNotSupported error = fmt.Errorf("size estimation is not supported for bookmarks")
|
||||
|
||||
// May return BookmarkSizeEstimationNotSupported as err if from is a bookmark.
|
||||
func ZFSSendDry(fs string, from, to string) (size int64, err error) {
|
||||
|
||||
fromV, err := absVersion(fs, from)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
toV := ""
|
||||
if to != "" {
|
||||
toV, err = absVersion(fs, to)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(fromV, "#") {
|
||||
/* TODO:
|
||||
* ZFS at the time of writing does not support dry-run send because size-estimation
|
||||
* uses fromSnap's deadlist. However, for a bookmark, that deadlist no longer exists.
|
||||
* Redacted send & recv will bring this functionality, see
|
||||
* https://github.com/openzfs/openzfs/pull/484
|
||||
*/
|
||||
return 0, BookmarkSizeEstimationNotSupported
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
args = append(args, "send", "-n", "-v", "-P")
|
||||
|
||||
if toV == "" { // Initial
|
||||
args = append(args, fromV)
|
||||
} else {
|
||||
args = append(args, "-i", fromV, toV)
|
||||
}
|
||||
|
||||
cmd := exec.Command(ZFS_BINARY, args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
o := string(output)
|
||||
lines := strings.Split(o, "\n")
|
||||
if len(lines) < 2 {
|
||||
return 0, errors.New("zfs send -n did not return the expected number of lines")
|
||||
}
|
||||
fields := strings.Fields(lines[1])
|
||||
if len(fields) != 2 {
|
||||
return 0, errors.New("zfs send -n returned unexpexted output")
|
||||
}
|
||||
|
||||
size, err = strconv.ParseInt(fields[1], 10, 64)
|
||||
return size, err
|
||||
}
|
||||
|
||||
func ZFSRecv(fs string, stream io.Reader, additionalArgs ...string) (err error) {
|
||||
|
||||
if err := validateZFSFilesystem(fs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
args = append(args, "recv")
|
||||
if len(args) > 0 {
|
||||
args = append(args, additionalArgs...)
|
||||
}
|
||||
args = append(args, fs.ToString())
|
||||
args = append(args, fs)
|
||||
|
||||
cmd := exec.Command(ZFS_BINARY, args...)
|
||||
|
||||
@@ -310,6 +413,27 @@ func ZFSRecv(fs *DatasetPath, stream io.Reader, additionalArgs ...string) (err e
|
||||
return nil
|
||||
}
|
||||
|
||||
func ZFSRecvWriter(fs *DatasetPath, additionalArgs ...string) (io.WriteCloser, error) {
|
||||
|
||||
args := make([]string, 0)
|
||||
args = append(args, "recv")
|
||||
if len(args) > 0 {
|
||||
args = append(args, additionalArgs...)
|
||||
}
|
||||
args = append(args, fs.ToString())
|
||||
|
||||
cmd, err := util.NewIOCommand(ZFS_BINARY, args, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cmd.Stdin, nil
|
||||
}
|
||||
|
||||
type ZFSProperties struct {
|
||||
m map[string]string
|
||||
}
|
||||
@@ -322,6 +446,10 @@ func (p *ZFSProperties) Set(key, val string) {
|
||||
p.m[key] = val
|
||||
}
|
||||
|
||||
func (p *ZFSProperties) Get(key string) string {
|
||||
return p.m[key]
|
||||
}
|
||||
|
||||
func (p *ZFSProperties) appendArgs(args *[]string) (err error) {
|
||||
for prop, val := range p.m {
|
||||
if strings.Contains(prop, "=") {
|
||||
@@ -333,14 +461,17 @@ func (p *ZFSProperties) appendArgs(args *[]string) (err error) {
|
||||
}
|
||||
|
||||
func ZFSSet(fs *DatasetPath, props *ZFSProperties) (err error) {
|
||||
return zfsSet(fs.ToString(), props)
|
||||
}
|
||||
|
||||
func zfsSet(path string, props *ZFSProperties) (err error) {
|
||||
args := make([]string, 0)
|
||||
args = append(args, "set")
|
||||
err = props.appendArgs(&args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args = append(args, fs.ToString())
|
||||
args = append(args, path)
|
||||
|
||||
cmd := exec.Command(ZFS_BINARY, args...)
|
||||
|
||||
@@ -361,8 +492,101 @@ func ZFSSet(fs *DatasetPath, props *ZFSProperties) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func ZFSGet(fs *DatasetPath, props []string) (*ZFSProperties, error) {
|
||||
return zfsGet(fs.ToString(), props, sourceAny)
|
||||
}
|
||||
|
||||
var zfsGetDatasetDoesNotExistRegexp = regexp.MustCompile(`^cannot open '(\S+)': (dataset does not exist|no such pool or dataset)`)
|
||||
|
||||
type DatasetDoesNotExist struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (d *DatasetDoesNotExist) Error() string { return fmt.Sprintf("dataset %q does not exist", d.Path) }
|
||||
|
||||
type zfsPropertySource uint
|
||||
|
||||
const (
|
||||
sourceLocal zfsPropertySource = 1 << iota
|
||||
sourceDefault
|
||||
sourceInherited
|
||||
sourceNone
|
||||
sourceTemporary
|
||||
|
||||
sourceAny zfsPropertySource = ^zfsPropertySource(0)
|
||||
)
|
||||
|
||||
func (s zfsPropertySource) zfsGetSourceFieldPrefixes() []string {
|
||||
prefixes := make([]string, 0, 5)
|
||||
if s&sourceLocal != 0 {prefixes = append(prefixes, "local")}
|
||||
if s&sourceDefault != 0 {prefixes = append(prefixes, "default")}
|
||||
if s&sourceInherited != 0 {prefixes = append(prefixes, "inherited")}
|
||||
if s&sourceNone != 0 {prefixes = append(prefixes, "-")}
|
||||
if s&sourceTemporary != 0 { prefixes = append(prefixes, "temporary")}
|
||||
return prefixes
|
||||
}
|
||||
|
||||
func zfsGet(path string, props []string, allowedSources zfsPropertySource) (*ZFSProperties, error) {
|
||||
args := []string{"get", "-Hp", "-o", "property,value,source", strings.Join(props, ","), path}
|
||||
cmd := exec.Command(ZFS_BINARY, args...)
|
||||
stdout, err := cmd.Output()
|
||||
if err != nil {
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
if exitErr.Exited() {
|
||||
// screen-scrape output
|
||||
if sm := zfsGetDatasetDoesNotExistRegexp.FindSubmatch(exitErr.Stderr); sm != nil {
|
||||
if string(sm[1]) == path {
|
||||
return nil, &DatasetDoesNotExist{path}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
o := string(stdout)
|
||||
lines := strings.Split(o, "\n")
|
||||
if len(lines) < 1 || // account for newlines
|
||||
len(lines)-1 != len(props) {
|
||||
return nil, fmt.Errorf("zfs get did not return the number of expected property values")
|
||||
}
|
||||
res := &ZFSProperties{
|
||||
make(map[string]string, len(lines)),
|
||||
}
|
||||
allowedPrefixes := allowedSources.zfsGetSourceFieldPrefixes()
|
||||
for _, line := range lines[:len(lines)-1] {
|
||||
fields := strings.FieldsFunc(line, func(r rune) bool {
|
||||
return r == '\t'
|
||||
})
|
||||
if len(fields) != 3 {
|
||||
return nil, fmt.Errorf("zfs get did not return property,value,source tuples")
|
||||
}
|
||||
for _, p := range allowedPrefixes {
|
||||
if strings.HasPrefix(fields[2],p) {
|
||||
res.m[fields[0]] = fields[1]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func ZFSDestroy(dataset string) (err error) {
|
||||
|
||||
var dstype, filesystem string
|
||||
idx := strings.IndexAny(dataset, "@#")
|
||||
if idx == -1 {
|
||||
dstype = "filesystem"
|
||||
filesystem = dataset
|
||||
} else {
|
||||
switch dataset[idx] {
|
||||
case '@': dstype = "snapshot"
|
||||
case '#': dstype = "bookmark"
|
||||
}
|
||||
filesystem = dataset[:idx]
|
||||
}
|
||||
|
||||
defer prometheus.NewTimer(prom.ZFSDestroyDuration.WithLabelValues(dstype, filesystem))
|
||||
|
||||
cmd := exec.Command(ZFS_BINARY, "destroy", dataset)
|
||||
|
||||
stderr := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||
|
||||
Reference in New Issue
Block a user