Repository: dragonflyoss/Dragonfly2
Pull request #1382 (2022-06-14 07:38:00)

feat: add EnablePeerGauge config to manager
Signed-off-by: Gaius <gaius.qi@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description
- Add `EnablePeerGauge` config to manager.
<!--- 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.



Patch:
diff --git a/cmd/dfget/cmd/daemon.go b/cmd/dfget/cmd/daemon.go
index c7c04abd..02f0b003 100644
--- a/cmd/dfget/cmd/daemon.go
+++ b/cmd/dfget/cmd/daemon.go
@@ -26,6 +26,7 @@ import (
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
+	"golang.org/x/sys/unix"
 	"gopkg.in/yaml.v3"
 
 	"d7y.io/dragonfly/v2/client/config"
@@ -91,13 +92,13 @@ func redirectStdoutAndStderr(console bool, logDir string) {
 	stdoutPath := path.Join(logDir, "daemon", "stdout.log")
 	if stdout, err := os.OpenFile(stdoutPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND|os.O_SYNC, 0644); err != nil {
 		logger.Warnf("open %s error: %s", stdoutPath, err)
-	} else if err := dup2(int(stdout.Fd()), 1); err != nil {
+	} else if err := unix.Dup2(int(stdout.Fd()), int(os.Stdout.Fd())); err != nil {
 		logger.Warnf("redirect stdout error: %s", err)
 	}
 	stderrPath := path.Join(logDir, "daemon", "stderr.log")
 	if stderr, err := os.OpenFile(stderrPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND|os.O_SYNC, 0644); err != nil {
 		logger.Warnf("open %s error: %s", stderrPath, err)
-	} else if err := dup2(int(stderr.Fd()), 2); err != nil {
+	} else if err := unix.Dup2(int(stderr.Fd()), int(os.Stderr.Fd())); err != nil {
 		logger.Warnf("redirect stderr error: %s", err)
 	}
 }
diff --git a/cmd/dfget/cmd/daemon_dup2.go b/cmd/dfget/cmd/daemon_dup2.go
deleted file mode 100644
index 22124703..00000000
--- a/cmd/dfget/cmd/daemon_dup2.go
+++ /dev/null
@@ -1,26 +0,0 @@
-//go:build !windows && !arm && !arm64
-// +build !windows,!arm,!arm64
-
-/*
- *     Copyright 2022 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 cmd
-
-import "syscall"
-
-func dup2(from int, to int) (err error) {
-	return syscall.Dup2(from, to)
-}
diff --git a/cmd/dfget/cmd/daemon_dup3.go b/cmd/dfget/cmd/daemon_dup3.go
deleted file mode 100644
index ed261643..00000000
--- a/cmd/dfget/cmd/daemon_dup3.go
+++ /dev/null
@@ -1,29 +0,0 @@
-//go:build !windows && (arm || arm64)
-// +build !windows
-// +build arm arm64
-
-/*
- *     Copyright 2022 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 cmd
-
-import "syscall"
-
-func dup2(from int, to int) (err error) {
-	// linux_arm64 platform doesn't have syscall.Dup2
-	// so use the nearly identical syscall.Dup3 instead.
-	return syscall.Dup3(from, to, 0)
-}
diff --git a/go.mod b/go.mod
index 0a3e24ac..0a55444f 100644
--- a/go.mod
+++ b/go.mod
@@ -72,7 +72,7 @@ require (
 	golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
 	golang.org/x/oauth2 v0.0.0-20220524215830-622c5d57e401
 	golang.org/x/sync v0.0.0-20220513210516-0976fa681c29
-	golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
+	golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d
 	golang.org/x/time v0.0.0-20220411224347-583f2d630306
 	google.golang.org/api v0.81.0
 	google.golang.org/grpc v1.46.2
diff --git a/go.sum b/go.sum
index f6d68d51..6189bd62 100644
--- a/go.sum
+++ b/go.sum
@@ -1536,8 +1536,9 @@ golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBc
 golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
 golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d h1:Zu/JngovGLVi6t2J3nmAf3AoTDwuzw85YZ3b9o4yU7s=
+golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
diff --git a/manager/config/config.go b/manager/config/config.go
index 62dc939b..4658d621 100644
--- a/manager/config/config.go
+++ b/manager/config/config.go
@@ -172,6 +172,9 @@ type MetricsConfig struct {
 
 	// Metrics service address.
 	Addr string `yaml:"addr" mapstructure:"addr"`
+
+	// Enable peer gauge metrics.
+	EnablePeerGauge bool `yaml:"enablePeerGauge" mapstructure:"enablePeerGauge"`
 }
 
 type TCPListenConfig struct {
@@ -246,7 +249,8 @@ func New() *Config {
 			Enable: false,
 		},
 		Metrics: &MetricsConfig{
-			Enable: false,
+			Enable:          false,
+			EnablePeerGauge: true,
 		},
 	}
 }
@@ -377,7 +381,11 @@ func (cfg *Config) Validate() error {
 		}
 	}
 
-	if cfg.Metrics != nil && cfg.Metrics.Enable {
+	if cfg.Metrics == nil {
+		return errors.New("config requires parameter metrics")
+	}
+
+	if cfg.Metrics.Enable {
 		if cfg.Metrics.Addr == "" {
 			return errors.New("metrics requires parameter addr")
 		}
diff --git a/manager/manager.go b/manager/manager.go
index c8678b83..0955bec3 100644
--- a/manager/manager.go
+++ b/manager/manager.go
@@ -130,7 +130,7 @@ func New(cfg *config.Config, d dfpath.Dfpath) (*Server, error) {
 			grpc.ChainStreamInterceptor(otelgrpc.StreamServerInterceptor()),
 		}
 	}
-	grpcServer := rpcserver.New(db, cache, searcher, objectStorage, cfg.ObjectStorage, grpcOptions...)
+	grpcServer := rpcserver.New(cfg, db, cache, searcher, objectStorage, cfg.ObjectStorage, grpcOptions...)
 	s.grpcServer = grpcServer
 
 	// Initialize prometheus
diff --git a/manager/rpcserver/rpcserver.go b/manager/rpcserver/rpcserver.go
index 548ed531..cae78b43 100644
--- a/manager/rpcserver/rpcserver.go
+++ b/manager/rpcserver/rpcserver.go
@@ -69,6 +69,8 @@ func defaultUnaryMiddleWares() []grpc.UnaryServerInterceptor {
 
 // Server is grpc server.
 type Server struct {
+	// Manager configuration.
+	config *config.Config
 	// GORM instance.
 	db *gorm.DB
 	// Redis client instance.
@@ -87,10 +89,11 @@ type Server struct {
 
 // New returns a new manager server from the given options.
 func New(
-	database *database.Database, cache *cache.Cache, searcher searcher.Searcher,
+	cfg *config.Config, database *database.Database, cache *cache.Cache, searcher searcher.Searcher,
 	objectStorage objectstorage.ObjectStorage, objectStorageConfig *config.ObjectStorageConfig, opts ...grpc.ServerOption,
 ) *grpc.Server {
 	server := &Server{
+		config:              cfg,
 		db:                  database.DB,
 		rdb:                 database.RDB,
 		cache:               cache,
@@ -456,7 +459,7 @@ func (s *Server) ListSchedulers(ctx context.Context, req *manager.ListSchedulers
 	log := logger.WithHostnameAndIP(req.HostName, req.Ip)
 
 	// Count the number of the active peer.
-	if req.SourceType == manager.SourceType_PEER_SOURCE {
+	if s.config.Metrics.EnablePeerGauge && req.SourceType == manager.SourceType_PEER_SOURCE {
 		count, err := s.getPeerCount(ctx, req)
 		if err != nil {
 			log.Warnf("get peer count failed: %s", err.Error())


Test patch:
diff --git a/manager/config/config_test.go b/manager/config/config_test.go
index b9ca8a92..c3e13b56 100644
--- a/manager/config/config_test.go
+++ b/manager/config/config_test.go
@@ -86,8 +86,9 @@ func TestManagerConfig_Load(t *testing.T) {
 			SecretKey: "bar",
 		},
 		Metrics: &MetricsConfig{
-			Enable: true,
-			Addr:   ":8000",
+			Enable:          true,
+			Addr:            ":8000",
+			EnablePeerGauge: false,
 		},
 	}
 
diff --git a/manager/config/testdata/manager.yaml b/manager/config/testdata/manager.yaml
index 45f5d1de..eb0ca2b9 100644
--- a/manager/config/testdata/manager.yaml
+++ b/manager/config/testdata/manager.yaml
@@ -47,3 +47,4 @@ objectStorage:
 metrics:
   enable: true
   addr: :8000
+  enablePeerGauge: false