Skip to content

Commit

Permalink
autopilot: update churn alert to contain timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Feb 23, 2024
1 parent b45b80f commit c17252a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
41 changes: 23 additions & 18 deletions autopilot/churn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (

type (
accumulatedChurn struct {
additions map[types.FileContractID][]contractSetAddition
removals map[types.FileContractID][]contractSetRemoval
additions map[types.FileContractID]contractSetAdditions
removals map[types.FileContractID]contractSetRemovals
}
)

func newAccumulatedChurn() *accumulatedChurn {
return &accumulatedChurn{
additions: make(map[types.FileContractID][]contractSetAddition),
removals: make(map[types.FileContractID][]contractSetRemoval),
additions: make(map[types.FileContractID]contractSetAdditions),
removals: make(map[types.FileContractID]contractSetRemovals),
}
}

Expand All @@ -27,13 +27,6 @@ func (c *accumulatedChurn) Alert(name string) alerts.Alert {
hint = "A high churn rate can lead to a lot of unnecessary migrations, it might be necessary to tweak your configuration depending on the reason hosts are being discarded from the set."
}

removedReasons := make(map[string][]string, len(c.removals))
for fcid, contractRemovals := range c.removals {
for _, removal := range contractRemovals {
removedReasons[fcid.String()] = append(removedReasons[fcid.String()], removal.Reason)
}
}

return alerts.Alert{
ID: alertChurnID,
Severity: alerts.SeverityInfo,
Expand All @@ -48,16 +41,28 @@ func (c *accumulatedChurn) Alert(name string) alerts.Alert {
}
}

func (c *accumulatedChurn) Apply(additions map[types.FileContractID]contractSetAddition, removals map[types.FileContractID]contractSetRemoval) {
for fcid, addition := range additions {
c.additions[fcid] = append(c.additions[fcid], addition)
func (c *accumulatedChurn) Apply(additions map[types.FileContractID]contractSetAdditions, removals map[types.FileContractID]contractSetRemovals) {
for fcid, a := range additions {
if _, exists := c.additions[fcid]; !exists {
c.additions[fcid] = a
} else {
additions := c.additions[fcid]
additions.Additions = append(additions.Additions, a.Additions...)
c.additions[fcid] = additions
}
}
for fcid, removal := range removals {
c.removals[fcid] = append(c.removals[fcid], removal)
for fcid, r := range removals {
if _, exists := c.removals[fcid]; !exists {
c.removals[fcid] = r
} else {
removals := c.removals[fcid]
removals.Removals = append(removals.Removals, r.Removals...)
c.removals[fcid] = removals
}
}
}

func (c *accumulatedChurn) Reset() {
c.additions = make(map[types.FileContractID][]contractSetAddition)
c.removals = make(map[types.FileContractID][]contractSetRemoval)
c.additions = make(map[types.FileContractID]contractSetAdditions)
c.removals = make(map[types.FileContractID]contractSetRemovals)
}
56 changes: 40 additions & 16 deletions autopilot/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,25 @@ type (
recoverable bool
}

contractSetAdditions struct {
HostKey types.PublicKey `json:"hostKey"`
Additions []contractSetAddition `json:"additions"`
}

contractSetAddition struct {
Size uint64 `json:"size"`
HostKey types.PublicKey `json:"hostKey"`
Size uint64 `json:"size"`
Time api.TimeRFC3339 `json:"time"`
}

contractSetRemovals struct {
HostKey types.PublicKey `json:"hostKey"`
Removals []contractSetRemoval `json:"removals"`
}

contractSetRemoval struct {
Size uint64 `json:"size"`
HostKey types.PublicKey `json:"hostKey"`
Reason string `json:"reasons"`
Size uint64 `json:"size"`
Reason string `json:"reasons"`
Time api.TimeRFC3339 `json:"time"`
}

renewal struct {
Expand Down Expand Up @@ -455,8 +465,9 @@ func (c *contractor) computeContractSetChanged(ctx context.Context, name string,
}

// log added and removed contracts
setAdditions := make(map[types.FileContractID]contractSetAddition)
setRemovals := make(map[types.FileContractID]contractSetRemoval)
setAdditions := make(map[types.FileContractID]contractSetAdditions)
setRemovals := make(map[types.FileContractID]contractSetRemovals)
now := api.TimeNow()
for _, contract := range oldSet {
_, exists := inNewSet[contract.ID]
_, renewed := inNewSet[renewalsFromTo[contract.ID]]
Expand All @@ -466,22 +477,36 @@ func (c *contractor) computeContractSetChanged(ctx context.Context, name string,
reason = "unknown"
}

setRemovals[contract.ID] = contractSetRemoval{
Size: contractData[contract.ID],
HostKey: contract.HostKey,
Reason: reason,
if _, exists := setRemovals[contract.ID]; !exists {
setRemovals[contract.ID] = contractSetRemovals{
HostKey: contract.HostKey,
}
}
removals := setRemovals[contract.ID]
removals.Removals = append(removals.Removals, contractSetRemoval{
Size: contractData[contract.ID],
Reason: reason,
Time: now,
})
setRemovals[contract.ID] = removals
c.logger.Debugf("contract %v was removed from the contract set, size: %v, reason: %v", contract.ID, contractData[contract.ID], reason)
}
}
for _, contract := range newSet {
_, existed := inOldSet[contract.ID]
_, renewed := renewalsToFrom[contract.ID]
if !existed && !renewed {
setAdditions[contract.ID] = contractSetAddition{
Size: contractData[contract.ID],
HostKey: contract.HostKey,
if _, exists := setAdditions[contract.ID]; !exists {
setAdditions[contract.ID] = contractSetAdditions{
HostKey: contract.HostKey,
}
}
additions := setAdditions[contract.ID]
additions.Additions = append(additions.Additions, contractSetAddition{
Size: contractData[contract.ID],
Time: now,
})
setAdditions[contract.ID] = additions
c.logger.Debugf("contract %v was added to the contract set, size: %v", contract.ID, contractData[contract.ID])
}
}
Expand All @@ -501,7 +526,6 @@ func (c *contractor) computeContractSetChanged(ctx context.Context, name string,
}

// record churn metrics
now := api.TimeNow()
var metrics []api.ContractSetChurnMetric
for fcid := range setAdditions {
metrics = append(metrics, api.ContractSetChurnMetric{
Expand All @@ -516,7 +540,7 @@ func (c *contractor) computeContractSetChanged(ctx context.Context, name string,
Name: c.ap.state.cfg.Contracts.Set,
ContractID: fcid,
Direction: api.ChurnDirRemoved,
Reason: removal.Reason,
Reason: removal.Removals[0].Reason,
Timestamp: now,
})
}
Expand Down

0 comments on commit c17252a

Please sign in to comment.