diff --git a/build/images/base/README.md b/build/images/base/README.md index 172e5f7970c..0494de04542 100644 --- a/build/images/base/README.md +++ b/build/images/base/README.md @@ -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 -``` \ No newline at end of file +``` + +# 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 \ No newline at end of file diff --git a/client/daemon/daemon.go b/client/daemon/daemon.go index de72f0c0f07..720ca603504 100644 --- a/client/daemon/daemon.go +++ b/client/daemon/daemon.go @@ -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" @@ -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 { @@ -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 @@ -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 } @@ -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, @@ -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 } @@ -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 } @@ -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 { @@ -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() { diff --git a/client/daemon/service/manager.go b/client/daemon/rpcserver/rpcserver.go similarity index 88% rename from client/daemon/service/manager.go rename to client/daemon/rpcserver/rpcserver.go index 345c3ea1c5d..81c8e792f13 100644 --- a/client/daemon/service/manager.go +++ b/client/daemon/rpcserver/rpcserver.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package service +package rpcserver import ( "context" @@ -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 @@ -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, @@ -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 { @@ -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 diff --git a/client/daemon/service/manager_test.go b/client/daemon/rpcserver/rpcserver_test.go similarity index 99% rename from client/daemon/service/manager_test.go rename to client/daemon/rpcserver/rpcserver_test.go index 7bda72f0573..931c9c6a597 100644 --- a/client/daemon/service/manager_test.go +++ b/client/daemon/rpcserver/rpcserver_test.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package service +package rpcserver import ( "context" @@ -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, @@ -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, diff --git a/hack/build.sh b/hack/build.sh index 4c1e81f582b..ff56684a380 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -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') @@ -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"