Repository: dragonflyoss/Dragonfly2
Pull request #1614 (2022-08-30 10:19:21)

feat: resolver addr add ServerName
Signed-off-by: Gaius <gaius.qi@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description
- Resolver addr add ServerName.
<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#1614](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (da58d86) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/c0048c8c601f4ae733d11c42159657347756e549?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (c0048c8) will **increase** coverage by `0.26%`.
> The diff coverage is `40.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff             @@
##             main    #1614      +/-   ##
==========================================
+ Coverage   41.71%   41.97%   +0.26%     
==========================================
  Files         125      125              
  Lines       11484    11496      +12     
==========================================
+ Hits         4791     4826      +35     
+ Misses       6184     6160      -24     
- Partials      509      510       +1     
```

| Flag | Coverage Δ | |
|---|---|---|
| Object-compatibility-e2etests | `?` | |
| e2etests | `?` | |
| unittests | `41.97% <40.00%> (+0.26%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [scheduler/config/dynconfig.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-c2NoZWR1bGVyL2NvbmZpZy9keW5jb25maWcuZ28=) | `25.47% <0.00%> (-1.00%)` | :arrow_down: |
| [client/config/dynconfig\_local.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2NvbmZpZy9keW5jb25maWdfbG9jYWwuZ28=) | `42.22% <60.00%> (+0.75%)` | :arrow_up: |
| [client/config/dynconfig\_manager.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2NvbmZpZy9keW5jb25maWdfbWFuYWdlci5nbw==) | `64.76% <60.00%> (-0.59%)` | :arrow_down: |
| [...ent/daemon/peer/peertask\_piecetask\_synchronizer.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19zeW5jaHJvbml6ZXIuZ28=) | `49.59% <0.00%> (+3.27%)` | :arrow_up: |
| [client/daemon/peer/peertask\_piecetask\_poller.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1614/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19wb2xsZXIuZ28=) | `46.89% <0.00%> (+15.86%)` | :arrow_up: |


Patch:
diff --git a/client/config/dynconfig_local.go b/client/config/dynconfig_local.go
index 76746c10..70769453 100644
--- a/client/config/dynconfig_local.go
+++ b/client/config/dynconfig_local.go
@@ -18,6 +18,7 @@ package config
 
 import (
 	"errors"
+	"net"
 	"time"
 
 	"google.golang.org/grpc/resolver"
@@ -26,7 +27,6 @@ import (
 
 	logger "d7y.io/dragonfly/v2/internal/dflog"
 	"d7y.io/dragonfly/v2/pkg/reachable"
-	"d7y.io/dragonfly/v2/pkg/slices"
 )
 
 var (
@@ -50,22 +50,31 @@ func newDynconfigLocal(cfg *DaemonOption) (Dynconfig, error) {
 
 // Get the dynamic schedulers resolve addrs.
 func (d *dynconfigLocal) GetResolveSchedulerAddrs() ([]resolver.Address, error) {
-	addrs := []string{}
+	var (
+		addrs        = map[string]bool{}
+		resolveAddrs = []resolver.Address{}
+	)
 	for _, schedulerAddr := range d.config.Scheduler.NetAddrs {
-		r := reachable.New(&reachable.Config{Address: schedulerAddr.Addr})
+		addr := schedulerAddr.Addr
+		r := reachable.New(&reachable.Config{Address: addr})
 		if err := r.Check(); err != nil {
-			logger.Warnf("scheduler address %s is unreachable", schedulerAddr.Addr)
+			logger.Warnf("scheduler address %s is unreachable", addr)
 		} else {
-			addrs = append(addrs, schedulerAddr.Addr)
-			continue
-		}
-	}
+			if addrs[addr] {
+				continue
+			}
 
-	resolveAddrs := []resolver.Address{}
-	for _, addr := range slices.RemoveDuplicates(addrs) {
-		resolveAddrs = append(resolveAddrs, resolver.Address{
-			Addr: addr,
-		})
+			host, _, err := net.SplitHostPort(addr)
+			if err != nil {
+				continue
+			}
+
+			resolveAddrs = append(resolveAddrs, resolver.Address{
+				ServerName: host,
+				Addr:       addr,
+			})
+			addrs[addr] = true
+		}
 	}
 
 	return resolveAddrs, nil
diff --git a/client/config/dynconfig_manager.go b/client/config/dynconfig_manager.go
index 067efc1e..053eaec8 100644
--- a/client/config/dynconfig_manager.go
+++ b/client/config/dynconfig_manager.go
@@ -34,7 +34,6 @@ import (
 	"d7y.io/dragonfly/v2/manager/searcher"
 	"d7y.io/dragonfly/v2/pkg/reachable"
 	managerclient "d7y.io/dragonfly/v2/pkg/rpc/manager/client"
-	"d7y.io/dragonfly/v2/pkg/slices"
 )
 
 // Daemon cache file name.
@@ -74,23 +73,26 @@ func (d *dynconfigManager) GetResolveSchedulerAddrs() ([]resolver.Address, error
 		return nil, err
 	}
 
-	addrs := []string{}
+	var (
+		addrs        = map[string]bool{}
+		resolveAddrs = []resolver.Address{}
+	)
 	for _, scheduler := range schedulers {
 		addr := fmt.Sprintf("%s:%d", scheduler.GetIp(), scheduler.GetPort())
 		r := reachable.New(&reachable.Config{Address: addr})
 		if err := r.Check(); err != nil {
 			logger.Warnf("scheduler address %s is unreachable", addr)
 		} else {
-			addrs = append(addrs, addr)
-			continue
-		}
-	}
+			if addrs[addr] {
+				continue
+			}
 
-	resolveAddrs := []resolver.Address{}
-	for _, addr := range slices.RemoveDuplicates(addrs) {
-		resolveAddrs = append(resolveAddrs, resolver.Address{
-			Addr: addr,
-		})
+			resolveAddrs = append(resolveAddrs, resolver.Address{
+				ServerName: scheduler.GetIp(),
+				Addr:       addr,
+			})
+			addrs[addr] = true
+		}
 	}
 
 	return resolveAddrs, nil
diff --git a/scheduler/config/dynconfig.go b/scheduler/config/dynconfig.go
index 5533fc29..8ad125d5 100644
--- a/scheduler/config/dynconfig.go
+++ b/scheduler/config/dynconfig.go
@@ -35,7 +35,6 @@ import (
 	"d7y.io/dragonfly/v2/manager/types"
 	"d7y.io/dragonfly/v2/pkg/reachable"
 	managerclient "d7y.io/dragonfly/v2/pkg/rpc/manager/client"
-	"d7y.io/dragonfly/v2/pkg/slices"
 )
 
 var (
@@ -177,23 +176,26 @@ func (d *dynconfig) GetResolveSeedPeerAddrs() ([]resolver.Address, error) {
 		return nil, err
 	}
 
-	addrs := []string{}
+	var (
+		addrs        = map[string]bool{}
+		resolveAddrs = []resolver.Address{}
+	)
 	for _, seedPeer := range seedPeers {
 		addr := fmt.Sprintf("%s:%d", seedPeer.IP, seedPeer.Port)
 		r := reachable.New(&reachable.Config{Address: addr})
 		if err := r.Check(); err != nil {
 			logger.Warnf("seed peer address %s is unreachable", addr)
 		} else {
-			addrs = append(addrs, addr)
-			continue
-		}
-	}
+			if addrs[addr] {
+				continue
+			}
 
-	resolveAddrs := []resolver.Address{}
-	for _, addr := range slices.RemoveDuplicates(addrs) {
-		resolveAddrs = append(resolveAddrs, resolver.Address{
-			Addr: addr,
-		})
+			resolveAddrs = append(resolveAddrs, resolver.Address{
+				ServerName: seedPeer.IP,
+				Addr:       addr,
+			})
+			addrs[addr] = true
+		}
 	}
 
 	return resolveAddrs, nil


Test patch:
diff --git a/client/config/dynconfig_local_test.go b/client/config/dynconfig_local_test.go
index 374480a2..217265fb 100644
--- a/client/config/dynconfig_local_test.go
+++ b/client/config/dynconfig_local_test.go
@@ -53,7 +53,7 @@ func TestDynconfigGetResolveSchedulerAddrs_LocalSourceType(t *testing.T) {
 				assert := assert.New(t)
 				result, err := dynconfig.GetResolveSchedulerAddrs()
 				assert.NoError(err)
-				assert.EqualValues(result, []resolver.Address{{Addr: "127.0.0.1:3000"}})
+				assert.EqualValues(result, []resolver.Address{{ServerName: "127.0.0.1", Addr: "127.0.0.1:3000"}})
 			},
 		},
 		{
@@ -92,7 +92,7 @@ func TestDynconfigGetResolveSchedulerAddrs_LocalSourceType(t *testing.T) {
 				assert := assert.New(t)
 				result, err := dynconfig.GetResolveSchedulerAddrs()
 				assert.NoError(err)
-				assert.EqualValues(result, []resolver.Address{{Addr: "127.0.0.1:3000"}})
+				assert.EqualValues(result, []resolver.Address{{ServerName: "127.0.0.1", Addr: "127.0.0.1:3000"}})
 			},
 		},
 	}
diff --git a/client/config/dynconfig_manager_test.go b/client/config/dynconfig_manager_test.go
index 791fb7e5..c2738d03 100644
--- a/client/config/dynconfig_manager_test.go
+++ b/client/config/dynconfig_manager_test.go
@@ -93,7 +93,7 @@ func TestDynconfigGetResolveSchedulerAddrs_ManagerSourceType(t *testing.T) {
 				assert := assert.New(t)
 				result, err := dynconfig.GetResolveSchedulerAddrs()
 				assert.NoError(err)
-				assert.EqualValues(result, []resolver.Address{{Addr: "127.0.0.1:3000"}})
+				assert.EqualValues(result, []resolver.Address{{ServerName: "127.0.0.1", Addr: "127.0.0.1:3000"}})
 			},
 		},
 		{
@@ -139,7 +139,7 @@ func TestDynconfigGetResolveSchedulerAddrs_ManagerSourceType(t *testing.T) {
 				assert := assert.New(t)
 				result, err := dynconfig.GetResolveSchedulerAddrs()
 				assert.NoError(err)
-				assert.EqualValues(result, []resolver.Address{{Addr: "127.0.0.1:3000"}})
+				assert.EqualValues(result, []resolver.Address{{ServerName: "127.0.0.1", Addr: "127.0.0.1:3000"}})
 			},
 		},
 		{
@@ -235,7 +235,7 @@ func TestDynconfigGetResolveSchedulerAddrs_ManagerSourceType(t *testing.T) {
 				assert := assert.New(t)
 				result, err := dynconfig.GetResolveSchedulerAddrs()
 				assert.NoError(err)
-				assert.EqualValues(result, []resolver.Address{{Addr: "127.0.0.1:3000"}})
+				assert.EqualValues(result, []resolver.Address{{ServerName: "127.0.0.1", Addr: "127.0.0.1:3000"}})
 			},
 		},
 		{
@@ -280,7 +280,7 @@ func TestDynconfigGetResolveSchedulerAddrs_ManagerSourceType(t *testing.T) {
 				assert := assert.New(t)
 				result, err := dynconfig.GetResolveSchedulerAddrs()
 				assert.NoError(err)
-				assert.EqualValues(result, []resolver.Address{{Addr: "127.0.0.1:3000"}})
+				assert.EqualValues(result, []resolver.Address{{ServerName: "127.0.0.1", Addr: "127.0.0.1:3000"}})
 			},
 		},
 		{