mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-29 20:04:55 +01:00
e0b5bd75f8
The motivation for this recatoring are based on two independent issues: - @JMoVS found that the changes merged as part of #259 slowed his OS X based installation down significantly. Analysis of the zfs command logging introduced in #296 showed that `zfs holds` took most of the execution time, and they pointed out that not all of those `zfs holds` invocations were actually necessary. I.e.: zrepl was inefficient about retrieving information from ZFS. - @InsanePrawn found that failures on initial replication would lead to step holds accumulating on the sending side, i.e. they would never be cleaned up in the HintMostRecentCommonAncestor RPC handler. That was because we only sent that RPC if there was a most recent common ancestor detected during replication planning. @InsanePrawn prototyped an implementation of a `zrepl zfs-abstractions release` command to mitigate the situation. As part of that development work and back-and-forth with @problame, it became evident that the abstractions that #259 built on top of zfs in package endpoint (step holds, replication cursor, last-received-hold), were not well-represented for re-use in the `zrepl zfs-abstractions release` subocommand prototype. This commit refactors package endpoint to address both of these issues: - endpoint abstractions now share an interface `Abstraction` that, among other things, provides a uniform `Destroy()` method. However, that method should not be destroyed directly but instead the package-level `BatchDestroy` function should be used in order to allow for a migration to zfs channel programs in the future. - endpoint now has a query facitilty (`ListAbstractions`) which is used to find on-disk - step holds and bookmarks - replication cursors (v1, v2) - last-received-holds By describing the query in a struct, we can centralized the retrieval of information via the ZFS CLI and only have to be clever once. We are "clever" in the following ways: - When asking for hold-based abstractions, we only run `zfs holds` on snapshot that have `userrefs` > 0 - To support this functionality, add field `UserRefs` to zfs.FilesystemVersion and retrieve it anywhere we retrieve zfs.FilesystemVersion from ZFS. - When asking only for bookmark-based abstractions, we only run `zfs list -t bookmark`, not with snapshots. - Currently unused (except for CLI) per-filesystem concurrent lookup - Option to only include abstractions with CreateTXG in a specified range - refactor `endpoint`'s various ZFS info retrieval methods to use `ListAbstractions` - rename the `zrepl holds list` command to `zrepl zfs-abstractions list` - make `zrepl zfs-abstractions list` consume endpoint.ListAbstractions - Add a `ListStale` method which, given a query template, lists stale holds and bookmarks. - it uses replication cursor has different modes - the new `zrepl zfs-abstractions release-{all,stale}` commands can be used to remove abstractions of package endpoint - Adjust HintMostRecentCommonAncestor RPC for stale-holds cleanup: - send it also if no most recent common ancestor exists between sender and receiver - have the sender clean up its abstractions when it receives the RPC with no most recent common ancestor, using `ListStale` - Due to changed semantics, bump the protocol version. - Adjust HintMostRecentCommonAncestor RPC for performance problems encountered by @JMoVS - by default, per (job,fs)-combination, only consider cleaning step holds in the createtxg range `[last replication cursor,conservatively-estimated-receive-side-version)` - this behavior ensures resumability at cost proportional to the time that replication was donw - however, as explained in a comment, we might leak holds if the zrepl daemon stops running - that trade-off is acceptable because in the presumably rare this might happen the user has two tools at their hand: - Tool 1: run `zrepl zfs-abstractions release-stale` - Tool 2: use env var `ZREPL_ENDPOINT_SENDER_HINT_MOST_RECENT_STEP_HOLD_CLEANUP_MODE` to adjust the lower bound of the createtxg range (search for it in the code). The env var can also be used to disable hold-cleanup on the send-side entirely. supersedes closes #293 supersedes closes #282 fixes #280 fixes #278 Additionaly, we fixed a couple of bugs: - zfs: fix half-nil error reporting of dataset-does-not-exist for ZFSListChan and ZFSBookmark - endpoint: Sender's `HintMostRecentCommonAncestor` handler would not check whether access to the specified filesystem was allowed.
235 lines
5.4 KiB
Go
235 lines
5.4 KiB
Go
package endpoint
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"runtime/debug"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
)
|
|
|
|
func TestCreateTXGRange(t *testing.T) {
|
|
|
|
type testCaseExpectation struct {
|
|
input uint64
|
|
expect bool
|
|
}
|
|
type testCase struct {
|
|
name string
|
|
config *CreateTXGRange
|
|
configAllowZeroCreateTXG bool
|
|
expectInvalid bool
|
|
expectString string
|
|
expect []testCaseExpectation
|
|
}
|
|
|
|
tcs := []testCase{
|
|
{
|
|
name: "unbounded",
|
|
expectInvalid: false,
|
|
config: &CreateTXGRange{
|
|
Since: nil,
|
|
Until: nil,
|
|
},
|
|
expectString: "~,~",
|
|
expect: []testCaseExpectation{
|
|
{0, true},
|
|
{math.MaxUint64, true},
|
|
{1, true},
|
|
{math.MaxUint64 - 1, true},
|
|
},
|
|
},
|
|
{
|
|
name: "wrong order obvious",
|
|
expectInvalid: true,
|
|
config: &CreateTXGRange{
|
|
Since: &CreateTXGRangeBound{23, &zfs.NilBool{B: true}},
|
|
Until: &CreateTXGRangeBound{20, &zfs.NilBool{B: true}},
|
|
},
|
|
expectString: "[23,20]",
|
|
},
|
|
{
|
|
name: "wrong order edge-case could also be empty",
|
|
expectInvalid: true,
|
|
config: &CreateTXGRange{
|
|
Since: &CreateTXGRangeBound{23, &zfs.NilBool{B: false}},
|
|
Until: &CreateTXGRangeBound{22, &zfs.NilBool{B: true}},
|
|
},
|
|
expectString: "(23,22]",
|
|
},
|
|
{
|
|
name: "empty",
|
|
expectInvalid: true,
|
|
config: &CreateTXGRange{
|
|
Since: &CreateTXGRangeBound{2, &zfs.NilBool{B: false}},
|
|
Until: &CreateTXGRangeBound{2, &zfs.NilBool{B: false}},
|
|
},
|
|
expectString: "(2,2)",
|
|
},
|
|
{
|
|
name: "inclusive-since-exclusive-until",
|
|
expectInvalid: false,
|
|
config: &CreateTXGRange{
|
|
Since: &CreateTXGRangeBound{2, &zfs.NilBool{B: true}},
|
|
Until: &CreateTXGRangeBound{5, &zfs.NilBool{B: false}},
|
|
},
|
|
expectString: "[2,5)",
|
|
expect: []testCaseExpectation{
|
|
{0, false},
|
|
{1, false},
|
|
{2, true},
|
|
{3, true},
|
|
{4, true},
|
|
{5, false},
|
|
{6, false},
|
|
},
|
|
},
|
|
{
|
|
name: "exclusive-since-inclusive-until",
|
|
expectInvalid: false,
|
|
config: &CreateTXGRange{
|
|
Since: &CreateTXGRangeBound{2, &zfs.NilBool{B: false}},
|
|
Until: &CreateTXGRangeBound{5, &zfs.NilBool{B: true}},
|
|
},
|
|
expectString: "(2,5]",
|
|
expect: []testCaseExpectation{
|
|
{0, false},
|
|
{1, false},
|
|
{2, false},
|
|
{3, true},
|
|
{4, true},
|
|
{5, true},
|
|
{6, false},
|
|
},
|
|
},
|
|
{
|
|
name: "zero-createtxg-not-allowed-because-likely-programmer-error",
|
|
expectInvalid: true,
|
|
config: &CreateTXGRange{
|
|
Since: nil,
|
|
Until: &CreateTXGRangeBound{0, &zfs.NilBool{B: true}},
|
|
},
|
|
expectString: "~,0]",
|
|
},
|
|
{
|
|
name: "half-open-no-until",
|
|
expectInvalid: false,
|
|
config: &CreateTXGRange{
|
|
Since: &CreateTXGRangeBound{2, &zfs.NilBool{B: false}},
|
|
Until: nil,
|
|
},
|
|
expectString: "(2,~",
|
|
expect: []testCaseExpectation{
|
|
{0, false},
|
|
{1, false},
|
|
{2, false},
|
|
{3, true},
|
|
{4, true},
|
|
{5, true},
|
|
{6, true},
|
|
},
|
|
},
|
|
{
|
|
name: "half-open-no-since",
|
|
expectInvalid: false,
|
|
config: &CreateTXGRange{
|
|
Since: nil,
|
|
Until: &CreateTXGRangeBound{4, &zfs.NilBool{B: true}},
|
|
},
|
|
expectString: "~,4]",
|
|
expect: []testCaseExpectation{
|
|
{0, true},
|
|
{1, true},
|
|
{2, true},
|
|
{3, true},
|
|
{4, true},
|
|
{5, false},
|
|
},
|
|
},
|
|
{
|
|
name: "edgeSince",
|
|
expectInvalid: false,
|
|
config: &CreateTXGRange{
|
|
Since: &CreateTXGRangeBound{math.MaxUint64, &zfs.NilBool{B: true}},
|
|
Until: nil,
|
|
},
|
|
expectString: "[18446744073709551615,~",
|
|
expect: []testCaseExpectation{
|
|
{math.MaxUint64, true},
|
|
{math.MaxUint64 - 1, false},
|
|
{0, false},
|
|
{1, false},
|
|
},
|
|
},
|
|
{
|
|
name: "edgeSinceNegative",
|
|
expectInvalid: true,
|
|
config: &CreateTXGRange{
|
|
Since: &CreateTXGRangeBound{math.MaxUint64, &zfs.NilBool{B: false}},
|
|
Until: nil,
|
|
},
|
|
expectString: "(18446744073709551615,~",
|
|
},
|
|
{
|
|
name: "edgeUntil",
|
|
expectInvalid: false,
|
|
config: &CreateTXGRange{
|
|
Until: &CreateTXGRangeBound{0, &zfs.NilBool{B: true}},
|
|
},
|
|
configAllowZeroCreateTXG: true,
|
|
expectString: "~,0]",
|
|
expect: []testCaseExpectation{
|
|
{0, true},
|
|
{math.MaxUint64, false},
|
|
{1, false},
|
|
},
|
|
},
|
|
{
|
|
name: "edgeUntilNegative",
|
|
expectInvalid: true,
|
|
configAllowZeroCreateTXG: true,
|
|
config: &CreateTXGRange{
|
|
Until: &CreateTXGRangeBound{0, &zfs.NilBool{B: false}},
|
|
},
|
|
expectString: "~,0)",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tcs {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
require.True(t, tc.expectInvalid != (len(tc.expect) > 0), "invalid test config: must either expect invalid or have expectations: %s", tc.name)
|
|
require.NotEmpty(t, tc.expectString)
|
|
assert.Equal(t, tc.expectString, tc.config.String())
|
|
|
|
save := createTXGRangeBoundAllowCreateTXG0
|
|
createTXGRangeBoundAllowCreateTXG0 = tc.configAllowZeroCreateTXG
|
|
defer func() {
|
|
createTXGRangeBoundAllowCreateTXG0 = save
|
|
}()
|
|
|
|
if tc.expectInvalid {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
assert.Error(t, tc.config.Validate())
|
|
})
|
|
} else {
|
|
for i, e := range tc.expect {
|
|
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
|
defer func() {
|
|
v := recover()
|
|
if v != nil {
|
|
t.Fatalf("should not panic: %T %v\n%s", v, v, debug.Stack())
|
|
}
|
|
}()
|
|
assert.Equal(t, e.expect, tc.config.Contains(e.input))
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|