From cdc3b3981941262849502488636649ccd2c3d861 Mon Sep 17 00:00:00 2001 From: Gaius Date: Mon, 30 Sep 2024 17:32:03 +0800 Subject: [PATCH] feat: removed network topology Because no one maintains it and has no users run in the production Signed-off-by: Gaius --- client/config/constants.go | 5 - client/config/peerhost.go | 46 +- client/config/peerhost_darwin.go | 6 - client/config/peerhost_linux.go | 6 - client/config/peerhost_test.go | 24 - client/config/testdata/config/daemon.yaml | 5 - client/daemon/daemon.go | 19 - .../mocks/network_topology_mock.go | 63 - .../networktopology/network_topology.go | 204 --- .../networktopology/network_topology_test.go | 545 ------- client/daemon/peer/peertask_dummy.go | 4 - .../template/scheduler.template.yaml | 14 - deploy/helm-charts | 2 +- go.mod | 3 +- go.sum | 6 +- manager/rpcserver/manager_server_v1.go | 22 +- pkg/redis/redis.go | 53 - pkg/redis/redis_test.go | 238 --- pkg/rpc/scheduler/client/client_v1.go | 17 - pkg/rpc/scheduler/client/client_v2.go | 17 - .../scheduler/client/mocks/client_v1_mock.go | 20 - .../scheduler/client/mocks/client_v2_mock.go | 20 - scheduler/config/config.go | 75 +- scheduler/config/config_test.go | 127 +- scheduler/config/constants.go | 21 - scheduler/config/testdata/scheduler.yaml | 9 - scheduler/metrics/metrics.go | 14 - .../mocks/network_topology_mock.go | 180 --- .../networktopology/mocks/probes_mock.go | 130 -- scheduler/networktopology/network_topology.go | 497 ------ .../networktopology/network_topology_test.go | 1381 ----------------- scheduler/networktopology/probes.go | 371 ----- scheduler/networktopology/probes_test.go | 1222 --------------- scheduler/rpcserver/rpcserver.go | 6 +- scheduler/rpcserver/rpcserver_test.go | 4 +- scheduler/rpcserver/scheduler_server_v1.go | 17 +- scheduler/rpcserver/scheduler_server_v2.go | 17 +- scheduler/scheduler.go | 43 +- scheduler/scheduling/evaluator/evaluator.go | 7 +- .../evaluator/evaluator_network_topology.go | 224 --- .../evaluator_network_topology_test.go | 888 ----------- .../scheduling/evaluator/evaluator_test.go | 19 +- scheduler/scheduling/scheduling.go | 4 +- scheduler/scheduling/scheduling_test.go | 3 - scheduler/service/service_v1.go | 119 +- scheduler/service/service_v1_test.go | 580 +------ scheduler/service/service_v2.go | 170 +- scheduler/service/service_v2_test.go | 566 +------ scheduler/storage/mocks/storage_mock.go | 72 - scheduler/storage/storage.go | 234 --- scheduler/storage/storage_test.go | 653 -------- scheduler/storage/types.go | 72 - test/e2e/v1/constants.go | 3 +- test/e2e/v1/network_topology_test.go | 161 -- .../charts/config-cache-list-metadata.yaml | 16 - .../testdata/charts/config-compatibility.yaml | 16 - .../charts/config-concurent-back-source.yaml | 16 - .../charts/config-disable-seed-peer.yaml | 12 - .../testdata/charts/config-grpc-tls-ipv6.yaml | 16 - test/testdata/charts/config-grpc-tls.yaml | 16 - test/testdata/charts/config-ipv6.yaml | 16 - .../charts/config-split-running-tasks.yaml | 16 - .../charts/config-write-buffer-size.yaml | 16 - test/testdata/charts/config.yaml | 16 - 64 files changed, 116 insertions(+), 9268 deletions(-) delete mode 100644 client/daemon/networktopology/mocks/network_topology_mock.go delete mode 100644 client/daemon/networktopology/network_topology.go delete mode 100644 client/daemon/networktopology/network_topology_test.go delete mode 100644 scheduler/networktopology/mocks/network_topology_mock.go delete mode 100644 scheduler/networktopology/mocks/probes_mock.go delete mode 100644 scheduler/networktopology/network_topology.go delete mode 100644 scheduler/networktopology/network_topology_test.go delete mode 100644 scheduler/networktopology/probes.go delete mode 100644 scheduler/networktopology/probes_test.go delete mode 100644 scheduler/scheduling/evaluator/evaluator_network_topology.go delete mode 100644 scheduler/scheduling/evaluator/evaluator_network_topology_test.go delete mode 100644 test/e2e/v1/network_topology_test.go diff --git a/client/config/constants.go b/client/config/constants.go index 33b8599b5ed..4265866c33a 100644 --- a/client/config/constants.go +++ b/client/config/constants.go @@ -89,11 +89,6 @@ var ( DefaultAnnouncerSchedulerInterval = 30 * time.Second ) -const ( - // DefaultProbeInterval is the default interval of probing host. - DefaultProbeInterval = 20 * time.Minute -) - const ( // DefaultLogRotateMaxSize is the default maximum size in megabytes of log files before rotation. DefaultLogRotateMaxSize = 1024 diff --git a/client/config/peerhost.go b/client/config/peerhost.go index ef54ccc5f72..c4d90f40550 100644 --- a/client/config/peerhost.go +++ b/client/config/peerhost.go @@ -70,20 +70,19 @@ type DaemonOption struct { DataDirMode uint32 `mapstructure:"dataDirMode" yaml:"dataDirMode"` KeepStorage bool `mapstructure:"keepStorage" yaml:"keepStorage"` - Security GlobalSecurityOption `mapstructure:"security" yaml:"security"` - Scheduler SchedulerOption `mapstructure:"scheduler" yaml:"scheduler"` - Host HostOption `mapstructure:"host" yaml:"host"` - Download DownloadOption `mapstructure:"download" yaml:"download"` - Proxy *ProxyOption `mapstructure:"proxy" yaml:"proxy"` - Upload UploadOption `mapstructure:"upload" yaml:"upload"` - ObjectStorage ObjectStorageOption `mapstructure:"objectStorage" yaml:"objectStorage"` - Storage StorageOption `mapstructure:"storage" yaml:"storage"` - Health *HealthOption `mapstructure:"health" yaml:"health"` - Reload ReloadOption `mapstructure:"reload" yaml:"reload"` - Network *NetworkOption `mapstructure:"network" yaml:"network"` - Announcer AnnouncerOption `mapstructure:"announcer" yaml:"announcer"` - NetworkTopology NetworkTopologyOption `mapstructure:"networkTopology" yaml:"networkTopology"` - PeerExchange PeerExchangeOption `mapstructure:"peerExchange" yaml:"peerExchange"` + Security GlobalSecurityOption `mapstructure:"security" yaml:"security"` + Scheduler SchedulerOption `mapstructure:"scheduler" yaml:"scheduler"` + Host HostOption `mapstructure:"host" yaml:"host"` + Download DownloadOption `mapstructure:"download" yaml:"download"` + Proxy *ProxyOption `mapstructure:"proxy" yaml:"proxy"` + Upload UploadOption `mapstructure:"upload" yaml:"upload"` + ObjectStorage ObjectStorageOption `mapstructure:"objectStorage" yaml:"objectStorage"` + Storage StorageOption `mapstructure:"storage" yaml:"storage"` + Health *HealthOption `mapstructure:"health" yaml:"health"` + Reload ReloadOption `mapstructure:"reload" yaml:"reload"` + Network *NetworkOption `mapstructure:"network" yaml:"network"` + Announcer AnnouncerOption `mapstructure:"announcer" yaml:"announcer"` + PeerExchange PeerExchangeOption `mapstructure:"peerExchange" yaml:"peerExchange"` } func NewDaemonConfig() *DaemonOption { @@ -226,12 +225,6 @@ func (p *DaemonOption) Validate() error { } } - if p.NetworkTopology.Enable { - if p.NetworkTopology.Probe.Interval <= 0 { - return errors.New("probe requires parameter interval") - } - } - return nil } @@ -965,19 +958,6 @@ type AnnouncerOption struct { SchedulerInterval time.Duration `mapstructure:"schedulerInterval" yaml:"schedulerInterval"` } -type NetworkTopologyOption struct { - // Enable network topology service. - Enable bool `mapstructure:"enable" yaml:"enable"` - - // Probe is the configuration of probe. - Probe ProbeOption `mapstructure:"probe" yaml:"probe"` -} - -type ProbeOption struct { - // Interval is the interval of probing hosts. - Interval time.Duration `mapstructure:"interval" yaml:"interval"` -} - type PeerExchangeOption struct { // Enable peer exchange service. Enable bool `mapstructure:"enable" yaml:"enable"` diff --git a/client/config/peerhost_darwin.go b/client/config/peerhost_darwin.go index dcf2aae27ad..d1a558b4d7f 100644 --- a/client/config/peerhost_darwin.go +++ b/client/config/peerhost_darwin.go @@ -181,12 +181,6 @@ var peerHostConfig = func() *DaemonOption { Announcer: AnnouncerOption{ SchedulerInterval: DefaultAnnouncerSchedulerInterval, }, - NetworkTopology: NetworkTopologyOption{ - Enable: false, - Probe: ProbeOption{ - Interval: DefaultProbeInterval, - }, - }, LogMaxSize: DefaultLogRotateMaxSize, LogMaxAge: DefaultLogRotateMaxAge, LogMaxBackups: DefaultLogRotateMaxBackups, diff --git a/client/config/peerhost_linux.go b/client/config/peerhost_linux.go index 7c4e5237c9e..f16fd7c7455 100644 --- a/client/config/peerhost_linux.go +++ b/client/config/peerhost_linux.go @@ -181,12 +181,6 @@ var peerHostConfig = func() *DaemonOption { Announcer: AnnouncerOption{ SchedulerInterval: DefaultAnnouncerSchedulerInterval, }, - NetworkTopology: NetworkTopologyOption{ - Enable: false, - Probe: ProbeOption{ - Interval: DefaultProbeInterval, - }, - }, LogMaxSize: DefaultLogRotateMaxSize, LogMaxAge: DefaultLogRotateMaxAge, LogMaxBackups: DefaultLogRotateMaxBackups, diff --git a/client/config/peerhost_test.go b/client/config/peerhost_test.go index 915980b589d..fc92b96f487 100644 --- a/client/config/peerhost_test.go +++ b/client/config/peerhost_test.go @@ -518,12 +518,6 @@ func TestPeerHostOption_Load(t *testing.T) { Announcer: AnnouncerOption{ SchedulerInterval: 1000000000, }, - NetworkTopology: NetworkTopologyOption{ - Enable: true, - Probe: ProbeOption{ - Interval: 20 * time.Minute, - }, - }, } peerHostOptionYAML := &DaemonOption{} @@ -763,24 +757,6 @@ func TestPeerHostOption_Validate(t *testing.T) { assert.EqualError(err, "certSpec requires parameter validityPeriod") }, }, - { - name: "probe requires parameter interval", - config: NewDaemonConfig(), - mock: func(cfg *DaemonConfig) { - cfg.Scheduler.NetAddrs = []dfnet.NetAddr{ - { - Type: dfnet.TCP, - Addr: "127.0.0.1:8002", - }, - } - cfg.NetworkTopology.Enable = true - cfg.NetworkTopology.Probe.Interval = 0 - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "probe requires parameter interval") - }, - }, } for _, tc := range tests { diff --git a/client/config/testdata/config/daemon.yaml b/client/config/testdata/config/daemon.yaml index b22cb594243..551517abbef 100644 --- a/client/config/testdata/config/daemon.yaml +++ b/client/config/testdata/config/daemon.yaml @@ -192,8 +192,3 @@ network: announcer: schedulerInterval: 1s - -networkTopology: - enable: true - probe: - interval: 20m diff --git a/client/daemon/daemon.go b/client/daemon/daemon.go index b61ade66db6..8d503be51c1 100644 --- a/client/daemon/daemon.go +++ b/client/daemon/daemon.go @@ -50,7 +50,6 @@ import ( "d7y.io/dragonfly/v2/client/daemon/announcer" "d7y.io/dragonfly/v2/client/daemon/gc" "d7y.io/dragonfly/v2/client/daemon/metrics" - "d7y.io/dragonfly/v2/client/daemon/networktopology" "d7y.io/dragonfly/v2/client/daemon/objectstorage" "d7y.io/dragonfly/v2/client/daemon/peer" "d7y.io/dragonfly/v2/client/daemon/pex" @@ -113,7 +112,6 @@ type clientDaemon struct { schedulerClient schedulerclient.V1 certifyClient *certify.Certify announcer announcer.Announcer - networkTopology networktopology.NetworkTopology } func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) { @@ -790,19 +788,6 @@ func (cd *clientDaemon) Serve() error { } }() - // serve network topology - if cd.Option.NetworkTopology.Enable { - cd.networkTopology, err = networktopology.NewNetworkTopology(&cd.Option, cd.schedPeerHost.Id, cd.schedPeerHost.RpcPort, cd.schedPeerHost.DownPort, cd.schedulerClient) - if err != nil { - logger.Errorf("failed to create network topology: %v", err) - return err - } - - // serve network topology service - logger.Infof("serve network topology") - go cd.networkTopology.Serve() - } - if cd.Option.AliveTime.Duration > 0 { g.Go(func() error { for { @@ -955,10 +940,6 @@ func (cd *clientDaemon) Stop() { logger.Errorf("announcer stop failed %s", err) } - if cd.networkTopology != nil { - cd.networkTopology.Stop() - } - if err := cd.dynconfig.Stop(); err != nil { logger.Errorf("dynconfig client closed failed %s", err) } else { diff --git a/client/daemon/networktopology/mocks/network_topology_mock.go b/client/daemon/networktopology/mocks/network_topology_mock.go deleted file mode 100644 index a89ce53eb4d..00000000000 --- a/client/daemon/networktopology/mocks/network_topology_mock.go +++ /dev/null @@ -1,63 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: network_topology.go -// -// Generated by this command: -// -// mockgen -destination mocks/network_topology_mock.go -source network_topology.go -package mocks -// - -// Package mocks is a generated GoMock package. -package mocks - -import ( - reflect "reflect" - - gomock "go.uber.org/mock/gomock" -) - -// MockNetworkTopology is a mock of NetworkTopology interface. -type MockNetworkTopology struct { - ctrl *gomock.Controller - recorder *MockNetworkTopologyMockRecorder -} - -// MockNetworkTopologyMockRecorder is the mock recorder for MockNetworkTopology. -type MockNetworkTopologyMockRecorder struct { - mock *MockNetworkTopology -} - -// NewMockNetworkTopology creates a new mock instance. -func NewMockNetworkTopology(ctrl *gomock.Controller) *MockNetworkTopology { - mock := &MockNetworkTopology{ctrl: ctrl} - mock.recorder = &MockNetworkTopologyMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockNetworkTopology) EXPECT() *MockNetworkTopologyMockRecorder { - return m.recorder -} - -// Serve mocks base method. -func (m *MockNetworkTopology) Serve() { - m.ctrl.T.Helper() - m.ctrl.Call(m, "Serve") -} - -// Serve indicates an expected call of Serve. -func (mr *MockNetworkTopologyMockRecorder) Serve() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Serve", reflect.TypeOf((*MockNetworkTopology)(nil).Serve)) -} - -// Stop mocks base method. -func (m *MockNetworkTopology) Stop() { - m.ctrl.T.Helper() - m.ctrl.Call(m, "Stop") -} - -// Stop indicates an expected call of Stop. -func (mr *MockNetworkTopologyMockRecorder) Stop() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stop", reflect.TypeOf((*MockNetworkTopology)(nil).Stop)) -} diff --git a/client/daemon/networktopology/network_topology.go b/client/daemon/networktopology/network_topology.go deleted file mode 100644 index e4ba0eb2f00..00000000000 --- a/client/daemon/networktopology/network_topology.go +++ /dev/null @@ -1,204 +0,0 @@ -/* - * Copyright 2023 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 mocks/network_topology_mock.go -source network_topology.go -package mocks - -package networktopology - -import ( - "context" - "io" - "sync" - "time" - - "google.golang.org/protobuf/types/known/durationpb" - "google.golang.org/protobuf/types/known/timestamppb" - - v1 "d7y.io/api/v2/pkg/apis/common/v1" - schedulerv1 "d7y.io/api/v2/pkg/apis/scheduler/v1" - - "d7y.io/dragonfly/v2/client/config" - logger "d7y.io/dragonfly/v2/internal/dflog" - "d7y.io/dragonfly/v2/pkg/container/set" - "d7y.io/dragonfly/v2/pkg/net/ping" - schedulerclient "d7y.io/dragonfly/v2/pkg/rpc/scheduler/client" -) - -type NetworkTopology interface { - // Serve starts network topology server. - Serve() - - // Stop stops network topology server. - Stop() -} - -// networkTopology implements NetworkTopology. -type networkTopology struct { - config *config.DaemonOption - hostID string - daemonPort int32 - daemonDownloadPort int32 - schedulerClient schedulerclient.V1 - done chan struct{} -} - -// NewNetworkTopology returns a new NetworkTopology interface. -func NewNetworkTopology(cfg *config.DaemonOption, hostID string, daemonPort int32, daemonDownloadPort int32, - schedulerClient schedulerclient.V1) (NetworkTopology, error) { - return &networkTopology{ - config: cfg, - hostID: hostID, - daemonPort: daemonPort, - daemonDownloadPort: daemonDownloadPort, - schedulerClient: schedulerClient, - done: make(chan struct{}), - }, nil -} - -// Serve starts network topology server. -func (nt *networkTopology) Serve() { - tick := time.NewTicker(nt.config.NetworkTopology.Probe.Interval) - for { - select { - case <-tick.C: - if err := nt.syncProbes(); err != nil { - logger.Error(err) - } - case <-nt.done: - return - } - } -} - -// Stop stops network topology server. -func (nt *networkTopology) Stop() { - close(nt.done) -} - -// syncProbes syncs probes to scheduler. -func (nt *networkTopology) syncProbes() error { - host := &v1.Host{ - Id: nt.hostID, - Ip: nt.config.Host.AdvertiseIP.String(), - Hostname: nt.config.Host.Hostname, - Port: nt.daemonPort, - DownloadPort: nt.daemonDownloadPort, - Location: nt.config.Host.Location, - Idc: nt.config.Host.IDC, - } - - stream, err := nt.schedulerClient.SyncProbes(context.Background(), &schedulerv1.SyncProbesRequest{ - Host: host, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }, - }) - if err != nil { - return err - } - - resp, err := stream.Recv() - if err != nil { - if err == io.EOF { - return nil - } - - return err - } - - // Ping the destination host with the ICMP protocol. - probes, failedProbes := nt.pingHosts(resp.Hosts) - if len(probes) > 0 { - if err := stream.Send(&schedulerv1.SyncProbesRequest{ - Host: host, - Request: &schedulerv1.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv1.ProbeFinishedRequest{ - Probes: probes, - }, - }, - }); err != nil { - return err - } - } - - if len(failedProbes) > 0 { - if err := stream.Send(&schedulerv1.SyncProbesRequest{ - Host: host, - Request: &schedulerv1.SyncProbesRequest_ProbeFailedRequest{ - ProbeFailedRequest: &schedulerv1.ProbeFailedRequest{ - Probes: failedProbes, - }, - }, - }); err != nil { - return err - } - } - - return nil -} - -// Ping the destination host with the ICMP protocol. If the host is unreachable, -// we will send the failed probe result to the scheduler. If the host is reachable, -// we will send the probe result to the scheduler. -func (nt *networkTopology) pingHosts(destHosts []*v1.Host) ([]*schedulerv1.Probe, []*schedulerv1.FailedProbe) { - var ( - probes = set.NewSafeSet[*schedulerv1.Probe]() - failedProbes = set.NewSafeSet[*schedulerv1.FailedProbe]() - ) - - wg := &sync.WaitGroup{} - wg.Add(len(destHosts)) - for _, destHost := range destHosts { - go func(destHost *v1.Host) { - defer wg.Done() - - stats, err := ping.Ping(destHost.Ip) - if err != nil { - failedProbes.Add(&schedulerv1.FailedProbe{ - Host: &v1.Host{ - Id: destHost.Id, - Ip: destHost.Ip, - Hostname: destHost.Hostname, - Port: destHost.Port, - DownloadPort: destHost.DownloadPort, - Location: destHost.Location, - Idc: destHost.Idc, - }, - Description: err.Error(), - }) - - return - } - - probes.Add(&schedulerv1.Probe{ - Host: &v1.Host{ - Id: destHost.Id, - Ip: destHost.Ip, - Hostname: destHost.Hostname, - Port: destHost.Port, - DownloadPort: destHost.DownloadPort, - Location: destHost.Location, - Idc: destHost.Idc, - }, - Rtt: durationpb.New(stats.AvgRtt), - CreatedAt: timestamppb.New(time.Now()), - }) - }(destHost) - } - - wg.Wait() - return probes.Values(), failedProbes.Values() -} diff --git a/client/daemon/networktopology/network_topology_test.go b/client/daemon/networktopology/network_topology_test.go deleted file mode 100644 index 6708ec5c71c..00000000000 --- a/client/daemon/networktopology/network_topology_test.go +++ /dev/null @@ -1,545 +0,0 @@ -/* - * Copyright 2023 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. - */ - -package networktopology - -import ( - "context" - "errors" - "io" - "net" - "reflect" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "go.uber.org/mock/gomock" - - v1 "d7y.io/api/v2/pkg/apis/common/v1" - schedulerv1 "d7y.io/api/v2/pkg/apis/scheduler/v1" - schedulerv1mocks "d7y.io/api/v2/pkg/apis/scheduler/v1/mocks" - - "d7y.io/dragonfly/v2/client/config" - "d7y.io/dragonfly/v2/pkg/idgen" - "d7y.io/dragonfly/v2/pkg/rpc/scheduler/client/mocks" - schedulerclientmocks "d7y.io/dragonfly/v2/pkg/rpc/scheduler/client/mocks" -) - -var ( - mockDaemonConfig = &config.DaemonOption{ - Host: config.HostOption{ - Location: mockHostLocation, - IDC: mockHostIDC, - Hostname: idgen.HostIDV2("127.0.0.1", "bar"), - AdvertiseIP: net.IPv4(127, 0, 0, 1), - }, - NetworkTopology: config.NetworkTopologyOption{ - Enable: true, - Probe: config.ProbeOption{ - Interval: 200 * time.Millisecond, - }, - }, - } - - mockPort = 8000 - mockDownloadPort = 8001 - mockHostLocation = "bar" - mockHostIDC = "baz" - - mockHost = &v1.Host{ - Id: "foo", - Ip: "127.0.0.1", - Hostname: idgen.HostIDV2("127.0.0.1", "foo"), - Port: int32(mockPort), - DownloadPort: int32(mockDownloadPort), - Location: mockHostLocation, - Idc: mockHostIDC, - } - - mockSeedHost = &v1.Host{ - Id: "bar", - Ip: "127.0.0.1", - Hostname: idgen.HostIDV2("127.0.0.1", "bar"), - Port: int32(mockPort), - DownloadPort: int32(mockDownloadPort), - Location: mockHostLocation, - Idc: mockHostIDC, - } -) - -func Test_NewNetworkTopology(t *testing.T) { - tests := []struct { - name string - expect func(t *testing.T, n NetworkTopology, err error) - }{ - { - name: "new network topology", - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.Equal(reflect.TypeOf(n).Elem().Name(), "networkTopology") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - schedulerClient := schedulerclientmocks.NewMockV1(ctl) - n, err := NewNetworkTopology(mockDaemonConfig, mockSeedHost.Id, int32(mockPort), int32(mockDownloadPort), schedulerClient) - tc.expect(t, n, err) - }) - } -} - -func TestNetworkTopology_Serve(t *testing.T) { - tests := []struct { - name string - sleep func() - mock func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) - expect func(t *testing.T, n NetworkTopology, err error) - }{ - { - name: "synchronize probes", - sleep: func() { - time.Sleep(300 * time.Millisecond) - }, - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(stream, nil).Times(1), - ms.Recv().Return(&schedulerv1.SyncProbesResponse{ - Hosts: []*v1.Host{mockHost}, - }, nil).Times(1), - ms.Send(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - go n.Serve() - }, - }, - { - name: "synchronize probes error", - sleep: func() { - time.Sleep(300 * time.Millisecond) - }, - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(nil, errors.New("synchronize probes error")).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - go n.Serve() - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - schedulerClient := schedulerclientmocks.NewMockV1(ctl) - stream := schedulerv1mocks.NewMockScheduler_SyncProbesClient(ctl) - tc.mock(schedulerClient.EXPECT(), stream, stream.EXPECT()) - n, err := NewNetworkTopology(mockDaemonConfig, mockSeedHost.Id, int32(mockPort), int32(mockDownloadPort), schedulerClient) - tc.expect(t, n, err) - tc.sleep() - n.Stop() - }) - } -} - -func TestNetworkTopology_syncProbes(t *testing.T) { - tests := []struct { - name string - mock func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) - expect func(t *testing.T, n NetworkTopology, err error) - }{ - { - name: "synchronize probes and send ProbeFinishedRequest", - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(stream, nil).Times(1), - ms.Recv().Return(&schedulerv1.SyncProbesResponse{ - Hosts: []*v1.Host{mockHost}, - }, nil).Times(1), - ms.Send(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(n.(*networkTopology).syncProbes()) - }, - }, - { - name: "synchronize fail probes and send ProbeFailedRequest", - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(stream, nil).Times(1), - ms.Recv().Return(&schedulerv1.SyncProbesResponse{ - Hosts: []*v1.Host{ - { - Id: idgen.HostIDV2("172.0.0.1", "foo"), - Ip: "172.0.0.1", - Hostname: "foo", - Port: 8003, - DownloadPort: 8001, - Location: "location", - Idc: "idc", - }, - }, - }, nil).Times(1), - ms.Send(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeFailedRequest{ - ProbeFailedRequest: &schedulerv1.ProbeFailedRequest{ - Probes: []*schedulerv1.FailedProbe{ - { - Host: &v1.Host{ - Id: idgen.HostIDV2("172.0.0.1", "foo"), - Ip: "172.0.0.1", - Hostname: "foo", - Port: 8003, - DownloadPort: 8001, - Location: "location", - Idc: "idc", - }, - Description: "receive packet failed", - }, - }, - }, - }, - }).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(n.(*networkTopology).syncProbes()) - }, - }, - { - name: "synchronize probes and fail probes, send ProbeFinishedRequest and ProbeFailedRequest", - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(stream, nil).Times(1), - ms.Recv().Return(&schedulerv1.SyncProbesResponse{ - Hosts: []*v1.Host{ - mockHost, - { - Id: idgen.HostIDV2("172.0.0.1", "foo"), - Ip: "172.0.0.1", - Hostname: "foo", - Port: 8003, - DownloadPort: 8001, - Location: "location", - Idc: "idc", - }, - }, - }, nil).Times(1), - ms.Send(gomock.Any()).Return(nil).Times(1), - ms.Send(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeFailedRequest{ - ProbeFailedRequest: &schedulerv1.ProbeFailedRequest{ - Probes: []*schedulerv1.FailedProbe{ - { - Host: &v1.Host{ - Id: idgen.HostIDV2("172.0.0.1", "foo"), - Ip: "172.0.0.1", - Hostname: "foo", - Port: 8003, - DownloadPort: 8001, - Location: "location", - Idc: "idc", - }, - Description: "receive packet failed", - }, - }, - }, - }, - }).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(n.(*networkTopology).syncProbes()) - }, - }, - { - name: "syncProbe error", - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(nil, errors.New("syncProbe error")).Times(1) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(n.(*networkTopology).syncProbes(), "syncProbe error") - }, - }, - { - name: "receive error", - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(stream, nil).Times(1), - ms.Recv().Return(nil, errors.New("receive error")).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(n.(*networkTopology).syncProbes(), "receive error") - }, - }, - { - name: "receive EOF", - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(stream, nil).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(n.(*networkTopology).syncProbes()) - }, - }, - { - name: "send ProbeFinishedRequest error", - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(stream, nil).Times(1), - ms.Recv().Return(&schedulerv1.SyncProbesResponse{ - Hosts: []*v1.Host{mockHost}, - }, nil).Times(1), - ms.Send(gomock.Any()).Return(errors.New("send ProbeFinishedRequest error")).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(n.(*networkTopology).syncProbes(), "send ProbeFinishedRequest error") - }, - }, - { - name: "send ProbeFailedRequest error", - mock: func(mv *mocks.MockV1MockRecorder, stream *schedulerv1mocks.MockScheduler_SyncProbesClient, - ms *schedulerv1mocks.MockScheduler_SyncProbesClientMockRecorder) { - gomock.InOrder( - mv.SyncProbes(gomock.Eq(context.Background()), gomock.Eq(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }})).Return(stream, nil).Times(1), - ms.Recv().Return(&schedulerv1.SyncProbesResponse{ - Hosts: []*v1.Host{ - { - Id: idgen.HostIDV2("172.0.0.1", "foo"), - Ip: "172.0.0.1", - Hostname: "foo", - Port: 8003, - DownloadPort: 8001, - Location: "location", - Idc: "idc", - }, - }, - }, nil).Times(1), - ms.Send(&schedulerv1.SyncProbesRequest{ - Host: mockSeedHost, - Request: &schedulerv1.SyncProbesRequest_ProbeFailedRequest{ - ProbeFailedRequest: &schedulerv1.ProbeFailedRequest{ - Probes: []*schedulerv1.FailedProbe{ - { - Host: &v1.Host{ - Id: idgen.HostIDV2("172.0.0.1", "foo"), - Ip: "172.0.0.1", - Hostname: "foo", - Port: 8003, - DownloadPort: 8001, - Location: "location", - Idc: "idc", - }, - Description: "receive packet failed", - }, - }, - }, - }, - }).Return(errors.New("send ProbeFailedRequest error")).Times(1), - ) - }, - expect: func(t *testing.T, n NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(n.(*networkTopology).syncProbes(), "send ProbeFailedRequest error") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - schedulerClient := schedulerclientmocks.NewMockV1(ctl) - stream := schedulerv1mocks.NewMockScheduler_SyncProbesClient(ctl) - tc.mock(schedulerClient.EXPECT(), stream, stream.EXPECT()) - n, err := NewNetworkTopology(mockDaemonConfig, mockSeedHost.Id, int32(mockPort), int32(mockDownloadPort), schedulerClient) - tc.expect(t, n, err) - n.Stop() - }) - } -} - -func TestNetworkTopology_pingHosts(t *testing.T) { - tests := []struct { - name string - destHosts []*v1.Host - expect func(t *testing.T, n NetworkTopology, err error, destHosts []*v1.Host) - }{ - { - name: "ping hosts and collect probes", - destHosts: []*v1.Host{mockHost}, - expect: func(t *testing.T, n NetworkTopology, err error, destHosts []*v1.Host) { - assert := assert.New(t) - assert.NoError(err) - probes, failProbes := n.(*networkTopology).pingHosts(destHosts) - assert.Equal(len(probes), 1) - assert.Equal(len(failProbes), 0) - }, - }, - { - name: "ping hosts and collect fail probes", - destHosts: []*v1.Host{ - { - Id: idgen.HostIDV2("172.0.0.1", "foo"), - Ip: "172.0.0.1", - Hostname: "foo", - Port: 8003, - DownloadPort: 8001, - Location: "location", - Idc: "idc", - }, - }, - expect: func(t *testing.T, n NetworkTopology, err error, destHosts []*v1.Host) { - assert := assert.New(t) - assert.NoError(err) - probes, failProbes := n.(*networkTopology).pingHosts(destHosts) - assert.Equal(len(probes), 0) - assert.Equal(len(failProbes), 1) - }, - }, - { - name: "ping hosts, collect probes and fail probes", - destHosts: []*v1.Host{ - mockHost, - { - Id: idgen.HostIDV2("172.0.0.1", "foo"), - Ip: "172.0.0.1", - Hostname: "foo", - Port: 8003, - DownloadPort: 8001, - Location: "location", - Idc: "idc", - }, - }, - expect: func(t *testing.T, n NetworkTopology, err error, destHosts []*v1.Host) { - assert := assert.New(t) - assert.NoError(err) - probes, failProbes := n.(*networkTopology).pingHosts(destHosts) - assert.Equal(len(probes), 1) - assert.Equal(len(failProbes), 1) - }, - }, - { - name: "dest hosts is empty", - destHosts: []*v1.Host{}, - expect: func(t *testing.T, n NetworkTopology, err error, destHosts []*v1.Host) { - assert := assert.New(t) - assert.NoError(err) - probes, failProbes := n.(*networkTopology).pingHosts(destHosts) - assert.Equal(len(probes), 0) - assert.Equal(len(failProbes), 0) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - schedulerClient := schedulerclientmocks.NewMockV1(ctl) - n, err := NewNetworkTopology(mockDaemonConfig, mockSeedHost.Id, int32(mockPort), int32(mockDownloadPort), schedulerClient) - tc.expect(t, n, err, tc.destHosts) - }) - } -} diff --git a/client/daemon/peer/peertask_dummy.go b/client/daemon/peer/peertask_dummy.go index 7aa4a656511..1b9e8576380 100644 --- a/client/daemon/peer/peertask_dummy.go +++ b/client/daemon/peer/peertask_dummy.go @@ -64,10 +64,6 @@ func (d *dummySchedulerClient) AnnounceTask(ctx context.Context, request *schedu panic("should not call this function") } -func (d *dummySchedulerClient) SyncProbes(ctx context.Context, req *schedulerv1.SyncProbesRequest, opts ...grpc.CallOption) (schedulerv1.Scheduler_SyncProbesClient, error) { - panic("should not call this function") -} - func (d *dummySchedulerClient) Close() error { return nil } diff --git a/deploy/docker-compose/template/scheduler.template.yaml b/deploy/docker-compose/template/scheduler.template.yaml index e4c0f2b9b64..e5e89c5088e 100644 --- a/deploy/docker-compose/template/scheduler.template.yaml +++ b/deploy/docker-compose/template/scheduler.template.yaml @@ -86,8 +86,6 @@ database: brokerDB: 1 # Redis backend DB. backendDB: 2 - # Network topology DB. - networkTopologyDB: 3 # Resource configuration. resource: @@ -147,18 +145,6 @@ job: # Number of workers in local queue. localWorkerNum: 1000 -# Network topology to collect configuration. -networkTopology: - # enable network topology service, including probe, network topology collection. - enable: true - # collectInterval is the interval of collecting network topology. - collectInterval: 2h - probe: - # queueLength is the length of probe queue. - queueLength: 5 - # count is the number of probing hosts. - count: 10 - # Store task download information. storage: # maxSize sets the maximum size in megabytes of storage file. diff --git a/deploy/helm-charts b/deploy/helm-charts index 4322fe6ba5a..eb5560e4f0b 160000 --- a/deploy/helm-charts +++ b/deploy/helm-charts @@ -1 +1 @@ -Subproject commit 4322fe6ba5ab7ac758e71afb70233bdd426ec759 +Subproject commit eb5560e4f0bd82fda085b73bc0743eaab4e3d9ff diff --git a/go.mod b/go.mod index 0ef27aea1a4..89ddb571267 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module d7y.io/dragonfly/v2 go 1.21 require ( - d7y.io/api/v2 v2.0.162 + d7y.io/api/v2 v2.0.164 github.com/MysteriousPotato/go-lockable v1.0.0 github.com/RichardKnop/machinery v1.10.8 github.com/Showmax/go-fqdn v1.0.0 @@ -32,7 +32,6 @@ require ( github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a github.com/go-playground/validator/v10 v10.22.0 github.com/go-redis/cache/v9 v9.0.0 - github.com/go-redis/redismock/v9 v9.2.0 github.com/go-sql-driver/mysql v1.7.1 github.com/gocarina/gocsv v0.0.0-20221105105431-c8ef78125b99 github.com/gofrs/flock v0.8.1 diff --git a/go.sum b/go.sum index d5f63cacb05..9e08069658e 100644 --- a/go.sum +++ b/go.sum @@ -53,8 +53,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= -d7y.io/api/v2 v2.0.162 h1:u96AbaVT1wGwYMONjm+3B6AdFtrz0m2y0b8eDaBOwW8= -d7y.io/api/v2 v2.0.162/go.mod h1:VOnTWgLrGtivgyyofZCfiSDTAKDJ9ohVqM6l3S8EPCE= +d7y.io/api/v2 v2.0.164 h1:bveQTYw6LFVTj6uEiXqqKCKxrFSbJPIFpiEugw2BUHs= +d7y.io/api/v2 v2.0.164/go.mod h1:VOnTWgLrGtivgyyofZCfiSDTAKDJ9ohVqM6l3S8EPCE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.0.0/go.mod h1:uGG2W01BaETf0Ozp+QxxKJdMBNRWPdstHG0Fmdwn1/U= @@ -543,8 +543,6 @@ github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1 github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg= github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w= -github.com/go-redis/redismock/v9 v9.2.0 h1:ZrMYQeKPECZPjOj5u9eyOjg8Nnb0BS9lkVIZ6IpsKLw= -github.com/go-redis/redismock/v9 v9.2.0/go.mod h1:18KHfGDK4Y6c2R0H38EUGWAdc7ZQS9gfYxc94k7rWT0= github.com/go-redsync/redsync/v4 v4.8.1 h1:rq2RvdTI0obznMdxKUWGdmmulo7lS9yCzb8fgDKOlbM= github.com/go-redsync/redsync/v4 v4.8.1/go.mod h1:LmUAsQuQxhzZAoGY7JS6+dNhNmZyonMZiiEDY9plotM= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= diff --git a/manager/rpcserver/manager_server_v1.go b/manager/rpcserver/manager_server_v1.go index c562dd10936..5efffd5e8d4 100644 --- a/manager/rpcserver/manager_server_v1.go +++ b/manager/rpcserver/manager_server_v1.go @@ -678,17 +678,6 @@ func (s *managerServerV1) ListBuckets(ctx context.Context, req *managerv1.ListBu return nil, status.Error(codes.Internal, "object storage is disabled") } - var pbListBucketsResponse managerv1.ListBucketsResponse - cacheKey := pkgredis.MakeBucketKeyInManager(s.config.ObjectStorage.Name) - - // Cache hit. - if err := s.cache.Get(ctx, cacheKey, &pbListBucketsResponse); err != nil { - log.Warnf("%s cache miss because of %s", cacheKey, err.Error()) - } else { - log.Debugf("%s cache hit", cacheKey) - return &pbListBucketsResponse, nil - } - // Cache miss and search buckets. buckets, err := s.objectStorage.ListBucketMetadatas(ctx) if err != nil { @@ -696,22 +685,13 @@ func (s *managerServerV1) ListBuckets(ctx context.Context, req *managerv1.ListBu } // Construct schedulers. + var pbListBucketsResponse managerv1.ListBucketsResponse for _, bucket := range buckets { pbListBucketsResponse.Buckets = append(pbListBucketsResponse.Buckets, &managerv1.Bucket{ Name: bucket.Name, }) } - // Cache data. - if err := s.cache.Once(&cachev9.Item{ - Ctx: ctx, - Key: cacheKey, - Value: &pbListBucketsResponse, - TTL: s.cache.TTL, - }); err != nil { - log.Error(err) - } - return &pbListBucketsResponse, nil } diff --git a/pkg/redis/redis.go b/pkg/redis/redis.go index 5f50f2dba3f..d2d741f84f7 100644 --- a/pkg/redis/redis.go +++ b/pkg/redis/redis.go @@ -19,7 +19,6 @@ package redis import ( "context" "fmt" - "strings" "github.com/redis/go-redis/v9" @@ -55,18 +54,6 @@ const ( // ApplicationsNamespace prefix of applications namespace cache key. ApplicationsNamespace = "applications" - - // BucketsNamespace prefix of buckets namespace cache key. - BucketsNamespace = "buckets" - - // NetworkTopologyNamespace prefix of network topology namespace cache key. - NetworkTopologyNamespace = "network-topology" - - // ProbesNamespace prefix of probes namespace cache key. - ProbesNamespace = "probes" - - // ProbedCountNamespace prefix of probed count namespace cache key. - ProbedCountNamespace = "probed-count" ) // NewRedis returns a new redis client. @@ -134,11 +121,6 @@ func MakeApplicationsKeyInManager() string { return MakeNamespaceKeyInManager(ApplicationsNamespace) } -// MakeBucketKeyInManager make bucket key in manager. -func MakeBucketKeyInManager(name string) string { - return MakeKeyInManager(BucketsNamespace, name) -} - // MakeNamespaceKeyInScheduler make namespace key in scheduler. func MakeNamespaceKeyInScheduler(namespace string) string { return fmt.Sprintf("%s:%s", types.SchedulerName, namespace) @@ -177,38 +159,3 @@ func MakePersistentCacheHostKeyInScheduler(schedulerClusterID uint, hostID strin func MakePersistentCacheHostsInScheduler(schedulerClusterID uint) string { return MakeKeyInScheduler(SchedulerClustersNamespace, fmt.Sprintf("%d:%s", schedulerClusterID, PersistentCacheHostsNamespace)) } - -// MakeNetworkTopologyKeyInScheduler make network topology key in scheduler. -func MakeNetworkTopologyKeyInScheduler(srcHostID, destHostID string) string { - return MakeKeyInScheduler(NetworkTopologyNamespace, fmt.Sprintf("%s:%s", srcHostID, destHostID)) -} - -// ParseNetworkTopologyKeyInScheduler parse network topology key in scheduler. -func ParseNetworkTopologyKeyInScheduler(key string) (string, string, string, string, error) { - elements := strings.Split(key, KeySeparator) - if len(elements) != 4 { - return "", "", "", "", fmt.Errorf("invalid network topology key: %s", key) - } - - return elements[0], elements[1], elements[2], elements[3], nil -} - -// MakeProbesKeyInScheduler make probes key in scheduler. -func MakeProbesKeyInScheduler(srcHostID, destHostID string) string { - return MakeKeyInScheduler(ProbesNamespace, fmt.Sprintf("%s:%s", srcHostID, destHostID)) -} - -// ParseProbedCountKeyInScheduler parse probed count key in scheduler. -func ParseProbedCountKeyInScheduler(key string) (string, string, string, error) { - elements := strings.Split(key, KeySeparator) - if len(elements) != 3 { - return "", "", "", fmt.Errorf("invalid probed count key: %s", key) - } - - return elements[0], elements[1], elements[2], nil -} - -// MakeProbedCountKeyInScheduler make probed count key in scheduler. -func MakeProbedCountKeyInScheduler(hostID string) string { - return MakeKeyInScheduler(ProbedCountNamespace, hostID) -} diff --git a/pkg/redis/redis_test.go b/pkg/redis/redis_test.go index 51abce73e58..8c31ed8206e 100644 --- a/pkg/redis/redis_test.go +++ b/pkg/redis/redis_test.go @@ -418,36 +418,6 @@ func Test_MakeApplicationsKeyInManager(t *testing.T) { } } -func Test_MakeBucketKeyInManager(t *testing.T) { - tests := []struct { - name string - namespace string - expect func(t *testing.T, s string) - }{ - { - name: "make bucket key in manager", - namespace: "baz", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "manager:buckets:baz") - }, - }, - { - name: "namespace is empty", - namespace: "", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "manager:buckets:") - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - tc.expect(t, MakeBucketKeyInManager(tc.namespace)) - }) - } -} - func Test_MakeNamespaceKeyInScheduler(t *testing.T) { tests := []struct { name string @@ -528,211 +498,3 @@ func Test_MakeKeyInScheduler(t *testing.T) { }) } } - -func Test_MakeNetworkTopologyKeyInScheduler(t *testing.T) { - tests := []struct { - name string - srcHostID string - destHostID string - expect func(t *testing.T, s string) - }{ - { - name: "make network topology key in scheduler", - srcHostID: "foo", - destHostID: "bar", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:network-topology:foo:bar") - }, - }, - { - name: "source host id is empty", - srcHostID: "", - destHostID: "bar", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:network-topology::bar") - }, - }, - { - name: "destination host id is empty", - srcHostID: "foo", - destHostID: "", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:network-topology:foo:") - }, - }, - { - name: "source host id and destination host id are empty", - srcHostID: "", - destHostID: "", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:network-topology::") - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - tc.expect(t, MakeNetworkTopologyKeyInScheduler(tc.srcHostID, tc.destHostID)) - }) - } -} - -func Test_MakeProbesKeyInScheduler(t *testing.T) { - tests := []struct { - name string - srcHostID string - destHostID string - expect func(t *testing.T, s string) - }{ - { - name: "make probes key in scheduler", - srcHostID: "foo", - destHostID: "bar", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:probes:foo:bar") - }, - }, - { - name: "source host id is empty", - srcHostID: "", - destHostID: "bar", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:probes::bar") - }, - }, - { - name: "destination host id is empty", - srcHostID: "foo", - destHostID: "", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:probes:foo:") - }, - }, - { - name: "source host id and destination host id are empty", - srcHostID: "", - destHostID: "", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:probes::") - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - tc.expect(t, MakeProbesKeyInScheduler(tc.srcHostID, tc.destHostID)) - }) - } -} - -func Test_ParseNetworkTopologyKeyInScheduler(t *testing.T) { - tests := []struct { - name string - key string - expect func(t *testing.T, schedulerNamespace, networkTopologyNamespace, srcHostID, destHostID string, err error) - }{ - { - name: "parse network topology key in scheduler", - key: "schedulers:network-topology:foo:bar", - expect: func(t *testing.T, schedulerNamespace, networkTopologyNamespace, srcHostID, destHostID string, err error) { - assert := assert.New(t) - assert.Equal(schedulerNamespace, SchedulersNamespace) - assert.Equal(networkTopologyNamespace, NetworkTopologyNamespace) - assert.Equal(srcHostID, "foo") - assert.Equal(destHostID, "bar") - assert.NoError(err) - }, - }, - { - name: "parse network topology key in scheduler error", - key: "foo", - expect: func(t *testing.T, schedulerNamespace, networkTopologyNamespace, srcHostID, destHostID string, err error) { - assert := assert.New(t) - assert.Equal(schedulerNamespace, "") - assert.Equal(networkTopologyNamespace, "") - assert.Equal(srcHostID, "") - assert.Equal(destHostID, "") - assert.EqualError(err, "invalid network topology key: foo") - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - schedulerNamespace, networkTopologyNamespace, srcHostID, destHostID, err := ParseNetworkTopologyKeyInScheduler(tc.key) - tc.expect(t, schedulerNamespace, networkTopologyNamespace, srcHostID, destHostID, err) - }) - } -} - -func Test_MakeProbedCountKeyInScheduler(t *testing.T) { - tests := []struct { - name string - hostID string - expect func(t *testing.T, s string) - }{ - { - name: "make probed count key in scheduler", - hostID: "foo", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:probed-count:foo") - }, - }, - { - name: "host id is empty", - hostID: "", - expect: func(t *testing.T, s string) { - assert := assert.New(t) - assert.Equal(s, "scheduler:probed-count:") - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - tc.expect(t, MakeProbedCountKeyInScheduler(tc.hostID)) - }) - } -} - -func Test_ParseProbedCountKeyInScheduler(t *testing.T) { - tests := []struct { - name string - key string - expect func(t *testing.T, schedulerNamespace, probedCountNamespace, hostID string, err error) - }{ - { - name: "parse probed count key in scheduler", - key: "schedulers:probed-count:foo", - expect: func(t *testing.T, schedulerNamespace, probedCountNamespace, hostID string, err error) { - assert := assert.New(t) - assert.Equal(schedulerNamespace, SchedulersNamespace) - assert.Equal(probedCountNamespace, probedCountNamespace) - assert.Equal(hostID, "foo") - assert.NoError(err) - }, - }, - { - name: "parse probed count key in scheduler error", - key: "foo", - expect: func(t *testing.T, schedulerNamespace, probedCountNamespace, hostID string, err error) { - assert := assert.New(t) - assert.Equal(schedulerNamespace, "") - assert.Equal(probedCountNamespace, "") - assert.Equal(hostID, "") - assert.EqualError(err, "invalid probed count key: foo") - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - schedulerNamespace, probedCountNamespace, hostID, err := ParseProbedCountKeyInScheduler(tc.key) - tc.expect(t, schedulerNamespace, probedCountNamespace, hostID, err) - }) - } -} diff --git a/pkg/rpc/scheduler/client/client_v1.go b/pkg/rpc/scheduler/client/client_v1.go index badbcf8e47f..18a6fa12215 100644 --- a/pkg/rpc/scheduler/client/client_v1.go +++ b/pkg/rpc/scheduler/client/client_v1.go @@ -156,9 +156,6 @@ type V1 interface { // LeaveHost releases host in scheduler. LeaveHost(context.Context, *schedulerv1.LeaveHostRequest, ...grpc.CallOption) error - // SyncProbes sync probes of the host. - SyncProbes(context.Context, *schedulerv1.SyncProbesRequest, ...grpc.CallOption) (schedulerv1.Scheduler_SyncProbesClient, error) - // Close tears down the ClientConn and all underlying connections. Close() error } @@ -313,17 +310,3 @@ func (v *v1) LeaveHost(ctx context.Context, req *schedulerv1.LeaveHostRequest, o return eg.Wait() } - -// SyncProbes sync probes of the host. -func (v *v1) SyncProbes(ctx context.Context, req *schedulerv1.SyncProbesRequest, opts ...grpc.CallOption) (schedulerv1.Scheduler_SyncProbesClient, error) { - stream, err := v.SchedulerClient.SyncProbes( - context.WithValue(ctx, pkgbalancer.ContextKey, req.Host.Id), - opts..., - ) - if err != nil { - return nil, err - } - - // Send begin of piece. - return stream, stream.Send(req) -} diff --git a/pkg/rpc/scheduler/client/client_v2.go b/pkg/rpc/scheduler/client/client_v2.go index 588b98d7643..58b5d999433 100644 --- a/pkg/rpc/scheduler/client/client_v2.go +++ b/pkg/rpc/scheduler/client/client_v2.go @@ -148,9 +148,6 @@ type V2 interface { // DeleteHost releases host in scheduler. DeleteHost(context.Context, *schedulerv2.DeleteHostRequest, ...grpc.CallOption) error - // SyncProbes sync probes of the host. - SyncProbes(context.Context, *schedulerv2.SyncProbesRequest, ...grpc.CallOption) (schedulerv2.Scheduler_SyncProbesClient, error) - // Close tears down the ClientConn and all underlying connections. Close() error } @@ -282,17 +279,3 @@ func (v *v2) DeleteHost(ctx context.Context, req *schedulerv2.DeleteHostRequest, return eg.Wait() } - -// SyncProbes sync probes of the host. -func (v *v2) SyncProbes(ctx context.Context, req *schedulerv2.SyncProbesRequest, opts ...grpc.CallOption) (schedulerv2.Scheduler_SyncProbesClient, error) { - stream, err := v.SchedulerClient.SyncProbes( - context.WithValue(ctx, pkgbalancer.ContextKey, req.Host.Id), - opts..., - ) - if err != nil { - return nil, err - } - - // Send begin of piece. - return stream, stream.Send(req) -} diff --git a/pkg/rpc/scheduler/client/mocks/client_v1_mock.go b/pkg/rpc/scheduler/client/mocks/client_v1_mock.go index e0a7a5bdc1b..0e9b055b380 100644 --- a/pkg/rpc/scheduler/client/mocks/client_v1_mock.go +++ b/pkg/rpc/scheduler/client/mocks/client_v1_mock.go @@ -209,23 +209,3 @@ func (mr *MockV1MockRecorder) StatTask(arg0, arg1 any, arg2 ...any) *gomock.Call varargs := append([]any{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StatTask", reflect.TypeOf((*MockV1)(nil).StatTask), varargs...) } - -// SyncProbes mocks base method. -func (m *MockV1) SyncProbes(arg0 context.Context, arg1 *scheduler.SyncProbesRequest, arg2 ...grpc.CallOption) (scheduler.Scheduler_SyncProbesClient, error) { - m.ctrl.T.Helper() - varargs := []any{arg0, arg1} - for _, a := range arg2 { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "SyncProbes", varargs...) - ret0, _ := ret[0].(scheduler.Scheduler_SyncProbesClient) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// SyncProbes indicates an expected call of SyncProbes. -func (mr *MockV1MockRecorder) SyncProbes(arg0, arg1 any, arg2 ...any) *gomock.Call { - mr.mock.ctrl.T.Helper() - varargs := append([]any{arg0, arg1}, arg2...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SyncProbes", reflect.TypeOf((*MockV1)(nil).SyncProbes), varargs...) -} diff --git a/pkg/rpc/scheduler/client/mocks/client_v2_mock.go b/pkg/rpc/scheduler/client/mocks/client_v2_mock.go index bf2e5bc7fd3..dff5dcfbf55 100644 --- a/pkg/rpc/scheduler/client/mocks/client_v2_mock.go +++ b/pkg/rpc/scheduler/client/mocks/client_v2_mock.go @@ -191,23 +191,3 @@ func (mr *MockV2MockRecorder) StatTask(arg0, arg1 any, arg2 ...any) *gomock.Call varargs := append([]any{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StatTask", reflect.TypeOf((*MockV2)(nil).StatTask), varargs...) } - -// SyncProbes mocks base method. -func (m *MockV2) SyncProbes(arg0 context.Context, arg1 *scheduler.SyncProbesRequest, arg2 ...grpc.CallOption) (scheduler.Scheduler_SyncProbesClient, error) { - m.ctrl.T.Helper() - varargs := []any{arg0, arg1} - for _, a := range arg2 { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "SyncProbes", varargs...) - ret0, _ := ret[0].(scheduler.Scheduler_SyncProbesClient) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// SyncProbes indicates an expected call of SyncProbes. -func (mr *MockV2MockRecorder) SyncProbes(arg0, arg1 any, arg2 ...any) *gomock.Call { - mr.mock.ctrl.T.Helper() - varargs := append([]any{arg0, arg1}, arg2...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SyncProbes", reflect.TypeOf((*MockV2)(nil).SyncProbes), varargs...) -} diff --git a/scheduler/config/config.go b/scheduler/config/config.go index 2c5c820a82f..be986506851 100644 --- a/scheduler/config/config.go +++ b/scheduler/config/config.go @@ -133,9 +133,6 @@ type SchedulerConfig struct { // GC configuration. GC GCConfig `yaml:"gc" mapstructure:"gc"` - - // NetworkTopology configuration. - NetworkTopology NetworkTopologyConfig `yaml:"networkTopology" mapstructure:"networkTopology"` } type DatabaseConfig struct { @@ -289,9 +286,6 @@ type RedisConfig struct { // BackendDB is backend database name. BackendDB int `yaml:"backendDB" mapstructure:"backendDB"` - - // NetworkTopologyDB is network topology database name. - NetworkTopologyDB int `yaml:"networkTopologyDB" mapstructure:"networkTopologyDB"` } type MetricsConfig struct { @@ -342,33 +336,6 @@ type NetworkConfig struct { EnableIPv6 bool `mapstructure:"enableIPv6" yaml:"enableIPv6"` } -type NetworkTopologyConfig struct { - // CollectInterval is the interval of collecting network topology. - CollectInterval time.Duration `mapstructure:"collectInterval" yaml:"collectInterval"` - - // Probe is the configuration of probe. - Probe ProbeConfig `yaml:"probe" mapstructure:"probe"` - - // Cache is the configuration of cache. - Cache CacheConfig `yaml:"cache" mapstructure:"cache"` -} - -type ProbeConfig struct { - // QueueLength is the length of probe queue. - QueueLength int `mapstructure:"queueLength" yaml:"queueLength"` - - // Count is the number of probing hosts. - Count int `mapstructure:"count" yaml:"count"` -} - -type CacheConfig struct { - // Interval is cache cleanup interval. - Interval time.Duration `yaml:"interval" mapstructure:"interval"` - - // TTL is networkTopology cache items TTL. - TTL time.Duration `yaml:"ttl" mapstructure:"ttl"` -} - // New default configuration. func New() *Config { return &Config{ @@ -394,23 +361,11 @@ func New() *Config { HostGCInterval: DefaultSchedulerHostGCInterval, HostTTL: DefaultSchedulerHostTTL, }, - NetworkTopology: NetworkTopologyConfig{ - CollectInterval: DefaultSchedulerNetworkTopologyCollectInterval, - Probe: ProbeConfig{ - QueueLength: DefaultSchedulerNetworkTopologyProbeQueueLength, - Count: DefaultSchedulerNetworkTopologyProbeCount, - }, - Cache: CacheConfig{ - Interval: DefaultSchedulerNetworkTopologyCacheInterval, - TTL: DefaultSchedulerNetworkTopologyCacheTLL, - }, - }, }, Database: DatabaseConfig{ Redis: RedisConfig{ - BrokerDB: DefaultRedisBrokerDB, - BackendDB: DefaultRedisBackendDB, - NetworkTopologyDB: DefaultNetworkTopologyDB, + BrokerDB: DefaultRedisBrokerDB, + BackendDB: DefaultRedisBackendDB, }, }, Resource: ResourceConfig{ @@ -544,10 +499,6 @@ func (cfg *Config) Validate() error { return errors.New("redis requires parameter backendDB") } - if cfg.Database.Redis.NetworkTopologyDB < 0 { - return errors.New("redis requires parameter networkTopologyDB") - } - if !slices.Contains([]string{"http", "https"}, cfg.Resource.Task.DownloadTiny.Scheme) { return errors.New("downloadTiny requires parameter scheme") } @@ -630,28 +581,6 @@ func (cfg *Config) Validate() error { } } - if cfg.Scheduler.Algorithm == NetworkTopologyAlgorithm { - if cfg.Scheduler.NetworkTopology.CollectInterval <= 0 { - return errors.New("networkTopology requires parameter collectInterval") - } - - if cfg.Scheduler.NetworkTopology.Probe.QueueLength <= 0 { - return errors.New("probe requires parameter queueLength") - } - - if cfg.Scheduler.NetworkTopology.Probe.Count <= 0 { - return errors.New("probe requires parameter count") - } - - if cfg.Scheduler.NetworkTopology.Cache.Interval <= 0 { - return errors.New("networkTopology requires parameter interval") - } - - if cfg.Scheduler.NetworkTopology.Cache.TTL <= 0 { - return errors.New("networkTopology requires parameter ttl") - } - } - return nil } diff --git a/scheduler/config/config_test.go b/scheduler/config/config_test.go index 9baee730fc7..74892104304 100644 --- a/scheduler/config/config_test.go +++ b/scheduler/config/config_test.go @@ -62,13 +62,12 @@ var ( } mockRedisConfig = RedisConfig{ - Addrs: []string{"127.0.0.0:6379"}, - MasterName: "master", - Username: "baz", - Password: "bax", - BrokerDB: DefaultRedisBrokerDB, - BackendDB: DefaultRedisBackendDB, - NetworkTopologyDB: DefaultNetworkTopologyDB, + Addrs: []string{"127.0.0.0:6379"}, + MasterName: "master", + Username: "baz", + Password: "bax", + BrokerDB: DefaultRedisBrokerDB, + BackendDB: DefaultRedisBackendDB, } ) @@ -88,17 +87,6 @@ func TestConfig_Load(t *testing.T) { HostGCInterval: 1 * time.Minute, HostTTL: 1 * time.Minute, }, - NetworkTopology: NetworkTopologyConfig{ - CollectInterval: 60 * time.Second, - Probe: ProbeConfig{ - QueueLength: 5, - Count: 10, - }, - Cache: CacheConfig{ - Interval: 5 * time.Minute, - TTL: 5 * time.Minute, - }, - }, }, Server: ServerConfig{ AdvertiseIP: net.ParseIP("127.0.0.1"), @@ -117,14 +105,13 @@ func TestConfig_Load(t *testing.T) { }, Database: DatabaseConfig{ Redis: RedisConfig{ - Host: "127.0.0.1", - Password: "foo", - Addrs: []string{"foo", "bar"}, - MasterName: "baz", - Port: 6379, - BrokerDB: DefaultRedisBrokerDB, - BackendDB: DefaultRedisBackendDB, - NetworkTopologyDB: DefaultNetworkTopologyDB, + Host: "127.0.0.1", + Password: "foo", + Addrs: []string{"foo", "bar"}, + MasterName: "baz", + Port: 6379, + BrokerDB: DefaultRedisBrokerDB, + BackendDB: DefaultRedisBackendDB, }, }, Resource: ResourceConfig{ @@ -309,19 +296,6 @@ func TestConfig_Validate(t *testing.T) { assert.EqualError(err, "redis requires parameter backendDB") }, }, - { - name: "redis requires parameter networkTopologyDB", - config: New(), - mock: func(cfg *Config) { - cfg.Manager = mockManagerConfig - cfg.Database.Redis = mockRedisConfig - cfg.Database.Redis.NetworkTopologyDB = -1 - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "redis requires parameter networkTopologyDB") - }, - }, { name: "scheduler requires parameter algorithm", config: New(), @@ -462,81 +436,6 @@ func TestConfig_Validate(t *testing.T) { assert.EqualError(err, "scheduler requires parameter hostGCInterval") }, }, - { - name: "networkTopology requires parameter collectInterval", - config: New(), - mock: func(cfg *Config) { - cfg.Manager = mockManagerConfig - cfg.Database.Redis = mockRedisConfig - cfg.Job = mockJobConfig - cfg.Scheduler.Algorithm = NetworkTopologyAlgorithm - cfg.Scheduler.NetworkTopology.CollectInterval = 0 - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "networkTopology requires parameter collectInterval") - }, - }, - { - name: "networkTopology requires parameter interval", - config: New(), - mock: func(cfg *Config) { - cfg.Manager = mockManagerConfig - cfg.Database.Redis = mockRedisConfig - cfg.Job = mockJobConfig - cfg.Scheduler.Algorithm = NetworkTopologyAlgorithm - cfg.Scheduler.NetworkTopology.Cache.Interval = 0 - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "networkTopology requires parameter interval") - }, - }, - { - name: "networkTopology requires parameter ttl", - config: New(), - mock: func(cfg *Config) { - cfg.Manager = mockManagerConfig - cfg.Database.Redis = mockRedisConfig - cfg.Job = mockJobConfig - cfg.Scheduler.Algorithm = NetworkTopologyAlgorithm - cfg.Scheduler.NetworkTopology.Cache.TTL = 0 - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "networkTopology requires parameter ttl") - }, - }, - { - name: "probe requires parameter queueLength", - config: New(), - mock: func(cfg *Config) { - cfg.Manager = mockManagerConfig - cfg.Database.Redis = mockRedisConfig - cfg.Job = mockJobConfig - cfg.Scheduler.Algorithm = NetworkTopologyAlgorithm - cfg.Scheduler.NetworkTopology.Probe.QueueLength = 0 - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "probe requires parameter queueLength") - }, - }, - { - name: "probe requires parameter count", - config: New(), - mock: func(cfg *Config) { - cfg.Manager = mockManagerConfig - cfg.Database.Redis = mockRedisConfig - cfg.Job = mockJobConfig - cfg.Scheduler.Algorithm = NetworkTopologyAlgorithm - cfg.Scheduler.NetworkTopology.Probe.Count = 0 - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "probe requires parameter count") - }, - }, { name: "downloadTiny requires parameter scheme", config: New(), diff --git a/scheduler/config/constants.go b/scheduler/config/constants.go index 3345db66829..2e30cb9ba92 100644 --- a/scheduler/config/constants.go +++ b/scheduler/config/constants.go @@ -51,9 +51,6 @@ const ( // DefaultRedisBackendDB is default db for redis backend. DefaultRedisBackendDB = 2 - - // DefaultNetworkTopologyDB is default db for network topology. - DefaultNetworkTopologyDB = 3 ) const ( @@ -95,24 +92,6 @@ const ( // DefaultCPU is default cpu usage. DefaultCPU = 1 - - // NetworkTopologyAlgorithm is a scheduling algorithm based on rules and network topology. - NetworkTopologyAlgorithm = "nt" - - // DefaultNetworkTopologyCollectInterval is the default interval of collecting network topology. - DefaultSchedulerNetworkTopologyCollectInterval = 2 * time.Hour - - // DefaultNetworkTopologyCacheInterval is the default cache cleanup interval. - DefaultSchedulerNetworkTopologyCacheInterval = 5 * time.Minute - - // DefaultNetworkTopologyCacheTLL is the default ttl of networkTopology cache. - DefaultSchedulerNetworkTopologyCacheTLL = 5 * time.Minute - - // DefaultProbeQueueLength is the default length of probe queue. - DefaultSchedulerNetworkTopologyProbeQueueLength = 5 - - // DefaultProbeCount is the default number of probing hosts. - DefaultSchedulerNetworkTopologyProbeCount = 5 ) const ( diff --git a/scheduler/config/testdata/scheduler.yaml b/scheduler/config/testdata/scheduler.yaml index 55b01466214..2cac83a3862 100644 --- a/scheduler/config/testdata/scheduler.yaml +++ b/scheduler/config/testdata/scheduler.yaml @@ -26,14 +26,6 @@ scheduler: taskGCInterval: 30s hostGCInterval: 1m hostTTL: 1m - networkTopology: - collectInterval: 60s - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m database: redis: @@ -44,7 +36,6 @@ database: password: foo brokerDB: 1 backendDB: 2 - networkTopologyDB: 3 resource: task: diff --git a/scheduler/metrics/metrics.go b/scheduler/metrics/metrics.go index ba52558153d..ee6708e32ab 100644 --- a/scheduler/metrics/metrics.go +++ b/scheduler/metrics/metrics.go @@ -238,20 +238,6 @@ var ( Help: "Counter of the number of failed of the leaving host.", }) - SyncProbesCount = promauto.NewCounter(prometheus.CounterOpts{ - Namespace: types.MetricsNamespace, - Subsystem: types.SchedulerMetricsName, - Name: "sync_probes_total", - Help: "Counter of the number of the synchronizing probes.", - }) - - SyncProbesFailureCount = promauto.NewCounter(prometheus.CounterOpts{ - Namespace: types.MetricsNamespace, - Subsystem: types.SchedulerMetricsName, - Name: "sync_probes_failure_total", - Help: "Counter of the number of failed of the synchronizing probes.", - }) - Traffic = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: types.MetricsNamespace, Subsystem: types.SchedulerMetricsName, diff --git a/scheduler/networktopology/mocks/network_topology_mock.go b/scheduler/networktopology/mocks/network_topology_mock.go deleted file mode 100644 index a34f6a6c3c1..00000000000 --- a/scheduler/networktopology/mocks/network_topology_mock.go +++ /dev/null @@ -1,180 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: network_topology.go -// -// Generated by this command: -// -// mockgen -destination mocks/network_topology_mock.go -source network_topology.go -package mocks -// - -// Package mocks is a generated GoMock package. -package mocks - -import ( - reflect "reflect" - - networktopology "d7y.io/dragonfly/v2/scheduler/networktopology" - standard "d7y.io/dragonfly/v2/scheduler/resource/standard" - gomock "go.uber.org/mock/gomock" -) - -// MockNetworkTopology is a mock of NetworkTopology interface. -type MockNetworkTopology struct { - ctrl *gomock.Controller - recorder *MockNetworkTopologyMockRecorder -} - -// MockNetworkTopologyMockRecorder is the mock recorder for MockNetworkTopology. -type MockNetworkTopologyMockRecorder struct { - mock *MockNetworkTopology -} - -// NewMockNetworkTopology creates a new mock instance. -func NewMockNetworkTopology(ctrl *gomock.Controller) *MockNetworkTopology { - mock := &MockNetworkTopology{ctrl: ctrl} - mock.recorder = &MockNetworkTopologyMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockNetworkTopology) EXPECT() *MockNetworkTopologyMockRecorder { - return m.recorder -} - -// DeleteHost mocks base method. -func (m *MockNetworkTopology) DeleteHost(arg0 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeleteHost", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// DeleteHost indicates an expected call of DeleteHost. -func (mr *MockNetworkTopologyMockRecorder) DeleteHost(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteHost", reflect.TypeOf((*MockNetworkTopology)(nil).DeleteHost), arg0) -} - -// FindProbedHosts mocks base method. -func (m *MockNetworkTopology) FindProbedHosts(arg0 string) ([]*standard.Host, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "FindProbedHosts", arg0) - ret0, _ := ret[0].([]*standard.Host) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// FindProbedHosts indicates an expected call of FindProbedHosts. -func (mr *MockNetworkTopologyMockRecorder) FindProbedHosts(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindProbedHosts", reflect.TypeOf((*MockNetworkTopology)(nil).FindProbedHosts), arg0) -} - -// Has mocks base method. -func (m *MockNetworkTopology) Has(arg0, arg1 string) bool { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Has", arg0, arg1) - ret0, _ := ret[0].(bool) - return ret0 -} - -// Has indicates an expected call of Has. -func (mr *MockNetworkTopologyMockRecorder) Has(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Has", reflect.TypeOf((*MockNetworkTopology)(nil).Has), arg0, arg1) -} - -// Neighbours mocks base method. -func (m *MockNetworkTopology) Neighbours(arg0 *standard.Host, arg1 int) ([]*standard.Host, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Neighbours", arg0, arg1) - ret0, _ := ret[0].([]*standard.Host) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Neighbours indicates an expected call of Neighbours. -func (mr *MockNetworkTopologyMockRecorder) Neighbours(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Neighbours", reflect.TypeOf((*MockNetworkTopology)(nil).Neighbours), arg0, arg1) -} - -// ProbedCount mocks base method. -func (m *MockNetworkTopology) ProbedCount(arg0 string) (uint64, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ProbedCount", arg0) - ret0, _ := ret[0].(uint64) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ProbedCount indicates an expected call of ProbedCount. -func (mr *MockNetworkTopologyMockRecorder) ProbedCount(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProbedCount", reflect.TypeOf((*MockNetworkTopology)(nil).ProbedCount), arg0) -} - -// Probes mocks base method. -func (m *MockNetworkTopology) Probes(arg0, arg1 string) networktopology.Probes { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Probes", arg0, arg1) - ret0, _ := ret[0].(networktopology.Probes) - return ret0 -} - -// Probes indicates an expected call of Probes. -func (mr *MockNetworkTopologyMockRecorder) Probes(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Probes", reflect.TypeOf((*MockNetworkTopology)(nil).Probes), arg0, arg1) -} - -// Serve mocks base method. -func (m *MockNetworkTopology) Serve() { - m.ctrl.T.Helper() - m.ctrl.Call(m, "Serve") -} - -// Serve indicates an expected call of Serve. -func (mr *MockNetworkTopologyMockRecorder) Serve() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Serve", reflect.TypeOf((*MockNetworkTopology)(nil).Serve)) -} - -// Snapshot mocks base method. -func (m *MockNetworkTopology) Snapshot() error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Snapshot") - ret0, _ := ret[0].(error) - return ret0 -} - -// Snapshot indicates an expected call of Snapshot. -func (mr *MockNetworkTopologyMockRecorder) Snapshot() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Snapshot", reflect.TypeOf((*MockNetworkTopology)(nil).Snapshot)) -} - -// Stop mocks base method. -func (m *MockNetworkTopology) Stop() { - m.ctrl.T.Helper() - m.ctrl.Call(m, "Stop") -} - -// Stop indicates an expected call of Stop. -func (mr *MockNetworkTopologyMockRecorder) Stop() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stop", reflect.TypeOf((*MockNetworkTopology)(nil).Stop)) -} - -// Store mocks base method. -func (m *MockNetworkTopology) Store(arg0, arg1 string) 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 *MockNetworkTopologyMockRecorder) Store(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Store", reflect.TypeOf((*MockNetworkTopology)(nil).Store), arg0, arg1) -} diff --git a/scheduler/networktopology/mocks/probes_mock.go b/scheduler/networktopology/mocks/probes_mock.go deleted file mode 100644 index e30045009fa..00000000000 --- a/scheduler/networktopology/mocks/probes_mock.go +++ /dev/null @@ -1,130 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: probes.go -// -// Generated by this command: -// -// mockgen -destination mocks/probes_mock.go -source probes.go -package mocks -// - -// Package mocks is a generated GoMock package. -package mocks - -import ( - reflect "reflect" - time "time" - - networktopology "d7y.io/dragonfly/v2/scheduler/networktopology" - gomock "go.uber.org/mock/gomock" -) - -// MockProbes is a mock of Probes interface. -type MockProbes struct { - ctrl *gomock.Controller - recorder *MockProbesMockRecorder -} - -// MockProbesMockRecorder is the mock recorder for MockProbes. -type MockProbesMockRecorder struct { - mock *MockProbes -} - -// NewMockProbes creates a new mock instance. -func NewMockProbes(ctrl *gomock.Controller) *MockProbes { - mock := &MockProbes{ctrl: ctrl} - mock.recorder = &MockProbesMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockProbes) EXPECT() *MockProbesMockRecorder { - return m.recorder -} - -// AverageRTT mocks base method. -func (m *MockProbes) AverageRTT() (time.Duration, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AverageRTT") - ret0, _ := ret[0].(time.Duration) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// AverageRTT indicates an expected call of AverageRTT. -func (mr *MockProbesMockRecorder) AverageRTT() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AverageRTT", reflect.TypeOf((*MockProbes)(nil).AverageRTT)) -} - -// CreatedAt mocks base method. -func (m *MockProbes) CreatedAt() (time.Time, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreatedAt") - ret0, _ := ret[0].(time.Time) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// CreatedAt indicates an expected call of CreatedAt. -func (mr *MockProbesMockRecorder) CreatedAt() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreatedAt", reflect.TypeOf((*MockProbes)(nil).CreatedAt)) -} - -// Enqueue mocks base method. -func (m *MockProbes) Enqueue(arg0 *networktopology.Probe) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Enqueue", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// Enqueue indicates an expected call of Enqueue. -func (mr *MockProbesMockRecorder) Enqueue(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Enqueue", reflect.TypeOf((*MockProbes)(nil).Enqueue), arg0) -} - -// Len mocks base method. -func (m *MockProbes) Len() (int64, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Len") - ret0, _ := ret[0].(int64) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Len indicates an expected call of Len. -func (mr *MockProbesMockRecorder) Len() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Len", reflect.TypeOf((*MockProbes)(nil).Len)) -} - -// Peek mocks base method. -func (m *MockProbes) Peek() (*networktopology.Probe, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Peek") - ret0, _ := ret[0].(*networktopology.Probe) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Peek indicates an expected call of Peek. -func (mr *MockProbesMockRecorder) Peek() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Peek", reflect.TypeOf((*MockProbes)(nil).Peek)) -} - -// UpdatedAt mocks base method. -func (m *MockProbes) UpdatedAt() (time.Time, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UpdatedAt") - ret0, _ := ret[0].(time.Time) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// UpdatedAt indicates an expected call of UpdatedAt. -func (mr *MockProbesMockRecorder) UpdatedAt() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdatedAt", reflect.TypeOf((*MockProbes)(nil).UpdatedAt)) -} diff --git a/scheduler/networktopology/network_topology.go b/scheduler/networktopology/network_topology.go deleted file mode 100644 index 98f65dca88f..00000000000 --- a/scheduler/networktopology/network_topology.go +++ /dev/null @@ -1,497 +0,0 @@ -/* - * Copyright 2023 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 mocks/network_topology_mock.go -source network_topology.go -package mocks - -package networktopology - -import ( - "context" - "errors" - "sort" - "strconv" - "time" - - "github.com/google/uuid" - "github.com/redis/go-redis/v9" - - logger "d7y.io/dragonfly/v2/internal/dflog" - "d7y.io/dragonfly/v2/pkg/cache" - "d7y.io/dragonfly/v2/pkg/container/set" - pkgredis "d7y.io/dragonfly/v2/pkg/redis" - "d7y.io/dragonfly/v2/scheduler/config" - resource "d7y.io/dragonfly/v2/scheduler/resource/standard" - "d7y.io/dragonfly/v2/scheduler/storage" -) - -const ( - // contextTimeout is the timeout of redis invoke. - contextTimeout = 2 * time.Minute - - // snapshotContextTimeout is the timeout of snapshot network topology. - snapshotContextTimeout = 20 * time.Minute - - // findProbedCandidateHostsLimit is the limit of find probed candidate hosts. - findProbedCandidateHostsLimit = 50 - - // defaultScanCountLimit is the predefined amount of work performed with each 'Scan' operation called when retrieve elements from Redis. - defaultScanCountLimit = 64 -) - -// NetworkTopology is an interface for network topology. -type NetworkTopology interface { - // Started network topology server. - Serve() - - // Stop network topology server. - Stop() - - // Has to check if there is a connection between source host and destination host. - Has(string, string) bool - - // Store stores source host and destination host. - Store(string, string) error - - // FindProbedHosts finds the most candidate destination host to be probed, randomly find a range of hosts, - // and then return the host with a smaller probed count. - FindProbedHosts(string) ([]*resource.Host, error) - - // DeleteHost deletes source host and all destination host connected to source host. - DeleteHost(string) error - - // Probes loads probes interface by source host id and destination host id. - Probes(string, string) Probes - - // ProbedCount is the number of times the host has been probed. - ProbedCount(string) (uint64, error) - - // Neighbours gets the specified number neighbors of source host for aggregation, by regexp scaning cache - // (if it is not enough for code to work, access redis to get neighbors), then parsing keys and loading host, - // while updating the cache data. - Neighbours(*resource.Host, int) ([]*resource.Host, error) - - // Snapshot writes the current network topology to the storage. - Snapshot() error -} - -// networkTopology is an implementation of network topology. -type networkTopology struct { - // config is the network topology config. - config config.NetworkTopologyConfig - - // rdb is Redis universal client interface. - rdb redis.UniversalClient - - // Cache instance. - cache cache.Cache - - // resource is resource interface. - resource resource.Resource - - // storage is storage interface. - storage storage.Storage - - // done channel will be closed when network topology serve stop. - done chan struct{} -} - -// New network topology interface. -func NewNetworkTopology(cfg config.NetworkTopologyConfig, rdb redis.UniversalClient, cache cache.Cache, resource resource.Resource, storage storage.Storage) (NetworkTopology, error) { - return &networkTopology{ - config: cfg, - rdb: rdb, - cache: cache, - resource: resource, - storage: storage, - done: make(chan struct{}), - }, nil -} - -// Started network topology server. -func (nt *networkTopology) Serve() { - logger.Info("collect network topology records") - tick := time.NewTicker(nt.config.CollectInterval) - for { - select { - case <-tick.C: - if err := nt.Snapshot(); err != nil { - logger.Error(err) - break - } - case <-nt.done: - return - } - } -} - -// Stop network topology server. -func (nt *networkTopology) Stop() { - close(nt.done) -} - -// Has to check if there is a connection between source host and destination host. -func (nt *networkTopology) Has(srcHostID string, destHostID string) bool { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - networkTopologyKey := pkgredis.MakeNetworkTopologyKeyInScheduler(srcHostID, destHostID) - if _, _, ok := nt.cache.GetWithExpiration(networkTopologyKey); ok { - return true - } - - networkTopology, err := nt.rdb.HGetAll(ctx, networkTopologyKey).Result() - if err != nil { - logger.Errorf("get networkTopology failed: %s", err.Error()) - return false - } - - if len(networkTopology) == 0 { - return false - } - - // Add cache data. - nt.cache.Set(networkTopologyKey, networkTopology, nt.config.Cache.TTL) - - return true -} - -// Store stores source host and destination host. -func (nt *networkTopology) Store(srcHostID string, destHostID string) error { - // If the network topology already exists, skip it. - if nt.Has(srcHostID, destHostID) { - return nil - } - - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - if err := nt.rdb.HSet(ctx, pkgredis.MakeNetworkTopologyKeyInScheduler(srcHostID, destHostID), "createdAt", time.Now().Format(time.RFC3339Nano)).Err(); err != nil { - return err - } - - return nil -} - -// FindProbedHosts finds the most candidate destination host to be probed, randomly find a range of hosts, -// and then return the host with a smaller probed count. -func (nt *networkTopology) FindProbedHosts(hostID string) ([]*resource.Host, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - blocklist := set.NewSafeSet[string]() - blocklist.Add(hostID) - candidateHosts := nt.resource.HostManager().LoadRandom(findProbedCandidateHostsLimit, blocklist) - if len(candidateHosts) == 0 { - return nil, errors.New("probed hosts not found") - } - - if len(candidateHosts) <= nt.config.Probe.Count { - return candidateHosts, nil - } - - var probedCountKeys []string - probedCounts := make(map[string]uint64) - for _, candidateHost := range candidateHosts { - probedCountKey := pkgredis.MakeProbedCountKeyInScheduler(candidateHost.ID) - cache, _, ok := nt.cache.GetWithExpiration(probedCountKey) - if ok { - if probedCount, ok := cache.(uint64); ok { - probedCounts[probedCountKey] = probedCount - } - continue - } - - probedCountKeys = append(probedCountKeys, probedCountKey) - } - - rawProbedCounts, err := nt.rdb.MGet(ctx, probedCountKeys...).Result() - if err != nil { - return nil, err - } - - // Filter invalid probed count. If probed key not exist, the probed count is nil. - for i, rawProbedCount := range rawProbedCounts { - // Initialize the probedCount value of host in redis when the host is first selected as the candidate probe target. - if rawProbedCount == nil { - if err := nt.rdb.Set(ctx, probedCountKeys[i], 0, 0).Err(); err != nil { - return nil, err - } - - continue - } - - value, ok := rawProbedCount.(string) - if !ok { - return nil, errors.New("invalid value type") - } - - probedCount, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return nil, errors.New("invalid probed count") - } - probedCounts[probedCountKeys[i]] = probedCount - - // Add cache data. - nt.cache.Set(probedCountKeys[i], probedCount, nt.config.Cache.TTL) - } - - // Sort candidate hosts by probed count. - sort.Slice(candidateHosts, func(i, j int) bool { - return probedCounts[pkgredis.MakeProbedCountKeyInScheduler(candidateHosts[i].ID)] < probedCounts[pkgredis.MakeProbedCountKeyInScheduler(candidateHosts[j].ID)] - }) - - return candidateHosts[:nt.config.Probe.Count], nil -} - -// DeleteHost deletes source host and all destination host connected to source host. -func (nt *networkTopology) DeleteHost(hostID string) error { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - deleteKeys := []string{pkgredis.MakeProbedCountKeyInScheduler(hostID)} - srcNetworkTopologyKeys, _, err := nt.rdb.Scan(ctx, 0, pkgredis.MakeNetworkTopologyKeyInScheduler(hostID, "*"), defaultScanCountLimit).Result() - if err != nil { - return err - } - deleteKeys = append(deleteKeys, srcNetworkTopologyKeys...) - - destNetworkTopologyKeys, _, err := nt.rdb.Scan(ctx, 0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", hostID), defaultScanCountLimit).Result() - if err != nil { - return err - } - deleteKeys = append(deleteKeys, destNetworkTopologyKeys...) - - srcProbesKeys, _, err := nt.rdb.Scan(ctx, 0, pkgredis.MakeProbesKeyInScheduler(hostID, "*"), defaultScanCountLimit).Result() - if err != nil { - return err - } - deleteKeys = append(deleteKeys, srcProbesKeys...) - - destProbesKeys, _, err := nt.rdb.Scan(ctx, 0, pkgredis.MakeProbesKeyInScheduler("*", hostID), defaultScanCountLimit).Result() - if err != nil { - return err - } - deleteKeys = append(deleteKeys, destProbesKeys...) - - if err := nt.rdb.Del(ctx, deleteKeys...).Err(); err != nil { - return err - } - - for _, deleteKey := range deleteKeys { - nt.cache.Delete(deleteKey) - } - - return nil -} - -// Probes loads probes interface by source host id and destination host id. -func (nt *networkTopology) Probes(srcHostID, destHostID string) Probes { - return NewProbes(nt.config, nt.rdb, nt.cache, srcHostID, destHostID) -} - -// ProbedCount is the number of times the host has been probed. -func (nt *networkTopology) ProbedCount(hostID string) (uint64, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - probedCountKey := pkgredis.MakeProbedCountKeyInScheduler(hostID) - if cache, _, ok := nt.cache.GetWithExpiration(probedCountKey); ok { - probedCount, ok := cache.(uint64) - if ok { - return probedCount, nil - } - - return uint64(0), errors.New("get probedCount failed") - } - - probedCount, err := nt.rdb.Get(ctx, probedCountKey).Uint64() - if err != nil { - return uint64(0), err - } - - // Add cache data. - nt.cache.Set(probedCountKey, probedCount, nt.config.Cache.TTL) - - return probedCount, nil -} - -// Neighbours gets the specified number neighbors of source host for aggregation by regexp scaning cache -// (if it is not enough for code to work, get neighbors from redis), then parsing keys and loading host, -// while updating the cache data. -func (nt *networkTopology) Neighbours(srcHost *resource.Host, n int) ([]*resource.Host, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - networkTopologyKeys, err := nt.cache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(srcHost.ID, "*"), n) - if err != nil { - return nil, err - } - - // If it is not enough for code to work, get neighbors from redis. - if len(networkTopologyKeys) < n { - networkTopologyKeys, _, err = nt.rdb.Scan(ctx, 0, pkgredis.MakeNetworkTopologyKeyInScheduler(srcHost.ID, "*"), defaultScanCountLimit).Result() - if err != nil { - return nil, err - } - - var networkTopology map[string]string - for _, networkTopologyKey := range networkTopologyKeys { - if networkTopology, err = nt.rdb.HGetAll(ctx, networkTopologyKey).Result(); err != nil { - logger.Error(err) - continue - } - - // Add cache data. - nt.cache.Set(networkTopologyKey, networkTopology, nt.config.Cache.TTL) - } - } - - neighbours := make([]*resource.Host, 0, n) - for _, networkTopologyKey := range networkTopologyKeys { - if len(neighbours) >= n { - break - } - - _, _, _, neighbourID, err := pkgredis.ParseNetworkTopologyKeyInScheduler(networkTopologyKey) - if err != nil { - logger.Error(err) - continue - } - - neighbour, loaded := nt.resource.HostManager().Load(neighbourID) - if !loaded { - logger.Errorf("host %s not found", neighbourID) - continue - } - neighbours = append(neighbours, neighbour) - } - - return neighbours, nil -} - -// Snapshot writes the current network topology to the storage. -func (nt *networkTopology) Snapshot() error { - ctx, cancel := context.WithTimeout(context.Background(), snapshotContextTimeout) - defer cancel() - - now := time.Now() - id := uuid.NewString() - probedCountKeys, _, err := nt.rdb.Scan(ctx, 0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).Result() - if err != nil { - return err - } - - for _, probedCountKey := range probedCountKeys { - _, _, srcHostID, err := pkgredis.ParseProbedCountKeyInScheduler(probedCountKey) - if err != nil { - logger.Error(err) - continue - } - - // Construct destination hosts for network topology. - networkTopologyKeys, _, err := nt.rdb.Scan(ctx, 0, pkgredis.MakeNetworkTopologyKeyInScheduler(srcHostID, "*"), defaultScanCountLimit).Result() - if err != nil { - logger.Error(err) - continue - } - - destHosts := make([]storage.DestHost, 0, len(networkTopologyKeys)) - for _, networkTopologyKey := range networkTopologyKeys { - _, _, srcHostID, destHostID, err := pkgredis.ParseNetworkTopologyKeyInScheduler(networkTopologyKey) - if err != nil { - logger.Error(err) - continue - } - - host, loaded := nt.resource.HostManager().Load(destHostID) - if !loaded { - logger.Errorf("host %s not found", destHostID) - continue - } - - ps := nt.Probes(srcHostID, destHostID) - averageRTT, err := ps.AverageRTT() - if err != nil { - logger.Error(err) - continue - } - - createdAt, err := ps.CreatedAt() - if err != nil { - logger.Error(err) - continue - } - - updatedAt, err := ps.UpdatedAt() - if err != nil { - logger.Error(err) - continue - } - - destHost := storage.DestHost{ - ID: host.ID, - Type: host.Type.Name(), - Hostname: host.Hostname, - IP: host.IP, - Port: host.Port, - Network: resource.Network{ - TCPConnectionCount: host.Network.TCPConnectionCount, - UploadTCPConnectionCount: host.Network.UploadTCPConnectionCount, - Location: host.Network.Location, - IDC: host.Network.IDC, - }, - Probes: storage.Probes{ - AverageRTT: averageRTT.Nanoseconds(), - CreatedAt: createdAt.UnixNano(), - UpdatedAt: updatedAt.UnixNano(), - }, - } - - destHosts = append(destHosts, destHost) - } - - // Construct source hosts for network topology. - host, loaded := nt.resource.HostManager().Load(srcHostID) - if !loaded { - logger.Errorf("host %s not found", srcHostID) - continue - } - - if err = nt.storage.CreateNetworkTopology(storage.NetworkTopology{ - ID: id, - Host: storage.SrcHost{ - ID: host.ID, - Type: host.Type.Name(), - Hostname: host.Hostname, - IP: host.IP, - Port: host.Port, - Network: resource.Network{ - TCPConnectionCount: host.Network.TCPConnectionCount, - UploadTCPConnectionCount: host.Network.UploadTCPConnectionCount, - Location: host.Network.Location, - IDC: host.Network.IDC, - }, - }, - DestHosts: destHosts, - CreatedAt: now.UnixNano(), - }); err != nil { - logger.Error(err) - continue - } - } - - return nil -} diff --git a/scheduler/networktopology/network_topology_test.go b/scheduler/networktopology/network_topology_test.go deleted file mode 100644 index df4eb7d489c..00000000000 --- a/scheduler/networktopology/network_topology_test.go +++ /dev/null @@ -1,1381 +0,0 @@ -/* - * Copyright 2023 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. - */ - -package networktopology - -import ( - "errors" - "reflect" - "strconv" - "testing" - "time" - - "github.com/go-redis/redismock/v9" - "github.com/stretchr/testify/assert" - "go.uber.org/mock/gomock" - - "d7y.io/dragonfly/v2/pkg/cache" - "d7y.io/dragonfly/v2/pkg/container/set" - pkgredis "d7y.io/dragonfly/v2/pkg/redis" - resource "d7y.io/dragonfly/v2/scheduler/resource/standard" - storagemocks "d7y.io/dragonfly/v2/scheduler/storage/mocks" -) - -func Test_NewNetworkTopology(t *testing.T) { - tests := []struct { - name string - expect func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "new network topology", - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.Equal(reflect.TypeOf(networkTopology).Elem().Name(), "networkTopology") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, _ := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - cache := cache.NewMockCache(ctl) - storage := storagemocks.NewMockStorage(ctl) - - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.expect(t, networkTopology, err) - }) - } -} - -func TestNetworkTopology_Serve(t *testing.T) { - tests := []struct { - name string - sleep func() - mock func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) - expect func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "start network topology server", - sleep: func() { - time.Sleep(3 * time.Second) - }, - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false), - mc.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(mockNetworkTopology, mockCacheExpiration, true).Times(2), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - go networkTopology.Serve() - }, - }, - { - name: "start network topology server error", - sleep: func() { - time.Sleep(5 * time.Second) - }, - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetErr( - errors.New("get probed count keys error")) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false), - mc.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(mockNetworkTopology, mockCacheExpiration, true).Times(2), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - go networkTopology.Serve() - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - hostManager := resource.NewMockHostManager(ctl) - storage := storagemocks.NewMockStorage(ctl) - cache := cache.NewMockCache(ctl) - tc.mock(res.EXPECT(), hostManager, hostManager.EXPECT(), storage.EXPECT(), mockRDBClient, cache.EXPECT()) - - mockNetworkTopologyConfig.CollectInterval = 2 * time.Second - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.expect(t, networkTopology, err) - tc.sleep() - networkTopology.Stop() - }) - } -} - -func TestNetworkTopology_Has(t *testing.T) { - tests := []struct { - name string - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) - run func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "network topology cache between src host and destination host exists", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, true) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.True(networkTopology.Has(mockSeedHost.ID, mockHost.ID)) - }, - }, - { - name: "check network topology between src host and destination host exist error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetErr( - errors.New("check network topology between src host and destination host exist error")) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.False(networkTopology.Has(mockSeedHost.ID, mockHost.ID)) - }, - }, - { - name: "network topology between src host and destination host exist", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.True(networkTopology.Has(mockSeedHost.ID, mockHost.ID)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - storage := storagemocks.NewMockStorage(ctl) - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT()) - - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.run(t, networkTopology, err) - mockRDBClient.ClearExpect() - }) - } -} - -func TestNetworkTopology_Store(t *testing.T) { - tests := []struct { - name string - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) - run func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "network topology between src host and destination host exists", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, true) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Store(mockSeedHost.ID, mockHost.ID)) - }, - }, - { - name: "network topology between src host and destination host does not exist", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(map[string]string{}) - mockRDBClient.Regexp().ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "createdAt", `.*`).SetVal(1) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Store(mockSeedHost.ID, mockHost.ID)) - }, - }, - { - name: "set createdAt error when network topology between src host and destination host does not exist", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(map[string]string{}) - mockRDBClient.Regexp().ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "createdAt", `.*`).SetErr(errors.New("set createdAt error")) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(networkTopology.Store(mockSeedHost.ID, mockHost.ID), "set createdAt error") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - storage := storagemocks.NewMockStorage(ctl) - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT()) - - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.run(t, networkTopology, err) - mockRDBClient.ClearExpect() - }) - } -} - -func TestNetworkTopology_FindProbedHosts(t *testing.T) { - tests := []struct { - name string - hosts []*resource.Host - mock func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) - expect func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) - }{ - { - name: "find probed hosts with cache", - hosts: []*resource.Host{ - mockHost, {ID: "foo"}, {ID: "bar"}, {ID: "baz"}, {ID: "bav"}, {ID: "bac"}, - }, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(uint64(6), mockCacheExpiration, true).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(nil, mockCacheExpiration, false).Times(5), - mc.Set(gomock.Any(), gomock.Any(), gomock.Any()).Times(5), - ) - - var probedCountKeys []string - for _, host := range hosts[1:] { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetVal([]any{"5", "4", "3", "2", "1"}) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.NoError(err) - assert.Equal(len(probedHosts), 5) - assert.EqualValues(probedHosts[0].ID, "bac") - assert.EqualValues(probedHosts[1].ID, "bav") - assert.EqualValues(probedHosts[2].ID, "baz") - assert.EqualValues(probedHosts[3].ID, "bar") - assert.EqualValues(probedHosts[4].ID, "foo") - }, - }, - { - name: "find probed hosts", - hosts: []*resource.Host{ - mockHost, {ID: "foo"}, {ID: "bar"}, {ID: "baz"}, {ID: "bav"}, {ID: "bac"}, - }, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(nil, mockCacheExpiration, false).Times(6), - mc.Set(gomock.Any(), gomock.Any(), gomock.Any()).Times(6), - ) - - var probedCountKeys []string - for _, host := range hosts { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetVal([]any{"6", "5", "4", "3", "2", "1"}) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.NoError(err) - assert.Equal(len(probedHosts), 5) - assert.EqualValues(probedHosts[0].ID, "bac") - assert.EqualValues(probedHosts[1].ID, "bav") - assert.EqualValues(probedHosts[2].ID, "baz") - assert.EqualValues(probedHosts[3].ID, "bar") - assert.EqualValues(probedHosts[4].ID, "foo") - }, - }, - { - name: "find probed hosts when map is insufficient", - hosts: []*resource.Host{mockHost}, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - ) - - var probedCountKeys []string - for _, host := range hosts { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetVal([]any{"1"}) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.NoError(err) - assert.Equal(len(probedHosts), 1) - assert.EqualValues(probedHosts[0].ID, mockHost.ID) - }, - }, - { - name: "get probed count error", - hosts: []*resource.Host{ - mockHost, {ID: "foo"}, {ID: "bar"}, {ID: "baz"}, {ID: "bav"}, {ID: "bac"}, - }, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(nil, mockCacheExpiration, false).Times(6), - ) - - var probedCountKeys []string - for _, host := range hosts { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetErr(errors.New("get probed count error")) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.Equal(len(probedHosts), 0) - assert.EqualError(err, "get probed count error") - }, - }, - { - name: "probed hosts not found", - hosts: []*resource.Host{}, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.Equal(len(probedHosts), 0) - assert.EqualError(err, "probed hosts not found") - }, - }, - { - name: "invalid probed count", - hosts: []*resource.Host{ - mockHost, {ID: "foo"}, {ID: "bar"}, {ID: "baz"}, {ID: "bav"}, {ID: "bac"}, - }, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(nil, mockCacheExpiration, false).Times(6), - ) - - var probedCountKeys []string - for _, host := range hosts { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetVal([]any{"foo", "5", "4", "3", "2", "1"}) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.Equal(len(probedHosts), 0) - assert.EqualError(err, "invalid probed count") - }, - }, - { - name: "invalid value type", - hosts: []*resource.Host{ - mockHost, {ID: "foo"}, {ID: "bar"}, {ID: "baz"}, {ID: "bav"}, {ID: "bac"}, - }, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(nil, mockCacheExpiration, false).Times(6), - ) - - var probedCountKeys []string - for _, host := range hosts { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetVal([]any{6, "5", "4", "3", "2", "1"}) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.Equal(len(probedHosts), 0) - assert.EqualError(err, "invalid value type") - }, - }, - { - name: "type convert error", - hosts: []*resource.Host{ - mockHost, {ID: "foo"}, {ID: "bar"}, {ID: "baz"}, {ID: "bav"}, {ID: "bac"}, - }, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(nil, mockCacheExpiration, false).Times(5), - mc.GetWithExpiration(gomock.Any()).Return("foo", mockCacheExpiration, true).Times(1), - mc.Set(gomock.Any(), gomock.Any(), gomock.Any()).Times(5), - ) - - var probedCountKeys []string - for _, host := range hosts[:len(hosts)-1] { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetVal([]any{"6", "5", "4", "3", "2"}) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.NoError(err) - assert.Equal(len(probedHosts), 5) - assert.EqualValues(probedHosts[0].ID, "bac") - assert.EqualValues(probedHosts[1].ID, "bav") - assert.EqualValues(probedHosts[2].ID, "baz") - assert.EqualValues(probedHosts[3].ID, "bar") - assert.EqualValues(probedHosts[4].ID, "foo") - }, - }, - { - name: "Initialize the probedCount value of host in redis", - hosts: []*resource.Host{ - mockHost, {ID: "foo"}, {ID: "bar"}, {ID: "baz"}, {ID: "bav"}, {ID: "bac"}, - }, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(nil, mockCacheExpiration, false).Times(6), - mc.Set(gomock.Any(), gomock.Any(), gomock.Any()).Times(5), - ) - - var probedCountKeys []string - for _, host := range hosts { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetVal([]any{nil, "5", "4", "3", "2", "1"}) - mockRDBClient.ExpectSet(probedCountKeys[0], 0, 0).SetVal("ok") - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.NoError(err) - assert.Equal(len(probedHosts), 5) - assert.EqualValues(probedHosts[0].ID, mockHost.ID) - assert.EqualValues(probedHosts[1].ID, "bac") - assert.EqualValues(probedHosts[2].ID, "bav") - assert.EqualValues(probedHosts[3].ID, "baz") - assert.EqualValues(probedHosts[4].ID, "bar") - }, - }, - { - name: "Initialize the probedCount value of host in redis error", - hosts: []*resource.Host{ - mockHost, {ID: "foo"}, {ID: "bar"}, {ID: "baz"}, {ID: "bav"}, {ID: "bac"}, - }, - mock: func(mockRDBClient redismock.ClientMock, mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mc *cache.MockCacheMockRecorder, hosts []*resource.Host) { - mockRDBClient.MatchExpectationsInOrder(true) - blocklist := set.NewSafeSet[string]() - blocklist.Add(mockSeedHost.ID) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.LoadRandom(gomock.Eq(findProbedCandidateHostsLimit), gomock.Eq(blocklist)).Return(hosts).Times(1), - mc.GetWithExpiration(gomock.Any()).Return(nil, mockCacheExpiration, false).Times(6), - ) - - var probedCountKeys []string - for _, host := range hosts { - probedCountKeys = append(probedCountKeys, pkgredis.MakeProbedCountKeyInScheduler(host.ID)) - } - - mockRDBClient.ExpectMGet(probedCountKeys...).SetVal([]any{nil, "5", "4", "3", "2", "1"}) - mockRDBClient.ExpectSet(probedCountKeys[0], 0, 0).SetErr(errors.New("Initialize the probedCount value of host in redis error")) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error, hosts []*resource.Host) { - assert := assert.New(t) - assert.NoError(err) - probedHosts, err := networkTopology.FindProbedHosts(mockSeedHost.ID) - assert.Equal(len(probedHosts), 0) - assert.EqualError(err, "Initialize the probedCount value of host in redis error") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - storage := storagemocks.NewMockStorage(ctl) - hostManager := resource.NewMockHostManager(ctl) - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, res.EXPECT(), hostManager, hostManager.EXPECT(), cache.EXPECT(), tc.hosts) - - mockNetworkTopologyConfig.Probe.Count = 5 - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.expect(t, networkTopology, err, tc.hosts) - mockRDBClient.ClearExpect() - }) - } -} - -func TestNetworkTopology_DeleteHost(t *testing.T) { - tests := []struct { - name string - deleteKeys []string - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, keys []string) - run func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "delete host", - deleteKeys: []string{pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID), - pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, keys []string) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetVal([]string{}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), defaultScanCountLimit).SetVal([]string{keys[1]}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetVal([]string{}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler("*", mockHost.ID), defaultScanCountLimit).SetVal([]string{keys[2]}, 0) - mockRDBClient.ExpectDel(keys...).SetVal(4) - mockCache.Delete(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)) - mockCache.Delete(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.DeleteHost(mockHost.ID)) - }, - }, - { - name: "get source network topology keys error", - deleteKeys: []string{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, keys []string) { - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetErr( - errors.New("get source network topology keys error")) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(networkTopology.DeleteHost(mockHost.ID), "get source network topology keys error") - }, - }, - { - name: "get destination network topology keys error", - deleteKeys: []string{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, keys []string) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetVal([]string{}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), defaultScanCountLimit).SetErr( - errors.New("get destination network topology keys error")) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(networkTopology.DeleteHost(mockHost.ID), "get destination network topology keys error") - }, - }, - { - name: "get source probes keys error", - deleteKeys: []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, keys []string) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetVal([]string{}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), defaultScanCountLimit).SetVal([]string{keys[0]}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetErr( - errors.New("get source probes keys error")) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(networkTopology.DeleteHost(mockHost.ID), "get source probes keys error") - }, - }, - { - name: "get destination probes keys error", - deleteKeys: []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, keys []string) { - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetVal([]string{}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), defaultScanCountLimit).SetVal([]string{keys[0]}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetVal([]string{}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler("*", mockHost.ID), defaultScanCountLimit).SetErr( - errors.New("get destination probes keys error")) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(networkTopology.DeleteHost(mockHost.ID), "get destination probes keys error") - }, - }, - { - name: "delete network topology and probes error", - deleteKeys: []string{pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID), - pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, keys []string) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetVal([]string{}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), defaultScanCountLimit).SetVal([]string{keys[1]}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler(mockHost.ID, "*"), defaultScanCountLimit).SetVal([]string{}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler("*", mockHost.ID), defaultScanCountLimit).SetVal([]string{keys[2]}, 0) - mockRDBClient.ExpectDel(keys...).SetErr(errors.New("delete network topology and probes error")) - }, - run: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(networkTopology.DeleteHost(mockHost.ID), "delete network topology and probes error") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - storage := storagemocks.NewMockStorage(ctl) - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT(), tc.deleteKeys) - - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.run(t, networkTopology, err) - mockRDBClient.ClearExpect() - }) - } -} - -func TestNetworkTopology_Probes(t *testing.T) { - tests := []struct { - name string - expect func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "loads probes interface", - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - - ps := networkTopology.Probes(mockSeedHost.ID, mockHost.ID) - probes := ps.(*probes) - assert.Equal(probes.config.Probe.QueueLength, 5) - assert.NotNil(probes.rdb) - assert.Equal(probes.srcHostID, mockSeedHost.ID) - assert.Equal(probes.destHostID, mockHost.ID) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, _ := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - storage := storagemocks.NewMockStorage(ctl) - cache := cache.NewMockCache(ctl) - - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.expect(t, networkTopology, err) - }) - } -} - -func TestNetworkTopology_ProbedCount(t *testing.T) { - tests := []struct { - name string - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) - expect func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "get probed count with cache", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).Return(uint64(mockProbedCount), mockCacheExpiration, true) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - - probedCount, err := networkTopology.ProbedCount(mockHost.ID) - assert.EqualValues(probedCount, mockProbedCount) - assert.NoError(err) - }, - }, - { - name: "get probed count", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectGet(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(strconv.Itoa(mockProbedCount)) - mockCache.Set(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID), uint64(mockProbedCount), gomock.Any()) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - - probedCount, err := networkTopology.ProbedCount(mockHost.ID) - assert.EqualValues(probedCount, mockProbedCount) - assert.NoError(err) - }, - }, - { - name: "type convert error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).Return("foo", mockCacheExpiration, true) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - - probedCount, err := networkTopology.ProbedCount(mockHost.ID) - assert.Equal(probedCount, uint64(0)) - assert.EqualError(err, "get probedCount failed") - }, - }, - { - name: "get probed count error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectGet(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetErr(errors.New("get probed count error")) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - - probedCount, _ := networkTopology.ProbedCount(mockHost.ID) - assert.Equal(probedCount, uint64(0)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - storage := storagemocks.NewMockStorage(ctl) - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT()) - - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.expect(t, networkTopology, err) - mockRDBClient.ClearExpect() - }) - } -} - -func TestNetworkTopology_Neighbours(t *testing.T) { - tests := []struct { - name string - mock func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) - expect func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "get neighbours from cache", - mock: func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) { - mockCache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), 1).Return( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, nil).Times(1) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - neighbours, err := networkTopology.Neighbours(mockSeedHost, 1) - assert.NoError(err) - assert.Equal(len(neighbours), 1) - }, - }, - { - name: "get neighbours from cache error", - mock: func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) { - mockCache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), 1).Return( - nil, errors.New("get neighbours from cache error")).Times(1) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - _, err = networkTopology.Neighbours(mockSeedHost, 1) - assert.EqualError(err, "get neighbours from cache error") - }, - }, - { - name: "get neighbours keys from redis", - mock: func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) { - mockCache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), 2).Return( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, nil).Times(1) - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{ - pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "bar"), - }, 0) - - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(map[string]string{}) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), map[string]string{}, mockCacheTLL) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "bar")).SetVal(map[string]string{}) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "bar"), map[string]string{}, mockCacheTLL) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq("bar")).Return(&resource.Host{ID: "bar"}, true).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - neighbours, err := networkTopology.Neighbours(mockSeedHost, 2) - assert.NoError(err) - assert.Equal(len(neighbours), 2) - }, - }, - { - name: "get neighbours keys from redis error", - mock: func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) { - mockCache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), 2).Return( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, nil).Times(1) - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetErr( - errors.New("get neighbours keys from redis error")) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - _, err = networkTopology.Neighbours(mockSeedHost, 2) - assert.EqualError(err, "get neighbours keys from redis error") - }, - }, - { - name: "get neighbours data from redis error", - mock: func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) { - mockCache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), 2).Return( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, nil).Times(1) - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{ - pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "bar"), - }, 0) - - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(map[string]string{}) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), map[string]string{}, mockCacheTLL) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "bar")).SetErr( - errors.New("get neighbours data from redis error")) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq("bar")).Return(&resource.Host{ID: "bar"}, true).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - neighbours, err := networkTopology.Neighbours(mockSeedHost, 2) - assert.NoError(err) - assert.Equal(len(neighbours), 2) - }, - }, - { - name: "parse network topology key error", - mock: func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) { - mockCache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), 1).Return( - []string{"foo"}, nil).Times(1) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - neighbours, err := networkTopology.Neighbours(mockSeedHost, 1) - assert.NoError(err) - assert.Equal(len(neighbours), 0) - }, - }, - { - name: "load host error", - mock: func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) { - mockCache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), 1).Return( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, nil).Times(1) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, false).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - neighbours, err := networkTopology.Neighbours(mockSeedHost, 1) - assert.NoError(err) - assert.Equal(len(neighbours), 0) - }, - }, - { - name: "neighbors number is greater than the required number", - mock: func(mr *resource.MockResourceMockRecorder, mockCache *cache.MockCacheMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, mockRDBClient redismock.ClientMock) { - mockCache.Scan(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), 2).Return( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, nil).Times(1) - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "bar"), - pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "baz")}, 0) - - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(map[string]string{}) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), map[string]string{}, mockCacheTLL) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "bar")).SetVal(map[string]string{}) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "bar"), map[string]string{}, mockCacheTLL) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "baz")).SetVal(map[string]string{}) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "baz"), map[string]string{}, mockCacheTLL) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq("bar")).Return(&resource.Host{ID: "bar"}, true).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - neighbours, err := networkTopology.Neighbours(mockSeedHost, 2) - assert.NoError(err) - assert.Equal(len(neighbours), 2) - assert.Equal(neighbours[0].ID, mockHost.ID) - assert.Equal(neighbours[1].ID, "bar") - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - hostManager := resource.NewMockHostManager(ctl) - cache := cache.NewMockCache(ctl) - storage := storagemocks.NewMockStorage(ctl) - tc.mock(res.EXPECT(), cache.EXPECT(), hostManager, hostManager.EXPECT(), mockRDBClient) - - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.expect(t, networkTopology, err) - mockRDBClient.ClearExpect() - }) - } -} - -func TestNetworkTopology_Snapshot(t *testing.T) { - tests := []struct { - name string - mock func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) - expect func(t *testing.T, networkTopology NetworkTopology, err error) - }{ - { - name: "writes the current network topology to the storage", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false).Times(1), - mc.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()).Times(1), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(mockNetworkTopology, mockCacheExpiration, true).Times(2), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "get probed count keys error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetErr( - errors.New("get probed count keys error")) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.EqualError(networkTopology.Snapshot(), "get probed count keys error") - }, - }, - { - name: "parse probed count keys in scheduler error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{"foo"}, 0) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "get network topology keys error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetErr( - errors.New("get network topology keys error")) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "parse network topology keys in scheduler error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{"foo"}, 0) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "construct destination hosts for network topology error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(nil, false), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "get averageRTT error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetErr( - errors.New("get averageRTT error")) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "get createdAt error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetErr( - errors.New("get createdAt error")) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false), - mc.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "get updatedAt error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetErr( - errors.New("get updatedAt error")) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false), - mc.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(mockNetworkTopology, mockCacheExpiration, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(nil).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "construct source hosts for network topology error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false), - mc.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(mockNetworkTopology, mockCacheExpiration, true).Times(2), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(nil, false), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - { - name: "inserts the network topology into csv file error", - mock: func(mr *resource.MockResourceMockRecorder, hostManager resource.HostManager, - mh *resource.MockHostManagerMockRecorder, ms *storagemocks.MockStorageMockRecorder, mockRDBClient redismock.ClientMock, mc *cache.MockCacheMockRecorder) { - mockRDBClient.MatchExpectationsInOrder(true) - mockRDBClient.ExpectScan(0, pkgredis.MakeProbedCountKeyInScheduler("*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeProbedCountKeyInScheduler(mockSeedHost.ID)}, 0) - mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, "*"), defaultScanCountLimit).SetVal( - []string{pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)}, 0) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - gomock.InOrder( - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockHost.ID)).Return(mockHost, true), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false), - mc.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()), - mc.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(mockNetworkTopology, mockCacheExpiration, true).Times(2), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockSeedHost.ID)).Return(mockSeedHost, true), - ms.CreateNetworkTopology(gomock.Any()).Return(errors.New("inserts the network topology into csv file error")).Times(1), - ) - }, - expect: func(t *testing.T, networkTopology NetworkTopology, err error) { - assert := assert.New(t) - assert.NoError(err) - assert.NoError(networkTopology.Snapshot()) - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - res := resource.NewMockResource(ctl) - hostManager := resource.NewMockHostManager(ctl) - storage := storagemocks.NewMockStorage(ctl) - cache := cache.NewMockCache(ctl) - tc.mock(res.EXPECT(), hostManager, hostManager.EXPECT(), storage.EXPECT(), mockRDBClient, cache.EXPECT()) - - networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, cache, res, storage) - tc.expect(t, networkTopology, err) - mockRDBClient.ClearExpect() - }) - } -} diff --git a/scheduler/networktopology/probes.go b/scheduler/networktopology/probes.go deleted file mode 100644 index 1652fe3da3f..00000000000 --- a/scheduler/networktopology/probes.go +++ /dev/null @@ -1,371 +0,0 @@ -/* - * Copyright 2023 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 mocks/probes_mock.go -source probes.go -package mocks - -package networktopology - -import ( - "context" - "encoding/json" - "errors" - "strconv" - "time" - - "github.com/redis/go-redis/v9" - - logger "d7y.io/dragonfly/v2/internal/dflog" - "d7y.io/dragonfly/v2/pkg/cache" - pkgredis "d7y.io/dragonfly/v2/pkg/redis" - "d7y.io/dragonfly/v2/scheduler/config" - resource "d7y.io/dragonfly/v2/scheduler/resource/standard" -) - -const ( - // defaultMovingAverageWeight is the default weight of moving average. - defaultMovingAverageWeight = 0.1 -) - -// Probe is the probe metadata. -type Probe struct { - // Host metadata. - Host *resource.Host `json:"host"` - - // RTT is the round-trip time sent via this pinger. - RTT time.Duration `json:"rtt"` - - // CreatedAt is the time to create probe. - CreatedAt time.Time `json:"createdAt"` -} - -// Probes is the interface to store probes. -type Probes interface { - // Peek returns the oldest probe without removing it. - Peek() (*Probe, error) - - // Enqueue enqueues probe into the queue. - Enqueue(*Probe) error - - // Len gets the length of probes. - Len() (int64, error) - - // CreatedAt is the creation time of probes. - CreatedAt() (time.Time, error) - - // UpdatedAt is the updated time to store probe. - UpdatedAt() (time.Time, error) - - // AverageRTT is the moving average round-trip time of probes. - AverageRTT() (time.Duration, error) -} - -// probes is the implementation of Probes. -type probes struct { - // config is the probe config. - config config.NetworkTopologyConfig - - // rdb is redis universal client interface. - rdb redis.UniversalClient - - // Cache instance. - cache cache.Cache - - // srcHostID is the source host id. - srcHostID string - - // destHostID is the destination host id. - destHostID string -} - -// NewProbes creates a probes interface. -func NewProbes(cfg config.NetworkTopologyConfig, rdb redis.UniversalClient, cache cache.Cache, srcHostID string, destHostID string) Probes { - return &probes{ - config: cfg, - rdb: rdb, - cache: cache, - srcHostID: srcHostID, - destHostID: destHostID, - } -} - -// Peek returns the oldest probe without removing it. -func (p *probes) Peek() (*Probe, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - probesKey := pkgredis.MakeProbesKeyInScheduler(p.srcHostID, p.destHostID) - if cache, _, ok := p.cache.GetWithExpiration(probesKey); ok { - probes, ok := cache.([]*Probe) - if !ok { - return nil, errors.New("get probes failed") - } - - if len(probes) == 0 { - return nil, errors.New("probes cache is empty") - } - - return probes[0], nil - } - - rawProbes, err := p.rdb.LRange(ctx, pkgredis.MakeProbesKeyInScheduler(p.srcHostID, p.destHostID), 0, -1).Result() - if err != nil { - logger.Errorf("get probes failed: %s", err.Error()) - return nil, err - } - - var probes []*Probe - for _, rawProbe := range rawProbes { - probe := &Probe{} - if err = json.Unmarshal([]byte(rawProbe), probe); err != nil { - return nil, err - } - probes = append(probes, probe) - } - - // Add cache data. - p.cache.Set(probesKey, probes, p.config.Cache.TTL) - - return probes[0], nil -} - -// Enqueue enqueues probe into the queue. -func (p *probes) Enqueue(probe *Probe) error { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - // Get the length of the queue. - length, err := p.Len() - if err != nil { - return err - } - - // If the queue is full, remove the oldest probe. - if length >= int64(p.config.Probe.QueueLength) { - if _, err := p.dequeue(); err != nil { - return err - } - } - - // Push the probe into the queue. - data, err := json.Marshal(probe) - if err != nil { - return err - } - - probesKey := pkgredis.MakeProbesKeyInScheduler(p.srcHostID, p.destHostID) - if err := p.rdb.RPush(ctx, probesKey, data).Err(); err != nil { - return err - } - p.cache.Delete(probesKey) - - // Calculate the moving average round-trip time. - var averageRTT time.Duration - if length > 0 { - // If the queue is not empty, calculate the - // moving average round-trip time. - rawProbes, err := p.rdb.LRange(context.Background(), probesKey, 0, -1).Result() - if err != nil { - return err - } - - for index, rawProbe := range rawProbes { - probe := &Probe{} - if err = json.Unmarshal([]byte(rawProbe), probe); err != nil { - return err - } - - if index == 0 { - averageRTT = probe.RTT - continue - } - - averageRTT = time.Duration(float64(averageRTT)*defaultMovingAverageWeight + - float64(probe.RTT)*(1-defaultMovingAverageWeight)) - } - } else { - // If the queue is empty, use the probe round-trip time as - // the moving average round-trip time. - averageRTT = probe.RTT - } - - // Update the moving average round-trip time and updated time. - networkTopologyKey := pkgredis.MakeNetworkTopologyKeyInScheduler(p.srcHostID, p.destHostID) - if err := p.rdb.HSet(ctx, networkTopologyKey, "averageRTT", averageRTT.Nanoseconds()).Err(); err != nil { - return err - } - if err := p.rdb.HSet(ctx, networkTopologyKey, "updatedAt", probe.CreatedAt.Format(time.RFC3339Nano)).Err(); err != nil { - return err - } - p.cache.Delete(networkTopologyKey) - - probedCountKey := pkgredis.MakeProbedCountKeyInScheduler(p.destHostID) - if err := p.rdb.Incr(ctx, probedCountKey).Err(); err != nil { - return err - } - p.cache.Delete(probedCountKey) - - return nil -} - -// Length gets the length of probes. -func (p *probes) Len() (int64, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - probesKey := pkgredis.MakeProbesKeyInScheduler(p.srcHostID, p.destHostID) - if cache, _, ok := p.cache.GetWithExpiration(probesKey); ok { - probes, ok := cache.([]*Probe) - if !ok { - return int64(0), errors.New("get probes failed") - } - - return int64(len(probes)), nil - } - - rawProbes, err := p.rdb.LRange(ctx, pkgredis.MakeProbesKeyInScheduler(p.srcHostID, p.destHostID), 0, -1).Result() - if err != nil { - logger.Errorf("get probes failed: %s", err.Error()) - return int64(0), err - } - - if len(rawProbes) == 0 { - return int64(0), err - } - - var probes []*Probe - for _, rawProbe := range rawProbes { - probe := &Probe{} - if err = json.Unmarshal([]byte(rawProbe), probe); err != nil { - return int64(0), err - } - probes = append(probes, probe) - } - - // Add cache data. - p.cache.Set(probesKey, probes, p.config.Cache.TTL) - - return int64(len(probes)), nil -} - -// CreatedAt is the creation time of probes. -func (p *probes) CreatedAt() (time.Time, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - var networkTopology map[string]string - networkTopologyKey := pkgredis.MakeNetworkTopologyKeyInScheduler(p.srcHostID, p.destHostID) - cache, _, ok := p.cache.GetWithExpiration(networkTopologyKey) - if ok { - if networkTopology, ok = cache.(map[string]string); !ok { - return time.Time{}, errors.New("get networkTopology failed") - } - } else { - var err error - if networkTopology, err = p.rdb.HGetAll(ctx, networkTopologyKey).Result(); err != nil { - return time.Time{}, err - } - - // Add cache data. - p.cache.Set(networkTopologyKey, networkTopology, p.config.Cache.TTL) - } - - createdAt, err := time.Parse(time.RFC3339Nano, networkTopology["createdAt"]) - if err != nil { - return time.Time{}, err - } - - return createdAt, nil -} - -// UpdatedAt is the updated time to store probe. -func (p *probes) UpdatedAt() (time.Time, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - var networkTopology map[string]string - networkTopologyKey := pkgredis.MakeNetworkTopologyKeyInScheduler(p.srcHostID, p.destHostID) - cache, _, ok := p.cache.GetWithExpiration(networkTopologyKey) - if ok { - if networkTopology, ok = cache.(map[string]string); !ok { - return time.Time{}, errors.New("get networkTopology failed") - } - } else { - var err error - if networkTopology, err = p.rdb.HGetAll(ctx, networkTopologyKey).Result(); err != nil { - return time.Time{}, err - } - - // Add cache data. - p.cache.Set(networkTopologyKey, networkTopology, p.config.Cache.TTL) - } - - updatedAt, err := time.Parse(time.RFC3339Nano, networkTopology["updatedAt"]) - if err != nil { - return time.Time{}, err - } - - return updatedAt, nil -} - -// AverageRTT is the moving average round-trip time of probes. -func (p *probes) AverageRTT() (time.Duration, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - var networkTopology map[string]string - networkTopologyKey := pkgredis.MakeNetworkTopologyKeyInScheduler(p.srcHostID, p.destHostID) - cache, _, ok := p.cache.GetWithExpiration(networkTopologyKey) - if ok { - if networkTopology, ok = cache.(map[string]string); !ok { - return time.Duration(0), errors.New("get networkTopology failed") - } - } else { - var err error - if networkTopology, err = p.rdb.HGetAll(ctx, networkTopologyKey).Result(); err != nil { - return time.Duration(0), err - } - - // Add cache data. - p.cache.Set(networkTopologyKey, networkTopology, p.config.Cache.TTL) - } - - averageRTT, err := strconv.ParseInt(networkTopology["averageRTT"], 10, 64) - if err != nil { - return time.Duration(0), err - } - - return time.Duration(averageRTT), nil -} - -// dequeue removes and returns the oldest probe. -func (p *probes) dequeue() (*Probe, error) { - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) - defer cancel() - - probesKey := pkgredis.MakeProbesKeyInScheduler(p.srcHostID, p.destHostID) - rawProbe, err := p.rdb.LPop(ctx, probesKey).Bytes() - if err != nil { - return nil, err - } - p.cache.Delete(probesKey) - - probe := &Probe{} - if err = json.Unmarshal(rawProbe, probe); err != nil { - return nil, err - } - - return probe, nil -} diff --git a/scheduler/networktopology/probes_test.go b/scheduler/networktopology/probes_test.go deleted file mode 100644 index f616f686482..00000000000 --- a/scheduler/networktopology/probes_test.go +++ /dev/null @@ -1,1222 +0,0 @@ -/* - * Copyright 2023 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. - */ - -package networktopology - -import ( - "encoding/json" - "errors" - "math" - "strconv" - "testing" - "time" - - "github.com/go-redis/redismock/v9" - "github.com/stretchr/testify/assert" - "go.uber.org/atomic" - "go.uber.org/mock/gomock" - - "d7y.io/dragonfly/v2/pkg/cache" - "d7y.io/dragonfly/v2/pkg/idgen" - pkgredis "d7y.io/dragonfly/v2/pkg/redis" - "d7y.io/dragonfly/v2/pkg/types" - "d7y.io/dragonfly/v2/scheduler/config" - resource "d7y.io/dragonfly/v2/scheduler/resource/standard" -) - -var ( - mockHost = &resource.Host{ - ID: idgen.HostIDV2("127.0.0.1", "foo"), - Type: types.HostTypeNormal, - Hostname: "foo", - IP: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - OS: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - ConcurrentUploadLimit: atomic.NewInt32(int32(300)), - ConcurrentUploadCount: atomic.NewInt32(0), - UploadCount: atomic.NewInt64(0), - UploadFailedCount: atomic.NewInt64(0), - CPU: mockCPU, - Memory: mockMemory, - Network: mockNetwork, - Disk: mockDisk, - Build: mockBuild, - CreatedAt: atomic.NewTime(time.Now()), - UpdatedAt: atomic.NewTime(time.Now()), - } - - mockSeedHost = &resource.Host{ - ID: idgen.HostIDV2("127.0.0.1", "bar"), - Type: types.HostTypeSuperSeed, - Hostname: "bar", - IP: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - OS: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - ConcurrentUploadLimit: atomic.NewInt32(int32(300)), - ConcurrentUploadCount: atomic.NewInt32(0), - UploadCount: atomic.NewInt64(0), - UploadFailedCount: atomic.NewInt64(0), - CPU: mockCPU, - Memory: mockMemory, - Network: mockNetwork, - Disk: mockDisk, - Build: mockBuild, - CreatedAt: atomic.NewTime(time.Now()), - UpdatedAt: atomic.NewTime(time.Now()), - } - - mockCPU = resource.CPU{ - LogicalCount: 4, - PhysicalCount: 2, - Percent: 1, - ProcessPercent: 0.5, - Times: resource.CPUTimes{ - User: 240662.2, - System: 317950.1, - Idle: 3393691.3, - Nice: 0, - Iowait: 0, - Irq: 0, - Softirq: 0, - Steal: 0, - Guest: 0, - GuestNice: 0, - }, - } - - mockMemory = resource.Memory{ - Total: 17179869184, - Available: 5962813440, - Used: 11217055744, - UsedPercent: 65.291858, - ProcessUsedPercent: 41.525125, - Free: 2749598908, - } - - mockNetwork = resource.Network{ - TCPConnectionCount: 10, - UploadTCPConnectionCount: 1, - Location: mockHostLocation, - IDC: mockHostIDC, - } - - mockDisk = resource.Disk{ - Total: 499963174912, - Free: 37226479616, - Used: 423809622016, - UsedPercent: 91.92547406065952, - InodesTotal: 4882452880, - InodesUsed: 7835772, - InodesFree: 4874617108, - InodesUsedPercent: 0.1604884305611568, - } - - mockBuild = resource.Build{ - GitVersion: "v1.0.0", - GitCommit: "221176b117c6d59366d68f2b34d38be50c935883", - GoVersion: "1.18", - Platform: "darwin", - } - - mockHostLocation = "location" - mockHostIDC = "idc" - - mockProbe = &Probe{ - Host: mockHost, - RTT: 30 * time.Millisecond, - CreatedAt: time.Now(), - } - - mockNetworkTopologyConfig = config.NetworkTopologyConfig{ - CollectInterval: 2 * time.Hour, - Probe: config.ProbeConfig{ - QueueLength: 5, - Count: 10, - }, - Cache: config.CacheConfig{ - Interval: 5 * time.Minute, - TTL: 5 * time.Minute, - }, - } - - mockNetworkTopology = map[string]string{ - "createdAt": time.Now().Format(time.RFC3339Nano), - "updatedAt": time.Now().Format(time.RFC3339Nano), - "averageRTT": strconv.FormatInt(mockProbe.RTT.Nanoseconds(), 10), - } - - mockCacheExpiration = time.Now() - mockProbedCount = 10 - mockCacheTLL = 5 * time.Minute -) - -func Test_NewProbes(t *testing.T) { - tests := []struct { - name string - expect func(t *testing.T, rawProbes Probes) - }{ - { - name: "new probes", - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - probes := ps.(*probes) - assert.Equal(probes.config.Probe.QueueLength, 5) - assert.NotNil(probes.rdb) - assert.Equal(probes.srcHostID, mockSeedHost.ID) - assert.Equal(probes.destHostID, mockHost.ID) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - rdb, _ := redismock.NewClientMock() - cache := cache.NewMockCache(ctl) - - tc.expect(t, NewProbes(mockNetworkTopologyConfig, rdb, cache, mockSeedHost.ID, mockHost.ID)) - }) - } -} - -func TestProbes_Peek(t *testing.T) { - tests := []struct { - name string - probes []*Probe - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) - expect func(t *testing.T, p Probes) - }{ - { - name: "queue has one probe", - probes: []*Probe{mockProbe}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - probe, err := ps.Peek() - assert.NoError(err) - assert.Equal(probe.Host.ID, mockProbe.Host.ID) - assert.Equal(probe.Host.Type, mockProbe.Host.Type) - assert.Equal(probe.Host.Hostname, mockProbe.Host.Hostname) - assert.Equal(probe.Host.IP, mockProbe.Host.IP) - assert.Equal(probe.Host.Port, mockProbe.Host.Port) - assert.Equal(probe.Host.DownloadPort, mockProbe.Host.DownloadPort) - assert.Equal(probe.Host.OS, mockProbe.Host.OS) - assert.Equal(probe.Host.Platform, mockProbe.Host.Platform) - assert.Equal(probe.Host.PlatformFamily, mockProbe.Host.PlatformFamily) - assert.Equal(probe.Host.PlatformVersion, mockProbe.Host.PlatformVersion) - assert.Equal(probe.Host.KernelVersion, mockProbe.Host.KernelVersion) - assert.Equal(probe.Host.ConcurrentUploadCount, mockProbe.Host.ConcurrentUploadCount) - assert.Equal(probe.Host.UploadCount, mockProbe.Host.UploadCount) - assert.Equal(probe.Host.UploadFailedCount, mockProbe.Host.UploadFailedCount) - assert.EqualValues(probe.Host.CPU, mockProbe.Host.CPU) - assert.EqualValues(probe.Host.Memory, mockProbe.Host.Memory) - assert.EqualValues(probe.Host.Network, mockProbe.Host.Network) - assert.EqualValues(probe.Host.Disk, mockProbe.Host.Disk) - assert.EqualValues(probe.Host.Build, mockProbe.Host.Build) - assert.Equal(probe.RTT, mockProbe.RTT) - assert.True(probe.CreatedAt.Equal(mockProbe.CreatedAt)) - }, - }, - { - name: "queue has six probe", - probes: []*Probe{ - {mockHost, 31 * time.Millisecond, time.Now()}, - {mockHost, 32 * time.Millisecond, time.Now()}, - {mockHost, 33 * time.Millisecond, time.Now()}, - {mockHost, 34 * time.Millisecond, time.Now()}, - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockRDBClient.MatchExpectationsInOrder(true) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(ps, mockCacheExpiration, true) - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(rawProbes[4]) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), []byte(rawProbes[4])).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "averageRTT", int64(30388900)).SetVal(1) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetVal(1) - mockCache.Delete(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(6) - mockCache.Delete(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(ps, mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - probe, err := ps.Peek() - assert.NoError(err) - assert.Equal(probe.RTT, 31*time.Millisecond) - assert.NoError(ps.Enqueue(mockProbe)) - - probe, err = ps.Peek() - assert.NoError(err) - assert.Equal(probe.RTT, 31*time.Millisecond) - }, - }, - { - name: "queue has no probe", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetErr(errors.New("no probe")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.Peek() - assert.EqualError(err, "no probe") - }, - }, - { - name: "type convert error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return("foo", mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.Peek() - assert.EqualError(err, "get probes failed") - }, - }, - { - name: "probes cache is empty", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return([]*Probe{}, mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.Peek() - assert.EqualError(err, "probes cache is empty") - }, - }, - { - name: "unmarshal probe error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal([]string{"foo"}) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.Peek() - assert.EqualError(err, "invalid character 'o' in literal false (expecting 'a')") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT(), tc.probes) - - tc.expect(t, NewProbes(mockNetworkTopologyConfig, rdb, cache, mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ClearExpect() - }) - } -} - -func TestProbes_Enqueue(t *testing.T) { - tests := []struct { - name string - probes []*Probe - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) - expect func(t *testing.T, ps Probes) - }{ - { - name: "enqueue probe when probes queue is empty", - probes: []*Probe{ - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - data, err := json.Marshal(ps[0]) - if err != nil { - t.Fatal(err) - } - - mockRDBClient.MatchExpectationsInOrder(true) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(nil) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), data).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "averageRTT", mockProbe.RTT.Nanoseconds()).SetVal(1) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetVal(1) - mockCache.Delete(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(1) - mockCache.Delete(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.NoError(ps.Enqueue(mockProbe)) - }, - }, - { - name: "enqueue probe when probes queue has one probe", - probes: []*Probe{ - mockProbe, - {mockHost, 31 * time.Millisecond, time.Now()}, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockRDBClient.MatchExpectationsInOrder(true) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal([]string{rawProbes[0]}) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.All()) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), []byte(rawProbes[0])).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal([]string{rawProbes[1], rawProbes[0]}) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "averageRTT", int64(30100000)).SetVal(1) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetVal(1) - mockCache.Delete(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(2) - mockCache.Delete(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.NoError(ps.Enqueue(mockProbe)) - }, - }, - { - name: "enqueue probe when probes queue has five probes", - probes: []*Probe{ - {mockHost, 31 * time.Millisecond, time.Now()}, - {mockHost, 32 * time.Millisecond, time.Now()}, - {mockHost, 33 * time.Millisecond, time.Now()}, - {mockHost, 34 * time.Millisecond, time.Now()}, - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockRDBClient.MatchExpectationsInOrder(true) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(rawProbes[0]) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), []byte(rawProbes[4])).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "averageRTT", int64(30388900)).SetVal(1) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetVal(1) - mockCache.Delete(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(6) - mockCache.Delete(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.NoError(ps.Enqueue(mockProbe)) - }, - }, - { - name: "get the length of the queue error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetErr(errors.New("get the length of the queue error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "get the length of the queue error") - }, - }, - { - name: "remove the oldest probe error when the queue is full", - probes: []*Probe{ - {mockHost, 31 * time.Millisecond, time.Now()}, - {mockHost, 32 * time.Millisecond, time.Now()}, - {mockHost, 33 * time.Millisecond, time.Now()}, - {mockHost, 34 * time.Millisecond, time.Now()}, - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetErr(errors.New("remove the oldest probe error when the queue is full")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "remove the oldest probe error when the queue is full") - }, - }, - { - name: "marshal probe error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(nil) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(&Probe{ - Host: &resource.Host{ - CPU: resource.CPU{ - Percent: math.NaN(), - }, - }, - RTT: 30 * time.Millisecond, - CreatedAt: time.Now(), - }), "json: unsupported value: NaN") - }, - }, - { - name: "push probe in queue error", - probes: []*Probe{mockProbe}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - data, err := json.Marshal(ps[0]) - if err != nil { - t.Fatal(err) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(nil) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), data).SetErr( - errors.New("push probe in queue error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "push probe in queue error") - }, - }, - { - name: "get probes error", - probes: []*Probe{ - {mockHost, 31 * time.Millisecond, time.Now()}, - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), []byte(rawProbes[1])).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetErr( - errors.New("get probes error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "get probes error") - }, - }, - { - name: "unmarshal probe error", - probes: []*Probe{ - {mockHost, 31 * time.Millisecond, time.Now()}, - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), []byte(rawProbes[1])).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal([]string{"foo"}) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "invalid character 'o' in literal false (expecting 'a')") - }, - }, - { - name: "update the moving average round-trip time error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - data, err := json.Marshal(mockProbe) - if err != nil { - t.Fatal(err) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(nil) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), data).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - "averageRTT", mockProbe.RTT.Nanoseconds()).SetErr(errors.New("update the moving average round-trip time error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "update the moving average round-trip time error") - }, - }, - { - name: "update the updated time error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - data, err := json.Marshal(mockProbe) - if err != nil { - t.Fatal(err) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(nil) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), data).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - "averageRTT", mockProbe.RTT.Nanoseconds()).SetVal(1) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - "updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetErr(errors.New("update the updated time error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "update the updated time error") - }, - }, - { - name: "update the number of times the host has been probed error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - data, err := json.Marshal(mockProbe) - if err != nil { - t.Fatal(err) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(nil) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), data).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - "averageRTT", mockProbe.RTT.Nanoseconds()).SetVal(1) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), - "updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetVal(1) - mockCache.Delete(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetErr(errors.New("update the number of times the host has been probed error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "update the number of times the host has been probed error") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT(), tc.probes) - - tc.expect(t, NewProbes(mockNetworkTopologyConfig, rdb, cache, mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ClearExpect() - }) - } -} - -// 1 -func TestProbes_Len(t *testing.T) { - tests := []struct { - name string - probes []*Probe - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) - expect func(t *testing.T, ps Probes) - }{ - { - name: "queue has one probe", - probes: []*Probe{mockProbe}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - length, err := ps.Len() - assert.NoError(err) - assert.Equal(length, int64(1)) - }, - }, - { - name: "queue has six probe", - probes: []*Probe{ - {mockHost, 31 * time.Millisecond, time.Now()}, - {mockHost, 32 * time.Millisecond, time.Now()}, - {mockHost, 33 * time.Millisecond, time.Now()}, - {mockHost, 34 * time.Millisecond, time.Now()}, - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockRDBClient.MatchExpectationsInOrder(true) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(ps, mockCacheExpiration, true) - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(string(rawProbes[4])) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), []byte(rawProbes[4])).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "averageRTT", int64(30388900)).SetVal(1) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetVal(1) - mockCache.Delete(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(6) - mockCache.Delete(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(ps, mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - length, err := ps.Len() - assert.NoError(err) - assert.Equal(length, int64(5)) - assert.NoError(ps.Enqueue(mockProbe)) - - length, err = ps.Len() - assert.NoError(err) - assert.Equal(length, int64(5)) - }, - }, - { - name: "queue has no probe", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(nil) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - length, err := ps.Len() - assert.NoError(err) - assert.Equal(length, int64(0)) - }, - }, - { - name: "get queue length error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetErr(errors.New("get queue length error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.Len() - assert.EqualError(err, "get queue length error") - }, - }, - { - name: "type convert error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return("foo", mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "get probes failed") - }, - }, - { - name: "unmarshal probe error", - probes: []*Probe{ - {mockHost, 31 * time.Millisecond, time.Now()}, - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal([]string{"foo"}) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.EqualError(ps.Enqueue(mockProbe), "invalid character 'o' in literal false (expecting 'a')") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT(), tc.probes) - - tc.expect(t, NewProbes(mockNetworkTopologyConfig, rdb, cache, mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ClearExpect() - }) - } -} - -func TestProbes_CreatedAt(t *testing.T) { - tests := []struct { - name string - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) - expect func(t *testing.T, ps Probes) - }{ - { - name: "get creation time of probes", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - createdAt, err := ps.CreatedAt() - assert.NoError(err) - assert.Equal(createdAt.Format(time.RFC3339Nano), mockNetworkTopology["createdAt"]) - }, - }, - { - name: "get creation time of probes error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetErr(errors.New("get creation time of probes error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.CreatedAt() - assert.EqualError(err, "get creation time of probes error") - }, - }, - { - name: "type convert error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return("foo", mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.CreatedAt() - assert.EqualError(err, "get networkTopology failed") - }, - }, - { - name: "time parse error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(map[string]string{"createdAt": "foo"}, mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.CreatedAt() - assert.EqualError(err, "parsing time \"foo\" as \"2006-01-02T15:04:05.999999999Z07:00\": cannot parse \"foo\" as \"2006\"") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT()) - - tc.expect(t, NewProbes(mockNetworkTopologyConfig, rdb, cache, mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ClearExpect() - }) - } -} - -func TestProbes_UpdatedAt(t *testing.T) { - tests := []struct { - name string - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) - expect func(t *testing.T, ps Probes) - }{ - { - name: "get update time of probes", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - updatedAt, err := ps.UpdatedAt() - assert.NoError(err) - assert.Equal(updatedAt.Format(time.RFC3339Nano), mockNetworkTopology["updatedAt"]) - }, - }, - { - name: "get update time of probes error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetErr(errors.New("get update time of probes error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.UpdatedAt() - assert.EqualError(err, "get update time of probes error") - }, - }, - { - name: "type convert error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return("foo", mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.UpdatedAt() - assert.EqualError(err, "get networkTopology failed") - }, - }, - { - name: "time parse error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(map[string]string{"updatedAt": "foo"}, mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.UpdatedAt() - assert.EqualError(err, "parsing time \"foo\" as \"2006-01-02T15:04:05.999999999Z07:00\": cannot parse \"foo\" as \"2006\"") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT()) - - tc.expect(t, NewProbes(mockNetworkTopologyConfig, rdb, cache, mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ClearExpect() - }) - } -} - -func TestProbes_AverageRTT(t *testing.T) { - tests := []struct { - name string - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) - expect func(t *testing.T, ps Probes) - }{ - { - name: "get averageRTT of probes", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(mockNetworkTopology) - mockCache.Set(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), mockNetworkTopology, gomock.Any()) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - averageRTT, err := ps.AverageRTT() - assert.NoError(err) - assert.Equal(averageRTT, mockProbe.RTT) - }, - }, - { - name: "get averageRTT of probes error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectHGetAll(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetErr(errors.New("get averageRTT of probes error")) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.AverageRTT() - assert.EqualError(err, "get averageRTT of probes error") - }, - }, - { - name: "get averageRTT of probes with cache", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(mockNetworkTopology, mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - averageRTT, err := ps.AverageRTT() - assert.NoError(err) - assert.Equal(averageRTT, mockProbe.RTT) - }, - }, - { - name: "type convert error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return("foo", mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.AverageRTT() - assert.EqualError(err, "get networkTopology failed") - }, - }, - { - name: "parseInt error", - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder) { - mockCache.GetWithExpiration(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(map[string]string{"averageRTT": "foo"}, mockCacheExpiration, true) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.AverageRTT() - assert.EqualError(err, "strconv.ParseInt: parsing \"foo\": invalid syntax") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT()) - - tc.expect(t, NewProbes(mockNetworkTopologyConfig, rdb, cache, mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ClearExpect() - }) - } -} - -func TestProbes_dequeue(t *testing.T) { - tests := []struct { - name string - probes []*Probe - mock func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) - expect func(t *testing.T, ps Probes) - }{ - { - name: "queue has one probe", - probes: []*Probe{ - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - data, err := json.Marshal(ps[0]) - if err != nil { - t.Fatal(err) - } - - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(string(data)) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - probe, err := ps.(*probes).dequeue() - assert.NoError(err) - assert.Equal(probe.Host.ID, mockProbe.Host.ID) - assert.Equal(probe.Host.Type, mockProbe.Host.Type) - assert.Equal(probe.Host.Hostname, mockProbe.Host.Hostname) - assert.Equal(probe.Host.IP, mockProbe.Host.IP) - assert.Equal(probe.Host.Port, mockProbe.Host.Port) - assert.Equal(probe.Host.DownloadPort, mockProbe.Host.DownloadPort) - assert.Equal(probe.Host.OS, mockProbe.Host.OS) - assert.Equal(probe.Host.Platform, mockProbe.Host.Platform) - assert.Equal(probe.Host.PlatformFamily, mockProbe.Host.PlatformFamily) - assert.Equal(probe.Host.PlatformVersion, mockProbe.Host.PlatformVersion) - assert.Equal(probe.Host.KernelVersion, mockProbe.Host.KernelVersion) - assert.Equal(probe.Host.ConcurrentUploadLimit, mockProbe.Host.ConcurrentUploadLimit) - assert.Equal(probe.Host.ConcurrentUploadCount, mockProbe.Host.ConcurrentUploadCount) - assert.Equal(probe.Host.UploadCount, mockProbe.Host.UploadCount) - assert.Equal(probe.Host.UploadFailedCount, mockProbe.Host.UploadFailedCount) - assert.EqualValues(probe.Host.CPU, mockProbe.Host.CPU) - assert.EqualValues(probe.Host.Memory, mockProbe.Host.Memory) - assert.EqualValues(probe.Host.Network, mockProbe.Host.Network) - assert.EqualValues(probe.Host.Disk, mockProbe.Host.Disk) - assert.EqualValues(probe.Host.Build, mockProbe.Host.Build) - assert.Equal(probe.RTT, mockProbe.RTT) - assert.True(probe.CreatedAt.Equal(mockProbe.CreatedAt)) - }, - }, - { - name: "queue has six probe", - probes: []*Probe{ - {mockHost, 31 * time.Millisecond, time.Now()}, - {mockHost, 32 * time.Millisecond, time.Now()}, - {mockHost, 33 * time.Millisecond, time.Now()}, - {mockHost, 34 * time.Millisecond, time.Now()}, - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - var rawProbes []string - for _, p := range ps { - data, err := json.Marshal(p) - if err != nil { - t.Fatal(err) - } - - rawProbes = append(rawProbes, string(data)) - } - - mockRDBClient.MatchExpectationsInOrder(true) - mockCache.GetWithExpiration(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).Return(nil, mockCacheExpiration, false) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockCache.Set(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), gomock.Any(), gomock.Any()) - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(string(rawProbes[4])) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), []byte(rawProbes[4])).SetVal(1) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectLRange(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0, -1).SetVal(rawProbes) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "averageRTT", int64(30388900)).SetVal(1) - mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID), "updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetVal(1) - mockCache.Delete(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(6) - mockCache.Delete(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)) - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(string(rawProbes[0])) - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - assert.NoError(ps.Enqueue(mockProbe)) - - probe, err := ps.(*probes).dequeue() - assert.NoError(err) - assert.Equal(probe.RTT, 31*time.Millisecond) - }, - }, - { - name: "dequeue probe from empty probes", - probes: []*Probe{ - mockProbe, - }, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).RedisNil() - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.(*probes).dequeue() - assert.EqualError(err, "redis: nil") - }, - }, - { - name: "unmarshal probe error", - probes: []*Probe{}, - mock: func(mockRDBClient redismock.ClientMock, mockCache *cache.MockCacheMockRecorder, ps []*Probe) { - mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal("foo") - mockCache.Delete(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)) - }, - expect: func(t *testing.T, ps Probes) { - assert := assert.New(t) - _, err := ps.(*probes).dequeue() - assert.EqualError(err, "invalid character 'o' in literal false (expecting 'a')") - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - rdb, mockRDBClient := redismock.NewClientMock() - cache := cache.NewMockCache(ctl) - tc.mock(mockRDBClient, cache.EXPECT(), tc.probes) - - tc.expect(t, NewProbes(mockNetworkTopologyConfig, rdb, cache, mockSeedHost.ID, mockHost.ID)) - mockRDBClient.ClearExpect() - }) - } -} diff --git a/scheduler/rpcserver/rpcserver.go b/scheduler/rpcserver/rpcserver.go index c7255c0dc56..bf10b83acea 100644 --- a/scheduler/rpcserver/rpcserver.go +++ b/scheduler/rpcserver/rpcserver.go @@ -21,7 +21,6 @@ import ( "d7y.io/dragonfly/v2/pkg/rpc/scheduler/server" "d7y.io/dragonfly/v2/scheduler/config" - "d7y.io/dragonfly/v2/scheduler/networktopology" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/scheduling" "d7y.io/dragonfly/v2/scheduler/storage" @@ -34,11 +33,10 @@ func New( scheduling scheduling.Scheduling, dynconfig config.DynconfigInterface, storage storage.Storage, - networkTopology networktopology.NetworkTopology, opts ...grpc.ServerOption, ) *grpc.Server { return server.New( - newSchedulerServerV1(cfg, resource, scheduling, dynconfig, storage, networkTopology), - newSchedulerServerV2(cfg, resource, scheduling, dynconfig, storage, networkTopology), + newSchedulerServerV1(cfg, resource, scheduling, dynconfig, storage), + newSchedulerServerV2(cfg, resource, scheduling, dynconfig, storage), opts...) } diff --git a/scheduler/rpcserver/rpcserver_test.go b/scheduler/rpcserver/rpcserver_test.go index f6fa179b3a3..eb016e443f0 100644 --- a/scheduler/rpcserver/rpcserver_test.go +++ b/scheduler/rpcserver/rpcserver_test.go @@ -26,7 +26,6 @@ import ( "d7y.io/dragonfly/v2/scheduler/config" configmocks "d7y.io/dragonfly/v2/scheduler/config/mocks" - networktopologymocks "d7y.io/dragonfly/v2/scheduler/networktopology/mocks" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/scheduling/mocks" storagemocks "d7y.io/dragonfly/v2/scheduler/storage/mocks" @@ -63,9 +62,8 @@ func TestRPCServer_New(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - svr := New(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svr := New(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.expect(t, svr) }) } diff --git a/scheduler/rpcserver/scheduler_server_v1.go b/scheduler/rpcserver/scheduler_server_v1.go index 58c966804b0..6dc92bbbdb3 100644 --- a/scheduler/rpcserver/scheduler_server_v1.go +++ b/scheduler/rpcserver/scheduler_server_v1.go @@ -28,7 +28,6 @@ import ( "d7y.io/dragonfly/v2/pkg/types" "d7y.io/dragonfly/v2/scheduler/config" "d7y.io/dragonfly/v2/scheduler/metrics" - "d7y.io/dragonfly/v2/scheduler/networktopology" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/scheduling" "d7y.io/dragonfly/v2/scheduler/service" @@ -48,9 +47,8 @@ func newSchedulerServerV1( scheduling scheduling.Scheduling, dynconfig config.DynconfigInterface, storage storage.Storage, - networkTopology networktopology.NetworkTopology, ) schedulerv1.SchedulerServer { - return &schedulerServerV1{service.NewV1(cfg, resource, scheduling, dynconfig, storage, networkTopology)} + return &schedulerServerV1{service.NewV1(cfg, resource, scheduling, dynconfig, storage)} } // RegisterPeerTask registers peer and triggers seed peer download task. @@ -155,16 +153,3 @@ func (s *schedulerServerV1) LeaveHost(ctx context.Context, req *schedulerv1.Leav return new(emptypb.Empty), nil } - -// SyncProbes sync probes of the host. -func (s *schedulerServerV1) SyncProbes(stream schedulerv1.Scheduler_SyncProbesServer) error { - // Collect SyncProbesCount metrics. - metrics.SyncProbesCount.Inc() - if err := s.service.SyncProbes(stream); err != nil { - // Collect SyncProbesFailureCount metrics. - metrics.SyncProbesFailureCount.Inc() - return err - } - - return nil -} diff --git a/scheduler/rpcserver/scheduler_server_v2.go b/scheduler/rpcserver/scheduler_server_v2.go index 1b7699e89b8..a36062b7f5f 100644 --- a/scheduler/rpcserver/scheduler_server_v2.go +++ b/scheduler/rpcserver/scheduler_server_v2.go @@ -26,7 +26,6 @@ import ( "d7y.io/dragonfly/v2/scheduler/config" "d7y.io/dragonfly/v2/scheduler/metrics" - "d7y.io/dragonfly/v2/scheduler/networktopology" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/scheduling" "d7y.io/dragonfly/v2/scheduler/service" @@ -47,9 +46,8 @@ func newSchedulerServerV2( scheduling scheduling.Scheduling, dynconfig config.DynconfigInterface, storage storage.Storage, - networkTopology networktopology.NetworkTopology, ) schedulerv2.SchedulerServer { - return &schedulerServerV2{service.NewV2(cfg, resource, scheduling, dynconfig, storage, networkTopology)} + return &schedulerServerV2{service.NewV2(cfg, resource, scheduling, dynconfig, storage)} } // AnnouncePeer announces peer to scheduler. @@ -157,19 +155,6 @@ func (s *schedulerServerV2) DeleteHost(ctx context.Context, req *schedulerv2.Del return new(emptypb.Empty), nil } -// SyncProbes sync probes of the host. -func (s *schedulerServerV2) SyncProbes(stream schedulerv2.Scheduler_SyncProbesServer) error { - // Collect SyncProbesCount metrics. - metrics.SyncProbesCount.Inc() - if err := s.service.SyncProbes(stream); err != nil { - // Collect SyncProbesFailureCount metrics. - metrics.SyncProbesFailureCount.Inc() - return err - } - - return nil -} - // AnnouncePersistentCachePeer announces persistent cache peer to scheduler. func (s *schedulerServerV2) AnnouncePersistentCachePeer(stream schedulerv2.Scheduler_AnnouncePersistentCachePeerServer) error { // Collect AnnouncePersistentCachePeerCount metrics. diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 5b84850f3e5..81d63c9f473 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -51,11 +51,9 @@ import ( "d7y.io/dragonfly/v2/scheduler/config" "d7y.io/dragonfly/v2/scheduler/job" "d7y.io/dragonfly/v2/scheduler/metrics" - "d7y.io/dragonfly/v2/scheduler/networktopology" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/rpcserver" "d7y.io/dragonfly/v2/scheduler/scheduling" - "d7y.io/dragonfly/v2/scheduler/scheduling/evaluator" "d7y.io/dragonfly/v2/scheduler/storage" ) @@ -97,9 +95,6 @@ type Server struct { // Announcer interface. announcer announcer.Announcer - // Network topology interface. - networkTopology networktopology.NetworkTopology - // GC service. gc gc.GC } @@ -146,7 +141,6 @@ func New(ctx context.Context, cfg *config.Config, d dfpath.Dfpath) (*Server, err rdb, err = pkgredis.NewRedis(&redis.UniversalOptions{ Addrs: cfg.Database.Redis.Addrs, MasterName: cfg.Database.Redis.MasterName, - DB: cfg.Database.Redis.NetworkTopologyDB, Username: cfg.Database.Redis.Username, Password: cfg.Database.Redis.Password, SentinelUsername: cfg.Database.Redis.SentinelUsername, @@ -244,20 +238,8 @@ func New(ctx context.Context, cfg *config.Config, d dfpath.Dfpath) (*Server, err } } - // Initialize options of network topology options. - evaluatorNetworkTopologyOptions := []evaluator.NetworkTopologyOption{} - if cfg.Scheduler.Algorithm == evaluator.NetworkTopologyAlgorithm && rdb != nil { - cache := cache.New(cfg.Scheduler.NetworkTopology.Cache.TTL, cfg.Scheduler.NetworkTopology.Cache.Interval) - s.networkTopology, err = networktopology.NewNetworkTopology(cfg.Scheduler.NetworkTopology, rdb, cache, resource, s.storage) - if err != nil { - return nil, err - } - - evaluatorNetworkTopologyOptions = append(evaluatorNetworkTopologyOptions, evaluator.WithNetworkTopology(s.networkTopology)) - } - // Initialize scheduling. - scheduling := scheduling.New(&cfg.Scheduler, dynconfig, d.PluginDir(), evaluatorNetworkTopologyOptions...) + scheduling := scheduling.New(&cfg.Scheduler, dynconfig, d.PluginDir()) // Initialize server options of scheduler grpc server. schedulerServerOptions := []grpc.ServerOption{} @@ -272,7 +254,7 @@ func New(ctx context.Context, cfg *config.Config, d dfpath.Dfpath) (*Server, err schedulerServerOptions = append(schedulerServerOptions, grpc.Creds(insecure.NewCredentials())) } - svr := rpcserver.New(cfg, resource, scheduling, dynconfig, s.storage, s.networkTopology, schedulerServerOptions...) + svr := rpcserver.New(cfg, resource, scheduling, dynconfig, s.storage, schedulerServerOptions...) s.grpcServer = svr // Initialize metrics. @@ -324,14 +306,6 @@ func (s *Server) Serve() error { logger.Info("announcer start successfully") }() - // Serve network topology. - if s.networkTopology != nil { - go func() { - s.networkTopology.Serve() - logger.Info("network topology start successfully") - }() - } - // Generate GRPC limit listener. ip, ok := ip.FormatIP(s.config.Server.ListenIP.String()) if !ok { @@ -377,13 +351,6 @@ func (s *Server) Stop() { logger.Info("clean download storage completed") } - // Clean network topology storage. - if err := s.storage.ClearNetworkTopology(); err != nil { - logger.Errorf("clean network topology storage failed %s", err.Error()) - } else { - logger.Info("clean network topology storage completed") - } - // Stop GC. s.gc.Stop() logger.Info("gc closed") @@ -419,12 +386,6 @@ func (s *Server) Stop() { } } - // Stop network topology. - if s.networkTopology != nil { - s.networkTopology.Stop() - logger.Info("network topology closed") - } - // Stop GRPC server. stopped := make(chan struct{}) go func() { diff --git a/scheduler/scheduling/evaluator/evaluator.go b/scheduler/scheduling/evaluator/evaluator.go index a4f5f47ec85..7cab68fe983 100644 --- a/scheduler/scheduling/evaluator/evaluator.go +++ b/scheduler/scheduling/evaluator/evaluator.go @@ -29,9 +29,6 @@ const ( // DefaultAlgorithm is a rule-based scheduling algorithm. DefaultAlgorithm = "default" - // NetworkTopologyAlgorithm is a scheduling algorithm based on rules and network topology. - NetworkTopologyAlgorithm = "nt" - // MLAlgorithm is a machine learning scheduling algorithm. MLAlgorithm = "ml" @@ -73,14 +70,12 @@ type Evaluator interface { type evaluator struct{} // New returns a new Evaluator. -func New(algorithm string, pluginDir string, networkTopologyOptions ...NetworkTopologyOption) Evaluator { +func New(algorithm string, pluginDir string) Evaluator { switch algorithm { case PluginAlgorithm: if plugin, err := LoadPlugin(pluginDir); err == nil { return plugin } - case NetworkTopologyAlgorithm: - return newEvaluatorNetworkTopology(networkTopologyOptions...) // TODO Implement MLAlgorithm. case MLAlgorithm, DefaultAlgorithm: return newEvaluatorBase() diff --git a/scheduler/scheduling/evaluator/evaluator_network_topology.go b/scheduler/scheduling/evaluator/evaluator_network_topology.go deleted file mode 100644 index f8ff364e00f..00000000000 --- a/scheduler/scheduling/evaluator/evaluator_network_topology.go +++ /dev/null @@ -1,224 +0,0 @@ -/* - * 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. - */ - -package evaluator - -import ( - "sort" - "strings" - "time" - - "d7y.io/dragonfly/v2/pkg/math" - "d7y.io/dragonfly/v2/pkg/types" - "d7y.io/dragonfly/v2/scheduler/networktopology" - resource "d7y.io/dragonfly/v2/scheduler/resource/standard" -) - -const ( - // Finished piece weight. - networkTopologyFinishedPieceWeight float64 = 0.2 - - // Parent's host upload success weight. - networkTopologyParentHostUploadSuccessWeight = 0.2 - - // Free upload weight. - networkTopologyFreeUploadWeight = 0.15 - - // Network topology weight. - networkTopologyProbeWeight = 0.12 - - // Host type weight. - networkTopologyHostTypeWeight = 0.11 - - // IDC affinity weight. - networkTopologyIDCAffinityWeight = 0.11 - - // Location affinity weight. - networkTopologyLocationAffinityWeight = 0.11 -) - -const ( - // defaultPingTimeout specifies a default timeout before ping exits. - defaultPingTimeout = 1 * time.Second -) - -// evaluatorNetworkTopology is an implementation of Evaluator. -type evaluatorNetworkTopology struct { - evaluator - networktopology networktopology.NetworkTopology -} - -// NetworkTopologyOption is a functional option for configuring the evaluatorNetworkTopology. -type NetworkTopologyOption func(e *evaluatorNetworkTopology) - -// WithNetworkTopology sets the networkTopology. -func WithNetworkTopology(networktopology networktopology.NetworkTopology) NetworkTopologyOption { - return func(e *evaluatorNetworkTopology) { - e.networktopology = networktopology - } -} - -func newEvaluatorNetworkTopology(options ...NetworkTopologyOption) Evaluator { - e := &evaluatorNetworkTopology{} - for _, opt := range options { - opt(e) - } - - return e -} - -// EvaluateParents sort parents by evaluating multiple feature scores. -func (e *evaluatorNetworkTopology) EvaluateParents(parents []*resource.Peer, child *resource.Peer, totalPieceCount int32) []*resource.Peer { - sort.Slice( - parents, - func(i, j int) bool { - return e.evaluate(parents[i], child, totalPieceCount) > e.evaluate(parents[j], child, totalPieceCount) - }, - ) - - return parents -} - -// The larger the value, the higher the priority. -func (e *evaluatorNetworkTopology) evaluate(parent *resource.Peer, child *resource.Peer, totalPieceCount int32) float64 { - parentLocation := parent.Host.Network.Location - parentIDC := parent.Host.Network.IDC - childLocation := child.Host.Network.Location - childIDC := child.Host.Network.IDC - - return networkTopologyFinishedPieceWeight*e.calculatePieceScore(parent, child, totalPieceCount) + - networkTopologyParentHostUploadSuccessWeight*e.calculateParentHostUploadSuccessScore(parent) + - networkTopologyFreeUploadWeight*e.calculateFreeUploadScore(parent.Host) + - networkTopologyHostTypeWeight*e.calculateHostTypeScore(parent) + - networkTopologyIDCAffinityWeight*e.calculateIDCAffinityScore(parentIDC, childIDC) + - networkTopologyLocationAffinityWeight*e.calculateMultiElementAffinityScore(parentLocation, childLocation) + - networkTopologyProbeWeight*e.calculateNetworkTopologyScore(parent.ID, child.ID) -} - -// calculatePieceScore 0.0~unlimited larger and better. -func (e *evaluatorNetworkTopology) calculatePieceScore(parent *resource.Peer, child *resource.Peer, totalPieceCount int32) float64 { - // If the total piece is determined, normalize the number of - // pieces downloaded by the parent node. - if totalPieceCount > 0 { - finishedPieceCount := parent.FinishedPieces.Count() - return float64(finishedPieceCount) / float64(totalPieceCount) - } - - // Use the difference between the parent node and the child node to - // download the piece to roughly represent the piece score. - parentFinishedPieceCount := parent.FinishedPieces.Count() - childFinishedPieceCount := child.FinishedPieces.Count() - return float64(parentFinishedPieceCount) - float64(childFinishedPieceCount) -} - -// calculateParentHostUploadSuccessScore 0.0~unlimited larger and better. -func (e *evaluatorNetworkTopology) calculateParentHostUploadSuccessScore(peer *resource.Peer) float64 { - uploadCount := peer.Host.UploadCount.Load() - uploadFailedCount := peer.Host.UploadFailedCount.Load() - if uploadCount < uploadFailedCount { - return minScore - } - - // Host has not been scheduled, then it is scheduled first. - if uploadCount == 0 && uploadFailedCount == 0 { - return maxScore - } - - return float64(uploadCount-uploadFailedCount) / float64(uploadCount) -} - -// calculateFreeUploadScore 0.0~1.0 larger and better. -func (e *evaluatorNetworkTopology) calculateFreeUploadScore(host *resource.Host) float64 { - ConcurrentUploadLimit := host.ConcurrentUploadLimit.Load() - freeUploadCount := host.FreeUploadCount() - if ConcurrentUploadLimit > 0 && freeUploadCount > 0 { - return float64(freeUploadCount) / float64(ConcurrentUploadLimit) - } - - return minScore -} - -// calculateHostTypeScore 0.0~1.0 larger and better. -func (e *evaluatorNetworkTopology) calculateHostTypeScore(peer *resource.Peer) float64 { - // When the task is downloaded for the first time, - // peer will be scheduled to seed peer first, - // otherwise it will be scheduled to dfdaemon first. - if peer.Host.Type != types.HostTypeNormal { - if peer.FSM.Is(resource.PeerStateReceivedNormal) || - peer.FSM.Is(resource.PeerStateRunning) { - return maxScore - } - - return minScore - } - - return maxScore * 0.5 -} - -// calculateIDCAffinityScore 0.0~1.0 larger and better. -func (e *evaluatorNetworkTopology) calculateIDCAffinityScore(dst, src string) float64 { - if dst == "" || src == "" { - return minScore - } - - if strings.EqualFold(dst, src) { - return maxScore - } - - return minScore -} - -// calculateMultiElementAffinityScore 0.0~1.0 larger and better. -func (e *evaluatorNetworkTopology) calculateMultiElementAffinityScore(dst, src string) float64 { - if dst == "" || src == "" { - return minScore - } - - if strings.EqualFold(dst, src) { - return maxScore - } - - // Calculate the number of multi-element matches divided by "|". - var score, elementLen int - dstElements := strings.Split(dst, types.AffinitySeparator) - srcElements := strings.Split(src, types.AffinitySeparator) - elementLen = math.Min(len(dstElements), len(srcElements)) - - // Maximum element length is 5. - if elementLen > maxElementLen { - elementLen = maxElementLen - } - - for i := 0; i < elementLen; i++ { - if !strings.EqualFold(dstElements[i], srcElements[i]) { - break - } - - score++ - } - - return float64(score) / float64(maxElementLen) -} - -// calculateNetworkTopologyScore 0.0~1.0 larger and better. -func (e *evaluatorNetworkTopology) calculateNetworkTopologyScore(dst, src string) float64 { - averageRTT, err := e.networktopology.Probes(dst, src).AverageRTT() - if err != nil { - return minScore - } - - return float64(defaultPingTimeout-averageRTT) / float64(defaultPingTimeout) -} diff --git a/scheduler/scheduling/evaluator/evaluator_network_topology_test.go b/scheduler/scheduling/evaluator/evaluator_network_topology_test.go deleted file mode 100644 index bbb7fc476de..00000000000 --- a/scheduler/scheduling/evaluator/evaluator_network_topology_test.go +++ /dev/null @@ -1,888 +0,0 @@ -/* - * 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. - */ - -package evaluator - -import ( - "errors" - "reflect" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "go.uber.org/mock/gomock" - - commonv2 "d7y.io/api/v2/pkg/apis/common/v2" - - "d7y.io/dragonfly/v2/pkg/idgen" - "d7y.io/dragonfly/v2/pkg/types" - "d7y.io/dragonfly/v2/scheduler/networktopology" - networktopologymocks "d7y.io/dragonfly/v2/scheduler/networktopology/mocks" - resource "d7y.io/dragonfly/v2/scheduler/resource/standard" -) - -func TestEvaluatorNetworkTopology_newEvaluatorNetworkTopology(t *testing.T) { - tests := []struct { - name string - expect func(t *testing.T, e any) - }{ - { - name: "new evaluator commonv1", - expect: func(t *testing.T, e any) { - assert := assert.New(t) - assert.Equal(reflect.TypeOf(e).Elem().Name(), "evaluatorNetworkTopology") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - tc.expect(t, newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology))) - }) - } -} - -func TestEvaluatorNetworkTopology_EvaluateParents(t *testing.T) { - tests := []struct { - name string - parents []*resource.Peer - child *resource.Peer - totalPieceCount int32 - mock func(parents []*resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) - expect func(t *testing.T, parents []*resource.Peer) - }{ - { - name: "parents is empty", - parents: []*resource.Peer{}, - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - totalPieceCount: 1, - mock: func(parents []*resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) { - }, - expect: func(t *testing.T, parents []*resource.Peer) { - assert := assert.New(t) - assert.Equal(len(parents), 0) - }, - }, - { - name: "evaluate single parent", - parents: []*resource.Peer{ - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - }, - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - totalPieceCount: 1, - mock: func(parents []*resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) { - }, - expect: func(t *testing.T, parents []*resource.Peer) { - assert := assert.New(t) - assert.Equal(len(parents), 1) - assert.Equal(parents[0].Task.ID, mockTaskID) - assert.Equal(parents[0].Host.ID, mockRawSeedHost.ID) - }, - }, - { - name: "evaluate parents with free upload count", - parents: []*resource.Peer{ - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "bar", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "baz", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "bac", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "bae", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - }, - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - totalPieceCount: 1, - mock: func(parents []*resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) { - parents[1].Host.ConcurrentUploadCount.Add(4) - parents[2].Host.ConcurrentUploadCount.Add(3) - parents[3].Host.ConcurrentUploadCount.Add(2) - parents[4].Host.ConcurrentUploadCount.Add(1) - mn.Probes(gomock.Any(), gomock.Any()).Return(p).Times(20) - mp.AverageRTT().Return(1*time.Second, nil).Times(20) - }, - expect: func(t *testing.T, parents []*resource.Peer) { - assert := assert.New(t) - assert.Equal(len(parents), 5) - assert.Equal(parents[0].Host.ID, mockRawSeedHost.ID) - assert.Equal(parents[1].Host.ID, "bae") - assert.Equal(parents[2].Host.ID, "bac") - assert.Equal(parents[3].Host.ID, "baz") - assert.Equal(parents[4].Host.ID, "bar") - }, - }, - { - name: "evaluate parents with pieces", - parents: []*resource.Peer{ - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "bar", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "baz", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "bac", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "bae", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - }, - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - totalPieceCount: 1, - mock: func(parents []*resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) { - parents[1].FinishedPieces.Set(0) - parents[2].FinishedPieces.Set(0).Set(1) - parents[3].FinishedPieces.Set(0).Set(1).Set(2) - parents[4].FinishedPieces.Set(0).Set(1).Set(2).Set(3) - mn.Probes(gomock.Any(), gomock.Any()).Return(p).Times(20) - mp.AverageRTT().Return(1*time.Second, nil).Times(20) - }, - expect: func(t *testing.T, parents []*resource.Peer) { - assert := assert.New(t) - assert.Equal(len(parents), 5) - assert.Equal(parents[0].Host.ID, "bae") - assert.Equal(parents[1].Host.ID, "bac") - assert.Equal(parents[2].Host.ID, "baz") - assert.Equal(parents[3].Host.ID, "bar") - assert.Equal(parents[4].Host.ID, mockRawSeedHost.ID) - }, - }, - { - name: "evaluate single parent with networkTopology", - parents: []*resource.Peer{ - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - "bar", mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - }, - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - totalPieceCount: 1, - mock: func(parents []*resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) { - mn.Probes(parents[0].ID, child.ID).Return(p) - mp.AverageRTT().Return(100*time.Millisecond, nil) - mn.Probes(parents[1].ID, child.ID).Return(p) - mp.AverageRTT().Return(200*time.Millisecond, nil) - }, - expect: func(t *testing.T, parents []*resource.Peer) { - assert := assert.New(t) - assert.Equal(len(parents), 2) - assert.Equal(parents[0].Host.ID, "bar") - assert.Equal(parents[1].Host.ID, mockRawSeedHost.ID) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - mockProbe := networktopologymocks.NewMockProbes(ctl) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - tc.mock(tc.parents, tc.child, mockProbe, mockNetworkTopology.EXPECT(), mockProbe.EXPECT()) - tc.expect(t, e.EvaluateParents(tc.parents, tc.child, tc.totalPieceCount)) - }) - } -} - -func TestEvaluatorNetworkTopology_evaluate(t *testing.T) { - tests := []struct { - name string - parent *resource.Peer - child *resource.Peer - totalPieceCount int32 - mock func(parent *resource.Peer, child *resource.Peer, p networktopology.Probes, mp *networktopologymocks.MockProbesMockRecorder, mn *networktopologymocks.MockNetworkTopologyMockRecorder) - expect func(t *testing.T, score float64) - }{ - { - name: "evaluate parent", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - totalPieceCount: 1, - mock: func(parent *resource.Peer, child *resource.Peer, p networktopology.Probes, mp *networktopologymocks.MockProbesMockRecorder, mn *networktopologymocks.MockNetworkTopologyMockRecorder) { - mn.Probes(parent.ID, child.ID).Return(p) - mp.AverageRTT().Return(500*time.Millisecond, nil) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.41)) - }, - }, - { - name: "evaluate parent with pieces", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - totalPieceCount: 1, - mock: func(parent *resource.Peer, child *resource.Peer, p networktopology.Probes, mp *networktopologymocks.MockProbesMockRecorder, mn *networktopologymocks.MockNetworkTopologyMockRecorder) { - parent.FinishedPieces.Set(0) - mn.Probes(parent.ID, child.ID).Return(p) - mp.AverageRTT().Return(1000*time.Millisecond, nil) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.55)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - mockProbe := networktopologymocks.NewMockProbes(ctl) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - tc.mock(tc.parent, tc.child, mockProbe, mockProbe.EXPECT(), mockNetworkTopology.EXPECT()) - tc.expect(t, e.(*evaluatorNetworkTopology).evaluate(tc.parent, tc.child, tc.totalPieceCount)) - }) - } -} - -func TestEvaluatorNetworkTopology_calculatePieceScore(t *testing.T) { - mockHost := resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) - mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) - - tests := []struct { - name string - parent *resource.Peer - child *resource.Peer - totalPieceCount int32 - mock func(parent *resource.Peer, child *resource.Peer) - expect func(t *testing.T, score float64) - }{ - { - name: "total piece count is zero and child pieces are empty", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - totalPieceCount: 0, - mock: func(parent *resource.Peer, child *resource.Peer) { - parent.FinishedPieces.Set(0) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - { - name: "total piece count is zero and parent pieces are empty", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - totalPieceCount: 0, - mock: func(parent *resource.Peer, child *resource.Peer) { - child.FinishedPieces.Set(0) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(-1)) - }, - }, - { - name: "total piece count is zero and child pieces of length greater than parent pieces", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - totalPieceCount: 0, - mock: func(parent *resource.Peer, child *resource.Peer) { - parent.FinishedPieces.Set(0) - child.FinishedPieces.Set(0) - child.FinishedPieces.Set(1) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(-1)) - }, - }, - { - name: "total piece count is zero and child pieces of length equal than parent pieces", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - totalPieceCount: 0, - mock: func(parent *resource.Peer, child *resource.Peer) { - parent.FinishedPieces.Set(0) - child.FinishedPieces.Set(0) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "total piece count is zero and parent pieces of length greater than child pieces", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - totalPieceCount: 0, - mock: func(parent *resource.Peer, child *resource.Peer) { - parent.FinishedPieces.Set(0) - parent.FinishedPieces.Set(1) - child.FinishedPieces.Set(0) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - { - name: "parent pieces are empty", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - totalPieceCount: 10, - mock: func(parent *resource.Peer, child *resource.Peer) {}, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "parent pieces of length greater than zero", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, mockTask, mockHost), - totalPieceCount: 10, - mock: func(parent *resource.Peer, child *resource.Peer) { - parent.FinishedPieces.Set(0) - parent.FinishedPieces.Set(1) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.2)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - tc.mock(tc.parent, tc.child) - tc.expect(t, e.(*evaluatorNetworkTopology).calculatePieceScore(tc.parent, tc.child, tc.totalPieceCount)) - }) - } -} - -func TestEvaluatorNetworkTopology_calculatehostUploadSuccessScore(t *testing.T) { - tests := []struct { - name string - mock func(host *resource.Host) - expect func(t *testing.T, score float64) - }{ - { - name: "UploadFailedCount is larger than UploadCount", - mock: func(host *resource.Host) { - host.UploadCount.Add(1) - host.UploadFailedCount.Add(2) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "UploadFailedCount and UploadCount is zero", - mock: func(host *resource.Host) { - host.UploadCount.Add(0) - host.UploadFailedCount.Add(0) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - { - name: "UploadCount is larger than UploadFailedCount", - mock: func(host *resource.Host) { - host.UploadCount.Add(2) - host.UploadFailedCount.Add(1) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.5)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - host := resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) - mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) - mockPeer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, host) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - tc.mock(host) - tc.expect(t, e.(*evaluatorNetworkTopology).calculateParentHostUploadSuccessScore(mockPeer)) - }) - } -} - -func TestEvaluatorNetworkTopology_calculateFreeUploadScore(t *testing.T) { - tests := []struct { - name string - mock func(host *resource.Host, mockPeer *resource.Peer) - expect func(t *testing.T, score float64) - }{ - { - name: "host peers is not empty", - mock: func(host *resource.Host, mockPeer *resource.Peer) { - mockPeer.Host.ConcurrentUploadCount.Add(1) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.995)) - }, - }, - { - name: "host peers is empty", - mock: func(host *resource.Host, mockPeer *resource.Peer) {}, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - { - name: "freeUploadCount is empty", - mock: func(host *resource.Host, mockPeer *resource.Peer) { - mockPeer.Host.ConcurrentUploadCount.Add(host.ConcurrentUploadLimit.Load()) - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - host := resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) - mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) - mockPeer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, host) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - tc.mock(host, mockPeer) - tc.expect(t, e.(*evaluatorNetworkTopology).calculateFreeUploadScore(host)) - }) - } -} - -func TestEvaluatorNetworkTopology_calculateHostTypeScore(t *testing.T) { - tests := []struct { - name string - mock func(peer *resource.Peer) - expect func(t *testing.T, score float64) - }{ - { - name: "host is normal peer", - mock: func(peer *resource.Peer) {}, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.5)) - }, - }, - { - name: "host is seed peer but peer state is PeerStateSucceeded", - mock: func(peer *resource.Peer) { - peer.FSM.SetState(resource.PeerStateSucceeded) - peer.Host.Type = types.HostTypeSuperSeed - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "host is seed peer but peer state is PeerStateRunning", - mock: func(peer *resource.Peer) { - peer.FSM.SetState(resource.PeerStateRunning) - peer.Host.Type = types.HostTypeSuperSeed - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - mockHost := resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) - mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) - peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - tc.mock(peer) - tc.expect(t, e.(*evaluatorNetworkTopology).calculateHostTypeScore(peer)) - }) - } -} - -func TestEvaluatorNetworkTopology_calculateIDCAffinityScore(t *testing.T) { - tests := []struct { - name string - mock func(dstHost *resource.Host, srcHost *resource.Host) - expect func(t *testing.T, score float64) - }{ - { - name: "idc is empty", - mock: func(dstHost *resource.Host, srcHost *resource.Host) { - dstHost.Network.IDC = "" - srcHost.Network.IDC = "" - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "dst host idc is empty", - mock: func(dstHost *resource.Host, srcHost *resource.Host) { - dstHost.Network.IDC = "" - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "src host idc is empty", - mock: func(dstHost *resource.Host, srcHost *resource.Host) { - srcHost.Network.IDC = "" - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "idc is not the same", - mock: func(dstHost *resource.Host, srcHost *resource.Host) { - dstHost.Network.IDC = "foo" - srcHost.Network.IDC = "bar" - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "idc is the same", - mock: func(dstHost *resource.Host, srcHost *resource.Host) { - dstHost.Network.IDC = "bal" - srcHost.Network.IDC = "bal" - }, - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - dstHost := resource.NewHost( - mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) - srcHost := resource.NewHost( - mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type) - tc.mock(dstHost, srcHost) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - tc.expect(t, e.(*evaluatorNetworkTopology).calculateIDCAffinityScore(dstHost.Network.IDC, srcHost.Network.IDC)) - }) - } -} - -func TestEvaluatorNetworkTopology_calculateMultiElementAffinityScore(t *testing.T) { - tests := []struct { - name string - dst string - src string - expect func(t *testing.T, score float64) - }{ - { - name: "dst is empty and src is empty", - dst: "", - src: "", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "dst is empty", - dst: "", - src: "baz", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "src is empty", - dst: "bar", - src: "", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "has only one element and matches", - dst: "foo", - src: "foo", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - { - name: "has only one element and does not match", - dst: "foo", - src: "bar", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "has multi element and match", - dst: "foo|bar", - src: "foo|bar", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - { - name: "has multi element and does not match", - dst: "foo|bar", - src: "bar|foo", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - { - name: "dst length is greater than src", - dst: "foo|bar|baz", - src: "foo|bar", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.4)) - }, - }, - { - name: "src length is greater than dst", - dst: "foo|bar", - src: "foo|bar|baz", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.4)) - }, - }, - { - name: "dst exceeds maximum length", - dst: "foo|bar|baz|bac|bae|baf", - src: "foo|bar|baz", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.6)) - }, - }, - { - name: "src exceeds maximum length", - dst: "foo|bar|baz", - src: "foo|bar|baz|bac|bae|baf", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.6)) - }, - }, - { - name: "dst and src both exceeds maximum length", - dst: "foo|bar|baz|bac|bae|baf", - src: "foo|bar|baz|bac|bae|bai", - expect: func(t *testing.T, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(1)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - tc.expect(t, e.(*evaluatorNetworkTopology).calculateMultiElementAffinityScore(tc.dst, tc.src)) - }) - } -} - -func TestEvaluatorNetworkTopology_calculateNetworkTopologyScore(t *testing.T) { - tests := []struct { - name string - parent *resource.Peer - child *resource.Peer - mock func(parent *resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) - expect func(t *testing.T, parent *resource.Peer, child *resource.Peer, score float64) - }{ - { - name: "calculate parent networktopology score", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost(mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost(mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - mock: func(parent *resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) { - mn.Probes(parent.ID, child.ID).Return(p) - mp.AverageRTT().Return(100*time.Millisecond, nil) - }, - expect: func(t *testing.T, parent *resource.Peer, child *resource.Peer, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0.9)) - }, - }, - { - name: "get average error", - parent: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost(mockRawSeedHost.ID, mockRawSeedHost.IP, mockRawSeedHost.Hostname, - mockRawSeedHost.Port, mockRawSeedHost.DownloadPort, mockRawSeedHost.Type)), - child: resource.NewPeer(idgen.PeerIDV1("127.0.0.1"), mockResourceConfig, - resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)), - resource.NewHost(mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, - mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)), - mock: func(parent *resource.Peer, child *resource.Peer, p networktopology.Probes, mn *networktopologymocks.MockNetworkTopologyMockRecorder, mp *networktopologymocks.MockProbesMockRecorder) { - mn.Probes(parent.ID, child.ID).Return(p) - mp.AverageRTT().Return(time.Duration(0), errors.New("foo")) - }, - expect: func(t *testing.T, parent *resource.Peer, child *resource.Peer, score float64) { - assert := assert.New(t) - assert.Equal(score, float64(0)) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - e := newEvaluatorNetworkTopology(WithNetworkTopology(mockNetworkTopology)) - mockProbe := networktopologymocks.NewMockProbes(ctl) - tc.mock(tc.parent, tc.child, mockProbe, mockNetworkTopology.EXPECT(), mockProbe.EXPECT()) - tc.expect(t, tc.parent, tc.child, e.(*evaluatorNetworkTopology).calculateNetworkTopologyScore(tc.parent.ID, tc.child.ID)) - }) - } -} diff --git a/scheduler/scheduling/evaluator/evaluator_test.go b/scheduler/scheduling/evaluator/evaluator_test.go index 1307645f17a..44d5403bf9e 100644 --- a/scheduler/scheduling/evaluator/evaluator_test.go +++ b/scheduler/scheduling/evaluator/evaluator_test.go @@ -22,25 +22,20 @@ import ( "github.com/stretchr/testify/assert" "go.uber.org/mock/gomock" - - networktopologymocks "d7y.io/dragonfly/v2/scheduler/networktopology/mocks" ) func TestEvaluator_New(t *testing.T) { pluginDir := "." ctl := gomock.NewController(t) defer ctl.Finish() - mockNetworkTopology := networktopologymocks.NewMockNetworkTopology(ctl) tests := []struct { name string algorithm string - options []NetworkTopologyOption expect func(t *testing.T, e any) }{ { name: "new evaluator with default algorithm", algorithm: "default", - options: []NetworkTopologyOption{}, expect: func(t *testing.T, e any) { assert := assert.New(t) assert.Equal(reflect.TypeOf(e).Elem().Name(), "evaluatorBase") @@ -49,7 +44,6 @@ func TestEvaluator_New(t *testing.T) { { name: "new evaluator with machine learning algorithm", algorithm: "ml", - options: []NetworkTopologyOption{}, expect: func(t *testing.T, e any) { assert := assert.New(t) assert.Equal(reflect.TypeOf(e).Elem().Name(), "evaluatorBase") @@ -58,7 +52,6 @@ func TestEvaluator_New(t *testing.T) { { name: "new evaluator with plugin", algorithm: "plugin", - options: []NetworkTopologyOption{}, expect: func(t *testing.T, e any) { assert := assert.New(t) assert.Equal(reflect.TypeOf(e).Elem().Name(), "evaluatorBase") @@ -67,26 +60,16 @@ func TestEvaluator_New(t *testing.T) { { name: "new evaluator with empty string", algorithm: "", - options: []NetworkTopologyOption{}, expect: func(t *testing.T, e any) { assert := assert.New(t) assert.Equal(reflect.TypeOf(e).Elem().Name(), "evaluatorBase") }, }, - { - name: "new evaluator with default algorithm and networkTopology", - algorithm: "nt", - options: []NetworkTopologyOption{WithNetworkTopology(mockNetworkTopology)}, - expect: func(t *testing.T, e any) { - assert := assert.New(t) - assert.Equal(reflect.TypeOf(e).Elem().Name(), "evaluatorNetworkTopology") - }, - }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - tc.expect(t, New(tc.algorithm, pluginDir, tc.options...)) + tc.expect(t, New(tc.algorithm, pluginDir)) }) } } diff --git a/scheduler/scheduling/scheduling.go b/scheduler/scheduling/scheduling.go index 8b5f503b46d..6a7cae3c6d1 100644 --- a/scheduler/scheduling/scheduling.go +++ b/scheduler/scheduling/scheduling.go @@ -72,9 +72,9 @@ type scheduling struct { dynconfig config.DynconfigInterface } -func New(cfg *config.SchedulerConfig, dynconfig config.DynconfigInterface, pluginDir string, networkTopologyOptions ...evaluator.NetworkTopologyOption) Scheduling { +func New(cfg *config.SchedulerConfig, dynconfig config.DynconfigInterface, pluginDir string) Scheduling { return &scheduling{ - evaluator: evaluator.New(cfg.Algorithm, pluginDir, networkTopologyOptions...), + evaluator: evaluator.New(cfg.Algorithm, pluginDir), config: cfg, dynconfig: dynconfig, } diff --git a/scheduler/scheduling/scheduling_test.go b/scheduler/scheduling/scheduling_test.go index 009bf87114c..220d375d930 100644 --- a/scheduler/scheduling/scheduling_test.go +++ b/scheduler/scheduling/scheduling_test.go @@ -199,13 +199,11 @@ func TestScheduling_New(t *testing.T) { tests := []struct { name string pluginDir string - options []evaluator.NetworkTopologyOption expect func(t *testing.T, s any) }{ { name: "new scheduling", pluginDir: "bar", - options: []evaluator.NetworkTopologyOption{}, expect: func(t *testing.T, s any) { assert := assert.New(t) assert.Equal(reflect.TypeOf(s).Elem().Name(), "scheduling") @@ -214,7 +212,6 @@ func TestScheduling_New(t *testing.T) { { name: "new scheduling with empty pluginDir", pluginDir: "", - options: []evaluator.NetworkTopologyOption{}, expect: func(t *testing.T, s any) { assert := assert.New(t) assert.Equal(reflect.TypeOf(s).Elem().Name(), "scheduling") diff --git a/scheduler/service/service_v1.go b/scheduler/service/service_v1.go index cd58aa08589..56097685d7d 100644 --- a/scheduler/service/service_v1.go +++ b/scheduler/service/service_v1.go @@ -27,7 +27,6 @@ import ( "github.com/go-http-utils/headers" "go.opentelemetry.io/otel/trace" - "google.golang.org/grpc/codes" "google.golang.org/grpc/status" commonv1 "d7y.io/api/v2/pkg/apis/common/v1" @@ -45,7 +44,6 @@ import ( "d7y.io/dragonfly/v2/pkg/types" "d7y.io/dragonfly/v2/scheduler/config" "d7y.io/dragonfly/v2/scheduler/metrics" - "d7y.io/dragonfly/v2/scheduler/networktopology" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/scheduling" "d7y.io/dragonfly/v2/scheduler/storage" @@ -67,9 +65,6 @@ type V1 struct { // Storage interface. storage storage.Storage - - // Network topology interface. - networkTopology networktopology.NetworkTopology } // New v1 version of service instance. @@ -79,15 +74,13 @@ func NewV1( scheduling scheduling.Scheduling, dynconfig config.DynconfigInterface, storage storage.Storage, - networktopology networktopology.NetworkTopology, ) *V1 { return &V1{ - resource: resource, - scheduling: scheduling, - config: cfg, - dynconfig: dynconfig, - storage: storage, - networkTopology: networktopology, + resource: resource, + scheduling: scheduling, + config: cfg, + dynconfig: dynconfig, + storage: storage, } } @@ -672,111 +665,9 @@ func (v *V1) LeaveHost(ctx context.Context, req *schedulerv1.LeaveHostRequest) e // Leave peers in host. host.LeavePeers() - - // Delete host from network topology. - if v.networkTopology != nil { - if err := v.networkTopology.DeleteHost(host.ID); err != nil { - log.Errorf("delete network topology host error: %s", err.Error()) - return err - } - } - return nil } -// SyncProbes sync probes of the host. -func (v *V1) SyncProbes(stream schedulerv1.Scheduler_SyncProbesServer) error { - if v.networkTopology == nil { - return status.Errorf(codes.Unimplemented, "network topology is not enabled") - } - - for { - req, err := stream.Recv() - if err != nil { - if err == io.EOF { - return nil - } - - logger.Errorf("receive error: %s", err.Error()) - return err - } - - log := logger.WithHost(req.Host.GetId(), req.Host.GetHostname(), req.Host.GetIp()) - switch syncProbesRequest := req.GetRequest().(type) { - case *schedulerv1.SyncProbesRequest_ProbeStartedRequest: - // Find probed hosts in network topology. Based on the source host information, - // the most candidate hosts will be evaluated. - log.Info("receive SyncProbesRequest_ProbeStartedRequest") - hosts, err := v.networkTopology.FindProbedHosts(req.Host.GetId()) - if err != nil { - log.Error(err) - return status.Error(codes.FailedPrecondition, err.Error()) - } - - var probedHosts []*commonv1.Host - for _, host := range hosts { - probedHosts = append(probedHosts, &commonv1.Host{ - Id: host.ID, - Ip: host.IP, - Hostname: host.Hostname, - Port: host.Port, - DownloadPort: host.DownloadPort, - Location: host.Network.Location, - Idc: host.Network.IDC, - }) - } - - log.Infof("probe started: %#v", probedHosts) - if err := stream.Send(&schedulerv1.SyncProbesResponse{ - Hosts: probedHosts, - }); err != nil { - log.Error(err) - return err - } - case *schedulerv1.SyncProbesRequest_ProbeFinishedRequest: - // Store probes in network topology. First create the association between - // source host and destination host, and then store the value of probe. - log.Info("receive SyncProbesRequest_ProbeFinishedRequest") - for _, probe := range syncProbesRequest.ProbeFinishedRequest.Probes { - probedHost, loaded := v.resource.HostManager().Load(probe.Host.Id) - if !loaded { - log.Errorf("host %s not found", probe.Host.Id) - continue - } - - if err := v.networkTopology.Store(req.Host.GetId(), probedHost.ID); err != nil { - log.Errorf("store failed: %s", err.Error()) - continue - } - - if err := v.networkTopology.Probes(req.Host.GetId(), probe.Host.Id).Enqueue(&networktopology.Probe{ - Host: probedHost, - RTT: probe.Rtt.AsDuration(), - CreatedAt: probe.CreatedAt.AsTime(), - }); err != nil { - log.Errorf("enqueue failed: %s", err.Error()) - continue - } - - log.Infof("probe finished: %#v", probe) - } - case *schedulerv1.SyncProbesRequest_ProbeFailedRequest: - // Log failed probes. - log.Info("receive SyncProbesRequest_ProbeFailedRequest") - var failedProbedHostIDs []string - for _, failedProbe := range syncProbesRequest.ProbeFailedRequest.Probes { - failedProbedHostIDs = append(failedProbedHostIDs, failedProbe.Host.Id) - } - - log.Warnf("probe failed: %#v", failedProbedHostIDs) - default: - msg := fmt.Sprintf("receive unknow request: %#v", syncProbesRequest) - log.Error(msg) - return status.Error(codes.FailedPrecondition, msg) - } - } -} - // prefetchTask prefetches the task with seed peer. func (v *V1) prefetchTask(ctx context.Context, rawReq *schedulerv1.PeerTaskRequest) (*resource.Task, error) { // If seed peer is disabled, then return error. diff --git a/scheduler/service/service_v1_test.go b/scheduler/service/service_v1_test.go index f060cdf3b45..cac0f77266a 100644 --- a/scheduler/service/service_v1_test.go +++ b/scheduler/service/service_v1_test.go @@ -38,7 +38,6 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/durationpb" - "google.golang.org/protobuf/types/known/timestamppb" commonv1 "d7y.io/api/v2/pkg/apis/common/v1" commonv2 "d7y.io/api/v2/pkg/apis/common/v2" @@ -46,7 +45,6 @@ import ( managerv2 "d7y.io/api/v2/pkg/apis/manager/v2" schedulerv1 "d7y.io/api/v2/pkg/apis/scheduler/v1" schedulerv1mocks "d7y.io/api/v2/pkg/apis/scheduler/v1/mocks" - schedulerv2 "d7y.io/api/v2/pkg/apis/scheduler/v2" "d7y.io/dragonfly/v2/internal/dferrors" "d7y.io/dragonfly/v2/manager/types" @@ -58,8 +56,6 @@ import ( pkgtypes "d7y.io/dragonfly/v2/pkg/types" "d7y.io/dragonfly/v2/scheduler/config" configmocks "d7y.io/dragonfly/v2/scheduler/config/mocks" - "d7y.io/dragonfly/v2/scheduler/networktopology" - networktopologymocks "d7y.io/dragonfly/v2/scheduler/networktopology/mocks" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/scheduling" "d7y.io/dragonfly/v2/scheduler/scheduling/mocks" @@ -80,14 +76,6 @@ var ( TaskDownloadTimeout: 1 * time.Hour, } - mockNetworkTopologyConfig = config.NetworkTopologyConfig{ - CollectInterval: 2 * time.Hour, - Probe: config.ProbeConfig{ - QueueLength: 5, - Count: 10, - }, - } - mockRawHost = resource.Host{ ID: mockHostID, Type: pkgtypes.HostTypeNormal, @@ -237,86 +225,6 @@ var ( Cost: 1 * time.Minute, CreatedAt: time.Now(), } - - mockV1Probe = &schedulerv1.Probe{ - Host: &commonv1.Host{ - Id: mockRawHost.ID, - Ip: mockRawHost.IP, - Hostname: mockRawHost.Hostname, - Port: mockRawHost.Port, - DownloadPort: mockRawHost.DownloadPort, - Location: mockRawHost.Network.Location, - Idc: mockRawHost.Network.IDC, - }, - Rtt: durationpb.New(30 * time.Millisecond), - CreatedAt: timestamppb.Now(), - } - - mockV2Probe = &schedulerv2.Probe{ - Host: &commonv2.Host{ - Id: mockHostID, - Type: uint32(pkgtypes.HostTypeNormal), - Hostname: "foo", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: &commonv2.CPU{ - LogicalCount: mockCPU.LogicalCount, - PhysicalCount: mockCPU.PhysicalCount, - Percent: mockCPU.Percent, - ProcessPercent: mockCPU.ProcessPercent, - Times: &commonv2.CPUTimes{ - User: mockCPU.Times.User, - System: mockCPU.Times.System, - Idle: mockCPU.Times.Idle, - Nice: mockCPU.Times.Nice, - Iowait: mockCPU.Times.Iowait, - Irq: mockCPU.Times.Irq, - Softirq: mockCPU.Times.Softirq, - Steal: mockCPU.Times.Steal, - Guest: mockCPU.Times.Guest, - GuestNice: mockCPU.Times.GuestNice, - }, - }, - Memory: &commonv2.Memory{ - Total: mockMemory.Total, - Available: mockMemory.Available, - Used: mockMemory.Used, - UsedPercent: mockMemory.UsedPercent, - ProcessUsedPercent: mockMemory.ProcessUsedPercent, - Free: mockMemory.Free, - }, - Network: &commonv2.Network{ - TcpConnectionCount: mockNetwork.TCPConnectionCount, - UploadTcpConnectionCount: mockNetwork.UploadTCPConnectionCount, - Location: &mockNetwork.Location, - Idc: &mockNetwork.IDC, - }, - Disk: &commonv2.Disk{ - Total: mockDisk.Total, - Free: mockDisk.Free, - Used: mockDisk.Used, - UsedPercent: mockDisk.UsedPercent, - InodesTotal: mockDisk.InodesTotal, - InodesUsed: mockDisk.InodesUsed, - InodesFree: mockDisk.InodesFree, - InodesUsedPercent: mockDisk.InodesUsedPercent, - }, - Build: &commonv2.Build{ - GitVersion: mockBuild.GitVersion, - GitCommit: &mockBuild.GitCommit, - GoVersion: &mockBuild.GoVersion, - Platform: &mockBuild.Platform, - }, - }, - Rtt: durationpb.New(30 * time.Millisecond), - CreatedAt: timestamppb.Now(), - } ) func TestService_NewV1(t *testing.T) { @@ -341,9 +249,8 @@ func TestService_NewV1(t *testing.T) { resource := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - tc.expect(t, NewV1(&config.Config{Scheduler: mockSchedulerConfig}, resource, scheduling, dynconfig, storage, networkTopology)) + tc.expect(t, NewV1(&config.Config{Scheduler: mockSchedulerConfig}, resource, scheduling, dynconfig, storage)) }) } } @@ -1005,11 +912,10 @@ func TestServiceV1_RegisterPeerTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) hostManager := resource.NewMockHostManager(ctl) taskManager := resource.NewMockTaskManager(ctl) peerManager := resource.NewMockPeerManager(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, @@ -1271,10 +1177,9 @@ func TestServiceV1_ReportPieceResult(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) stream := schedulerv1mocks.NewMockScheduler_ReportPieceResultServer(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, @@ -1455,9 +1360,8 @@ func TestServiceV1_ReportPeerResult(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, @@ -1520,9 +1424,8 @@ func TestServiceV1_StatTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) taskManager := resource.NewMockTaskManager(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) tc.mock(mockTask, taskManager, res.EXPECT(), taskManager.EXPECT()) @@ -1815,11 +1718,10 @@ func TestServiceV1_AnnounceTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) hostManager := resource.NewMockHostManager(ctl) taskManager := resource.NewMockTaskManager(ctl) peerManager := resource.NewMockPeerManager(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) @@ -2016,14 +1918,13 @@ func TestServiceV1_LeaveTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockSeedPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) tc.mock(peer, peerManager, scheduling.EXPECT(), res.EXPECT(), peerManager.EXPECT()) tc.expect(t, peer, svc.LeaveTask(context.Background(), &schedulerv1.PeerTarget{})) @@ -2447,12 +2348,11 @@ func TestServiceV1_AnnounceHost(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) hostManager := resource.NewMockHostManager(ctl) host := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.req, host, hostManager, res.EXPECT(), hostManager.EXPECT(), dynconfig.EXPECT()) }) @@ -2462,12 +2362,12 @@ func TestServiceV1_AnnounceHost(t *testing.T) { func TestServiceV1_LeaveHost(t *testing.T) { tests := []struct { name string - mock func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) + mock func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) expect func(t *testing.T, peer *resource.Peer, err error) }{ { name: "host not found", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(nil, false).Times(1), @@ -2480,11 +2380,10 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "host has not peers", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2494,13 +2393,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateLeave", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateLeave) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2510,13 +2408,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStatePending", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStatePending) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2527,13 +2424,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateReceivedEmpty", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateReceivedEmpty) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2544,13 +2440,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateReceivedTiny", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateReceivedTiny) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2561,13 +2456,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateReceivedSmall", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateReceivedSmall) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2578,13 +2472,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateReceivedNormal", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateReceivedNormal) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2595,13 +2488,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateRunning", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateRunning) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2612,13 +2504,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateBackToSource", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateBackToSource) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2629,13 +2520,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateSucceeded", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateSucceeded) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2646,13 +2536,12 @@ func TestServiceV1_LeaveHost(t *testing.T) { }, { name: "peer state is PeerStateFailed", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, ms *mocks.MockSchedulingMockRecorder, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStateFailed) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -2671,16 +2560,15 @@ func TestServiceV1_LeaveHost(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) hostManager := resource.NewMockHostManager(ctl) host := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) mockPeer := resource.NewPeer(mockSeedPeerID, mockResourceConfig, mockTask, host) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) - tc.mock(host, mockPeer, hostManager, scheduling.EXPECT(), res.EXPECT(), hostManager.EXPECT(), networkTopology.EXPECT()) + tc.mock(host, mockPeer, hostManager, scheduling.EXPECT(), res.EXPECT(), hostManager.EXPECT()) tc.expect(t, mockPeer, svc.LeaveHost(context.Background(), &schedulerv1.LeaveHostRequest{ Id: idgen.HostIDV2(host.IP, host.Hostname), })) @@ -2688,389 +2576,6 @@ func TestServiceV1_LeaveHost(t *testing.T) { } } -func TestServiceV1_SyncProbes(t *testing.T) { - tests := []struct { - name string - mock func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) - expect func(t *testing.T, err error) - }{ - { - name: "network topology is not enabled", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - svc.networkTopology = nil - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "rpc error: code = Unimplemented desc = network topology is not enabled") - }, - }, - { - name: "synchronize probes when receive ProbeStartedRequest", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }, - }, nil).Times(1), - mn.FindProbedHosts(gomock.Eq(mockRawSeedHost.ID)).Return([]*resource.Host{&mockRawHost}, nil).Times(1), - ms.Send(gomock.Eq(&schedulerv1.SyncProbesResponse{ - Hosts: []*commonv1.Host{ - { - Id: mockRawHost.ID, - Ip: mockRawHost.IP, - Hostname: mockRawHost.Hostname, - Port: mockRawHost.Port, - DownloadPort: mockRawHost.DownloadPort, - Location: mockRawHost.Network.Location, - Idc: mockRawHost.Network.IDC, - }, - }, - })).Return(nil).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "synchronize probes when receive ProbeFinishedRequest", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: &schedulerv1.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv1.ProbeFinishedRequest{ - Probes: []*schedulerv1.Probe{mockV1Probe}, - }, - }, - }, nil).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockRawHost.ID)).Return(&mockRawHost, true), - mn.Store(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(nil).Times(1), - mn.Probes(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(probes).Times(1), - mp.Enqueue(gomock.Eq(&networktopology.Probe{ - Host: &mockRawHost, - RTT: mockV1Probe.Rtt.AsDuration(), - CreatedAt: mockV1Probe.CreatedAt.AsTime(), - })).Return(nil).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "synchronize probes when receive ProbeFailedRequest", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: &schedulerv1.SyncProbesRequest_ProbeFailedRequest{ - ProbeFailedRequest: &schedulerv1.ProbeFailedRequest{ - Probes: []*schedulerv1.FailedProbe{ - { - Host: &commonv1.Host{ - Id: mockRawHost.ID, - Ip: mockRawHost.IP, - Hostname: mockRawHost.Hostname, - Port: mockRawHost.Port, - DownloadPort: mockRawHost.DownloadPort, - Location: mockRawHost.Network.Location, - Idc: mockRawHost.Network.IDC, - }, - Description: "foo", - }, - }, - }, - }, - }, nil).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "synchronize probes when receive fail type request", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: nil, - }, nil).Times(1) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "rpc error: code = FailedPrecondition desc = receive unknow request: ") - }, - }, - { - name: "receive error", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - ms.Recv().Return(nil, errors.New("receive error")).Times(1) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "receive error") - }, - }, - { - name: "receive end of file", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - ms.Recv().Return(nil, io.EOF).Times(1) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "find probed host ids error", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }, - }, nil).Times(1), - mn.FindProbedHosts(gomock.Eq(mockRawSeedHost.ID)).Return(nil, errors.New("find probed host ids error")).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "rpc error: code = FailedPrecondition desc = find probed host ids error") - }, - }, - { - name: "send synchronize probes response error", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: &schedulerv1.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv1.ProbeStartedRequest{}, - }, - }, nil).Times(1), - mn.FindProbedHosts(gomock.Eq(mockRawSeedHost.ID)).Return([]*resource.Host{&mockRawHost}, nil).Times(1), - ms.Send(gomock.Eq(&schedulerv1.SyncProbesResponse{ - Hosts: []*commonv1.Host{ - { - Id: mockRawHost.ID, - Ip: mockRawHost.IP, - Hostname: mockRawHost.Hostname, - Port: mockRawHost.Port, - DownloadPort: mockRawHost.DownloadPort, - Location: mockRawHost.Network.Location, - Idc: mockRawHost.Network.IDC, - }, - }, - })).Return(errors.New("send synchronize probes response error")).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "send synchronize probes response error") - }, - }, - { - name: "load host error when receive ProbeFinishedRequest", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: &schedulerv1.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv1.ProbeFinishedRequest{ - Probes: []*schedulerv1.Probe{mockV1Probe}, - }, - }, - }, nil).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockRawHost.ID)).Return(nil, false), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "store error when receive ProbeFinishedRequest", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: &schedulerv1.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv1.ProbeFinishedRequest{ - Probes: []*schedulerv1.Probe{mockV1Probe}, - }, - }, - }, nil).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockRawHost.ID)).Return(&mockRawHost, true), - mn.Store(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(errors.New("store error")).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "enqueue probe error when receive ProbeFinishedRequest", - mock: func(svc *V1, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv1mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv1.SyncProbesRequest{ - Host: &commonv1.Host{ - Id: mockRawSeedHost.ID, - Ip: mockRawSeedHost.IP, - Hostname: mockRawSeedHost.Hostname, - Port: mockRawSeedHost.Port, - DownloadPort: mockRawSeedHost.DownloadPort, - Location: mockRawSeedHost.Network.Location, - Idc: mockRawSeedHost.Network.IDC, - }, - Request: &schedulerv1.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv1.ProbeFinishedRequest{ - Probes: []*schedulerv1.Probe{mockV1Probe}, - }, - }, - }, nil).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockRawHost.ID)).Return(&mockRawHost, true), - mn.Store(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(nil).Times(1), - mn.Probes(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(probes).Times(1), - mp.Enqueue(gomock.Any()).Return(errors.New("enqueue probe error")).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - scheduling := mocks.NewMockScheduling(ctl) - res := resource.NewMockResource(ctl) - dynconfig := configmocks.NewMockDynconfigInterface(ctl) - storage := storagemocks.NewMockStorage(ctl) - probes := networktopologymocks.NewMockProbes(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - hostManager := resource.NewMockHostManager(ctl) - stream := schedulerv1mocks.NewMockScheduler_SyncProbesServer(ctl) - svc := NewV1(&config.Config{Scheduler: config.SchedulerConfig{NetworkTopology: mockNetworkTopologyConfig}, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) - - tc.mock(svc, res.EXPECT(), probes, probes.EXPECT(), networkTopology.EXPECT(), hostManager, hostManager.EXPECT(), stream.EXPECT()) - tc.expect(t, svc.SyncProbes(stream)) - }) - } -} - func TestServiceV1_prefetchTask(t *testing.T) { fmt.Println("TestServiceV1_prefetchTask") tests := []struct { @@ -3160,14 +2665,13 @@ func TestServiceV1_prefetchTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) seedPeer := resource.NewMockSeedPeer(ctl) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) task := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, task, mockHost) - svc := NewV1(tc.config, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(tc.config, res, scheduling, dynconfig, storage) taskManager := resource.NewMockTaskManager(ctl) tc.mock(task, peer, taskManager, seedPeer, res.EXPECT(), taskManager.EXPECT(), seedPeer.EXPECT()) @@ -3627,8 +3131,7 @@ func TestServiceV1_triggerTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - svc := NewV1(tc.config, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(tc.config, res, scheduling, dynconfig, storage) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, @@ -3732,8 +3235,7 @@ func TestServiceV1_storeTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) taskManager := resource.NewMockTaskManager(ctl) tc.run(t, svc, taskManager, res.EXPECT(), taskManager.EXPECT()) }) @@ -3811,8 +3313,7 @@ func TestServiceV1_storeHost(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) hostManager := resource.NewMockHostManager(ctl) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, @@ -3896,8 +3397,7 @@ func TestServiceV1_storePeer(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) peerManager := resource.NewMockPeerManager(ctl) tc.run(t, svc, peerManager, res.EXPECT(), peerManager.EXPECT()) @@ -3956,14 +3456,13 @@ func TestServiceV1_triggerSeedPeerTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) seedPeer := resource.NewMockSeedPeer(ctl) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) task := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, task, mockHost) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, SeedPeer: mockSeedPeerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, SeedPeer: mockSeedPeerConfig}, res, scheduling, dynconfig, storage) tc.mock(task, peer, seedPeer, res.EXPECT(), seedPeer.EXPECT()) svc.triggerSeedPeerTask(context.Background(), &mockPeerRange, task) @@ -4039,13 +3538,12 @@ func TestServiceV1_handleBeginOfPiece(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.mock(peer, scheduling.EXPECT()) svc.handleBeginOfPiece(context.Background(), peer) @@ -4179,9 +3677,8 @@ func TestServiceV1_handlePieceSuccess(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) tc.mock(tc.peer, peerManager, res.EXPECT(), peerManager.EXPECT()) svc.handlePieceSuccess(context.Background(), tc.peer, tc.piece) @@ -4369,7 +3866,6 @@ func TestServiceV1_handlePieceFail(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, @@ -4378,7 +3874,7 @@ func TestServiceV1_handlePieceFail(t *testing.T) { peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) parent := resource.NewPeer(mockSeedPeerID, mockResourceConfig, mockTask, mockHost) seedPeer := resource.NewMockSeedPeer(ctl) - svc := NewV1(tc.config, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(tc.config, res, scheduling, dynconfig, storage) tc.run(t, svc, peer, parent, tc.piece, peerManager, seedPeer, scheduling.EXPECT(), res.EXPECT(), peerManager.EXPECT(), seedPeer.EXPECT()) }) @@ -4478,7 +3974,6 @@ func TestServiceV1_handlePeerSuccess(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) url, err := url.Parse(s.URL) if err != nil { @@ -4502,7 +3997,7 @@ func TestServiceV1_handlePeerSuccess(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) tc.mock(peer) svc.handlePeerSuccess(context.Background(), peer) @@ -4577,8 +4072,7 @@ func TestServiceV1_handlePeerFail(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) @@ -4664,8 +4158,7 @@ func TestServiceV1_handleTaskSuccess(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) task := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) tc.mock(task) @@ -4804,8 +4297,7 @@ func TestServiceV1_handleTaskFail(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV1(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) task := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) tc.mock(task) diff --git a/scheduler/service/service_v2.go b/scheduler/service/service_v2.go index 0975ec8b82a..c92c6ccae79 100644 --- a/scheduler/service/service_v2.go +++ b/scheduler/service/service_v2.go @@ -39,7 +39,6 @@ import ( "d7y.io/dragonfly/v2/pkg/types" "d7y.io/dragonfly/v2/scheduler/config" "d7y.io/dragonfly/v2/scheduler/metrics" - "d7y.io/dragonfly/v2/scheduler/networktopology" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" "d7y.io/dragonfly/v2/scheduler/scheduling" "d7y.io/dragonfly/v2/scheduler/storage" @@ -61,9 +60,6 @@ type V2 struct { // Storage interface. storage storage.Storage - - // Network topology interface. - networkTopology networktopology.NetworkTopology } // New v2 version of service instance. @@ -73,15 +69,13 @@ func NewV2( scheduling scheduling.Scheduling, dynconfig config.DynconfigInterface, storage storage.Storage, - networkTopology networktopology.NetworkTopology, ) *V2 { return &V2{ - resource: resource, - scheduling: scheduling, - config: cfg, - dynconfig: dynconfig, - storage: storage, - networkTopology: networkTopology, + resource: resource, + scheduling: scheduling, + config: cfg, + dynconfig: dynconfig, + storage: storage, } } @@ -707,163 +701,9 @@ func (v *V2) DeleteHost(ctx context.Context, req *schedulerv2.DeleteHostRequest) // Leave peers in host. host.LeavePeers() - - // Delete host from network topology. - if v.networkTopology != nil { - if err := v.networkTopology.DeleteHost(host.ID); err != nil { - log.Errorf("delete network topology host error: %s", err.Error()) - return err - } - } - return nil } -// SyncProbes sync probes of the host. -func (v *V2) SyncProbes(stream schedulerv2.Scheduler_SyncProbesServer) error { - if v.networkTopology == nil { - return status.Errorf(codes.Unimplemented, "network topology is not enabled") - } - - for { - req, err := stream.Recv() - if err != nil { - if err == io.EOF { - return nil - } - - logger.Errorf("receive error: %s", err.Error()) - return err - } - - log := logger.WithHost(req.Host.GetId(), req.Host.GetHostname(), req.Host.GetIp()) - switch syncProbesRequest := req.GetRequest().(type) { - case *schedulerv2.SyncProbesRequest_ProbeStartedRequest: - // Find probed hosts in network topology. Based on the source host information, - // the most candidate hosts will be evaluated. - log.Info("receive SyncProbesRequest_ProbeStartedRequest") - hosts, err := v.networkTopology.FindProbedHosts(req.Host.GetId()) - if err != nil { - log.Error(err) - return status.Error(codes.FailedPrecondition, err.Error()) - } - - var probedHosts []*commonv2.Host - for _, host := range hosts { - probedHosts = append(probedHosts, &commonv2.Host{ - Id: host.ID, - Type: uint32(host.Type), - Hostname: host.Hostname, - Ip: host.IP, - Port: host.Port, - DownloadPort: host.DownloadPort, - Os: host.OS, - Platform: host.Platform, - PlatformFamily: host.PlatformFamily, - PlatformVersion: host.PlatformVersion, - KernelVersion: host.KernelVersion, - Cpu: &commonv2.CPU{ - LogicalCount: host.CPU.LogicalCount, - PhysicalCount: host.CPU.PhysicalCount, - Percent: host.CPU.Percent, - ProcessPercent: host.CPU.ProcessPercent, - Times: &commonv2.CPUTimes{ - User: host.CPU.Times.User, - System: host.CPU.Times.System, - Idle: host.CPU.Times.Idle, - Nice: host.CPU.Times.Nice, - Iowait: host.CPU.Times.Iowait, - Irq: host.CPU.Times.Irq, - Softirq: host.CPU.Times.Softirq, - Steal: host.CPU.Times.Steal, - Guest: host.CPU.Times.Guest, - GuestNice: host.CPU.Times.GuestNice, - }, - }, - Memory: &commonv2.Memory{ - Total: host.Memory.Total, - Available: host.Memory.Available, - Used: host.Memory.Used, - UsedPercent: host.Memory.UsedPercent, - ProcessUsedPercent: host.Memory.ProcessUsedPercent, - Free: host.Memory.Free, - }, - Network: &commonv2.Network{ - TcpConnectionCount: host.Network.TCPConnectionCount, - UploadTcpConnectionCount: host.Network.UploadTCPConnectionCount, - Location: &host.Network.Location, - Idc: &host.Network.IDC, - }, - Disk: &commonv2.Disk{ - Total: host.Disk.Total, - Free: host.Disk.Free, - Used: host.Disk.Used, - UsedPercent: host.Disk.UsedPercent, - InodesTotal: host.Disk.InodesTotal, - InodesUsed: host.Disk.InodesUsed, - InodesFree: host.Disk.InodesFree, - InodesUsedPercent: host.Disk.InodesUsedPercent, - }, - Build: &commonv2.Build{ - GitVersion: host.Build.GitVersion, - GitCommit: &host.Build.GitCommit, - GoVersion: &host.Build.GoVersion, - Platform: &host.Build.Platform, - }, - }) - } - - log.Infof("probe started: %#v", probedHosts) - if err := stream.Send(&schedulerv2.SyncProbesResponse{ - Hosts: probedHosts, - }); err != nil { - log.Error(err) - return err - } - case *schedulerv2.SyncProbesRequest_ProbeFinishedRequest: - // Store probes in network topology. First create the association between - // source host and destination host, and then store the value of probe. - log.Info("receive SyncProbesRequest_ProbeFinishedRequest") - for _, probe := range syncProbesRequest.ProbeFinishedRequest.Probes { - probedHost, loaded := v.resource.HostManager().Load(probe.Host.Id) - if !loaded { - log.Errorf("host %s not found", probe.Host.Id) - continue - } - - if err := v.networkTopology.Store(req.Host.GetId(), probedHost.ID); err != nil { - log.Errorf("store failed: %s", err.Error()) - continue - } - - if err := v.networkTopology.Probes(req.Host.GetId(), probe.Host.Id).Enqueue(&networktopology.Probe{ - Host: probedHost, - RTT: probe.Rtt.AsDuration(), - CreatedAt: probe.CreatedAt.AsTime(), - }); err != nil { - log.Errorf("enqueue failed: %s", err.Error()) - continue - } - - log.Infof("probe finished: %#v", probe) - } - case *schedulerv2.SyncProbesRequest_ProbeFailedRequest: - // Log failed probes. - log.Info("receive SyncProbesRequest_ProbeFailedRequest") - var failedProbedHostIDs []string - for _, failedProbe := range syncProbesRequest.ProbeFailedRequest.Probes { - failedProbedHostIDs = append(failedProbedHostIDs, failedProbe.Host.Id) - } - - log.Warnf("probe failed: %#v", failedProbedHostIDs) - default: - msg := fmt.Sprintf("receive unknow request: %#v", syncProbesRequest) - log.Error(msg) - return status.Error(codes.FailedPrecondition, msg) - } - } -} - // handleRegisterPeerRequest handles RegisterPeerRequest of AnnouncePeerRequest. func (v *V2) handleRegisterPeerRequest(ctx context.Context, stream schedulerv2.Scheduler_AnnouncePeerServer, hostID, taskID, peerID string, req *schedulerv2.RegisterPeerRequest) error { // Handle resource included host, task, and peer. diff --git a/scheduler/service/service_v2_test.go b/scheduler/service/service_v2_test.go index 1882c48da81..7b6bae0f571 100644 --- a/scheduler/service/service_v2_test.go +++ b/scheduler/service/service_v2_test.go @@ -19,7 +19,6 @@ package service import ( "context" "errors" - "io" "net" "net/http" "net/http/httptest" @@ -48,10 +47,7 @@ import ( pkgtypes "d7y.io/dragonfly/v2/pkg/types" "d7y.io/dragonfly/v2/scheduler/config" configmocks "d7y.io/dragonfly/v2/scheduler/config/mocks" - "d7y.io/dragonfly/v2/scheduler/networktopology" - networktopologymocks "d7y.io/dragonfly/v2/scheduler/networktopology/mocks" resource "d7y.io/dragonfly/v2/scheduler/resource/standard" - "d7y.io/dragonfly/v2/scheduler/scheduling/mocks" schedulingmocks "d7y.io/dragonfly/v2/scheduler/scheduling/mocks" storagemocks "d7y.io/dragonfly/v2/scheduler/storage/mocks" ) @@ -78,9 +74,8 @@ func TestService_NewV2(t *testing.T) { resource := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - tc.expect(t, NewV2(&config.Config{Scheduler: mockSchedulerConfig}, resource, scheduling, dynconfig, storage, networkTopology)) + tc.expect(t, NewV2(&config.Config{Scheduler: mockSchedulerConfig}, resource, scheduling, dynconfig, storage)) }) } } @@ -246,14 +241,13 @@ func TestServiceV2_StatPeer(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockSeedPeerID, mockResourceConfig, mockTask, mockHost, resource.WithRange(mockPeerRange)) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) tc.mock(peer, peerManager, res.EXPECT(), peerManager.EXPECT()) resp, err := svc.StatPeer(context.Background(), &schedulerv2.StatPeerRequest{TaskId: mockTaskID, PeerId: mockPeerID}) @@ -318,14 +312,13 @@ func TestServiceV2_DeletePeer(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockSeedPeerID, mockResourceConfig, mockTask, mockHost, resource.WithRange(mockPeerRange)) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) tc.mock(peer, peerManager, res.EXPECT(), peerManager.EXPECT()) tc.expect(t, svc.DeletePeer(context.Background(), &schedulerv2.DeletePeerRequest{TaskId: mockTaskID, PeerId: mockPeerID})) @@ -407,10 +400,9 @@ func TestServiceV2_StatTask(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) taskManager := resource.NewMockTaskManager(ctl) task := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) tc.mock(task, taskManager, res.EXPECT(), taskManager.EXPECT()) resp, err := svc.StatTask(context.Background(), &schedulerv2.StatTaskRequest{TaskId: mockTaskID}) @@ -859,12 +851,11 @@ func TestServiceV2_AnnounceHost(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) hostManager := resource.NewMockHostManager(ctl) host := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.req, host, hostManager, res.EXPECT(), hostManager.EXPECT(), dynconfig.EXPECT()) }) @@ -874,12 +865,12 @@ func TestServiceV2_AnnounceHost(t *testing.T) { func TestServiceV2_DeleteHost(t *testing.T) { tests := []struct { name string - mock func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) + mock func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) expect func(t *testing.T, peer *resource.Peer, err error) }{ { name: "host not found", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(nil, false).Times(1), @@ -892,11 +883,10 @@ func TestServiceV2_DeleteHost(t *testing.T) { }, { name: "host has not peers", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -906,13 +896,12 @@ func TestServiceV2_DeleteHost(t *testing.T) { }, { name: "peer leaves succeeded", - mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder, mnt *networktopologymocks.MockNetworkTopologyMockRecorder) { + mock: func(host *resource.Host, mockPeer *resource.Peer, hostManager resource.HostManager, mr *resource.MockResourceMockRecorder, mh *resource.MockHostManagerMockRecorder) { host.Peers.Store(mockPeer.ID, mockPeer) mockPeer.FSM.SetState(resource.PeerStatePending) gomock.InOrder( mr.HostManager().Return(hostManager).Times(1), mh.Load(gomock.Any()).Return(host, true).Times(1), - mnt.DeleteHost(host.ID).Return(nil).Times(1), ) }, expect: func(t *testing.T, peer *resource.Peer, err error) { @@ -931,511 +920,20 @@ func TestServiceV2_DeleteHost(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) hostManager := resource.NewMockHostManager(ctl) host := resource.NewHost( mockRawHost.ID, mockRawHost.IP, mockRawHost.Hostname, mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) mockPeer := resource.NewPeer(mockSeedPeerID, mockResourceConfig, mockTask, host) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage) - tc.mock(host, mockPeer, hostManager, res.EXPECT(), hostManager.EXPECT(), networkTopology.EXPECT()) + tc.mock(host, mockPeer, hostManager, res.EXPECT(), hostManager.EXPECT()) tc.expect(t, mockPeer, svc.DeleteHost(context.Background(), &schedulerv2.DeleteHostRequest{HostId: mockHostID})) }) } } -func TestServiceV2_SyncProbes(t *testing.T) { - tests := []struct { - name string - mock func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) - expect func(t *testing.T, err error) - }{ - { - name: "network topology is not enabled", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - svc.networkTopology = nil - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "rpc error: code = Unimplemented desc = network topology is not enabled") - }, - }, - { - name: "synchronize probes when receive ProbeStartedRequest", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: &schedulerv2.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv2.ProbeStartedRequest{}, - }, - }, nil).Times(1), - mn.FindProbedHosts(gomock.Eq(mockRawSeedHost.ID)).Return([]*resource.Host{&mockRawHost}, nil).Times(1), - ms.Send(gomock.Eq(&schedulerv2.SyncProbesResponse{ - Hosts: []*commonv2.Host{ - { - Id: mockRawHost.ID, - Type: uint32(mockRawHost.Type), - Hostname: mockRawHost.Hostname, - Ip: mockRawHost.IP, - Port: mockRawHost.Port, - DownloadPort: mockRawHost.DownloadPort, - Os: mockRawHost.OS, - Platform: mockRawHost.Platform, - PlatformFamily: mockRawHost.PlatformFamily, - PlatformVersion: mockRawHost.PlatformVersion, - KernelVersion: mockRawHost.KernelVersion, - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - }, - })).Return(nil).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "synchronize probes when receive ProbeFinishedRequest", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: &schedulerv2.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv2.ProbeFinishedRequest{ - Probes: []*schedulerv2.Probe{mockV2Probe}, - }, - }, - }, nil).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockRawHost.ID)).Return(&mockRawHost, true), - mn.Store(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(nil).Times(1), - mn.Probes(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(probes).Times(1), - mp.Enqueue(gomock.Eq(&networktopology.Probe{ - Host: &mockRawHost, - RTT: mockV2Probe.Rtt.AsDuration(), - CreatedAt: mockV2Probe.CreatedAt.AsTime(), - })).Return(nil).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "synchronize probes when receive ProbeFailedRequest", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: &schedulerv2.SyncProbesRequest_ProbeFailedRequest{ - ProbeFailedRequest: &schedulerv2.ProbeFailedRequest{ - Probes: []*schedulerv2.FailedProbe{ - { - Host: &commonv2.Host{ - Id: mockRawHost.ID, - Type: uint32(mockRawHost.Type), - Hostname: mockRawHost.Hostname, - Ip: mockRawHost.IP, - Port: mockRawHost.Port, - DownloadPort: mockRawHost.DownloadPort, - Os: mockRawHost.OS, - Platform: mockRawHost.Platform, - PlatformFamily: mockRawHost.PlatformFamily, - PlatformVersion: mockRawHost.PlatformVersion, - KernelVersion: mockRawHost.KernelVersion, - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - }, - }, - }, - }, - }, nil).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "synchronize probes when receive fail type request", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: nil, - }, nil).Times(1) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "rpc error: code = FailedPrecondition desc = receive unknow request: ") - }, - }, - { - name: "receive error", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - ms.Recv().Return(nil, errors.New("receive error")).Times(1) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "receive error") - }, - }, - { - name: "receive end of file", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - ms.Recv().Return(nil, io.EOF).Times(1) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "find probed host ids error", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: &schedulerv2.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv2.ProbeStartedRequest{}, - }, - }, nil).Times(1), - mn.FindProbedHosts(gomock.Eq(mockRawSeedHost.ID)).Return(nil, errors.New("find probed host ids error")).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "rpc error: code = FailedPrecondition desc = find probed host ids error") - }, - }, - { - name: "send synchronize probes response error", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: &schedulerv2.SyncProbesRequest_ProbeStartedRequest{ - ProbeStartedRequest: &schedulerv2.ProbeStartedRequest{}, - }, - }, nil).Times(1), - mn.FindProbedHosts(gomock.Eq(mockRawSeedHost.ID)).Return([]*resource.Host{&mockRawHost}, nil).Times(1), - ms.Send(gomock.Eq(&schedulerv2.SyncProbesResponse{ - Hosts: []*commonv2.Host{ - { - Id: mockRawHost.ID, - Type: uint32(mockRawHost.Type), - Hostname: mockRawHost.Hostname, - Ip: mockRawHost.IP, - Port: mockRawHost.Port, - DownloadPort: mockRawHost.DownloadPort, - Os: mockRawHost.OS, - Platform: mockRawHost.Platform, - PlatformFamily: mockRawHost.PlatformFamily, - PlatformVersion: mockRawHost.PlatformVersion, - KernelVersion: mockRawHost.KernelVersion, - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - }, - })).Return(errors.New("send synchronize probes response error")).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.EqualError(err, "send synchronize probes response error") - }, - }, - { - name: "load host error when receive ProbeFinishedRequest", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: &schedulerv2.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv2.ProbeFinishedRequest{ - Probes: []*schedulerv2.Probe{mockV2Probe}, - }, - }, - }, nil).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockRawHost.ID)).Return(nil, false), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "store error when receive ProbeFinishedRequest", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: &schedulerv2.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv2.ProbeFinishedRequest{ - Probes: []*schedulerv2.Probe{mockV2Probe}, - }, - }, - }, nil).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockRawHost.ID)).Return(&mockRawHost, true), - mn.Store(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(errors.New("store error")).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - { - name: "enqueue probe error when receive ProbeFinishedRequest", - mock: func(svc *V2, mr *resource.MockResourceMockRecorder, probes *networktopologymocks.MockProbes, mp *networktopologymocks.MockProbesMockRecorder, - mn *networktopologymocks.MockNetworkTopologyMockRecorder, hostManager resource.HostManager, mh *resource.MockHostManagerMockRecorder, - ms *schedulerv2mocks.MockScheduler_SyncProbesServerMockRecorder) { - gomock.InOrder( - ms.Recv().Return(&schedulerv2.SyncProbesRequest{ - Host: &commonv2.Host{ - Id: mockSeedHostID, - Type: uint32(pkgtypes.HostTypeSuperSeed), - Hostname: "bar", - Ip: "127.0.0.1", - Port: 8003, - DownloadPort: 8001, - Os: "darwin", - Platform: "darwin", - PlatformFamily: "Standalone Workstation", - PlatformVersion: "11.1", - KernelVersion: "20.2.0", - Cpu: mockV2Probe.Host.Cpu, - Memory: mockV2Probe.Host.Memory, - Network: mockV2Probe.Host.Network, - Disk: mockV2Probe.Host.Disk, - Build: mockV2Probe.Host.Build, - }, - Request: &schedulerv2.SyncProbesRequest_ProbeFinishedRequest{ - ProbeFinishedRequest: &schedulerv2.ProbeFinishedRequest{ - Probes: []*schedulerv2.Probe{mockV2Probe}, - }, - }, - }, nil).Times(1), - mr.HostManager().Return(hostManager).Times(1), - mh.Load(gomock.Eq(mockRawHost.ID)).Return(&mockRawHost, true), - mn.Store(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(nil).Times(1), - mn.Probes(gomock.Eq(mockRawSeedHost.ID), gomock.Eq(mockRawHost.ID)).Return(probes).Times(1), - mp.Enqueue(gomock.Any()).Return(errors.New("enqueue probe error")).Times(1), - ms.Recv().Return(nil, io.EOF).Times(1), - ) - }, - expect: func(t *testing.T, err error) { - assert := assert.New(t) - assert.NoError(err) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - ctl := gomock.NewController(t) - defer ctl.Finish() - - scheduling := mocks.NewMockScheduling(ctl) - res := resource.NewMockResource(ctl) - dynconfig := configmocks.NewMockDynconfigInterface(ctl) - storage := storagemocks.NewMockStorage(ctl) - probes := networktopologymocks.NewMockProbes(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) - hostManager := resource.NewMockHostManager(ctl) - stream := schedulerv2mocks.NewMockScheduler_SyncProbesServer(ctl) - svc := NewV2(&config.Config{Scheduler: config.SchedulerConfig{NetworkTopology: mockNetworkTopologyConfig}, Metrics: config.MetricsConfig{EnableHost: true}}, res, scheduling, dynconfig, storage, networkTopology) - - tc.mock(svc, res.EXPECT(), probes, probes.EXPECT(), networkTopology.EXPECT(), hostManager, hostManager.EXPECT(), stream.EXPECT()) - tc.expect(t, svc.SyncProbes(stream)) - }) - } -} - func TestServiceV2_handleRegisterPeerRequest(t *testing.T) { dgst := mockTaskDigest.String() @@ -1719,7 +1217,6 @@ func TestServiceV2_handleRegisterPeerRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) hostManager := resource.NewMockHostManager(ctl) peerManager := resource.NewMockPeerManager(ctl) taskManager := resource.NewMockTaskManager(ctl) @@ -1731,7 +1228,7 @@ func TestServiceV2_handleRegisterPeerRequest(t *testing.T) { mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) seedPeer := resource.NewPeer(mockSeedPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.req, peer, seedPeer, hostManager, taskManager, peerManager, stream, res.EXPECT(), hostManager.EXPECT(), taskManager.EXPECT(), peerManager.EXPECT(), stream.EXPECT(), scheduling.EXPECT()) }) @@ -1816,7 +1313,6 @@ func TestServiceV2_handleDownloadPeerStartedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -1824,7 +1320,7 @@ func TestServiceV2_handleDownloadPeerStartedRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, peer, peerManager, res.EXPECT(), peerManager.EXPECT(), dynconfig.EXPECT()) }) @@ -1909,7 +1405,6 @@ func TestServiceV2_handleDownloadPeerBackToSourceStartedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -1917,7 +1412,7 @@ func TestServiceV2_handleDownloadPeerBackToSourceStartedRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, peer, peerManager, res.EXPECT(), peerManager.EXPECT(), dynconfig.EXPECT()) }) @@ -1981,7 +1476,6 @@ func TestServiceV2_handleRescheduleRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -1989,7 +1483,7 @@ func TestServiceV2_handleRescheduleRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, peer, peerManager, res.EXPECT(), peerManager.EXPECT(), scheduling.EXPECT()) }) @@ -2055,7 +1549,6 @@ func TestServiceV2_handleDownloadPeerFinishedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -2063,7 +1556,7 @@ func TestServiceV2_handleDownloadPeerFinishedRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, peer, peerManager, res.EXPECT(), peerManager.EXPECT(), dynconfig.EXPECT()) }) @@ -2237,7 +1730,6 @@ func TestServiceV2_handleDownloadPeerBackToSourceFinishedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) url, err := url.Parse(s.URL) @@ -2263,7 +1755,7 @@ func TestServiceV2_handleDownloadPeerBackToSourceFinishedRequest(t *testing.T) { mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.req, peer, peerManager, res.EXPECT(), peerManager.EXPECT(), dynconfig.EXPECT()) }) @@ -2328,7 +1820,6 @@ func TestServiceV2_handleDownloadPeerFailedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -2336,7 +1827,7 @@ func TestServiceV2_handleDownloadPeerFailedRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, peer, peerManager, res.EXPECT(), peerManager.EXPECT(), dynconfig.EXPECT()) }) @@ -2448,7 +1939,6 @@ func TestServiceV2_handleDownloadPeerBackToSourceFailedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -2456,7 +1946,7 @@ func TestServiceV2_handleDownloadPeerBackToSourceFailedRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, peer, peerManager, res.EXPECT(), peerManager.EXPECT(), dynconfig.EXPECT()) }) @@ -2607,7 +2097,6 @@ func TestServiceV2_handleDownloadPieceFinishedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -2615,7 +2104,7 @@ func TestServiceV2_handleDownloadPieceFinishedRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.req, peer, peerManager, res.EXPECT(), peerManager.EXPECT()) }) @@ -2733,7 +2222,6 @@ func TestServiceV2_handleDownloadPieceBackToSourceFinishedRequest(t *testing.T) res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -2741,7 +2229,7 @@ func TestServiceV2_handleDownloadPieceBackToSourceFinishedRequest(t *testing.T) mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.req, peer, peerManager, res.EXPECT(), peerManager.EXPECT()) }) @@ -2844,7 +2332,6 @@ func TestServiceV2_handleDownloadPieceFailedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -2852,7 +2339,7 @@ func TestServiceV2_handleDownloadPieceFailedRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.req, peer, peerManager, res.EXPECT(), peerManager.EXPECT()) }) @@ -2910,7 +2397,6 @@ func TestServiceV2_handleDownloadPieceBackToSourceFailedRequest(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) peerManager := resource.NewMockPeerManager(ctl) mockHost := resource.NewHost( @@ -2918,7 +2404,7 @@ func TestServiceV2_handleDownloadPieceBackToSourceFailedRequest(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.req, peer, peerManager, res.EXPECT(), peerManager.EXPECT()) }) @@ -3121,7 +2607,6 @@ func TestServiceV2_handleResource(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) hostManager := resource.NewMockHostManager(ctl) taskManager := resource.NewMockTaskManager(ctl) peerManager := resource.NewMockPeerManager(ctl) @@ -3132,7 +2617,7 @@ func TestServiceV2_handleResource(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) mockPeer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&config.Config{Scheduler: mockSchedulerConfig}, res, scheduling, dynconfig, storage) tc.run(t, svc, tc.download, stream, mockHost, mockTask, mockPeer, hostManager, taskManager, peerManager, res.EXPECT(), hostManager.EXPECT(), taskManager.EXPECT(), peerManager.EXPECT()) }) @@ -3401,7 +2886,6 @@ func TestServiceV2_downloadTaskBySeedPeer(t *testing.T) { res := resource.NewMockResource(ctl) dynconfig := configmocks.NewMockDynconfigInterface(ctl) storage := storagemocks.NewMockStorage(ctl) - networkTopology := networktopologymocks.NewMockNetworkTopology(ctl) seedPeerClient := resource.NewMockSeedPeer(ctl) mockHost := resource.NewHost( @@ -3409,7 +2893,7 @@ func TestServiceV2_downloadTaskBySeedPeer(t *testing.T) { mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type) mockTask := resource.NewTask(mockTaskID, mockTaskURL, mockTaskTag, mockTaskApplication, commonv2.TaskType_STANDARD, mockTaskFilteredQueryParams, mockTaskHeader, mockTaskBackToSourceLimit, resource.WithDigest(mockTaskDigest), resource.WithPieceLength(mockTaskPieceLength)) peer := resource.NewPeer(mockPeerID, mockResourceConfig, mockTask, mockHost) - svc := NewV2(&tc.config, res, scheduling, dynconfig, storage, networkTopology) + svc := NewV2(&tc.config, res, scheduling, dynconfig, storage) tc.run(t, svc, peer, seedPeerClient, res.EXPECT(), seedPeerClient.EXPECT()) }) diff --git a/scheduler/storage/mocks/storage_mock.go b/scheduler/storage/mocks/storage_mock.go index e6fd3479d7c..1474c19171b 100644 --- a/scheduler/storage/mocks/storage_mock.go +++ b/scheduler/storage/mocks/storage_mock.go @@ -54,20 +54,6 @@ func (mr *MockStorageMockRecorder) ClearDownload() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ClearDownload", reflect.TypeOf((*MockStorage)(nil).ClearDownload)) } -// ClearNetworkTopology mocks base method. -func (m *MockStorage) ClearNetworkTopology() error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ClearNetworkTopology") - ret0, _ := ret[0].(error) - return ret0 -} - -// ClearNetworkTopology indicates an expected call of ClearNetworkTopology. -func (mr *MockStorageMockRecorder) ClearNetworkTopology() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ClearNetworkTopology", reflect.TypeOf((*MockStorage)(nil).ClearNetworkTopology)) -} - // CreateDownload mocks base method. func (m *MockStorage) CreateDownload(arg0 storage.Download) error { m.ctrl.T.Helper() @@ -82,20 +68,6 @@ func (mr *MockStorageMockRecorder) CreateDownload(arg0 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateDownload", reflect.TypeOf((*MockStorage)(nil).CreateDownload), arg0) } -// CreateNetworkTopology mocks base method. -func (m *MockStorage) CreateNetworkTopology(arg0 storage.NetworkTopology) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateNetworkTopology", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// CreateNetworkTopology indicates an expected call of CreateNetworkTopology. -func (mr *MockStorageMockRecorder) CreateNetworkTopology(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateNetworkTopology", reflect.TypeOf((*MockStorage)(nil).CreateNetworkTopology), arg0) -} - // DownloadCount mocks base method. func (m *MockStorage) DownloadCount() int64 { m.ctrl.T.Helper() @@ -125,35 +97,6 @@ func (mr *MockStorageMockRecorder) ListDownload() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListDownload", reflect.TypeOf((*MockStorage)(nil).ListDownload)) } -// ListNetworkTopology mocks base method. -func (m *MockStorage) ListNetworkTopology() ([]storage.NetworkTopology, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListNetworkTopology") - ret0, _ := ret[0].([]storage.NetworkTopology) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListNetworkTopology indicates an expected call of ListNetworkTopology. -func (mr *MockStorageMockRecorder) ListNetworkTopology() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListNetworkTopology", reflect.TypeOf((*MockStorage)(nil).ListNetworkTopology)) -} - -// NetworkTopologyCount mocks base method. -func (m *MockStorage) NetworkTopologyCount() int64 { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NetworkTopologyCount") - ret0, _ := ret[0].(int64) - return ret0 -} - -// NetworkTopologyCount indicates an expected call of NetworkTopologyCount. -func (mr *MockStorageMockRecorder) NetworkTopologyCount() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NetworkTopologyCount", reflect.TypeOf((*MockStorage)(nil).NetworkTopologyCount)) -} - // OpenDownload mocks base method. func (m *MockStorage) OpenDownload() (io.ReadCloser, error) { m.ctrl.T.Helper() @@ -168,18 +111,3 @@ func (mr *MockStorageMockRecorder) OpenDownload() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenDownload", reflect.TypeOf((*MockStorage)(nil).OpenDownload)) } - -// OpenNetworkTopology mocks base method. -func (m *MockStorage) OpenNetworkTopology() (io.ReadCloser, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "OpenNetworkTopology") - ret0, _ := ret[0].(io.ReadCloser) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// OpenNetworkTopology indicates an expected call of OpenNetworkTopology. -func (mr *MockStorageMockRecorder) OpenNetworkTopology() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenNetworkTopology", reflect.TypeOf((*MockStorage)(nil).OpenNetworkTopology)) -} diff --git a/scheduler/storage/storage.go b/scheduler/storage/storage.go index 58144d1ce7f..2eca71be684 100644 --- a/scheduler/storage/storage.go +++ b/scheduler/storage/storage.go @@ -40,9 +40,6 @@ const ( // DownloadFilePrefix is prefix of download file name. DownloadFilePrefix = "download" - // NetworkTopologyFilePrefix is prefix of network topology file name. - NetworkTopologyFilePrefix = "networktopology" - // CSVFileExt is extension of file name. CSVFileExt = "csv" ) @@ -60,32 +57,17 @@ type Storage interface { // CreateDownload inserts the download into csv file. CreateDownload(Download) error - // CreateNetworkTopology inserts the network topology into csv file. - CreateNetworkTopology(NetworkTopology) error - // ListDownload returns all downloads in csv file. ListDownload() ([]Download, error) - // ListNetworkTopology returns all network topologies in csv file. - ListNetworkTopology() ([]NetworkTopology, error) - // DownloadCount returns the count of downloads. DownloadCount() int64 - // NetworkTopologyCount returns the count of network topologies. - NetworkTopologyCount() int64 - // OpenDownload opens download files for read, it returns io.ReadCloser of download files. OpenDownload() (io.ReadCloser, error) - // OpenNetworkTopology opens network topology files for read, it returns io.ReadCloser of network topology files. - OpenNetworkTopology() (io.ReadCloser, error) - // ClearDownload removes all download files. ClearDownload() error - - // ClearNetworkTopology removes all network topology files. - ClearNetworkTopology() error } // storage provides storage function. @@ -99,11 +81,6 @@ type storage struct { downloadFilename string downloadBuffer []Download downloadCount int64 - - networkTopologyMu *sync.RWMutex - networkTopologyFilename string - networkTopologyBuffer []NetworkTopology - networkTopologyCount int64 } // New returns a new Storage instance. @@ -117,10 +94,6 @@ func New(baseDir string, maxSize, maxBackups, bufferSize int) (Storage, error) { downloadMu: &sync.RWMutex{}, downloadFilename: filepath.Join(baseDir, fmt.Sprintf("%s.%s", DownloadFilePrefix, CSVFileExt)), downloadBuffer: make([]Download, 0, bufferSize), - - networkTopologyMu: &sync.RWMutex{}, - networkTopologyFilename: filepath.Join(baseDir, fmt.Sprintf("%s.%s", NetworkTopologyFilePrefix, CSVFileExt)), - networkTopologyBuffer: make([]NetworkTopology, 0, bufferSize), } downloadFile, err := os.OpenFile(s.downloadFilename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) @@ -129,12 +102,6 @@ func New(baseDir string, maxSize, maxBackups, bufferSize int) (Storage, error) { } downloadFile.Close() - networkTopologyFile, err := os.OpenFile(s.networkTopologyFilename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return nil, err - } - networkTopologyFile.Close() - return s, nil } @@ -172,40 +139,6 @@ func (s *storage) CreateDownload(download Download) error { return nil } -// CreateNetworkTopology inserts the network topology into csv file. -func (s *storage) CreateNetworkTopology(networkTopology NetworkTopology) error { - s.networkTopologyMu.Lock() - defer s.networkTopologyMu.Unlock() - - // Write without buffer. - if s.bufferSize == 0 { - if err := s.createNetworkTopology(networkTopology); err != nil { - return err - } - - // Update network topology count. - s.networkTopologyCount++ - return nil - } - - // Write network topologies to file. - if len(s.networkTopologyBuffer) >= s.bufferSize { - if err := s.createNetworkTopology(s.networkTopologyBuffer...); err != nil { - return err - } - - // Update network topology count. - s.networkTopologyCount += int64(s.bufferSize) - - // Keep allocated memory. - s.networkTopologyBuffer = s.networkTopologyBuffer[:0] - } - - // Write network topologies to buffer. - s.networkTopologyBuffer = append(s.networkTopologyBuffer, networkTopology) - return nil -} - // ListDownload returns all downloads in csv file. func (s *storage) ListDownload() ([]Download, error) { s.downloadMu.RLock() @@ -244,54 +177,11 @@ func (s *storage) ListDownload() ([]Download, error) { return downloads, nil } -// ListNetworkTopology returns all network topologies in csv file. -func (s *storage) ListNetworkTopology() ([]NetworkTopology, error) { - s.networkTopologyMu.RLock() - defer s.networkTopologyMu.RUnlock() - - fileInfos, err := s.networkTopologyBackups() - if err != nil { - return nil, err - } - - var readers []io.Reader - var readClosers []io.ReadCloser - defer func() { - for _, readCloser := range readClosers { - if err := readCloser.Close(); err != nil { - logger.Error(err) - } - } - }() - - for _, fileInfo := range fileInfos { - file, err := os.Open(filepath.Join(s.baseDir, fileInfo.Name())) - if err != nil { - return nil, err - } - - readers = append(readers, file) - readClosers = append(readClosers, file) - } - - var networkTopologies []NetworkTopology - if err := gocsv.UnmarshalWithoutHeaders(io.MultiReader(readers...), &networkTopologies); err != nil { - return nil, err - } - - return networkTopologies, nil -} - // DownloadCount returns the count of downloads. func (s *storage) DownloadCount() int64 { return s.downloadCount } -// NetworkTopologyCount returns the count of network topologies. -func (s *storage) NetworkTopologyCount() int64 { - return s.networkTopologyCount -} - // OpenDownload opens download files for read, it returns io.ReadCloser of download files. func (s *storage) OpenDownload() (io.ReadCloser, error) { s.downloadMu.RLock() @@ -315,29 +205,6 @@ func (s *storage) OpenDownload() (io.ReadCloser, error) { return pkgio.MultiReadCloser(readClosers...), nil } -// OpenNetworkTopology opens network topology files for read, it returns io.ReadCloser of network topology files. -func (s *storage) OpenNetworkTopology() (io.ReadCloser, error) { - s.networkTopologyMu.RLock() - defer s.networkTopologyMu.RUnlock() - - fileInfos, err := s.networkTopologyBackups() - if err != nil { - return nil, err - } - - var readClosers []io.ReadCloser - for _, fileInfo := range fileInfos { - file, err := os.Open(filepath.Join(s.baseDir, fileInfo.Name())) - if err != nil { - return nil, err - } - - readClosers = append(readClosers, file) - } - - return pkgio.MultiReadCloser(readClosers...), nil -} - // ClearDownload removes all downloads. func (s *storage) ClearDownload() error { s.downloadMu.Lock() @@ -358,26 +225,6 @@ func (s *storage) ClearDownload() error { return nil } -// ClearNetworkTopology removes all network topologies. -func (s *storage) ClearNetworkTopology() error { - s.networkTopologyMu.Lock() - defer s.networkTopologyMu.Unlock() - - fileInfos, err := s.networkTopologyBackups() - if err != nil { - return err - } - - for _, fileInfo := range fileInfos { - filename := filepath.Join(s.baseDir, fileInfo.Name()) - if err := os.Remove(filename); err != nil { - return err - } - } - - return nil -} - // createDownload inserts the downloads into csv file. func (s *storage) createDownload(downloads ...Download) (err error) { file, err := s.openDownloadFile() @@ -393,21 +240,6 @@ func (s *storage) createDownload(downloads ...Download) (err error) { return gocsv.MarshalWithoutHeaders(downloads, file) } -// createNetworkTopology inserts the network topologies into csv file. -func (s *storage) createNetworkTopology(networkTopologies ...NetworkTopology) (err error) { - file, err := s.openNetworkTopologyFile() - if err != nil { - return err - } - defer func() { - if cerr := file.Close(); cerr != nil { - err = errors.Join(err, cerr) - } - }() - - return gocsv.MarshalWithoutHeaders(networkTopologies, file) -} - // openDownloadFile opens the download file and removes download files that exceed the total size. func (s *storage) openDownloadFile() (*os.File, error) { fileInfo, err := os.Stat(s.downloadFilename) @@ -441,51 +273,12 @@ func (s *storage) openDownloadFile() (*os.File, error) { return file, nil } -// openNetworkTopologyFile opens the network topology file and removes network topology files that exceed the total size. -func (s *storage) openNetworkTopologyFile() (*os.File, error) { - fileInfo, err := os.Stat(s.networkTopologyFilename) - if err != nil { - return nil, err - } - - if s.maxSize <= fileInfo.Size() { - if err := os.Rename(s.networkTopologyFilename, s.networkTopologyBackupFilename()); err != nil { - return nil, err - } - } - - fileInfos, err := s.networkTopologyBackups() - if err != nil { - return nil, err - } - - if s.maxBackups < len(fileInfos)+1 { - filename := filepath.Join(s.baseDir, fileInfos[0].Name()) - if err := os.Remove(filename); err != nil { - return nil, err - } - } - - file, err := os.OpenFile(s.networkTopologyFilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) - if err != nil { - return nil, err - } - - return file, nil -} - // downloadBackupFilename generates download file name of backup files. func (s *storage) downloadBackupFilename() string { timestamp := time.Now().Format(backupTimeFormat) return filepath.Join(s.baseDir, fmt.Sprintf("%s_%s.%s", DownloadFilePrefix, timestamp, CSVFileExt)) } -// networkTopologyBackupFilename generates network topology file name of backup files. -func (s *storage) networkTopologyBackupFilename() string { - timestamp := time.Now().Format(backupTimeFormat) - return filepath.Join(s.baseDir, fmt.Sprintf("%s_%s.%s", NetworkTopologyFilePrefix, timestamp, CSVFileExt)) -} - // downloadBackups returns download backup file information. func (s *storage) downloadBackups() ([]fs.FileInfo, error) { fileInfos, err := os.ReadDir(s.baseDir) @@ -512,30 +305,3 @@ func (s *storage) downloadBackups() ([]fs.FileInfo, error) { return backups, nil } - -// networkTopologyBackups returns network topology backup file information. -func (s *storage) networkTopologyBackups() ([]fs.FileInfo, error) { - fileInfos, err := os.ReadDir(s.baseDir) - if err != nil { - return nil, err - } - - var backups []fs.FileInfo - regexp := regexp.MustCompile(NetworkTopologyFilePrefix) - for _, fileInfo := range fileInfos { - if !fileInfo.IsDir() && regexp.MatchString(fileInfo.Name()) { - info, _ := fileInfo.Info() - backups = append(backups, info) - } - } - - if len(backups) <= 0 { - return nil, errors.New("network topology files backup does not exist") - } - - sort.Slice(backups, func(i, j int) bool { - return backups[i].ModTime().Before(backups[j].ModTime()) - }) - - return backups, nil -} diff --git a/scheduler/storage/storage_test.go b/scheduler/storage/storage_test.go index 48bf1c36ae4..6fd42255821 100644 --- a/scheduler/storage/storage_test.go +++ b/scheduler/storage/storage_test.go @@ -18,7 +18,6 @@ package storage import ( "fmt" - "io/fs" "os" "path/filepath" "reflect" @@ -157,48 +156,6 @@ var ( CreatedAt: time.Now().UnixNano(), UpdatedAt: time.Now().UnixNano(), } - - mockSrcHost = SrcHost{ - ID: "3", - Type: "super", - Hostname: "foo", - IP: "127.0.0.1", - Port: 8080, - Network: resource.Network{ - TCPConnectionCount: 400, - UploadTCPConnectionCount: 200, - Location: "china", - IDC: "e1", - }, - } - - mockDestHost = DestHost{ - ID: "2", - Type: "normal", - Hostname: "localhost", - IP: "127.0.0.1", - Port: 8080, - Network: resource.Network{ - TCPConnectionCount: 400, - UploadTCPConnectionCount: 200, - Location: "china", - IDC: "e1", - }, - Probes: Probes{ - AverageRTT: 10, - CreatedAt: time.Now().UnixNano(), - UpdatedAt: time.Now().UnixNano(), - }, - } - - mockDestHosts = []DestHost{mockDestHost, mockDestHost, mockDestHost, mockDestHost, mockDestHost} - - mockNetworkTopology = NetworkTopology{ - ID: "6", - Host: mockSrcHost, - DestHosts: mockDestHosts, - CreatedAt: time.Now().UnixNano(), - } ) func TestStorage_New(t *testing.T) { @@ -222,17 +179,9 @@ func TestStorage_New(t *testing.T) { assert.Equal(len(s.(*storage).downloadBuffer), 0) assert.Equal(s.(*storage).downloadCount, int64(0)) - assert.Equal(cap(s.(*storage).networkTopologyBuffer), config.DefaultStorageBufferSize) - assert.Equal(len(s.(*storage).networkTopologyBuffer), 0) - assert.Equal(s.(*storage).networkTopologyCount, int64(0)) - if err := s.ClearDownload(); err != nil { t.Fatal(err) } - - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } }, }, { @@ -332,85 +281,6 @@ func TestStorage_CreateDownload(t *testing.T) { } } -func TestStorage_CreateNetworkTopology(t *testing.T) { - tests := []struct { - name string - baseDir string - bufferSize int - mock func(s Storage) - expect func(t *testing.T, s Storage, baseDir string) - }{ - { - name: "create network topology", - baseDir: os.TempDir(), - bufferSize: 1, - mock: func(s Storage) {}, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - assert.NoError(s.CreateNetworkTopology(NetworkTopology{})) - assert.Equal(s.(*storage).networkTopologyCount, int64(0)) - }, - }, - { - name: "create network topology without buffer", - baseDir: os.TempDir(), - bufferSize: 0, - mock: func(s Storage) { - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - assert.NoError(s.CreateNetworkTopology(NetworkTopology{})) - assert.Equal(s.(*storage).networkTopologyCount, int64(1)) - - networkTopologies, err := s.ListNetworkTopology() - assert.NoError(err) - assert.Equal(len(networkTopologies), 1) - }, - }, - { - name: "write network topology to file", - baseDir: os.TempDir(), - bufferSize: 1, - mock: func(s Storage) { - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - assert.NoError(s.CreateNetworkTopology(NetworkTopology{})) - assert.NoError(s.CreateNetworkTopology(NetworkTopology{})) - assert.Equal(s.(*storage).networkTopologyCount, int64(1)) - }, - }, - { - name: "open file failed", - baseDir: os.TempDir(), - bufferSize: 0, - mock: func(s Storage) { - s.(*storage).baseDir = "baw" - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - assert.Error(s.CreateNetworkTopology(NetworkTopology{})) - s.(*storage).baseDir = baseDir - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - s, err := New(tc.baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, tc.bufferSize) - if err != nil { - t.Fatal(err) - } - - tc.mock(s) - tc.expect(t, s, tc.baseDir) - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } - }) - } -} - func TestStorage_ListDownload(t *testing.T) { tests := []struct { name string @@ -540,126 +410,6 @@ func TestStorage_ListDownload(t *testing.T) { } } -func TestStorage_ListNetworkTopology(t *testing.T) { - tests := []struct { - name string - baseDir string - bufferSize int - networkTopology NetworkTopology - mock func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) - expect func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) - }{ - { - name: "empty csv file given", - baseDir: os.TempDir(), - bufferSize: config.DefaultStorageBufferSize, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) {}, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - _, err := s.ListNetworkTopology() - assert.Error(err) - }, - }, - { - name: "get file infos failed", - baseDir: os.TempDir(), - bufferSize: config.DefaultStorageBufferSize, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - s.(*storage).baseDir = "bae" - }, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - _, err := s.ListNetworkTopology() - assert.Error(err) - s.(*storage).baseDir = baseDir - }, - }, - { - name: "open file failed", - baseDir: os.TempDir(), - bufferSize: config.DefaultStorageBufferSize, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - file, err := os.OpenFile(filepath.Join(baseDir, "networktopology_test.csv"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0300) - if err != nil { - t.Fatal(err) - } - file.Close() - }, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - _, err := s.ListNetworkTopology() - assert.Error(err) - }, - }, - { - name: "list network topologies of a file", - baseDir: os.TempDir(), - bufferSize: 1, - networkTopology: mockNetworkTopology, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - if err := s.CreateNetworkTopology(networkTopology); err != nil { - t.Fatal(err) - } - }, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - _, err := s.ListNetworkTopology() - assert.Error(err) - - if err := s.CreateNetworkTopology(networkTopology); err != nil { - t.Fatal(err) - } - networkTopologies, err := s.ListNetworkTopology() - assert.NoError(err) - assert.Equal(len(networkTopologies), 1) - assert.EqualValues(networkTopologies[0], networkTopology) - }, - }, - { - name: "list network topologies of multi files", - baseDir: os.TempDir(), - bufferSize: 1, - networkTopology: NetworkTopology{}, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - if err := s.CreateNetworkTopology(NetworkTopology{ID: "2"}); err != nil { - t.Fatal(err) - } - - if err := s.CreateNetworkTopology(NetworkTopology{ID: "1"}); err != nil { - t.Fatal(err) - } - - if err := s.CreateNetworkTopology(NetworkTopology{ID: "3"}); err != nil { - t.Fatal(err) - } - }, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - networkTopologies, err := s.ListNetworkTopology() - assert.NoError(err) - assert.Equal(len(networkTopologies), 2) - assert.Equal(networkTopologies[0].ID, "2") - assert.Equal(networkTopologies[1].ID, "1") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - s, err := New(tc.baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, tc.bufferSize) - if err != nil { - t.Fatal(err) - } - - tc.mock(t, s, tc.baseDir, tc.networkTopology) - tc.expect(t, s, tc.baseDir, tc.networkTopology) - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } - }) - } -} - func TestStorage_DownloadCount(t *testing.T) { tests := []struct { name string @@ -693,38 +443,6 @@ func TestStorage_DownloadCount(t *testing.T) { } } -func TestStorage_NetworkTopologyCount(t *testing.T) { - tests := []struct { - name string - baseDir string - mock func(s Storage) - expect func(t *testing.T, s Storage) - }{ - { - name: "get the count of network topologies", - baseDir: os.TempDir(), - mock: func(s Storage) { - s.(*storage).networkTopologyCount = 1 - }, - expect: func(t *testing.T, s Storage) { - assert := assert.New(t) - assert.Equal(int64(1), s.NetworkTopologyCount()) - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - s, err := New(tc.baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, config.DefaultStorageBufferSize) - if err != nil { - t.Fatal(err) - } - - tc.mock(s) - tc.expect(t, s) - }) - } -} - func TestStorage_OpenDownload(t *testing.T) { tests := []struct { name string @@ -846,118 +564,6 @@ func TestStorage_OpenDownload(t *testing.T) { } } -func TestStorage_OpenNetworkTopology(t *testing.T) { - tests := []struct { - name string - baseDir string - bufferSize int - networkTopology NetworkTopology - mock func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) - expect func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) - }{ - { - name: "open storage with empty csv file given", - baseDir: os.TempDir(), - bufferSize: config.DefaultStorageBufferSize, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) {}, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - _, err := s.OpenNetworkTopology() - assert.NoError(err) - }, - }, - { - name: "open file infos failed", - baseDir: os.TempDir(), - bufferSize: config.DefaultStorageBufferSize, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - s.(*storage).baseDir = "bas" - }, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - _, err := s.OpenNetworkTopology() - assert.Error(err) - s.(*storage).baseDir = baseDir - }, - }, - { - name: "open storage with network topologies of a file", - baseDir: os.TempDir(), - bufferSize: 1, - networkTopology: mockNetworkTopology, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - if err := s.CreateNetworkTopology(networkTopology); err != nil { - t.Fatal(err) - } - }, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - _, err := s.OpenNetworkTopology() - assert.NoError(err) - - if err := s.CreateNetworkTopology(networkTopology); err != nil { - t.Fatal(err) - } - - readCloser, err := s.OpenNetworkTopology() - assert.NoError(err) - - var networkTopologies []NetworkTopology - err = gocsv.UnmarshalWithoutHeaders(readCloser, &networkTopologies) - assert.NoError(err) - assert.Equal(len(networkTopologies), 1) - assert.EqualValues(networkTopologies[0], networkTopology) - }, - }, - { - name: "open storage with network topologies of multi files", - baseDir: os.TempDir(), - bufferSize: 1, - networkTopology: NetworkTopology{}, - mock: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - if err := s.CreateNetworkTopology(NetworkTopology{ID: "2"}); err != nil { - t.Fatal(err) - } - - if err := s.CreateNetworkTopology(NetworkTopology{ID: "1"}); err != nil { - t.Fatal(err) - } - - if err := s.CreateNetworkTopology(NetworkTopology{ID: "3"}); err != nil { - t.Fatal(err) - } - }, - expect: func(t *testing.T, s Storage, baseDir string, networkTopology NetworkTopology) { - assert := assert.New(t) - readCloser, err := s.OpenNetworkTopology() - assert.NoError(err) - - var networkTopologies []NetworkTopology - err = gocsv.UnmarshalWithoutHeaders(readCloser, &networkTopologies) - assert.NoError(err) - assert.Equal(len(networkTopologies), 2) - assert.Equal(networkTopologies[0].ID, "2") - assert.Equal(networkTopologies[1].ID, "1") - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - s, err := New(tc.baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, tc.bufferSize) - if err != nil { - t.Fatal(err) - } - - tc.mock(t, s, tc.baseDir, tc.networkTopology) - tc.expect(t, s, tc.baseDir, tc.networkTopology) - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } - }) - } -} - func TestStorage_ClearDownload(t *testing.T) { tests := []struct { name string @@ -1014,63 +620,6 @@ func TestStorage_ClearDownload(t *testing.T) { } } -func TestStorage_ClearNetworkTopology(t *testing.T) { - tests := []struct { - name string - baseDir string - mock func(s Storage) - expect func(t *testing.T, s Storage, baseDir string) - }{ - { - name: "clear file", - baseDir: os.TempDir(), - mock: func(s Storage) {}, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - assert.NoError(s.ClearNetworkTopology()) - fileInfos, err := os.ReadDir(filepath.Join(baseDir)) - assert.NoError(err) - - var backups []fs.FileInfo - regexp := regexp.MustCompile(NetworkTopologyFilePrefix) - for _, fileInfo := range fileInfos { - if !fileInfo.IsDir() && regexp.MatchString(fileInfo.Name()) { - info, _ := fileInfo.Info() - backups = append(backups, info) - } - } - assert.Equal(len(backups), 0) - }, - }, - { - name: "open file failed", - baseDir: os.TempDir(), - mock: func(s Storage) { - s.(*storage).baseDir = "baz" - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - assert.Error(s.ClearNetworkTopology()) - - s.(*storage).baseDir = baseDir - assert.NoError(s.ClearNetworkTopology()) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - s, err := New(tc.baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, config.DefaultStorageBufferSize) - if err != nil { - t.Fatal(err) - } - - tc.mock(s) - tc.expect(t, s, tc.baseDir) - }) - } -} - func TestStorage_createDownload(t *testing.T) { tests := []struct { name string @@ -1117,52 +666,6 @@ func TestStorage_createDownload(t *testing.T) { } } -func TestStorage_createNetworkTopology(t *testing.T) { - tests := []struct { - name string - baseDir string - mock func(s Storage) - expect func(t *testing.T, s Storage, baseDir string) - }{ - { - name: "create network topology", - baseDir: os.TempDir(), - mock: func(s Storage) {}, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - assert.NoError(s.(*storage).createNetworkTopology(NetworkTopology{})) - }, - }, - { - name: "open file failed", - baseDir: os.TempDir(), - mock: func(s Storage) { - s.(*storage).baseDir = "baw" - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - assert.Error(s.(*storage).createNetworkTopology(NetworkTopology{})) - s.(*storage).baseDir = baseDir - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - s, err := New(tc.baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, config.DefaultStorageBufferSize) - if err != nil { - t.Fatal(err) - } - - tc.mock(s) - tc.expect(t, s, tc.baseDir) - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } - }) - } -} - func TestStorage_openDownloadFile(t *testing.T) { tests := []struct { name string @@ -1250,93 +753,6 @@ func TestStorage_openDownloadFile(t *testing.T) { } } -func TestStorage_openNetworkTopologyFile(t *testing.T) { - tests := []struct { - name string - baseDir string - maxSize int - maxBackups int - - bufferSize int - mock func(t *testing.T, s Storage) - expect func(t *testing.T, s Storage, baseDir string) - }{ - { - name: "open file failed", - baseDir: os.TempDir(), - maxSize: config.DefaultStorageMaxSize, - maxBackups: config.DefaultStorageMaxBackups, - bufferSize: config.DefaultStorageBufferSize, - mock: func(t *testing.T, s Storage) { - s.(*storage).baseDir = "bat" - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - _, err := s.(*storage).openNetworkTopologyFile() - assert.Error(err) - s.(*storage).baseDir = baseDir - }, - }, - { - name: "open new network topology file", - baseDir: os.TempDir(), - maxSize: 0, - maxBackups: config.DefaultStorageMaxBackups, - bufferSize: 1, - mock: func(t *testing.T, s Storage) { - if err := s.CreateNetworkTopology(NetworkTopology{ID: "1"}); err != nil { - t.Fatal(err) - } - - if err := s.CreateNetworkTopology(NetworkTopology{ID: "2"}); err != nil { - t.Fatal(err) - } - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - file, err := s.(*storage).openNetworkTopologyFile() - assert.NoError(err) - assert.Equal(file.Name(), filepath.Join(baseDir, fmt.Sprintf("%s.%s", NetworkTopologyFilePrefix, CSVFileExt))) - file.Close() - }, - }, - { - name: "remove network topology file", - baseDir: os.TempDir(), - maxSize: 0, - maxBackups: 1, - bufferSize: 1, - mock: func(t *testing.T, s Storage) { - if err := s.CreateNetworkTopology(NetworkTopology{ID: "1"}); err != nil { - t.Fatal(err) - } - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - file, err := s.(*storage).openNetworkTopologyFile() - assert.NoError(err) - assert.Equal(file.Name(), filepath.Join(baseDir, fmt.Sprintf("%s.%s", NetworkTopologyFilePrefix, CSVFileExt))) - file.Close() - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - s, err := New(tc.baseDir, tc.maxSize, tc.maxBackups, tc.bufferSize) - if err != nil { - t.Fatal(err) - } - - tc.mock(t, s) - tc.expect(t, s, tc.baseDir) - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } - }) - } -} - func TestStorage_downloadBackupFilename(t *testing.T) { baseDir := os.TempDir() s, err := New(baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, config.DefaultStorageBufferSize) @@ -1354,23 +770,6 @@ func TestStorage_downloadBackupFilename(t *testing.T) { } } -func TestStorage_networkTopologyBackupFilename(t *testing.T) { - baseDir := os.TempDir() - s, err := New(baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, config.DefaultStorageBufferSize) - if err != nil { - t.Fatal(err) - } - - filename := s.(*storage).networkTopologyBackupFilename() - regexp := regexp.MustCompile(fmt.Sprintf("%s_.*.%s$", NetworkTopologyFilePrefix, CSVFileExt)) - assert := assert.New(t) - assert.True(regexp.MatchString(filename)) - - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } -} - func TestStorage_downloadBackups(t *testing.T) { tests := []struct { name string @@ -1422,55 +821,3 @@ func TestStorage_downloadBackups(t *testing.T) { }) } } - -func TestStorage_networkTopologyBackups(t *testing.T) { - tests := []struct { - name string - baseDir string - mock func(t *testing.T, s Storage) - expect func(t *testing.T, s Storage, baseDir string) - }{ - { - name: "open file failed", - baseDir: os.TempDir(), - mock: func(t *testing.T, s Storage) { - s.(*storage).baseDir = "bar" - }, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - _, err := s.(*storage).networkTopologyBackups() - assert.Error(err) - s.(*storage).baseDir = baseDir - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } - }, - }, - { - name: "not found network topology file", - baseDir: os.TempDir(), - mock: func(t *testing.T, s Storage) {}, - expect: func(t *testing.T, s Storage, baseDir string) { - assert := assert.New(t) - if err := s.ClearNetworkTopology(); err != nil { - t.Fatal(err) - } - - _, err := s.(*storage).networkTopologyBackups() - assert.Error(err) - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - s, err := New(tc.baseDir, config.DefaultStorageMaxSize, config.DefaultStorageMaxBackups, config.DefaultStorageBufferSize) - if err != nil { - t.Fatal(err) - } - - tc.mock(t, s) - tc.expect(t, s, tc.baseDir) - }) - } -} diff --git a/scheduler/storage/types.go b/scheduler/storage/types.go index eb433125ff1..e9cd2c68bbc 100644 --- a/scheduler/storage/types.go +++ b/scheduler/storage/types.go @@ -223,75 +223,3 @@ type Download struct { // UpdatedAt is peer update nanosecond time. UpdatedAt int64 `csv:"updatedAt"` } - -// Probes contains content for probes. -type Probes struct { - // AverageRTT is the average round-trip time of probes. - AverageRTT int64 `csv:"averageRTT"` - - // CreatedAt is probe create nanosecond time. - CreatedAt int64 `csv:"createdAt"` - - // UpdatedAt is probe update nanosecond time. - UpdatedAt int64 `csv:"updatedAt"` -} - -// SrcHost contains content for source host. -type SrcHost struct { - // ID is host id. - ID string `csv:"id"` - - // Type is host type. - Type string `csv:"type"` - - // Hostname is host name. - Hostname string `csv:"hostname"` - - // IP is host ip. - IP string `csv:"ip"` - - // Port is grpc service port. - Port int32 `csv:"port"` - - // Network Stat. - Network resource.Network `csv:"network"` -} - -// DestHost contains content for destination host. -type DestHost struct { - // ID is host id. - ID string `csv:"id"` - - // Type is host type. - Type string `csv:"type"` - - // Hostname is host name. - Hostname string `csv:"hostname"` - - // IP is host ip. - IP string `csv:"ip"` - - // Port is grpc service port. - Port int32 `csv:"port"` - - // Network Stat. - Network resource.Network `csv:"network"` - - // Probes is the network information probed to destination host. - Probes Probes `csv:"probes"` -} - -// NetworkTopology contains content for network topology. -type NetworkTopology struct { - // ID is network topology id. - ID string `csv:"id"` - - // Host is probe source host. - Host SrcHost `csv:"host"` - - // DestHosts is the destination hosts probed from source host. - DestHosts []DestHost `csv:"destHosts" csv[]:"5"` - - // CreatedAt is network topology create nanosecond time. - CreatedAt int64 `csv:"createdAt"` -} diff --git a/test/e2e/v1/constants.go b/test/e2e/v1/constants.go index 48e3a36e80c..d6e2f959643 100644 --- a/test/e2e/v1/constants.go +++ b/test/e2e/v1/constants.go @@ -24,8 +24,7 @@ const ( ) const ( - dfdaemonCompatibilityTestMode = "dfdaemon" - schedulerCompatibilityTestMode = "scheduler" + dfdaemonCompatibilityTestMode = "dfdaemon" ) const ( diff --git a/test/e2e/v1/network_topology_test.go b/test/e2e/v1/network_topology_test.go deleted file mode 100644 index 0fbd50b4e00..00000000000 --- a/test/e2e/v1/network_topology_test.go +++ /dev/null @@ -1,161 +0,0 @@ -/* - * 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. - */ - -package e2e - -import ( - "context" - "fmt" - "os" - "strconv" - "strings" - "time" - - . "github.com/onsi/ginkgo/v2" //nolint - . "github.com/onsi/gomega" //nolint - - "d7y.io/dragonfly/v2/test/e2e/v1/util" -) - -var _ = Describe("Evaluator with networkTopology", func() { - Context("networkTopology", func() { - It("check networkTopology in redis", Label("networkTopology"), func() { - mode := os.Getenv("DRAGONFLY_COMPATIBILITY_E2E_TEST_MODE") - if mode == schedulerCompatibilityTestMode { - fmt.Println("networkTopology is disable, skip") - return - } - Expect(waitForProbedInNetworkTopology()).Should(BeTrue()) - - if waitForProbedInNetworkTopology() == true { - time.Sleep(3 * time.Minute) - Expect(checkNetworkTopologyUpdated()).Should(BeTrue()) - } - }) - }) -}) - -// getRedisExec get redis pod. -func getRedisExec() *util.PodExec { - out, err := util.KubeCtlCommand("-n", dragonflyNamespace, "get", "pod", "-l", "app.kubernetes.io/name=redis", - "-o", "jsonpath='{range .items[0]}{.metadata.name}{end}'").CombinedOutput() - podName := strings.Trim(string(out), "'") - Expect(err).NotTo(HaveOccurred()) - fmt.Println(podName) - Expect(strings.HasPrefix(podName, "dragonfly-redis")).Should(BeTrue()) - return util.NewPodExec(dragonflyNamespace, podName, "redis") -} - -func waitForProbedInNetworkTopology() bool { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) - defer cancel() - - ticker := time.NewTicker(5 * time.Second) - defer ticker.Stop() - - redisPod := getRedisExec() - - for { - select { - case <-ctx.Done(): - return false - case <-ticker.C: - out, err := redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "dbsize").CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - key, err := strconv.Atoi(strings.Split(string(out), "\n")[1]) - if key == 0 || err != nil { - continue - } - - out, err = redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "KEYS", "scheduler:network-topology:*").CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - networkTopologyKey := strings.Split(string(out), "\n")[1] - if networkTopologyKey == "" || err != nil { - continue - } - networkTopologyOut, err := redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "HGETALL", networkTopologyKey).CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - if networkTopologyOut == nil || err != nil { - continue - } - - out, err = redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "KEYS", "scheduler:probes:*").CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - probesKey := strings.Split(string(out), "\n")[1] - if probesKey == "" || err != nil { - continue - } - probesOut, err := redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "LRANGE", probesKey, "0", "-1").CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - if probesOut == nil || err != nil { - continue - } - - out, err = redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "KEYS", "scheduler:probed-count:*").CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - probedCountKey := strings.Split(string(out), "\n")[1] - if probedCountKey == "" || err != nil { - continue - } - probedCountOut, err := redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "GET", probedCountKey).CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - if probedCountOut == nil || err != nil { - continue - } - - return true - } - } -} - -func checkNetworkTopologyUpdated() bool { - redisPod := getRedisExec() - - out, err := redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "KEYS", "scheduler:network-topology:*").CombinedOutput() - networkTopologyKey := strings.Split(string(out), "\n") - Expect(err).NotTo(HaveOccurred()) - for i := 1; i <= 3; i++ { - updatedAtOut, err := redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "HGET", networkTopologyKey[i], "updatedAt").CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - createdAtOut, err := redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "HGET", networkTopologyKey[i], "createdAt").CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - if strings.Split(string(updatedAtOut), "\n")[1] == strings.Split(string(createdAtOut), "\n")[1] { - return false - } - } - - out, err = redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "KEYS", "scheduler:probed-count:*").CombinedOutput() - Expect(err).NotTo(HaveOccurred()) - probedCountKey := strings.Split(string(out), "\n") - for i := 1; i <= 3; i++ { - probedCountOut, err := redisPod.Command("redis-cli", "-a", "dragonfly", "-n", "3", "GET", probedCountKey[i]).CombinedOutput() - str := string(probedCountOut) - Expect(err).NotTo(HaveOccurred()) - for _, c := range str { - if c < '0' || c > '9' { - str = strings.ReplaceAll(str, string(c), "") - } - } - probedCount, err := strconv.Atoi(str) - Expect(err).NotTo(HaveOccurred()) - fmt.Printf("%s probedCount: %d \n", probedCountKey[i], probedCount) - if probedCount <= 1 || probedCount >= 500 { - return false - } - } - - return true -} diff --git a/test/testdata/charts/config-cache-list-metadata.yaml b/test/testdata/charts/config-cache-list-metadata.yaml index 3e450c79305..a800bf25cb5 100644 --- a/test/testdata/charts/config-cache-list-metadata.yaml +++ b/test/testdata/charts/config-cache-list-metadata.yaml @@ -28,14 +28,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m seedPeer: image: @@ -67,10 +59,6 @@ seedPeer: verbose: true download: prefetch: true - networkTopology: - enable: true - probe: - interval: 20s dfdaemon: image: @@ -106,10 +94,6 @@ dfdaemon: cacheRecursiveMetadata: 10m scheduler: disableAutoBackSource: true - networkTopology: - enable: true - probe: - interval: 20s proxy: defaultFilter: "Expires&Signature&ns" security: diff --git a/test/testdata/charts/config-compatibility.yaml b/test/testdata/charts/config-compatibility.yaml index 8fe759fe997..d1a6155057f 100644 --- a/test/testdata/charts/config-compatibility.yaml +++ b/test/testdata/charts/config-compatibility.yaml @@ -28,14 +28,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m seedPeer: image: @@ -67,10 +59,6 @@ seedPeer: verbose: true download: prefetch: true - networkTopology: - enable: true - probe: - interval: 20s dfdaemon: image: @@ -117,10 +105,6 @@ dfdaemon: proxies: - regx: blobs/sha256.* - regx: file-server - networkTopology: - enable: true - probe: - interval: 20s manager: image: diff --git a/test/testdata/charts/config-concurent-back-source.yaml b/test/testdata/charts/config-concurent-back-source.yaml index 47d6ddf8dca..e5654c1fc1f 100644 --- a/test/testdata/charts/config-concurent-back-source.yaml +++ b/test/testdata/charts/config-concurent-back-source.yaml @@ -28,14 +28,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m seedPeer: image: @@ -71,10 +63,6 @@ seedPeer: thresholdSize: 10M thresholdSpeed: 2M goroutineCount: 4 - networkTopology: - enable: true - probe: - interval: 20s dfdaemon: image: @@ -125,10 +113,6 @@ dfdaemon: proxies: - regx: blobs/sha256.* - regx: file-server - networkTopology: - enable: true - probe: - interval: 20s manager: image: diff --git a/test/testdata/charts/config-disable-seed-peer.yaml b/test/testdata/charts/config-disable-seed-peer.yaml index 566bae56aeb..33ff1547112 100644 --- a/test/testdata/charts/config-disable-seed-peer.yaml +++ b/test/testdata/charts/config-disable-seed-peer.yaml @@ -30,14 +30,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m seedPeer: enable: false @@ -89,10 +81,6 @@ dfdaemon: proxies: - regx: blobs/sha256.* - regx: file-server - networkTopology: - enable: true - probe: - interval: 20s manager: image: diff --git a/test/testdata/charts/config-grpc-tls-ipv6.yaml b/test/testdata/charts/config-grpc-tls-ipv6.yaml index be90a107e3d..7d178d79f0b 100644 --- a/test/testdata/charts/config-grpc-tls-ipv6.yaml +++ b/test/testdata/charts/config-grpc-tls-ipv6.yaml @@ -28,14 +28,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m security: autoIssueCert: true tlsVerify: true @@ -110,10 +102,6 @@ seedPeer: thresholdSize: 10M thresholdSpeed: 2M goroutineCount: 4 - networkTopology: - enable: true - probe: - interval: 20s security: autoIssueCert: true tlsVerify: true @@ -203,10 +191,6 @@ dfdaemon: proxies: - regx: blobs/sha256.* - regx: file-server - networkTopology: - enable: true - probe: - interval: 20s security: autoIssueCert: true tlsVerify: true diff --git a/test/testdata/charts/config-grpc-tls.yaml b/test/testdata/charts/config-grpc-tls.yaml index 91219de4c27..af618581174 100644 --- a/test/testdata/charts/config-grpc-tls.yaml +++ b/test/testdata/charts/config-grpc-tls.yaml @@ -28,14 +28,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m security: autoIssueCert: true tlsVerify: true @@ -108,10 +100,6 @@ seedPeer: thresholdSize: 10M thresholdSpeed: 2M goroutineCount: 4 - networkTopology: - enable: true - probe: - interval: 20s security: autoIssueCert: true tlsVerify: true @@ -185,10 +173,6 @@ dfdaemon: goroutineCount: 4 scheduler: disableAutoBackSource: true - networkTopology: - enable: true - probe: - interval: 20s proxy: defaultFilter: "Expires&Signature&ns" security: diff --git a/test/testdata/charts/config-ipv6.yaml b/test/testdata/charts/config-ipv6.yaml index 3c5d7f5194f..e6d89b93e7e 100644 --- a/test/testdata/charts/config-ipv6.yaml +++ b/test/testdata/charts/config-ipv6.yaml @@ -30,14 +30,6 @@ scheduler: enableIPv6: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m seedPeer: image: @@ -71,10 +63,6 @@ seedPeer: prefetch: true network: enableIPv6: true - networkTopology: - enable: true - probe: - interval: 20s dfdaemon: image: @@ -123,10 +111,6 @@ dfdaemon: - regx: file-server network: enableIPv6: true - networkTopology: - enable: true - probe: - interval: 20s manager: image: diff --git a/test/testdata/charts/config-split-running-tasks.yaml b/test/testdata/charts/config-split-running-tasks.yaml index 9026bd7505b..61a831f4bfa 100644 --- a/test/testdata/charts/config-split-running-tasks.yaml +++ b/test/testdata/charts/config-split-running-tasks.yaml @@ -28,14 +28,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m seedPeer: image: @@ -67,10 +59,6 @@ seedPeer: verbose: true download: prefetch: true - networkTopology: - enable: true - probe: - interval: 20s dfdaemon: image: @@ -118,10 +106,6 @@ dfdaemon: proxies: - regx: blobs/sha256.* - regx: file-server - networkTopology: - enable: true - probe: - interval: 20s manager: image: diff --git a/test/testdata/charts/config-write-buffer-size.yaml b/test/testdata/charts/config-write-buffer-size.yaml index d4375af04e6..b25dcd49798 100644 --- a/test/testdata/charts/config-write-buffer-size.yaml +++ b/test/testdata/charts/config-write-buffer-size.yaml @@ -28,14 +28,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m seedPeer: image: @@ -69,10 +61,6 @@ seedPeer: writeBufferSize: 1Mi download: prefetch: true - networkTopology: - enable: true - probe: - interval: 20s dfdaemon: image: @@ -121,10 +109,6 @@ dfdaemon: proxies: - regx: blobs/sha256.* - regx: file-server - networkTopology: - enable: true - probe: - interval: 20s manager: image: diff --git a/test/testdata/charts/config.yaml b/test/testdata/charts/config.yaml index 2da99a5ae5d..03a5eef2de1 100644 --- a/test/testdata/charts/config.yaml +++ b/test/testdata/charts/config.yaml @@ -28,14 +28,6 @@ scheduler: verbose: true scheduler: algorithm: nt - networkTopology: - collectInterval: 2m - probe: - queueLength: 5 - count: 10 - cache: - interval: 5m - ttl: 5m seedPeer: image: @@ -67,10 +59,6 @@ seedPeer: verbose: true download: prefetch: true - networkTopology: - enable: true - probe: - interval: 20s dfdaemon: image: @@ -117,10 +105,6 @@ dfdaemon: proxies: - regx: blobs/sha256.* - regx: file-server - networkTopology: - enable: true - probe: - interval: 20s manager: image: