Skip to content

Commit

Permalink
feat: remove ProbedAt function in network topology (#2529)
Browse files Browse the repository at this point in the history
Signed-off-by: XZ <834756128@qq.com>
  • Loading branch information
fcgxz2003 authored Jul 11, 2023
1 parent 355d9dd commit a45fcd9
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 160 deletions.
8 changes: 0 additions & 8 deletions pkg/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ const (

// ProbedCountNamespace prefix of probed count namespace cache key.
ProbedCountNamespace = "probed-count"

// ProbedAtNamespace prefix of last probed time namespace cache key.
ProbedAtNamespace = "probed-at"
)

// NewRedis returns a new redis client.
Expand Down Expand Up @@ -167,8 +164,3 @@ func ParseProbedCountKeyInScheduler(key string) (string, string, string, error)
func MakeProbedCountKeyInScheduler(hostID string) string {
return MakeKeyInScheduler(ProbedCountNamespace, hostID)
}

// MakeProbedAtKeyInScheduler make last probed time in scheduler.
func MakeProbedAtKeyInScheduler(hostID string) string {
return MakeKeyInScheduler(ProbedAtNamespace, hostID)
}
30 changes: 0 additions & 30 deletions pkg/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,33 +669,3 @@ func Test_ParseProbedCountKeyInScheduler(t *testing.T) {
})
}
}

func Test_MakeProbedAtKeyInScheduler(t *testing.T) {
tests := []struct {
name string
hostID string
expect func(t *testing.T, s string)
}{
{
name: "make probed at key in scheduler",
hostID: "bas",
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal("scheduler:probed-at:bas", s)
},
},
{
name: "host id is empty",
hostID: "",
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal("scheduler:probed-at:", s)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.expect(t, MakeProbedAtKeyInScheduler(tc.hostID))
})
}
}
16 changes: 0 additions & 16 deletions scheduler/networktopology/mocks/network_topology_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 1 addition & 12 deletions scheduler/networktopology/network_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ type NetworkTopology interface {
// ProbedCount is the number of times the host has been probed.
ProbedCount(string) (uint64, error)

// ProbedAt is the time when the host was last probed.
ProbedAt(string) (time.Time, error)

// Snapshot writes the current network topology to the storage.
Snapshot() error
}
Expand Down Expand Up @@ -218,7 +215,7 @@ func (nt *networkTopology) DeleteHost(hostID string) error {
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
defer cancel()

deleteKeys := []string{pkgredis.MakeProbedAtKeyInScheduler(hostID), pkgredis.MakeProbedCountKeyInScheduler(hostID)}
deleteKeys := []string{pkgredis.MakeProbedCountKeyInScheduler(hostID)}
srcNetworkTopologyKeys, _, err := nt.rdb.Scan(ctx, 0, pkgredis.MakeNetworkTopologyKeyInScheduler(hostID, "*"), math.MaxInt64).Result()
if err != nil {
return err
Expand Down Expand Up @@ -263,14 +260,6 @@ func (nt *networkTopology) ProbedCount(hostID string) (uint64, error) {
return nt.rdb.Get(ctx, pkgredis.MakeProbedCountKeyInScheduler(hostID)).Uint64()
}

// ProbedAt is the time when the host was last probed.
func (nt *networkTopology) ProbedAt(hostID string) (time.Time, error) {
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
defer cancel()

return nt.rdb.Get(ctx, pkgredis.MakeProbedAtKeyInScheduler(hostID)).Time()
}

// Snapshot writes the current network topology to the storage.
func (nt *networkTopology) Snapshot() error {
ctx, cancel := context.WithTimeout(context.Background(), snapshotContextTimeout)
Expand Down
66 changes: 6 additions & 60 deletions scheduler/networktopology/network_topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,16 +481,15 @@ func TestNetworkTopology_DeleteHost(t *testing.T) {
}{
{
name: "delete host",
deleteKeys: []string{pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID),
pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID),
deleteKeys: []string{pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID),
pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID),
pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)},
mock: func(mockRDBClient redismock.ClientMock, keys []string) {
mockRDBClient.MatchExpectationsInOrder(true)
mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockHost.ID, "*"), math.MaxInt64).SetVal([]string{}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), math.MaxInt64).SetVal([]string{keys[2]}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), math.MaxInt64).SetVal([]string{keys[1]}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler(mockHost.ID, "*"), math.MaxInt64).SetVal([]string{}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler("*", mockHost.ID), math.MaxInt64).SetVal([]string{keys[3]}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler("*", mockHost.ID), math.MaxInt64).SetVal([]string{keys[2]}, 0)
mockRDBClient.ExpectDel(keys...).SetVal(4)
},
expect: func(t *testing.T, networkTopology NetworkTopology, err error) {
Expand Down Expand Up @@ -561,16 +560,15 @@ func TestNetworkTopology_DeleteHost(t *testing.T) {
},
{
name: "delete network topology and probes error",
deleteKeys: []string{pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID),
pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID),
deleteKeys: []string{pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID),
pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID),
pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)},
mock: func(mockRDBClient redismock.ClientMock, keys []string) {
mockRDBClient.MatchExpectationsInOrder(true)
mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler(mockHost.ID, "*"), math.MaxInt64).SetVal([]string{}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), math.MaxInt64).SetVal([]string{keys[2]}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeNetworkTopologyKeyInScheduler("*", mockHost.ID), math.MaxInt64).SetVal([]string{keys[1]}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler(mockHost.ID, "*"), math.MaxInt64).SetVal([]string{}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler("*", mockHost.ID), math.MaxInt64).SetVal([]string{keys[3]}, 0)
mockRDBClient.ExpectScan(0, pkgredis.MakeProbesKeyInScheduler("*", mockHost.ID), math.MaxInt64).SetVal([]string{keys[2]}, 0)
mockRDBClient.ExpectDel(keys...).SetErr(errors.New("delete network topology and probes error"))
},
expect: func(t *testing.T, networkTopology NetworkTopology, err error) {
Expand Down Expand Up @@ -686,58 +684,6 @@ func TestNetworkTopology_ProbedCount(t *testing.T) {
}
}

