Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add idgen peer id #800

Merged
merged 4 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions cdn/cdnutil/cdn_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 0 additions & 31 deletions cdn/cdnutil/cdn_util_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions cdn/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion client/daemon/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions client/daemon/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 8 additions & 6 deletions client/clientutil/peer.go → internal/idgen/peer_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
103 changes: 103 additions & 0 deletions internal/idgen/peer_id_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
1 change: 0 additions & 1 deletion pkg/rpc/scheduler/scheduler.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ message PeerTaskRequest{
}

message RegisterResult{

// task id
string task_id = 2;
// file content length scope for the url
Expand Down