Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion job/remote_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *Runner) remoteDatasetSnapshots(host, remoteDataset string) ([]zfs.Datas
r.cacheLock.RUnlock()

ctx, cancel := context.WithTimeout(r.ctx, requestTimeout)
remoteSnaps, err := r.sendClient.DatasetSnapshots(ctx, host, remoteDataset, []string{r.config.Properties.snapshotCreatedAt()})
remoteSnaps, err := r.zfsClient.DatasetSnapshots(ctx, host, remoteDataset, []string{r.config.Properties.snapshotCreatedAt()})
cancel()
switch {
case errors.Is(err, zfs.ErrDatasetNotFound):
Expand Down
2 changes: 1 addition & 1 deletion job/remote_snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func cacheRunner(t *testing.T, cacheAgeSeconds int64) *Runner {

return &Runner{
remoteCache: make(map[string]map[string]*datasetCache),
sendClient: zfshttp.NewClient(nil, slog.Default()),
zfsClient: zfshttp.NewClient(nil, slog.Default()),
config: Config{
MaximumRemoteSnapshotCacheAgeSeconds: cacheAgeSeconds,
},
Expand Down
23 changes: 16 additions & 7 deletions job/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"log/slog"
"net/http"
"strings"
"sync"
"time"
Expand All @@ -30,19 +29,28 @@ const (

// NewRunner creates a new job runner
func NewRunner(ctx context.Context, conf Config, logger *slog.Logger) *Runner {
return NewRunnerWithCustomClient(ctx, nil, conf, logger)
}

// NewRunnerWithCustomClient creates a new job runner with a custom zfshttp.Client
func NewRunnerWithCustomClient(ctx context.Context, zfsClient *zfshttp.Client, conf Config, logger *slog.Logger) *Runner {
if zfsClient == nil {
zfsClient = zfshttp.NewClient(nil, logger)
}

r := &Runner{
Emitter: eventemitter.NewEmitter(false),
config: conf,
datasetLock: make(map[string]struct{}),
remoteCache: make(map[string]map[string]*datasetCache),
sendChan: make(chan string),
sendClient: zfshttp.NewClient(http.DefaultClient, logger),
zfsClient: zfsClient,
logger: logger,
ctx: ctx,
}

for hdr := range r.config.HTTPHeaders {
r.sendClient.SetHeader(hdr, r.config.HTTPHeaders[hdr])
r.zfsClient.SetHeader(hdr, r.config.HTTPHeaders[hdr])
}

r.attachListeners()
Expand All @@ -61,10 +69,11 @@ type Runner struct {
remoteCache map[string]map[string]*datasetCache // Snapshots indexed by server, then dataset name
cacheLock sync.RWMutex

sendChan chan string
sends []*zfsSend
sendLock sync.RWMutex
sendClient *zfshttp.Client
sendChan chan string
sends []*zfsSend
sendLock sync.RWMutex

zfsClient *zfshttp.Client

logger *slog.Logger
ctx context.Context
Expand Down
6 changes: 3 additions & 3 deletions job/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func runnerTest(t *testing.T, fn func(url string, runner *Runner)) {
datasetLock: make(map[string]struct{}),
remoteCache: make(map[string]map[string]*datasetCache),
sendChan: make(chan string),
sendClient: zfshttp.NewClient(nil, slog.Default()),
zfsClient: zfshttp.NewClient(nil, slog.Default()),
config: Config{
ParentDataset: testZPool,
DatasetType: zfs.DatasetFilesystem,
Expand Down Expand Up @@ -89,14 +89,14 @@ func TestNewRunner(t *testing.T) {
require.NotNil(t, r.datasetLock)
require.NotNil(t, r.remoteCache)
require.NotNil(t, r.sendChan)
require.NotNil(t, r.sendClient)
require.NotNil(t, r.zfsClient)
require.Equal(t, testZPool, r.config.ParentDataset)
require.Equal(t, defaultSendRoutines, r.config.SendRoutines)
require.Equal(t, defaultNamespace, r.config.Properties.Namespace)
require.Empty(t, r.ListCurrentSends())

// The configured HTTP headers must be wired into the send client
snaps, err := r.sendClient.DatasetSnapshots(t.Context(), server.URL, "testfs1", nil)
snaps, err := r.zfsClient.DatasetSnapshots(t.Context(), server.URL, "testfs1", nil)
require.NoError(t, err)
require.Empty(t, snaps)
require.Equal(t, "hello", gotHeader)
Expand Down
2 changes: 1 addition & 1 deletion job/snapshots_mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (r *Runner) markRemoteDatasetSnapshot(localSnap *zfs.Dataset, host, deleteP
ctx, cancel := context.WithTimeout(r.ctx, 5*time.Minute)
defer cancel()

return r.sendClient.SetSnapshotProperties(ctx, host, datasetName(localSnap.Name, true), snapshotName(localSnap.Name), zfshttp.SetProperties{
return r.zfsClient.SetSnapshotProperties(ctx, host, datasetName(localSnap.Name, true), snapshotName(localSnap.Name), zfshttp.SetProperties{
Set: map[string]string{
deleteProp: deleteAt.Format(dateTimeFormat),
},
Expand Down
8 changes: 4 additions & 4 deletions job/snapshots_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (r *Runner) sendDatasetSnapshots(ds *zfs.Dataset) error {

func (r *Runner) resumeSendSnapshot(host string, ds *zfs.Dataset, remoteDataset, sendingSnapName string) (bool, error) {
ctx, cancel := context.WithTimeout(r.ctx, requestTimeout)
resumeToken, curBytes, err := r.sendClient.ResumableSendToken(ctx, host, remoteDataset)
resumeToken, curBytes, err := r.zfsClient.ResumableSendToken(ctx, host, remoteDataset)
cancel()
switch {
case isContextError(err):
Expand Down Expand Up @@ -243,7 +243,7 @@ func (r *Runner) resumeSendSnapshot(host string, ds *zfs.Dataset, remoteDataset,

r.EmitEvent(ResumeSendingSnapshotEvent, fullSnapName, host, curBytes)

result, err := r.sendClient.ResumeSend(ctx, host, datasetName(ds.Name, true), resumeToken, zfshttp.ResumeSendOptions{
result, err := r.zfsClient.ResumeSend(ctx, host, datasetName(ds.Name, true), resumeToken, zfshttp.ResumeSendOptions{
ResumeSendOptions: zfs.ResumeSendOptions{
BytesPerSecond: r.config.SendSpeedBytesPerSecond,
CompressionLevel: r.config.SendCompressionLevel,
Expand Down Expand Up @@ -315,7 +315,7 @@ func (r *Runner) sendSnapshot(host string, send zfshttp.SnapshotSendOptions) err

r.EmitEvent(StartSendingSnapshotEvent, send.Snapshot.Name, host)

result, err := r.sendClient.Send(ctx, host, send)
result, err := r.zfsClient.Send(ctx, host, send)
cancel()
switch {
case errors.Is(err, zfs.ErrDatasetExists):
Expand Down Expand Up @@ -369,7 +369,7 @@ func (r *Runner) setSendSnapshotProperties(host, snapName string) error {
Set: snapProps,
}

err = r.sendClient.SetSnapshotProperties(r.ctx, host, datasetName(snapName, true), snapshotName(snapName), setProps)
err = r.zfsClient.SetSnapshotProperties(r.ctx, host, datasetName(snapName, true), snapshotName(snapName), setProps)
if err != nil {
return fmt.Errorf("error setting snapshot properties for snapshot %s: %w", snapName, err)
}
Expand Down