package main // What the operator's ask for a recreate names (design 47 H2 H-A-686 (3), H-A-706 on DQ-42): per object whether it is changed in // place or made again empty, its held and asked settings, the messages, bytes and oldest message's time it holds, read when // the ask is raised, and what the change removes; and the room the caps it asks need on the bus's account. import ( "context" "fmt" "strings" "time" "github.com/nats-io/nats.go/jetstream" "nats-tools/internal/keeper" ) // olderThan counts the messages of a stream stored before a time. type olderThan func(stream string, before time.Time) (uint64, error) // LiveOlderThan counts them on the bus by an ordered consumer that starts at the time: the messages before it are those the // stream holds less the ones the consumer has pending. func LiveOlderThan(bus Bus) olderThan { return func(stream string, before time.Time) (uint64, error) { ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() js, closeBus, err := bus(ctx) if err != nil { return 0, err } defer closeBus() s, err := js.Stream(ctx, stream) if err != nil { return 0, err } info, err := s.Info(ctx) if err != nil { return 0, err } c, err := s.CreateOrUpdateConsumer(ctx, jetstream.ConsumerConfig{DeliverPolicy: jetstream.DeliverByStartTimePolicy, OptStartTime: &before, AckPolicy: jetstream.AckNonePolicy, InactiveThreshold: time.Minute}) if err != nil { return 0, err } ci, err := c.Info(ctx) if err != nil { return 0, err } _ = s.DeleteConsumer(ctx, ci.Name) if info.State.Msgs < ci.NumPending { return 0, nil } return info.State.Msgs - ci.NumPending, nil } } func settingsWords(s keeper.Settings) string { age := "no age" if s.MaxAgeSeconds > 0 { age = "age " + (time.Duration(s.MaxAgeSeconds) * time.Second).String() } return fmt.Sprintf("cap %d MiB, %s, history %d, discard %s, lifetime %v", s.MaxBytes/keeper.MiB, age, s.History, s.Discard, s.KeyTTL) } // recreateWords says, for each object of the entry, what the rebuild does and removes, read from the entry the keeper made and // the bus's streams as the monitoring endpoint answers them; and the room its caps need. func recreateWords(held map[string]keeper.Asked, status func() (keeper.Status, error), jsz []byte, now time.Time, older olderThan, module, local string) (string, error) { m, ok := held[module].Made[local] if !ok || !m.Created { return "", fmt.Errorf("the bus module made no %s of %s", local, module) } streams, err := streamsHeld(jsz) if err != nil { return "", err } var lines []string var caps int64 for _, o := range m.Plan.Objects { caps += o.Settings.MaxBytes s, found := streams[o.Stream()] if !found { lines = append(lines, fmt.Sprintf("%s: the bus holds none, so it is made, holding nothing to remove", o.Stream())) continue } have := keeper.SettingsOf(o.Kind, s.Config) inPlace := have.WorkQueue == o.Settings.WorkQueue oldest := "none" if !s.State.FirstTS.IsZero() && s.State.Messages > 0 { oldest = s.State.FirstTS.UTC().Format(time.RFC3339) } how, removes := "changed in place", []string{} if !inPlace { how, removes = "made again empty, its retention changes", []string{fmt.Sprintf("every message (%d)", s.State.Messages)} } else { if over := int64(s.State.Bytes) - o.Settings.MaxBytes; over > 0 { removes = append(removes, fmt.Sprintf("the oldest messages beyond the cap, %d bytes over it", over)) } if age := o.Settings.MaxAgeSeconds; age > 0 && (have.MaxAgeSeconds == 0 || age < have.MaxAgeSeconds) && s.State.Messages > 0 { cutoff := now.Add(-time.Duration(age) * time.Second) switch { case !s.State.FirstTS.Before(cutoff): removes = append(removes, "no message older than the age") case older == nil: removes = append(removes, "the messages older than the age, not counted here") default: n, err := older(o.Stream(), cutoff) if err != nil { return "", fmt.Errorf("the messages of %s older than the age cannot be counted: %w", o.Stream(), err) } removes = append(removes, fmt.Sprintf("%d messages older than the age", n)) } } } if len(removes) == 0 { removes = []string{"nothing"} } lines = append(lines, fmt.Sprintf("%s %s; held {%s}, asked {%s}; it holds %d messages, %d bytes, the oldest from %s; the change removes: %s", o.Stream(), how, settingsWords(have), settingsWords(o.Settings), s.State.Messages, s.State.Bytes, oldest, strings.Join(removes, ", "))) } room := fmt.Sprintf("The caps it asks sum to %d MiB", caps/keeper.MiB) if st, err := status(); err == nil && st.Limits.AccountBytes > 0 { need := caps + keeper.CapSum(held, module, local) + st.OldBytes room += fmt.Sprintf(", %d MiB with every other cap of the bus, against the account's storage limit of %d MiB", need/keeper.MiB, st.Limits.AccountBytes/keeper.MiB) } return room + ". Per object: " + strings.Join(lines, " | "), nil }