Skip to content

Commit

Permalink
refactor: dfpath pkg (#879)
Browse files Browse the repository at this point in the history
* feat: dfpath

Signed-off-by: Gaius <[email protected]>

* feat: dfpath

Signed-off-by: Gaius <[email protected]>

* feat: path

Signed-off-by: Gaius <[email protected]>

* feat: plugin

Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Dec 8, 2021
1 parent f0f4d6c commit ee23dec
Show file tree
Hide file tree
Showing 55 changed files with 529 additions and 310 deletions.
3 changes: 3 additions & 0 deletions cdn/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ type BaseProperties struct {
// StorageMode disk/hybrid/memory
StorageMode string `yaml:"storageMode" mapstructure:"storageMode"`

// Log directory
LogDir string `yaml:"logDir" mapstructure:"logDir"`

// Manager configuration
Manager ManagerConfig `yaml:"manager" mapstructure:"manager"`

Expand Down
9 changes: 9 additions & 0 deletions client/config/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ type ClientOption struct {
// ShowProgress shows progress bar, it's conflict with `--console`.
ShowProgress bool `yaml:"show-progress,omitempty" mapstructure:"show-progress,omitempty"`

// DaemonSockPath is dfget daemon socket path.
DaemonSockPath string `yaml:"daemonSockPath,omitempty" mapstructure:"daemonSockPath,omitempty"`

// LogDir is log directory of dfget.
LogDir string `yaml:"logDir,omitempty" mapstructure:"logDir,omitempty"`

// WorkHome is working directory of dfget.
WorkHome string `yaml:"workHome,omitempty" mapstructure:"workHome,omitempty"`

RateLimit rate.Limit `yaml:"rateLimit,omitempty" mapstructure:"rateLimit,omitempty"`

// Config file paths,
Expand Down
12 changes: 7 additions & 5 deletions client/config/dynconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ import (
"time"

logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/internal/dfpath"
internaldynconfig "d7y.io/dragonfly/v2/internal/dynconfig"
"d7y.io/dragonfly/v2/manager/searcher"
"d7y.io/dragonfly/v2/pkg/rpc/manager"
managerclient "d7y.io/dragonfly/v2/pkg/rpc/manager/client"
)

var (
// Dynconfig configure the cache path
cachePath = filepath.Join(dfpath.DefaultCacheDir, "daemon_dynconfig")
// Daemon cache file name
cacheFileName = "daemon_dynconfig"

// Watch dynconfig interval
watchInterval = 10 * time.Second
Expand Down Expand Up @@ -73,9 +72,11 @@ type dynconfig struct {
*internaldynconfig.Dynconfig
observers map[Observer]struct{}
done chan bool
cachePath string
}

func NewDynconfig(rawManagerClient managerclient.Client, hostOption HostOption, expire time.Duration) (Dynconfig, error) {
func NewDynconfig(rawManagerClient managerclient.Client, cacheDir string, hostOption HostOption, expire time.Duration) (Dynconfig, error) {
cachePath := filepath.Join(cacheDir, cacheFileName)
client, err := internaldynconfig.New(
internaldynconfig.ManagerSourceType,
internaldynconfig.WithManagerClient(newManagerClient(rawManagerClient, hostOption)),
Expand All @@ -89,6 +90,7 @@ func NewDynconfig(rawManagerClient managerclient.Client, hostOption HostOption,
return &dynconfig{
observers: map[Observer]struct{}{},
done: make(chan bool),
cachePath: cachePath,
Dynconfig: client,
}, nil
}
Expand Down Expand Up @@ -159,7 +161,7 @@ func (d *dynconfig) watch() {

func (d *dynconfig) Stop() error {
close(d.done)
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(d.cachePath); err != nil {
return err
}

Expand Down
46 changes: 33 additions & 13 deletions client/config/dynconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package config

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

Expand All @@ -30,6 +32,12 @@ import (
)

func TestDynconfigNewDynconfig(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test")
if err != nil {
t.Fatal(err)
}

mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct {
name string
expire time.Duration
Expand All @@ -45,7 +53,7 @@ func TestDynconfigNewDynconfig(t *testing.T) {
Hostname: "foo",
},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand All @@ -62,7 +70,7 @@ func TestDynconfigNewDynconfig(t *testing.T) {
expire: 10 * time.Millisecond,
hostOption: HostOption{},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand Down Expand Up @@ -109,14 +117,20 @@ func TestDynconfigNewDynconfig(t *testing.T) {

mockManagerClient := mocks.NewMockClient(ctl)
tc.mock(mockManagerClient.EXPECT())
_, err := NewDynconfig(mockManagerClient, tc.hostOption, tc.expire)
_, err := NewDynconfig(mockManagerClient, mockCacheDir, tc.hostOption, tc.expire)
tc.expect(t, err)
tc.cleanFileCache(t)
})
}
}

func TestDynconfigGet(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test")
if err != nil {
t.Fatal(err)
}

mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct {
name string
expire time.Duration
Expand All @@ -142,7 +156,7 @@ func TestDynconfigGet(t *testing.T) {
},
sleep: func() {},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand Down Expand Up @@ -181,7 +195,7 @@ func TestDynconfigGet(t *testing.T) {
time.Sleep(100 * time.Millisecond)
},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand Down Expand Up @@ -221,7 +235,7 @@ func TestDynconfigGet(t *testing.T) {
time.Sleep(100 * time.Millisecond)
},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand Down Expand Up @@ -257,7 +271,7 @@ func TestDynconfigGet(t *testing.T) {
time.Sleep(100 * time.Millisecond)
},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand All @@ -283,7 +297,7 @@ func TestDynconfigGet(t *testing.T) {

mockManagerClient := mocks.NewMockClient(ctl)
tc.mock(mockManagerClient.EXPECT(), tc.data)
dynconfig, err := NewDynconfig(mockManagerClient, tc.hostOption, tc.expire)
dynconfig, err := NewDynconfig(mockManagerClient, mockCacheDir, tc.hostOption, tc.expire)
if err != nil {
t.Fatal(err)
}
Expand All @@ -296,6 +310,12 @@ func TestDynconfigGet(t *testing.T) {
}

func TestDynconfigGetSchedulers(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test")
if err != nil {
t.Fatal(err)
}

mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct {
name string
expire time.Duration
Expand All @@ -321,7 +341,7 @@ func TestDynconfigGetSchedulers(t *testing.T) {
},
sleep: func() {},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand Down Expand Up @@ -360,7 +380,7 @@ func TestDynconfigGetSchedulers(t *testing.T) {
time.Sleep(100 * time.Millisecond)
},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand Down Expand Up @@ -400,7 +420,7 @@ func TestDynconfigGetSchedulers(t *testing.T) {
time.Sleep(100 * time.Millisecond)
},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand Down Expand Up @@ -436,7 +456,7 @@ func TestDynconfigGetSchedulers(t *testing.T) {
time.Sleep(100 * time.Millisecond)
},
cleanFileCache: func(t *testing.T) {
if err := os.Remove(cachePath); err != nil {
if err := os.Remove(mockCachePath); err != nil {
t.Fatal(err)
}
},
Expand All @@ -462,7 +482,7 @@ func TestDynconfigGetSchedulers(t *testing.T) {

mockManagerClient := mocks.NewMockClient(ctl)
tc.mock(mockManagerClient.EXPECT(), tc.data)
dynconfig, err := NewDynconfig(mockManagerClient, tc.hostOption, tc.expire)
dynconfig, err := NewDynconfig(mockManagerClient, mockCacheDir, tc.hostOption, tc.expire)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion client/config/peerhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ type DaemonOption struct {
AliveTime clientutil.Duration `mapstructure:"aliveTime" yaml:"aliveTime"`
GCInterval clientutil.Duration `mapstructure:"gcInterval" yaml:"gcInterval"`

DataDir string `mapstructure:"dataDir" yaml:"dataDir"`
WorkHome string `mapstructure:"workHome" yaml:"workHome"`
CacheDir string `mapstructure:"cacheDir" yaml:"cacheDir"`
LogDir string `mapstructure:"logDir" yaml:"logDir"`
KeepStorage bool `mapstructure:"keepStorage" yaml:"keepStorage"`

Scheduler SchedulerOption `mapstructure:"scheduler" yaml:"scheduler"`
Expand Down
11 changes: 1 addition & 10 deletions client/config/peerhost_darwin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin
// +build darwin

/*
Expand Down Expand Up @@ -26,21 +27,11 @@ import (

"d7y.io/dragonfly/v2/client/clientutil"
"d7y.io/dragonfly/v2/internal/dfnet"
"d7y.io/dragonfly/v2/pkg/basic"
"d7y.io/dragonfly/v2/pkg/util/hostutils"
"d7y.io/dragonfly/v2/pkg/util/net/iputils"
)

var (
PeerHostConfigPath = basic.HomeDir + "/.dragonfly/dfget-daemon.yaml"

peerHostWorkHome = basic.HomeDir + "/.dragonfly/dfdaemon/"
peerHostDataDir = peerHostWorkHome
)

var peerHostConfig = DaemonOption{
DataDir: peerHostDataDir,
WorkHome: peerHostWorkHome,
AliveTime: clientutil.Duration{Duration: DefaultDaemonAliveTime},
GCInterval: clientutil.Duration{Duration: DefaultGCInterval},
KeepStorage: false,
Expand Down
11 changes: 1 addition & 10 deletions client/config/peerhost_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand Down Expand Up @@ -26,21 +27,11 @@ import (

"d7y.io/dragonfly/v2/client/clientutil"
"d7y.io/dragonfly/v2/internal/dfnet"
"d7y.io/dragonfly/v2/pkg/basic"
"d7y.io/dragonfly/v2/pkg/util/hostutils"
"d7y.io/dragonfly/v2/pkg/util/net/iputils"
)

var (
PeerHostConfigPath = "/etc/dragonfly/dfget-daemon.yaml"

peerHostWorkHome = basic.HomeDir + "/.dragonfly/dfget-daemon/"
peerHostDataDir = peerHostWorkHome
)

var peerHostConfig = DaemonOption{
DataDir: peerHostDataDir,
WorkHome: peerHostWorkHome,
AliveTime: clientutil.Duration{Duration: DefaultDaemonAliveTime},
GCInterval: clientutil.Duration{Duration: DefaultGCInterval},
KeepStorage: false,
Expand Down
1 change: 0 additions & 1 deletion client/config/peerhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ func TestPeerHostOption_Load(t *testing.T) {
GCInterval: clientutil.Duration{
Duration: 60000000000,
},
DataDir: "/tmp/dragonfly/dfdaemon/",
WorkHome: "/tmp/dragonfly/dfdaemon/",
KeepStorage: false,
Scheduler: SchedulerOption{
Expand Down
1 change: 0 additions & 1 deletion client/config/testdata/config/daemon.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
aliveTime: 0s
gcInterval: 1m0s
dataDir: /tmp/dragonfly/dfdaemon/
workHome: /tmp/dragonfly/dfdaemon/
keepStorage: false
scheduler:
Expand Down
10 changes: 6 additions & 4 deletions client/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ type clientDaemon struct {
PieceManager peer.PieceManager

dynconfig config.Dynconfig
dfpath dfpath.Dfpath
schedulers []*manager.Scheduler
schedulerClient schedulerclient.SchedulerClient
}

func New(opt *config.DaemonOption) (Daemon, error) {
func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) {
host := &scheduler.PeerHost{
Uuid: idgen.UUIDString(),
Ip: opt.Host.AdvertiseIP,
Expand All @@ -113,7 +114,7 @@ func New(opt *config.DaemonOption) (Daemon, error) {
}

// New dynconfig client
if dynconfig, err = config.NewDynconfig(managerClient, opt.Host, opt.Scheduler.Manager.RefreshInterval); err != nil {
if dynconfig, err = config.NewDynconfig(managerClient, d.CacheDir(), opt.Host, opt.Scheduler.Manager.RefreshInterval); err != nil {
return nil, err
}

Expand All @@ -137,7 +138,7 @@ func New(opt *config.DaemonOption) (Daemon, error) {
}

// Storage.Option.DataPath is same with Daemon DataDir
opt.Storage.DataPath = opt.DataDir
opt.Storage.DataPath = d.WorkHome()
storageManager, err := storage.NewStorageManager(opt.Storage.StoreStrategy, &opt.Storage,
/* gc callback */
func(request storage.CommonTaskRequest) {
Expand Down Expand Up @@ -217,6 +218,7 @@ func New(opt *config.DaemonOption) (Daemon, error) {
StorageManager: storageManager,
GCManager: gc.NewManager(opt.GCInterval.Duration),
dynconfig: dynconfig,
dfpath: d,
schedulers: schedulers,
schedulerClient: sched,
}, nil
Expand Down Expand Up @@ -320,7 +322,7 @@ func (*clientDaemon) prepareTCPListener(opt config.ListenOption, withTLS bool) (
func (cd *clientDaemon) Serve() error {
cd.GCManager.Start()
// TODO remove this field, and use directly dfpath.DaemonSockPath
cd.Option.Download.DownloadGRPC.UnixListen.Socket = dfpath.DaemonSockPath
cd.Option.Download.DownloadGRPC.UnixListen.Socket = cd.dfpath.DaemonSockPath()
// prepare download service listen
if cd.Option.Download.DownloadGRPC.UnixListen == nil {
return errors.New("download grpc unix listen option is empty")
Expand Down
Loading

0 comments on commit ee23dec

Please sign in to comment.