func TestNetworkTopology_ProbedAt(t *testing.T) {
tests := []struct {
name string
mock func(mockRDBClient redismock.ClientMock)
expect func(t *testing.T, networkTopology NetworkTopology, err error)
}{
{
name: "get the time when the host was last probed",
mock: func(mockRDBClient redismock.ClientMock) {
mockRDBClient.ExpectGet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID)).SetVal(mockProbe.CreatedAt.Format(time.RFC3339Nano))
},
expect: func(t *testing.T, networkTopology NetworkTopology, err error) {
assert := assert.New(t)
assert.NoError(err)

probedAt, err := networkTopology.ProbedAt(mockHost.ID)
assert.NoError(err)
assert.True(mockProbe.CreatedAt.Equal(probedAt))
},
},
{
name: "get the time when the host was last probed error",
mock: func(mockRDBClient redismock.ClientMock) {
mockRDBClient.ExpectGet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID)).SetErr(errors.New("get the time when the host was last probed error"))
},
expect: func(t *testing.T, networkTopology NetworkTopology, err error) {
assert := assert.New(t)
assert.NoError(err)

_, err = networkTopology.ProbedAt(mockHost.ID)
assert.EqualError(err, "get the time when the host was last 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()
res := resource.NewMockResource(ctl)
storage := storagemocks.NewMockStorage(ctl)
tc.mock(mockRDBClient)

networkTopology, err := NewNetworkTopology(mockNetworkTopologyConfig, rdb, res, storage)
tc.expect(t, networkTopology, err)
mockRDBClient.ClearExpect()
})
}
}

