Stream bucket bandwidth measurements (#11014)

master
Ritesh H Shukla 4 years ago committed by GitHub
parent d54cf77356
commit 7e2b79984e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      cmd/admin-handlers.go
  2. 45
      pkg/madmin/bandwidth.go
  3. 17
      pkg/madmin/examples/bucket-bandwidth.go

@ -41,6 +41,7 @@ import (
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/minio/cmd/logger/message/log" "github.com/minio/minio/cmd/logger/message/log"
"github.com/minio/minio/pkg/auth" "github.com/minio/minio/pkg/auth"
"github.com/minio/minio/pkg/bandwidth"
"github.com/minio/minio/pkg/handlers" "github.com/minio/minio/pkg/handlers"
iampolicy "github.com/minio/minio/pkg/iam/policy" iampolicy "github.com/minio/minio/pkg/iam/policy"
"github.com/minio/minio/pkg/madmin" "github.com/minio/minio/pkg/madmin"
@ -1428,6 +1429,8 @@ func (a adminAPIHandlers) HealthInfoHandler(w http.ResponseWriter, r *http.Reque
func (a adminAPIHandlers) BandwidthMonitorHandler(w http.ResponseWriter, r *http.Request) { func (a adminAPIHandlers) BandwidthMonitorHandler(w http.ResponseWriter, r *http.Request) {
ctx := newContext(r, w, "BandwidthMonitor") ctx := newContext(r, w, "BandwidthMonitor")
defer logger.AuditLog(w, r, "BandwidthMonitor", mustGetClaimsFromToken(r))
// Validate request signature. // Validate request signature.
_, adminAPIErr := checkAdminRequestAuthType(ctx, r, iampolicy.BandwidthMonitorAction, "") _, adminAPIErr := checkAdminRequestAuthType(ctx, r, iampolicy.BandwidthMonitorAction, "")
if adminAPIErr != ErrNone { if adminAPIErr != ErrNone {
@ -1436,15 +1439,41 @@ func (a adminAPIHandlers) BandwidthMonitorHandler(w http.ResponseWriter, r *http
} }
setEventStreamHeaders(w) setEventStreamHeaders(w)
reportCh := make(chan bandwidth.Report, 1)
keepAliveTicker := time.NewTicker(500 * time.Millisecond)
defer keepAliveTicker.Stop()
bucketsRequestedString := r.URL.Query().Get("buckets") bucketsRequestedString := r.URL.Query().Get("buckets")
bucketsRequested := strings.Split(bucketsRequestedString, ",") bucketsRequested := strings.Split(bucketsRequestedString, ",")
consolidatedReport := globalNotificationSys.GetBandwidthReports(ctx, bucketsRequested...) go func() {
for {
reportCh <- globalNotificationSys.GetBandwidthReports(ctx, bucketsRequested...)
select {
case <-ctx.Done():
return
default:
time.Sleep(2 * time.Second)
}
}
}()
for {
select {
case report := <-reportCh:
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
err := enc.Encode(consolidatedReport) err := enc.Encode(report)
if err != nil { if err != nil {
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInternalError), r.URL) writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInternalError), r.URL)
return
}
w.(http.Flusher).Flush()
case <-keepAliveTicker.C:
if _, err := w.Write([]byte(" ")); err != nil {
return
} }
w.(http.Flusher).Flush() w.(http.Flusher).Flush()
case <-ctx.Done():
return
}
}
} }
// ServerInfoHandler - GET /minio/admin/v3/info // ServerInfoHandler - GET /minio/admin/v3/info

@ -20,7 +20,6 @@ package madmin
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -28,10 +27,17 @@ import (
"github.com/minio/minio/pkg/bandwidth" "github.com/minio/minio/pkg/bandwidth"
) )
// GetBucketBandwidth - Get a snapshot of the bandwidth measurements for replication buckets. If no buckets // Report includes the bandwidth report or the error encountered.
// generate replication traffic an empty map is returned. type Report struct {
func (adm *AdminClient) GetBucketBandwidth(ctx context.Context, buckets ...string) (bandwidth.Report, error) { Report bandwidth.Report
Err error
}
// GetBucketBandwidth - Gets a channel reporting bandwidth measurements for replication buckets. If no buckets
// generate replication traffic an empty map is returned in the report until traffic is seen.
func (adm *AdminClient) GetBucketBandwidth(ctx context.Context, buckets ...string) <-chan Report {
queryValues := url.Values{} queryValues := url.Values{}
ch := make(chan Report)
if len(buckets) > 0 { if len(buckets) > 0 {
queryValues.Set("buckets", strings.Join(buckets, ",")) queryValues.Set("buckets", strings.Join(buckets, ","))
} }
@ -40,22 +46,37 @@ func (adm *AdminClient) GetBucketBandwidth(ctx context.Context, buckets ...strin
relPath: adminAPIPrefix + "/bandwidth", relPath: adminAPIPrefix + "/bandwidth",
queryValues: queryValues, queryValues: queryValues,
} }
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
defer closeResponse(resp)
if err != nil { if err != nil {
return bandwidth.Report{}, err defer closeResponse(resp)
ch <- Report{bandwidth.Report{}, err}
return ch
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return bandwidth.Report{}, httpRespToErrorResponse(resp) ch <- Report{bandwidth.Report{}, httpRespToErrorResponse(resp)}
return ch
} }
dec := json.NewDecoder(resp.Body) dec := json.NewDecoder(resp.Body)
go func(ctx context.Context, ch chan<- Report, resp *http.Response) {
defer func() {
closeResponse(resp)
close(ch)
}()
for { for {
var report bandwidth.Report var report bandwidth.Report
err = dec.Decode(&report)
if err != nil && err != io.EOF { if err = dec.Decode(&report); err != nil {
return bandwidth.Report{}, err ch <- Report{bandwidth.Report{}, err}
return
}
select {
case <-ctx.Done():
return
case ch <- Report{Report: report, Err: err}:
} }
return report, nil
} }
}(ctx, ch, resp)
return ch
} }

@ -35,16 +35,15 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
ctx := context.Background() ctx := context.Background()
report, err := madminClient.GetBucketBandwidth(ctx) reportCh := madminClient.GetBucketBandwidth(ctx)
if err != nil {
log.Fatalln(err) for i := 0; i < 10; i++ {
return report := <-reportCh
}
fmt.Printf("Report: %+v\n", report) fmt.Printf("Report: %+v\n", report)
report, err = madminClient.GetBucketBandwidth(ctx, "sourceBucket", "sourceBucket2")
if err != nil {
log.Fatalln(err)
return
} }
reportCh = madminClient.GetBucketBandwidth(ctx, "sourceBucket", "sourceBucket2")
for i := 0; i < 10; i++ {
report := <-reportCh
fmt.Printf("Report: %+v\n", report) fmt.Printf("Report: %+v\n", report)
} }
}

Loading…
Cancel
Save