diff --git a/scheduler/resource/persistentcache/host_manager.go b/scheduler/resource/persistentcache/host_manager.go index 20d52706480..0ed0e87e81c 100644 --- a/scheduler/resource/persistentcache/host_manager.go +++ b/scheduler/resource/persistentcache/host_manager.go @@ -55,9 +55,7 @@ type hostManager struct { rdb redis.UniversalClient } -// TODO: Use newTaskManager for resource management. // New host manager interface. -// nolint func newHostManager(cfg *config.Config, rdb redis.UniversalClient) HostManager { return &hostManager{config: cfg, rdb: rdb} } diff --git a/scheduler/resource/persistentcache/peer_manager.go b/scheduler/resource/persistentcache/peer_manager.go index 0f9fbcd8a79..a93f21655c0 100644 --- a/scheduler/resource/persistentcache/peer_manager.go +++ b/scheduler/resource/persistentcache/peer_manager.go @@ -14,6 +14,8 @@ * limitations under the License. */ +//go:generate mockgen -destination peer_manager_mock.go -source peer_manager.go -package persistentcache + package persistentcache import ( @@ -61,11 +63,9 @@ type peerManager struct { rdb redis.UniversalClient } -// TODO: Use newPeerManager for resource management. // New peer manager interface. -// nolint -func newPeerManager(cfg *config.Config, taskManager TaskManager, hostManager HostManager, rdb redis.UniversalClient) PeerManager { - return &peerManager{config: cfg, taskManager: taskManager, hostManager: hostManager, rdb: rdb} +func newPeerManager(cfg *config.Config, rdb redis.UniversalClient, taskManager TaskManager, hostManager HostManager) PeerManager { + return &peerManager{config: cfg, rdb: rdb, taskManager: taskManager, hostManager: hostManager} } // Load returns persistent cache peer by a key. diff --git a/scheduler/resource/persistentcache/peer_manager_mock.go b/scheduler/resource/persistentcache/peer_manager_mock.go new file mode 100644 index 00000000000..bbbc5322d43 --- /dev/null +++ b/scheduler/resource/persistentcache/peer_manager_mock.go @@ -0,0 +1,98 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: peer_manager.go +// +// Generated by this command: +// +// mockgen -destination peer_manager_mock.go -source peer_manager.go -package persistentcache +// + +// Package persistentcache is a generated GoMock package. +package persistentcache + +import ( + context "context" + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockPeerManager is a mock of PeerManager interface. +type MockPeerManager struct { + ctrl *gomock.Controller + recorder *MockPeerManagerMockRecorder +} + +// MockPeerManagerMockRecorder is the mock recorder for MockPeerManager. +type MockPeerManagerMockRecorder struct { + mock *MockPeerManager +} + +// NewMockPeerManager creates a new mock instance. +func NewMockPeerManager(ctrl *gomock.Controller) *MockPeerManager { + mock := &MockPeerManager{ctrl: ctrl} + mock.recorder = &MockPeerManagerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockPeerManager) EXPECT() *MockPeerManagerMockRecorder { + return m.recorder +} + +// Delete mocks base method. +func (m *MockPeerManager) Delete(arg0 context.Context, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Delete", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// Delete indicates an expected call of Delete. +func (mr *MockPeerManagerMockRecorder) Delete(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockPeerManager)(nil).Delete), arg0, arg1) +} + +// Load mocks base method. +func (m *MockPeerManager) Load(arg0 context.Context, arg1 string) (*Peer, bool) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Load", arg0, arg1) + ret0, _ := ret[0].(*Peer) + ret1, _ := ret[1].(bool) + return ret0, ret1 +} + +// Load indicates an expected call of Load. +func (mr *MockPeerManagerMockRecorder) Load(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Load", reflect.TypeOf((*MockPeerManager)(nil).Load), arg0, arg1) +} + +// LoadAll mocks base method. +func (m *MockPeerManager) LoadAll(arg0 context.Context) ([]*Peer, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LoadAll", arg0) + ret0, _ := ret[0].([]*Peer) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// LoadAll indicates an expected call of LoadAll. +func (mr *MockPeerManagerMockRecorder) LoadAll(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LoadAll", reflect.TypeOf((*MockPeerManager)(nil).LoadAll), arg0) +} + +// Store mocks base method. +func (m *MockPeerManager) Store(arg0 context.Context, arg1 *Peer) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Store", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// Store indicates an expected call of Store. +func (mr *MockPeerManagerMockRecorder) Store(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Store", reflect.TypeOf((*MockPeerManager)(nil).Store), arg0, arg1) +} diff --git a/scheduler/resource/persistentcache/resource.go b/scheduler/resource/persistentcache/resource.go new file mode 100644 index 00000000000..ae706d46238 --- /dev/null +++ b/scheduler/resource/persistentcache/resource.go @@ -0,0 +1,72 @@ +/* + * Copyright 2024 The Dragonfly Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//go:generate mockgen -destination resource_mock.go -source resource.go -package persistentcache + +package persistentcache + +import ( + "github.com/redis/go-redis/v9" + + "d7y.io/dragonfly/v2/scheduler/config" +) + +// Resource is the interface used for resource. +type Resource interface { + // Host manager interface. + HostManager() HostManager + + // Peer manager interface. + PeerManager() PeerManager + + // Task manager interface. + TaskManager() TaskManager +} + +// resource contains content for resource. +type resource struct { + // Peer manager interface. + peerManager PeerManager + + // Task manager interface. + taskManager TaskManager + + // Host manager interface. + hostManager HostManager +} + +// New returns Resource interface. +func New(cfg *config.Config, rdb redis.UniversalClient) Resource { + taskManager := newTaskManager(cfg, rdb) + hostManager := newHostManager(cfg, rdb) + peerManager := newPeerManager(cfg, rdb, taskManager, hostManager) + return &resource{peerManager, taskManager, hostManager} +} + +// Host manager interface. +func (r *resource) HostManager() HostManager { + return r.hostManager +} + +// Peer manager interface. +func (r *resource) PeerManager() PeerManager { + return r.peerManager +} + +// Task manager interface. +func (r *resource) TaskManager() TaskManager { + return r.taskManager +} diff --git a/scheduler/resource/persistentcache/resource_mock.go b/scheduler/resource/persistentcache/resource_mock.go new file mode 100644 index 00000000000..2135868742e --- /dev/null +++ b/scheduler/resource/persistentcache/resource_mock.go @@ -0,0 +1,81 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: resource.go +// +// Generated by this command: +// +// mockgen -destination resource_mock.go -source resource.go -package persistentcache +// + +// Package persistentcache is a generated GoMock package. +package persistentcache + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockResource is a mock of Resource interface. +type MockResource struct { + ctrl *gomock.Controller + recorder *MockResourceMockRecorder +} + +// MockResourceMockRecorder is the mock recorder for MockResource. +type MockResourceMockRecorder struct { + mock *MockResource +} + +// NewMockResource creates a new mock instance. +func NewMockResource(ctrl *gomock.Controller) *MockResource { + mock := &MockResource{ctrl: ctrl} + mock.recorder = &MockResourceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockResource) EXPECT() *MockResourceMockRecorder { + return m.recorder +} + +// HostManager mocks base method. +func (m *MockResource) HostManager() HostManager { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HostManager") + ret0, _ := ret[0].(HostManager) + return ret0 +} + +// HostManager indicates an expected call of HostManager. +func (mr *MockResourceMockRecorder) HostManager() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HostManager", reflect.TypeOf((*MockResource)(nil).HostManager)) +} + +// PeerManager mocks base method. +func (m *MockResource) PeerManager() PeerManager { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PeerManager") + ret0, _ := ret[0].(PeerManager) + return ret0 +} + +// PeerManager indicates an expected call of PeerManager. +func (mr *MockResourceMockRecorder) PeerManager() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PeerManager", reflect.TypeOf((*MockResource)(nil).PeerManager)) +} + +// TaskManager mocks base method. +func (m *MockResource) TaskManager() TaskManager { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "TaskManager") + ret0, _ := ret[0].(TaskManager) + return ret0 +} + +// TaskManager indicates an expected call of TaskManager. +func (mr *MockResourceMockRecorder) TaskManager() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TaskManager", reflect.TypeOf((*MockResource)(nil).TaskManager)) +} diff --git a/scheduler/resource/persistentcache/task_manager.go b/scheduler/resource/persistentcache/task_manager.go index 8ba04082072..506b6e04960 100644 --- a/scheduler/resource/persistentcache/task_manager.go +++ b/scheduler/resource/persistentcache/task_manager.go @@ -55,9 +55,7 @@ type taskManager struct { rdb redis.UniversalClient } -// TODO: Use newTaskManager for resource management. // New persistent cache task manager interface. -// nolint func newTaskManager(cfg *config.Config, rdb redis.UniversalClient) TaskManager { return &taskManager{config: cfg, rdb: rdb} } diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 8346c958e48..4f0ea69fb22 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -42,7 +42,8 @@ import ( "d7y.io/dragonfly/v2/scheduler/config" "d7y.io/dragonfly/v2/scheduler/job" "d7y.io/dragonfly/v2/scheduler/metrics" - resource "d7y.io/dragonfly/v2/scheduler/resource/standard" + persistentcache "d7y.io/dragonfly/v2/scheduler/resource/persistentcache" + standard "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/rpcserver" "d7y.io/dragonfly/v2/scheduler/scheduling" "d7y.io/dragonfly/v2/scheduler/storage" @@ -69,7 +70,10 @@ type Server struct { managerClient managerclient.V2 // Resource interface. - resource resource.Resource + resource standard.Resource + + // Persistent cache resource interface. + persistentCacheResource persistentcache.Resource // Dynamic config. dynconfig config.DynconfigInterface @@ -172,12 +176,15 @@ func New(ctx context.Context, cfg *config.Config, d dfpath.Dfpath) (*Server, err s.dynconfig = dynconfig // Initialize resource. - resource, err := resource.New(cfg, s.gc, dynconfig, clientTransportCredentials) + resource, err := standard.New(cfg, s.gc, dynconfig, clientTransportCredentials) if err != nil { return nil, err } s.resource = resource + // Initialize persistent cache resource. + s.persistentCacheResource = persistentcache.New(cfg, rdb) + // Initialize job service. if cfg.Job.Enable && rdb != nil { s.job, err = job.New(cfg, resource)