func TestNetworkTopology_Snapshot(t *testing.T) {
tests := []struct {
name string
Expand Down
4 changes: 0 additions & 4 deletions scheduler/networktopology/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ func (p *probes) Enqueue(probe *Probe) error {
return err
}

if err := p.rdb.Set(ctx, pkgredis.MakeProbedAtKeyInScheduler(p.destHostID), probe.CreatedAt.Format(time.RFC3339Nano), 0).Err(); err != nil {
return err
}

if err := p.rdb.Incr(ctx, pkgredis.MakeProbedCountKeyInScheduler(p.destHostID)).Err(); err != nil {
return err
}
Expand Down
30 changes: 0 additions & 30 deletions scheduler/networktopology/probes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ func TestProbes_Peek(t *testing.T) {
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)
mockRDBClient.ExpectSet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID), mockProbe.CreatedAt.Format(time.RFC3339Nano), 0).SetVal("ok")
mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(6)
mockRDBClient.ExpectLIndex(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), 0).SetVal(rawProbes[0])
},
Expand Down Expand Up @@ -341,7 +340,6 @@ func TestProbes_Enqueue(t *testing.T) {
mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), data).SetVal(1)
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)
mockRDBClient.ExpectSet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID), mockProbe.CreatedAt.Format(time.RFC3339Nano), 0).SetVal("ok")
mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(1)
},
expect: func(t *testing.T, ps Probes) {
Expand Down Expand Up @@ -372,7 +370,6 @@ func TestProbes_Enqueue(t *testing.T) {
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)
mockRDBClient.ExpectSet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID), mockProbe.CreatedAt.Format(time.RFC3339Nano), 0).SetVal("ok")
mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(2)
},
expect: func(t *testing.T, ps Probes) {
Expand Down Expand Up @@ -407,7 +404,6 @@ func TestProbes_Enqueue(t *testing.T) {
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)
mockRDBClient.ExpectSet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID), mockProbe.CreatedAt.Format(time.RFC3339Nano), 0).SetVal("ok")
mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(6)
},
expect: func(t *testing.T, ps Probes) {
Expand Down Expand Up @@ -568,29 +564,6 @@ func TestProbes_Enqueue(t *testing.T) {
assert.EqualError(ps.Enqueue(mockProbe), "update the updated time error")
},
},
{
name: "update the time of the last probe error",
probes: []*Probe{},
mock: func(mockRDBClient redismock.ClientMock, ps []*Probe) {
data, err := json.Marshal(mockProbe)
if err != nil {
t.Fatal(err)
}

mockRDBClient.ExpectLLen(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(0)
mockRDBClient.ExpectRPush(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID), data).SetVal(1)
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)
mockRDBClient.ExpectSet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID), mockProbe.CreatedAt.Format(time.RFC3339Nano), 0).SetErr(
errors.New("update the time of the last probe error"))
},
expect: func(t *testing.T, ps Probes) {
assert := assert.New(t)
assert.EqualError(ps.Enqueue(mockProbe), "update the time of the last probe error")
},
},
{
name: "update the number of times the host has been probed error",
probes: []*Probe{},
Expand All @@ -606,7 +579,6 @@ func TestProbes_Enqueue(t *testing.T) {
"averageRTT", mockProbe.RTT.Nanoseconds()).SetVal(1)
mockRDBClient.ExpectHSet(pkgredis.MakeNetworkTopologyKeyInScheduler(mockSeedHost.ID, mockHost.ID),
"updatedAt", mockProbe.CreatedAt.Format(time.RFC3339Nano)).SetVal(1)
mockRDBClient.ExpectSet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID), mockProbe.CreatedAt.Format(time.RFC3339Nano), 0).SetVal("ok")
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) {
Expand Down Expand Up @@ -678,7 +650,6 @@ func TestProbes_Len(t *testing.T) {
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)
mockRDBClient.ExpectSet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID), mockProbe.CreatedAt.Format(time.RFC3339Nano), 0).SetVal("ok")
mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(6)
mockRDBClient.ExpectLLen(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(5)
},
Expand Down Expand Up @@ -945,7 +916,6 @@ func TestProbes_dequeue(t *testing.T) {
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)
mockRDBClient.ExpectSet(pkgredis.MakeProbedAtKeyInScheduler(mockHost.ID), mockProbe.CreatedAt.Format(time.RFC3339Nano), 0).SetVal("ok")
mockRDBClient.ExpectIncr(pkgredis.MakeProbedCountKeyInScheduler(mockHost.ID)).SetVal(6)
mockRDBClient.ExpectLPop(pkgredis.MakeProbesKeyInScheduler(mockSeedHost.ID, mockHost.ID)).SetVal(string(rawProbes[0]))
},
Expand Down

0 comments on commit a45fcd9

Please sign in to comment.