From 7f2fc6d65b0ce6beddba29ce2e879092bfcc0ecb Mon Sep 17 00:00:00 2001 From: yeya24 Date: Fri, 16 Aug 2019 21:55:04 +0800 Subject: [PATCH] add supernode piece downloaded bytes Signed-off-by: yeya24 --- docs/user_guide/metrics.md | 1 + supernode/server/0.3_bridge.go | 5 ++++ supernode/server/metrics.go | 12 ++++++--- supernode/util/range_util.go | 25 ++++++++++++++++++- supernode/util/range_util_test.go | 41 +++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 5 deletions(-) diff --git a/docs/user_guide/metrics.md b/docs/user_guide/metrics.md index 692c5f13d..022673c55 100644 --- a/docs/user_guide/metrics.md +++ b/docs/user_guide/metrics.md @@ -21,6 +21,7 @@ This doc contains all the metrics that Dragonfly components currently support. N - dragonfly_supernode_cdn_cache_hit_total{} - total times of hitting cdn cache. counter type. - dragonfly_supernode_cdn_download_total{} - total times of cdn downloading. counter type. - dragonfly_supernode_cdn_download_failed_total{} - total failure times of cdn downloading. counter type. +- dragonfly_supernode_pieces_downloaded_size_bytes{} - total size of pieces downloaded from supernode in bytes. counter type. ## Dfdaemon diff --git a/supernode/server/0.3_bridge.go b/supernode/server/0.3_bridge.go index bc3724a03..2311aa11d 100644 --- a/supernode/server/0.3_bridge.go +++ b/supernode/server/0.3_bridge.go @@ -209,6 +209,11 @@ func (s *Server) reportPiece(ctx context.Context, rw http.ResponseWriter, req *h return err } + // If piece is downloaded from supernode, add metrics. + if s.Config.IsSuperCID(dstCID) { + m.pieceDownloadedBytes.WithLabelValues().Add(float64(sutil.CalculatePieceSize(pieceRange))) + } + request := &types.PieceUpdateRequest{ ClientID: srcCID, DstPID: dstDfgetTask.PeerID, diff --git a/supernode/server/metrics.go b/supernode/server/metrics.go index 20aa7eb8d..73b0f0dc0 100644 --- a/supernode/server/metrics.go +++ b/supernode/server/metrics.go @@ -28,10 +28,11 @@ import ( // metrics defines some prometheus metrics for monitoring supernode type metrics struct { - requestCounter *prometheus.CounterVec - requestDuration *prometheus.HistogramVec - requestSize *prometheus.HistogramVec - responseSize *prometheus.HistogramVec + requestCounter *prometheus.CounterVec + requestDuration *prometheus.HistogramVec + requestSize *prometheus.HistogramVec + responseSize *prometheus.HistogramVec + pieceDownloadedBytes *prometheus.CounterVec } func newMetrics(register prometheus.Registerer) *metrics { @@ -51,6 +52,9 @@ func newMetrics(register prometheus.Registerer) *metrics { "Histogram of response size for HTTP requests.", []string{"handler"}, prometheus.ExponentialBuckets(100, 10, 8), register, ), + pieceDownloadedBytes: metricsutils.NewCounter(config.SubsystemSupernode, "pieces_downloaded_size_bytes", + "total bytes of pieces downloaded from supernode", []string{}, register, + ), } } diff --git a/supernode/util/range_util.go b/supernode/util/range_util.go index 81f937427..c67470cb3 100644 --- a/supernode/util/range_util.go +++ b/supernode/util/range_util.go @@ -26,6 +26,30 @@ const ( separator = "-" ) +// CalculatePieceSize calculates the size of piece +// according to the parameter range. +func CalculatePieceSize(rangeStr string) int64 { + ranges := strings.Split(rangeStr, separator) + if len(ranges) != 2 { + return 0 + } + + startIndex, err := strconv.ParseInt(ranges[0], 10, 64) + if err != nil { + return 0 + } + endIndex, err := strconv.ParseInt(ranges[1], 10, 64) + if err != nil { + return 0 + } + if endIndex < startIndex { + return 0 + } + + pieceSize := endIndex - startIndex + 1 + return pieceSize +} + // CalculatePieceNum calculates the number of piece // according to the parameter range. func CalculatePieceNum(rangeStr string) int { @@ -47,7 +71,6 @@ func CalculatePieceNum(rangeStr string) int { } pieceSize := endIndex - startIndex + 1 - return int(startIndex / pieceSize) } diff --git a/supernode/util/range_util_test.go b/supernode/util/range_util_test.go index 14beebbec..059238df8 100644 --- a/supernode/util/range_util_test.go +++ b/supernode/util/range_util_test.go @@ -32,6 +32,47 @@ func init() { check.Suite(&RangeUtilSuite{}) } +func (suite *RangeUtilSuite) TestCalculatePieceSize(c *check.C) { + var cases = []struct { + rangeStr string + expected int64 + }{ + { + rangeStr: "foo", + expected: 0, + }, + { + rangeStr: "aaa-bbb", + expected: 0, + }, + { + rangeStr: "3-2", + expected: 0, + }, + { + rangeStr: "1 -3", + expected: 0, + }, + { + rangeStr: "0-0", + expected: 1, + }, + { + rangeStr: "6-8", + expected: 3, + }, + { + rangeStr: "0-40000", + expected: 40001, + }, + } + + for _, v := range cases { + result := CalculatePieceSize(v.rangeStr) + c.Assert(result, check.Equals, v.expected) + } +} + func (suite *RangeUtilSuite) TestCalculatePieceNum(c *check.C) { var cases = []struct { rangeStr string