2017-06-22 21:49:14 +02:00
|
|
|
package zfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-18 13:28:46 +01:00
|
|
|
"context"
|
2017-06-22 21:49:14 +02:00
|
|
|
"fmt"
|
2018-08-25 21:30:25 +02:00
|
|
|
"io"
|
2017-06-22 21:49:14 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-06-22 19:04:48 +02:00
|
|
|
"time"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
2019-03-22 20:45:27 +01:00
|
|
|
"github.com/pkg/errors"
|
2019-03-22 19:41:12 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-06-22 21:49:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type VersionType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
Bookmark VersionType = "bookmark"
|
2019-03-22 20:45:27 +01:00
|
|
|
Snapshot VersionType = "snapshot"
|
2017-06-22 21:49:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (t VersionType) DelimiterChar() string {
|
|
|
|
switch t {
|
|
|
|
case Bookmark:
|
|
|
|
return "#"
|
|
|
|
case Snapshot:
|
|
|
|
return "@"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected VersionType %#v", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:12:25 +02:00
|
|
|
func (t VersionType) String() string {
|
|
|
|
return string(t)
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
func DecomposeVersionString(v string) (fs string, versionType VersionType, name string, err error) {
|
|
|
|
if len(v) < 3 {
|
2019-03-22 20:45:27 +01:00
|
|
|
err = fmt.Errorf("snapshot or bookmark name implausibly short: %s", v)
|
2018-06-20 20:20:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
snapSplit := strings.SplitN(v, "@", 2)
|
|
|
|
bookmarkSplit := strings.SplitN(v, "#", 2)
|
|
|
|
if len(snapSplit)*len(bookmarkSplit) != 2 {
|
2019-03-22 20:45:27 +01:00
|
|
|
err = fmt.Errorf("dataset cannot be snapshot and bookmark at the same time: %s", v)
|
2018-06-20 20:20:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(snapSplit) == 2 {
|
|
|
|
return snapSplit[0], Snapshot, snapSplit[1], nil
|
|
|
|
} else {
|
|
|
|
return bookmarkSplit[0], Bookmark, bookmarkSplit[1], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 23:43:17 +01:00
|
|
|
// The data in a FilesystemVersion is guaranteed to stem from a ZFS CLI invocation.
|
2017-06-22 21:49:14 +02:00
|
|
|
type FilesystemVersion struct {
|
|
|
|
Type VersionType
|
|
|
|
|
|
|
|
// Display name. Should not be used for identification, only for user output
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// GUID as exported by ZFS. Uniquely identifies a snapshot across pools
|
|
|
|
Guid uint64
|
|
|
|
|
|
|
|
// The TXG in which the snapshot was created. For bookmarks,
|
|
|
|
// this is the GUID of the snapshot it was initially tied to.
|
|
|
|
CreateTXG uint64
|
2017-06-22 19:04:48 +02:00
|
|
|
|
|
|
|
// The time the dataset was created
|
|
|
|
Creation time.Time
|
2017-06-22 21:49:14 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 23:43:17 +01:00
|
|
|
func (v FilesystemVersion) GetCreateTXG() uint64 { return v.CreateTXG }
|
|
|
|
func (v FilesystemVersion) GetGUID() uint64 { return v.Guid }
|
|
|
|
func (v FilesystemVersion) GetGuid() uint64 { return v.Guid }
|
|
|
|
func (v FilesystemVersion) GetName() string { return v.Name }
|
|
|
|
func (v FilesystemVersion) IsSnapshot() bool { return v.Type == Snapshot }
|
|
|
|
func (v FilesystemVersion) IsBookmark() bool { return v.Type == Bookmark }
|
|
|
|
func (v FilesystemVersion) RelName() string {
|
2017-08-06 18:28:05 +02:00
|
|
|
return fmt.Sprintf("%s%s", v.Type.DelimiterChar(), v.Name)
|
|
|
|
}
|
2020-03-26 23:43:17 +01:00
|
|
|
func (v FilesystemVersion) String() string { return v.RelName() }
|
2017-08-06 18:28:05 +02:00
|
|
|
|
2017-08-06 13:04:29 +02:00
|
|
|
func (v FilesystemVersion) ToAbsPath(p *DatasetPath) string {
|
2017-06-22 21:49:14 +02:00
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString(p.ToString())
|
|
|
|
b.WriteString(v.Type.DelimiterChar())
|
|
|
|
b.WriteString(v.Name)
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2020-03-26 23:43:17 +01:00
|
|
|
func (v FilesystemVersion) FullPath(fs string) string {
|
|
|
|
return fmt.Sprintf("%s%s", fs, v.RelName())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v FilesystemVersion) ToSendArgVersion() ZFSSendArgVersion {
|
|
|
|
return ZFSSendArgVersion{
|
|
|
|
RelName: v.RelName(),
|
|
|
|
GUID: v.Guid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ParseFilesystemVersionArgs struct {
|
|
|
|
fullname string
|
|
|
|
guid, createtxg, creation string
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseFilesystemVersion(args ParseFilesystemVersionArgs) (v FilesystemVersion, err error) {
|
|
|
|
_, v.Type, v.Name, err = DecomposeVersionString(args.fullname)
|
|
|
|
if err != nil {
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Guid, err = strconv.ParseUint(args.guid, 10, 64); err != nil {
|
|
|
|
err = errors.Wrapf(err, "cannot parse GUID %q", args.guid)
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.CreateTXG, err = strconv.ParseUint(args.createtxg, 10, 64); err != nil {
|
|
|
|
err = errors.Wrapf(err, "cannot parse CreateTXG %q", args.createtxg)
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
creationUnix, err := strconv.ParseInt(args.creation, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrapf(err, "cannot parse creation date %q", args.creation)
|
|
|
|
return v, err
|
|
|
|
} else {
|
|
|
|
v.Creation = time.Unix(creationUnix, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2017-06-22 21:49:14 +02:00
|
|
|
type FilesystemVersionFilter interface {
|
2018-06-20 20:20:37 +02:00
|
|
|
Filter(t VersionType, name string) (accept bool, err error)
|
2017-06-22 21:49:14 +02:00
|
|
|
}
|
|
|
|
|
new features: {resumable,encrypted,hold-protected} send-recv, last-received-hold
- **Resumable Send & Recv Support**
No knobs required, automatically used where supported.
- **Hold-Protected Send & Recv**
Automatic ZFS holds to ensure that we can always resume a replication step.
- **Encrypted Send & Recv Support** for OpenZFS native encryption.
Configurable at the job level, i.e., for all filesystems a job is responsible for.
- **Receive-side hold on last received dataset**
The counterpart to the replication cursor bookmark on the send-side.
Ensures that incremental replication will always be possible between a sender and receiver.
Design Doc
----------
`replication/design.md` doc describes how we use ZFS holds and bookmarks to ensure that a single replication step is always resumable.
The replication algorithm described in the design doc introduces the notion of job IDs (please read the details on this design doc).
We reuse the job names for job IDs and use `JobID` type to ensure that a job name can be embedded into hold tags, bookmark names, etc.
This might BREAK CONFIG on upgrade.
Protocol Version Bump
---------------------
This commit makes backwards-incompatible changes to the replication/pdu protobufs.
Thus, bump the version number used in the protocol handshake.
Replication Cursor Format Change
--------------------------------
The new replication cursor bookmark format is: `#zrepl_CURSOR_G_${this.GUID}_J_${jobid}`
Including the GUID enables transaction-safe moving-forward of the cursor.
Including the job id enables that multiple sending jobs can send the same filesystem without interfering.
The `zrepl migrate replication-cursor:v1-v2` subcommand can be used to safely destroy old-format cursors once zrepl has created new-format cursors.
Changes in This Commit
----------------------
- package zfs
- infrastructure for holds
- infrastructure for resume token decoding
- implement a variant of OpenZFS's `entity_namecheck` and use it for validation in new code
- ZFSSendArgs to specify a ZFS send operation
- validation code protects against malicious resume tokens by checking that the token encodes the same send parameters that the send-side would use if no resume token were available (i.e. same filesystem, `fromguid`, `toguid`)
- RecvOptions support for `recv -s` flag
- convert a bunch of ZFS operations to be idempotent
- achieved through more differentiated error message scraping / additional pre-/post-checks
- package replication/pdu
- add field for encryption to send request messages
- add fields for resume handling to send & recv request messages
- receive requests now contain `FilesystemVersion To` in addition to the filesystem into which the stream should be `recv`d into
- can use `zfs recv $root_fs/$client_id/path/to/dataset@${To.Name}`, which enables additional validation after recv (i.e. whether `To.Guid` matched what we received in the stream)
- used to set `last-received-hold`
- package replication/logic
- introduce `PlannerPolicy` struct, currently only used to configure whether encrypted sends should be requested from the sender
- integrate encryption and resume token support into `Step` struct
- package endpoint
- move the concepts that endpoint builds on top of ZFS to a single file `endpoint/endpoint_zfs.go`
- step-holds + step-bookmarks
- last-received-hold
- new replication cursor + old replication cursor compat code
- adjust `endpoint/endpoint.go` handlers for
- encryption
- resumability
- new replication cursor
- last-received-hold
- client subcommand `zrepl holds list`: list all holds and hold-like bookmarks that zrepl thinks belong to it
- client subcommand `zrepl migrate replication-cursor:v1-v2`
2019-09-11 17:19:17 +02:00
|
|
|
type closureFilesystemVersionFilter struct {
|
|
|
|
cb func(t VersionType, name string) (accept bool, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *closureFilesystemVersionFilter) Filter(t VersionType, name string) (accept bool, err error) {
|
|
|
|
return f.cb(t, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterFromClosure(cb func(t VersionType, name string) (accept bool, err error)) FilesystemVersionFilter {
|
|
|
|
return &closureFilesystemVersionFilter{cb}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returned versions are sorted by createtxg
|
2017-08-06 13:04:29 +02:00
|
|
|
func ZFSListFilesystemVersions(fs *DatasetPath, filter FilesystemVersionFilter) (res []FilesystemVersion, err error) {
|
2018-02-18 13:28:46 +01:00
|
|
|
listResults := make(chan ZFSListResult)
|
|
|
|
|
2018-04-05 22:12:25 +02:00
|
|
|
promTimer := prometheus.NewTimer(prom.ZFSListFilesystemVersionDuration.WithLabelValues(fs.ToString()))
|
|
|
|
defer promTimer.ObserveDuration()
|
|
|
|
|
2018-02-18 13:28:46 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go ZFSListChan(ctx, listResults,
|
2017-06-22 19:04:48 +02:00
|
|
|
[]string{"name", "guid", "createtxg", "creation"},
|
2017-06-22 21:49:14 +02:00
|
|
|
"-r", "-d", "1",
|
|
|
|
"-t", "bookmark,snapshot",
|
|
|
|
"-s", "createtxg", fs.ToString())
|
2018-02-18 13:28:46 +01:00
|
|
|
|
|
|
|
res = make([]FilesystemVersion, 0)
|
|
|
|
for listResult := range listResults {
|
2018-06-20 20:20:37 +02:00
|
|
|
if listResult.Err != nil {
|
|
|
|
if listResult.Err == io.ErrUnexpectedEOF {
|
|
|
|
// Since we specified the fs on the command line, we'll treat this like the filesystem doesn't exist
|
|
|
|
return []FilesystemVersion{}, nil
|
|
|
|
}
|
|
|
|
return nil, listResult.Err
|
2017-06-22 21:49:14 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
line := listResult.Fields
|
2020-03-26 23:43:17 +01:00
|
|
|
args := ParseFilesystemVersionArgs{
|
|
|
|
fullname: line[0],
|
|
|
|
guid: line[1],
|
|
|
|
createtxg: line[2],
|
|
|
|
creation: line[3],
|
2017-06-22 21:49:14 +02:00
|
|
|
}
|
2020-03-26 23:43:17 +01:00
|
|
|
v, err := ParseFilesystemVersion(args)
|
2017-06-22 19:04:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-22 21:49:14 +02:00
|
|
|
accept := true
|
|
|
|
if filter != nil {
|
2018-06-20 20:20:37 +02:00
|
|
|
accept, err = filter.Filter(v.Type, v.Name)
|
2017-06-22 21:49:14 +02:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error executing filter: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if accept {
|
|
|
|
res = append(res, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-03-26 23:43:17 +01:00
|
|
|
|
2020-03-27 17:14:12 +01:00
|
|
|
func ZFSGetFilesystemVersion(ctx context.Context, ds string) (v FilesystemVersion, _ error) {
|
|
|
|
props, err := zfsGet(ctx, ds, []string{"createtxg", "guid", "creation"}, sourceAny)
|
2020-03-26 23:43:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
return ParseFilesystemVersion(ParseFilesystemVersionArgs{
|
|
|
|
fullname: ds,
|
|
|
|
createtxg: props.Get("createtxg"),
|
|
|
|
guid: props.Get("guid"),
|
|
|
|
creation: props.Get("creation"),
|
|
|
|
})
|
|
|
|
}
|