| index e6688c3a3..38247d378 100644 |
| --- a/internal/host/hadrain.go |
| +++ b/internal/host/hadrain.go |
| @@ -28,6 +28,7 @@ import ( |
| "context" |
| "crypto/rand" |
| "encoding/hex" |
| + "encoding/json" |
| "errors" |
| "fmt" |
| "log" |
| @@ -35,6 +36,7 @@ import ( |
| "strings" |
| "sync" |
| "time" |
| + "unicode/utf8" |
| |
| "github.com/carlosframework/platform/internal/activator" |
| "github.com/carlosframework/platform/internal/boxchoice" |
| @@ -429,17 +431,73 @@ type drainWaiting struct { |
| // Held is settle's entries still here, by host. |
| Held []string `json:"held"` |
| HeldCount int `json:"held_count"` |
| + // Truncated is set when a name was cut short or left out to keep the |
| + // block within drainWaitingMaxBytes; the counts are always whole. |
| + Truncated bool `json:"truncated"` |
| } |
| |
| // drainNamesMax is how many hosts the log, the refusal and the status |
| -// name per count; the counts themselves are whole. |
| -const drainNamesMax = 3 |
| +// name per count; the counts themselves are whole. drainNameMaxBytes |
| +// bounds each name (an owed key that is not a lock's is named whole), and |
| +// drainWaitingMaxBytes the waiting block's JSON, well inside the status |
| +// verb's 8192-byte answer. |
| +const ( |
| + drainNamesMax = 3 |
| + drainNameMaxBytes = 128 |
| + drainWaitingMaxBytes = 2048 |
| + // draft for copy review (D1062-5) |
| + drainNameCut = "(cut)" |
| +) |
| |
| -func firstNames(names []string) []string { |
| +// firstNames is the first drainNamesMax names, each cut at |
| +// drainNameMaxBytes (on a rune boundary, marked drainNameCut). |
| +func firstNames(names []string, cut *bool) []string { |
| if len(names) > drainNamesMax { |
| names = names[:drainNamesMax] |
| } |
| - return append([]string{}, names...) |
| + out := make([]string, 0, len(names)) |
| + for _, n := range names { |
| + if len(n) > drainNameMaxBytes { |
| + i := drainNameMaxBytes |
| + for i > 0 && !utf8.RuneStart(n[i]) { |
| + i-- |
| + } |
| + n = n[:i] + drainNameCut |
| + *cut = true |
| + } |
| + out = append(out, n) |
| + } |
| + return out |
| +} |
| + |
| +// newDrainWaiting is the projection the log, the refusal and the status |
| +// share: at most drainNamesMax names per count, each bounded, and names |
| +// dropped from the last count first until the encoded block fits |
| +// drainWaitingMaxBytes. The counts stay whole. |
| +func newDrainWaiting(step string, since time.Time, q activator.DrainQuiet, names activator.DrainQuietHosts, held []string) *drainWaiting { |
| + w := &drainWaiting{Step: step, Since: since, Quiet: q, HeldCount: len(held)} |
| + w.Held = firstNames(held, &w.Truncated) |
| + w.Hosts = activator.DrainQuietHosts{Waking: firstNames(names.Waking, &w.Truncated), Stopping: firstNames(names.Stopping, &w.Truncated), |
| + Recovering: firstNames(names.Recovering, &w.Truncated), Owed: firstNames(names.Owed, &w.Truncated)} |
| + lists := []*[]string{&w.Held, &w.Hosts.Waking, &w.Hosts.Stopping, &w.Hosts.Recovering, &w.Hosts.Owed} |
| + for { |
| + b, err := json.Marshal(w) |
| + if err == nil && len(b) <= drainWaitingMaxBytes { |
| + return w |
| + } |
| + dropped := false |
| + for i := len(lists) - 1; i >= 0; i-- { |
| + if l := lists[i]; len(*l) > 0 { |
| + *l = (*l)[:len(*l)-1] |
| + dropped = true |
| + break |
| + } |
| + } |
| + w.Truncated = true |
| + if !dropped { |
| + return w |
| + } |
| + } |
| } |
| |
| // describe is the waiting counts that are not zero, each with its hosts: |
| @@ -813,9 +871,7 @@ func (d *drainDriver) waitQuiet(ctx context.Context, step string, held bool) err |
| d.note(func() { d.waiting = nil }) |
| return nil |
| } |
| - w := &drainWaiting{Step: step, Since: start, Quiet: q, Held: firstNames(h), HeldCount: len(h), |
| - Hosts: activator.DrainQuietHosts{Waking: firstNames(names.Waking), Stopping: firstNames(names.Stopping), |
| - Recovering: firstNames(names.Recovering), Owed: firstNames(names.Owed)}} |
| + w := newDrainWaiting(step, start, q, names, h) |
| now := d.now() |
| waited := now.Sub(start) |
| if waited >= drainQuietBound { |