Files
floci/src
c229311f8f perf(logs): evict past the ceiling and retention without scanning the store (#5167)
* perf(logs): evict past the ceiling and retention without scanning the store

Every PutLogEvents listed the account's whole event store twice: once
to apply the group's retention policy, once to enforce the stored-event
ceiling, which also loaded and sorted every event once the store was
full. Each listing walks every key of every account. So once an account
reached max-stored-events (20000 by default), each call cost a sort of
the full store, however few events it carried.

Sources that put one event per call pay that on every line: EKS
control-plane audit logs forward each audit record as its own call.
At the default ceiling with a retention policy set, a single-event
call took about 4.3 ms; it now takes about 5 us.

An account's event keys are now kept in an in-memory index ordered as
the store is read (timestamp, ingestion sequence, event id), with one
ordered set per log group. Retention removes a group's events older
than the cutoff from the head of its set; the ceiling removes the
oldest across the account. Which events are dropped, and when, is
unchanged, except for names that hold "::".

Keys join region, group and stream with "::" and are not escaped, so
"r::/app::" is a prefix of "/app::logs"'s keys and of "/app"'s stream
"logs", and "/app"'s stream "logs::s" and "/app::logs"'s stream "s"
share one stream key. For retention and stream deletion the group now
comes from the group, not the prefix:

- each index entry carries the group it was written to, so retention
  for "/app" takes neither "/app::logs"'s events nor the reverse
- deleting a stream removes only keys that are that stream's prefix
  followed by the timestamp and id, and drops them from the index by
  key, whatever group they are filed under
- PutLogEvents and DeleteLogStream refuse, as a missing stream, a
  stream found by key whose recorded group is another;
  DescribeLogStreams lists only streams that record its group
- DeleteLogGroup deletes only the streams, metric filters and
  subscription filters that record its group
- a refill reads each key's group off the stream it names, one stream
  lookup per stored event; an event whose stream is gone is deleted,
  as DeleteLogStream would have, since nothing says which group it
  was in

Every stream is created with its group recorded. A record without one
can only be built by hand, and counts as the caller's.

Reads (GetLogEvents, FilterLogEvents, Insights) still select events by
prefix, as before; this change leaves them alone.

Each account keeps one index for the life of the service, and every
write to its events (PutLogEvents and deleting a stream or group)
changes the store and the index while holding the index's lock, so the
two change together and concurrent calls no longer overshoot the
ceiling. The index starts stale and the first call holding it fills it
from the store, so events stored before a restart count. A key leaves
the index only after its store delete succeeds. A store write that
throws part-way, exception or Error, marks the index stale. Emulator
reset empties every index and marks it stale in beforeReset(), clear()
and afterReset(), so an account that never writes again holds nothing
for the wiped events; the next call refills from the store. The index
is never replaced, so a call waiting on its lock never writes under a
different one.

A put racing DeleteLogStream could write events after the stream was
gone, or write the deleted stream's metadata back. DeleteLogStream and
DeleteLogGroup now remove the stream record and its events together
under the lock. PutLogEvents checks the stream again under it, and
writes the stream's metadata there too. So a put lands before the
delete or finds no stream, and a stream recreated under the same name
cannot write until the old one's events are gone.

Calls for one account now take that lock in turn, store write
included; different accounts do not wait on each other.

* refactor(logs): read an event's group off its key

AWS allows no colon in a log group or stream name, so the key layout
needs no defending inside this change. The index takes each event's
group from its key, the refill no longer probes streams split by split
or deletes events it can't place, and the stream lookups go back to
what main does. The tests for separator-bearing names go with it.

---------

Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
Co-authored-by: Priamo <pgermosen@gmail.com>
2026-10-17 18:00:18 -04:00
..