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

Optimize client rpc package name and other docs #541

Merged
merged 1 commit into from
Aug 12, 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
26 changes: 25 additions & 1 deletion build/images/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,28 @@ export GOGCFLAGS="all=-N -l"
export BASE_IMAGE=dragonflyoss/base:bpftrace-v0.13.0-go-v1.16.6

make docker-build
```
```

# Debug With Delve

## Prepare Code for Debug

```shell
COMMIT_ID=c1c3d652
mkdir -p /go/src/d7y.io/dragonfly
git clone /~https://github.com/dragonflyoss/Dragonfly2.git /go/src/d7y.io/dragonfly/v2
git reset --hard ${COMMIT_ID}
```

## Debug Operations

1. Attach Process

```shell
pid=$(pidof scheduler) # or dfget, cdn, manager
dlv attach $pid
```

2. Debug

Follow /~https://github.com/go-delve/delve/tree/v1.7.0/Documentation/cli
24 changes: 12 additions & 12 deletions client/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import (
"sync"
"time"

"d7y.io/dragonfly/v2/internal/dfpath"
"d7y.io/dragonfly/v2/internal/idgen"
"d7y.io/dragonfly/v2/pkg/util/net/iputils"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"golang.org/x/sync/errgroup"
"golang.org/x/time/rate"
"google.golang.org/grpc"
Expand All @@ -43,15 +41,17 @@ import (
"d7y.io/dragonfly/v2/client/daemon/gc"
"d7y.io/dragonfly/v2/client/daemon/peer"
"d7y.io/dragonfly/v2/client/daemon/proxy"
"d7y.io/dragonfly/v2/client/daemon/service"
"d7y.io/dragonfly/v2/client/daemon/rpcserver"
"d7y.io/dragonfly/v2/client/daemon/storage"
"d7y.io/dragonfly/v2/client/daemon/upload"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/internal/dfpath"
"d7y.io/dragonfly/v2/internal/idgen"
"d7y.io/dragonfly/v2/pkg/basic/dfnet"
"d7y.io/dragonfly/v2/pkg/rpc"
"d7y.io/dragonfly/v2/pkg/rpc/scheduler"
schedulerclient "d7y.io/dragonfly/v2/pkg/rpc/scheduler/client"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"d7y.io/dragonfly/v2/pkg/util/net/iputils"
)

type Daemon interface {
Expand All @@ -72,7 +72,7 @@ type clientDaemon struct {

Option config.DaemonOption

ServiceManager service.Manager
RPCManager rpcserver.Server
UploadManager upload.Manager
ProxyManager proxy.Manager
StorageManager storage.Manager
Expand Down Expand Up @@ -154,7 +154,7 @@ func New(opt *config.DaemonOption) (Daemon, error) {
}
peerServerOption = append(peerServerOption, grpc.Creds(tlsCredentials))
}
serviceManager, err := service.NewManager(host, peerTaskManager, storageManager, downloadServerOption, peerServerOption)
rpcManager, err := rpcserver.NewServer(host, peerTaskManager, storageManager, downloadServerOption, peerServerOption)
if err != nil {
return nil, err
}
Expand All @@ -177,7 +177,7 @@ func New(opt *config.DaemonOption) (Daemon, error) {
schedPeerHost: host,
Option: *opt,

ServiceManager: serviceManager,
RPCManager: rpcManager,
PeerTaskManager: peerTaskManager,
PieceManager: pieceManager,
ProxyManager: proxyManager,
Expand Down Expand Up @@ -327,7 +327,7 @@ func (cd *clientDaemon) Serve() error {
g.Go(func() error {
defer downloadListener.Close()
logger.Infof("serve download grpc at unix://%s", cd.Option.Download.DownloadGRPC.UnixListen.Socket)
if err := cd.ServiceManager.ServeDownload(downloadListener); err != nil {
if err := cd.RPCManager.ServeDownload(downloadListener); err != nil {
logger.Errorf("failed to serve for download grpc service: %v", err)
return err
}
Expand All @@ -338,7 +338,7 @@ func (cd *clientDaemon) Serve() error {
g.Go(func() error {
defer peerListener.Close()
logger.Infof("serve peer grpc at %s://%s", peerListener.Addr().Network(), peerListener.Addr().String())
if err := cd.ServiceManager.ServePeer(peerListener); err != nil {
if err := cd.RPCManager.ServePeer(peerListener); err != nil {
logger.Errorf("failed to serve for peer grpc service: %v", err)
return err
}
Expand Down Expand Up @@ -388,7 +388,7 @@ func (cd *clientDaemon) Serve() error {
case <-time.After(cd.Option.AliveTime.Duration):
var keepalives = []clientutil.KeepAlive{
cd.StorageManager,
cd.ServiceManager,
cd.RPCManager,
}
var keep bool
for _, keepalive := range keepalives {
Expand Down Expand Up @@ -416,7 +416,7 @@ func (cd *clientDaemon) Stop() {
cd.once.Do(func() {
close(cd.done)
cd.GCManager.Stop()
cd.ServiceManager.Stop()
cd.RPCManager.Stop()
cd.UploadManager.Stop()

if cd.ProxyManager.IsEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package service
package rpcserver

import (
"context"
Expand All @@ -40,14 +40,14 @@ import (
"d7y.io/dragonfly/v2/pkg/rpc/scheduler"
)

type Manager interface {
type Server interface {
clientutil.KeepAlive
ServeDownload(listener net.Listener) error
ServePeer(listener net.Listener) error
Stop()
}

type manager struct {
type server struct {
clientutil.KeepAlive
peerHost *scheduler.PeerHost
peerTaskManager peer.TaskManager
Expand All @@ -58,12 +58,12 @@ type manager struct {
uploadAddr string
}

var _ dfdaemonserver.DaemonServer = (*manager)(nil)
var _ Manager = (*manager)(nil)
var _ dfdaemonserver.DaemonServer = (*server)(nil)
var _ Server = (*server)(nil)

func NewManager(peerHost *scheduler.PeerHost, peerTaskManager peer.TaskManager, storageManager storage.Manager, downloadOpts []grpc.ServerOption, peerOpts []grpc.ServerOption) (Manager, error) {
mgr := &manager{
KeepAlive: clientutil.NewKeepAlive("service manager"),
func NewServer(peerHost *scheduler.PeerHost, peerTaskManager peer.TaskManager, storageManager storage.Manager, downloadOpts []grpc.ServerOption, peerOpts []grpc.ServerOption) (Server, error) {
mgr := &server{
KeepAlive: clientutil.NewKeepAlive("rpc server"),
peerHost: peerHost,
peerTaskManager: peerTaskManager,
storageManager: storageManager,
Expand All @@ -73,21 +73,21 @@ func NewManager(peerHost *scheduler.PeerHost, peerTaskManager peer.TaskManager,
return mgr, nil
}

func (m *manager) ServeDownload(listener net.Listener) error {
func (m *server) ServeDownload(listener net.Listener) error {
return m.downloadServer.Serve(listener)
}

func (m *manager) ServePeer(listener net.Listener) error {
func (m *server) ServePeer(listener net.Listener) error {
m.uploadAddr = fmt.Sprintf("%s:%d", m.peerHost.Ip, m.peerHost.DownPort)
return m.peerServer.Serve(listener)
}

func (m *manager) Stop() {
func (m *server) Stop() {
m.peerServer.GracefulStop()
m.downloadServer.GracefulStop()
}

func (m *manager) GetPieceTasks(ctx context.Context, request *base.PieceTaskRequest) (*base.PiecePacket, error) {
func (m *server) GetPieceTasks(ctx context.Context, request *base.PieceTaskRequest) (*base.PiecePacket, error) {
m.Keep()
p, err := m.storageManager.GetPieces(ctx, request)
if err != nil {
Expand Down Expand Up @@ -128,12 +128,12 @@ func (m *manager) GetPieceTasks(ctx context.Context, request *base.PieceTaskRequ
return p, nil
}

func (m *manager) CheckHealth(context.Context) error {
func (m *server) CheckHealth(context.Context) error {
m.Keep()
return nil
}

func (m *manager) Download(ctx context.Context,
func (m *server) Download(ctx context.Context,
req *dfdaemongrpc.DownRequest, results chan<- *dfdaemongrpc.DownResult) error {
m.Keep()
// init peer task request, peer uses different peer id to generate every request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package service
package rpcserver

import (
"context"
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestDownloadManager_ServeDownload(t *testing.T) {
}()
return ch, false, nil
})
m := &manager{
m := &server{
KeepAlive: clientutil.NewKeepAlive("test"),
peerHost: &scheduler.PeerHost{},
peerTaskManager: mockPeerTaskManager,
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestDownloadManager_ServePeer(t *testing.T) {
PieceMd5Sign: "",
}, nil
})
m := &manager{
m := &server{
KeepAlive: clientutil.NewKeepAlive("test"),
peerHost: &scheduler.PeerHost{},
storageManager: mockStorageManger,
Expand Down
4 changes: 3 additions & 1 deletion hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SCHEDULER_BINARY_NAME=scheduler
MANAGER_BINARY_NAME=manager

PKG=d7y.io/dragonfly/v2
BUILD_IMAGE=golang:1.15.8
BUILD_IMAGE=golang:1.16.6-alpine

VERSION=$(git rev-parse --short HEAD)
BUILD_TIME=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
Expand Down Expand Up @@ -78,6 +78,8 @@ build-docker() {
-e CGO_ENABLED=0 \
-e GO111MODULE=on \
-e GOPROXY="${GOPROXY}" \
-e GOTAGS="${GOTAGS}" \
-e GOGCFLAGS="${GOGCFLAGS}" \
-w /go/src/${PKG} \
${BUILD_IMAGE} \
go build -o "/go/bin/$1" ./cmd/"$2"
Expand Down