Files
madmin-go/replication-api.go
M AlveeandGitHub 47606f6238 distributed job framework (#557)
* feat: add DistJobType enum and sync distjob status types

Adds an iota-based DistJobType enum (mirrors the HealingDriveReason/
OfflineReason pattern already used in heal-commands.go) so distributed
job identity is a typed wire value instead of a bare string, per review
feedback on the eos distributed-job framework PR.

Also syncs DistJobLeaderStatus/DistJobNodeStatus field shapes to match
the current server-side JSON response (JobID/JobType replace JobName;
drops the unused Generation/AssignedSets/top-level CurrentBucket
fields that were never populated by the server).

* fix: bump vulnerable prometheus dep

- Add msgp generation (//msgp:tag json + //go:generate msgp) for
  DistJobLeaderStatus/DistJobNodeStatus, matching the pattern used by
  every other *-commands.go file in this package.
- Add unit tests for ListDistJobStatuses (success path, job-filter
  query param, DistJobTypeUnknown no-filter case, non-200 error path).
- Bump github.com/prometheus/prometheus v0.304.1 -> v0.312.0 to clear
  3 of the 4 govulncheck findings on this branch (GO-2026-5710,
  GO-2026-5381, GO-2026-5264, all fixed by that version). The 4th,
  GO-2026-5662, has no fix published upstream yet and also currently
  fails govulncheck on main independent of this branch.

* fix: pin go:generate directives to go.mod tool versions

Consolidate distjob-commands.go's separate //msgp:tag json directive
into the //go:generate msgp -d "tag json" flag, matching net-stats.go's
style (review feedback, PR #557).

Change every //go:generate msgp / //go:generate stringer directive
(31 occurrences, 30 files) to //go:generate go tool msgp / go tool
stringer, so generation always resolves the exact version pinned in
go.mod's tool block instead of whatever binary happens to be on PATH.
This was the root cause of the earlier stringer-drift CI failure.
Drop the now-unnecessary `go install -v tool` step from lint.yml.
2026-07-01 05:10:23 -04:00

259 lines
7.2 KiB
Go

//
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package madmin
import (
"context"
"encoding/json"
"net/http"
"net/url"
"time"
)
//go:generate go tool msgp -d clearomitted -d "timezone utc" -file $GOFILE
// ReplDiffOpts holds options for `mc replicate diff` command
//
//msgp:ignore ReplDiffOpts
type ReplDiffOpts struct {
ARN string
Verbose bool
Prefix string
}
// TgtDiffInfo returns status of unreplicated objects
// for the target ARN
//msgp:ignore TgtDiffInfo
type TgtDiffInfo struct {
ReplicationStatus string `json:"rStatus,omitempty"` // target replication status
DeleteReplicationStatus string `json:"drStatus,omitempty"` // target delete replication status
}
// DiffInfo represents relevant replication status and last attempt to replicate
// for the replication targets configured for the bucket
//msgp:ignore DiffInfo
type DiffInfo struct {
Object string `json:"object"`
VersionID string `json:"versionId"`
Targets map[string]TgtDiffInfo `json:"targets,omitempty"`
Err error `json:"error,omitempty"`
ReplicationStatus string `json:"rStatus,omitempty"` // overall replication status
DeleteReplicationStatus string `json:"dStatus,omitempty"` // overall replication status of version delete
ReplicationTimestamp time.Time `json:"replTimestamp,omitempty"`
LastModified time.Time `json:"lastModified,omitempty"`
IsDeleteMarker bool `json:"deletemarker"`
}
// BucketReplicationDiff - gets diff for non-replicated entries.
func (adm *AdminClient) BucketReplicationDiff(ctx context.Context, bucketName string, opts ReplDiffOpts) <-chan DiffInfo {
diffCh := make(chan DiffInfo)
// start a routine to start reading line by line.
go func(diffCh chan<- DiffInfo) {
defer close(diffCh)
queryValues := url.Values{}
queryValues.Set("bucket", bucketName)
if opts.Verbose {
queryValues.Set("verbose", "true")
}
if opts.ARN != "" {
queryValues.Set("arn", opts.ARN)
}
if opts.Prefix != "" {
queryValues.Set("prefix", opts.Prefix)
}
reqData := requestData{
relPath: adminAPIPrefix + "/replication/diff",
queryValues: queryValues,
}
// Execute PUT on /minio/admin/v4/diff to set quota for a bucket.
resp, err := adm.executeMethod(ctx, http.MethodPost, reqData)
if err != nil {
diffCh <- DiffInfo{Err: err}
return
}
defer closeResponse(resp)
if resp.StatusCode != http.StatusOK {
diffCh <- DiffInfo{Err: httpRespToErrorResponse(resp)}
return
}
dec := json.NewDecoder(resp.Body)
for {
var di DiffInfo
if err = dec.Decode(&di); err != nil {
break
}
select {
case <-ctx.Done():
return
case diffCh <- di:
}
}
}(diffCh)
// Returns the diff channel, for caller to start reading from.
return diffCh
}
// ReplicationMRF represents MRF backlog for a bucket
type ReplicationMRF struct {
NodeName string `json:"nodeName" msg:"n"`
Bucket string `json:"bucket" msg:"b"`
Object string `json:"object" msg:"o"`
VersionID string `json:"versionId" msg:"v"`
RetryCount int `json:"retryCount" msg:"rc"`
Err string `json:"error,omitempty" msg:"err"`
}
// BucketReplicationMRF - gets MRF entries for bucket and node. Return MRF across buckets if bucket is empty, across nodes
// if node is `all`
func (adm *AdminClient) BucketReplicationMRF(ctx context.Context, bucketName string, node string) <-chan ReplicationMRF {
mrfCh := make(chan ReplicationMRF)
// start a routine to start reading line by line.
go func(mrfCh chan<- ReplicationMRF) {
defer close(mrfCh)
queryValues := url.Values{}
queryValues.Set("bucket", bucketName)
if node != "" {
queryValues.Set("node", node)
}
reqData := requestData{
relPath: adminAPIPrefix + "/replication/mrf",
queryValues: queryValues,
}
// Execute GET on /minio/admin/v4/replication/mrf to get mrf backlog for a bucket.
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
if err != nil {
mrfCh <- ReplicationMRF{Err: err.Error()}
return
}
defer closeResponse(resp)
if resp.StatusCode != http.StatusOK {
mrfCh <- ReplicationMRF{Err: httpRespToErrorResponse(resp).Error()}
return
}
dec := json.NewDecoder(resp.Body)
for {
var bk ReplicationMRF
if err = dec.Decode(&bk); err != nil {
break
}
select {
case <-ctx.Done():
return
case mrfCh <- bk:
}
}
}(mrfCh)
// Returns the mrf backlog channel, for caller to start reading from.
return mrfCh
}
// LatencyStat represents replication link latency statistics
type LatencyStat struct {
Curr time.Duration `json:"curr"`
Avg time.Duration `json:"avg"`
Max time.Duration `json:"max"`
}
// TimedErrStats has failed replication stats across time windows
type TimedErrStats struct {
LastMinute RStat `json:"lastMinute"`
LastHour RStat `json:"lastHour"`
Totals RStat `json:"totals"`
// ErrCounts is a map of error codes to count of errors since server start - tracks
// only AccessDenied errors for now.
ErrCounts map[string]int `json:"errCounts,omitempty"`
}
// Add - adds two TimedErrStats
func (te TimedErrStats) Add(o TimedErrStats) TimedErrStats {
m := make(map[string]int)
for k, v := range te.ErrCounts {
m[k] = v
}
for k, v := range o.ErrCounts {
m[k] += v
}
return TimedErrStats{
LastMinute: te.LastMinute.Add(o.LastMinute),
LastHour: te.LastHour.Add(o.LastHour),
Totals: te.Totals.Add(o.Totals),
ErrCounts: m,
}
}
// RStat represents count and bytes replicated/failed
type RStat struct {
Count float64 `json:"count"`
Bytes int64 `json:"bytes"`
}
// Add - adds two RStats
func (r RStat) Add(r1 RStat) RStat {
return RStat{
Count: r.Count + r1.Count,
Bytes: r.Bytes + r1.Bytes,
}
}
// DowntimeInfo captures the downtime information
type DowntimeInfo struct {
Duration StatRecorder `json:"duration"`
Count StatRecorder `json:"count"`
}
// RecordCount records the value
func (d *DowntimeInfo) RecordCount(value int64) {
d.Count.Record(value)
}
// RecordDuration records the value
func (d *DowntimeInfo) RecordDuration(value int64) {
d.Duration.Record(value)
}
// StatRecorder records and calculates the aggregates
type StatRecorder struct {
Total int64 `json:"total"`
Avg int64 `json:"avg"`
Max int64 `json:"max"`
count int64 `json:"-"`
}
// Record will record the value and calculates the aggregates on the fly
func (s *StatRecorder) Record(value int64) {
s.Total += value
if s.count == 0 || value > s.Max {
s.Max = value
}
s.count++
s.Avg = s.Total / s.count
}