Repository: dragonflyoss/Dragonfly2
Pull request #800 (2021-11-15 10:12:43)

feat: add idgen peer id
<!--- Provide a general summary of your changes in the Title above -->

## Description
- Generate peer id and cdn peer id func.
<!--- 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! -->

- [x] Code compiles correctly.
- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.



Patch:
diff --git a/cdn/cdnutil/cdn_util.go b/cdn/cdnutil/cdn_util.go
index 3112a129..57ab262f 100644
--- a/cdn/cdnutil/cdn_util.go
+++ b/cdn/cdnutil/cdn_util.go
@@ -17,16 +17,9 @@
 package cdnutil
 
 import (
-	"fmt"
-
 	"d7y.io/dragonfly/v2/cdn/config"
-	"d7y.io/dragonfly/v2/pkg/util/net/iputils"
 )
 
-func GenCDNPeerID(taskID string) string {
-	return fmt.Sprintf("%s-%s_%s", iputils.HostName, taskID, "CDN")
-}
-
 // ComputePieceSize computes the piece size with specified fileLength.
 //
 // If the fileLength<=0, which means failed to get fileLength
diff --git a/cdn/rpcserver/rpcserver.go b/cdn/rpcserver/rpcserver.go
index ae2d328c..82201ce2 100644
--- a/cdn/rpcserver/rpcserver.go
+++ b/cdn/rpcserver/rpcserver.go
@@ -26,7 +26,6 @@ import (
 	"go.opentelemetry.io/otel/trace"
 	"google.golang.org/grpc"
 
-	"d7y.io/dragonfly/v2/cdn/cdnutil"
 	"d7y.io/dragonfly/v2/cdn/config"
 	cdnerrors "d7y.io/dragonfly/v2/cdn/errors"
 	"d7y.io/dragonfly/v2/cdn/supervisor"
@@ -137,7 +136,7 @@ func (css *server) ObtainSeeds(ctx context.Context, req *cdnsystem.SeedRequest,
 		span.RecordError(err)
 		return err
 	}
-	peerID := cdnutil.GenCDNPeerID(req.TaskId)
+	peerID := idgen.CDNPeerID(css.cfg.AdvertiseIP)
 	for piece := range pieceChan {
 		psc <- &cdnsystem.PieceSeed{
 			PeerId:   peerID,
diff --git a/client/daemon/rpcserver/rpcserver.go b/client/daemon/rpcserver/rpcserver.go
index 072af247..1d899312 100644
--- a/client/daemon/rpcserver/rpcserver.go
+++ b/client/daemon/rpcserver/rpcserver.go
@@ -33,6 +33,7 @@ import (
 	"d7y.io/dragonfly/v2/internal/dfcodes"
 	"d7y.io/dragonfly/v2/internal/dferrors"
 	logger "d7y.io/dragonfly/v2/internal/dflog"
+	"d7y.io/dragonfly/v2/internal/idgen"
 	"d7y.io/dragonfly/v2/pkg/rpc/base"
 	dfdaemongrpc "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon"
 	dfdaemonserver "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/server"
@@ -137,7 +138,7 @@ func (m *server) Download(ctx context.Context,
 		PeerTaskRequest: scheduler.PeerTaskRequest{
 			Url:      req.Url,
 			UrlMeta:  req.UrlMeta,
-			PeerId:   clientutil.GenPeerID(m.peerHost),
+			PeerId:   idgen.PeerID(m.peerHost.Ip),
 			PeerHost: m.peerHost,
 		},
 		Output:            req.Output,
diff --git a/client/daemon/transport/transport.go b/client/daemon/transport/transport.go
index 323bd74c..800398b7 100644
--- a/client/daemon/transport/transport.go
+++ b/client/daemon/transport/transport.go
@@ -28,10 +28,10 @@ import (
 
 	"github.com/go-http-utils/headers"
 
-	"d7y.io/dragonfly/v2/client/clientutil"
 	"d7y.io/dragonfly/v2/client/config"
 	"d7y.io/dragonfly/v2/client/daemon/peer"
 	logger "d7y.io/dragonfly/v2/internal/dflog"
+	"d7y.io/dragonfly/v2/internal/idgen"
 	"d7y.io/dragonfly/v2/pkg/rpc/base"
 	"d7y.io/dragonfly/v2/pkg/rpc/scheduler"
 	"d7y.io/dragonfly/v2/pkg/util/net/httputils"
@@ -174,7 +174,7 @@ func NeedUseDragonfly(req *http.Request) bool {
 // download uses dragonfly to download.
 func (rt *transport) download(req *http.Request) (*http.Response, error) {
 	url := req.URL.String()
-	peerID := clientutil.GenPeerID(rt.peerHost)
+	peerID := idgen.PeerID(rt.peerHost.Ip)
 	log := logger.With("peer", peerID, "component", "transport")
 	log.Infof("start download with url: %s", url)
 
diff --git a/client/clientutil/peer.go b/internal/idgen/peer_id.go
similarity index 74%
rename from client/clientutil/peer.go
rename to internal/idgen/peer_id.go
index 8d0749f0..42813f7e 100644
--- a/client/clientutil/peer.go
+++ b/internal/idgen/peer_id.go
@@ -14,17 +14,19 @@
  * limitations under the License.
  */
 
-package clientutil
+package idgen
 
 import (
 	"fmt"
 	"os"
 
-	"github.com/pborman/uuid"
-
-	"d7y.io/dragonfly/v2/pkg/rpc/scheduler"
+	"github.com/google/uuid"
 )
 
-func GenPeerID(peerHost *scheduler.PeerHost) string {
-	return fmt.Sprintf("%s-%d-%s", peerHost.Ip, os.Getpid(), uuid.New())
+func CDNPeerID(ip string) string {
+	return fmt.Sprintf("%s-%s", PeerID(ip), "cdn")
+}
+
+func PeerID(ip string) string {
+	return fmt.Sprintf("%s-%d-%s", ip, os.Getpid(), uuid.New())
 }
diff --git a/pkg/rpc/scheduler/scheduler.proto b/pkg/rpc/scheduler/scheduler.proto
index 287a1fe4..395318ef 100644
--- a/pkg/rpc/scheduler/scheduler.proto
+++ b/pkg/rpc/scheduler/scheduler.proto
@@ -39,7 +39,6 @@ message PeerTaskRequest{
 }
 
 message RegisterResult{
-
   // task id
   string task_id = 2;
   // file content length scope for the url


Test patch:
diff --git a/cdn/cdnutil/cdn_util_test.go b/cdn/cdnutil/cdn_util_test.go
deleted file mode 100644
index e1ff8ae1..00000000
--- a/cdn/cdnutil/cdn_util_test.go
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- *     Copyright 2020 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 cdnutil
-
-import (
-	"fmt"
-	"testing"
-
-	"d7y.io/dragonfly/v2/pkg/util/net/iputils"
-)
-
-func TestGenCdnPeerID(t *testing.T) {
-	var taskID = "123456"
-	if got, want := GenCDNPeerID(taskID), fmt.Sprint(iputils.HostName, "-", taskID, "_CDN"); got != want {
-		t.Errorf("GenCdnPeerID() = %v, want %v", got, want)
-	}
-}
diff --git a/internal/idgen/peer_id_test.go b/internal/idgen/peer_id_test.go
new file mode 100644
index 00000000..6d6d7002
--- /dev/null
+++ b/internal/idgen/peer_id_test.go
@@ -0,0 +1,103 @@
+/*
+ *     Copyright 2020 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 idgen
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestPeerID(t *testing.T) {
+	tests := []struct {
+		name   string
+		ip     string
+		expect func(t *testing.T, d interface{})
+	}{
+		{
+			name: "generate PeerID with ipv4",
+			ip:   "127.0.0.1",
+			expect: func(t *testing.T, d interface{}) {
+				assert := assert.New(t)
+				assert.Len(d, 52)
+			},
+		},
+		{
+			name: "generate PeerID with ipv6",
+			ip:   "2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b",
+			expect: func(t *testing.T, d interface{}) {
+				assert := assert.New(t)
+				assert.Len(d, 82)
+			},
+		},
+		{
+			name: "generate PeerID with empty string",
+			ip:   "",
+			expect: func(t *testing.T, d interface{}) {
+				assert := assert.New(t)
+				assert.Len(d, 43)
+			},
+		},
+	}
+
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			data := PeerID(tc.ip)
+			tc.expect(t, data)
+		})
+	}
+}
+
+func TestCDNPeerID(t *testing.T) {
+	tests := []struct {
+		name   string
+		ip     string
+		expect func(t *testing.T, d interface{})
+	}{
+		{
+			name: "generate id with ipv4",
+			ip:   "127.0.0.1",
+			expect: func(t *testing.T, d interface{}) {
+				assert := assert.New(t)
+				assert.Len(d, 56)
+			},
+		},
+		{
+			name: "generate id with ipv6",
+			ip:   "2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b",
+			expect: func(t *testing.T, d interface{}) {
+				assert := assert.New(t)
+				assert.Len(d, 86)
+			},
+		},
+		{
+			name: "generate id with empty string",
+			ip:   "",
+			expect: func(t *testing.T, d interface{}) {
+				assert := assert.New(t)
+				assert.Len(d, 47)
+			},
+		},
+	}
+
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			data := CDNPeerID(tc.ip)
+			tc.expect(t, data)
+		})
+	}
+}