Answer 429 to AI usage events when a write buffer is nearly full

What does this MR do and why?

Today, when an AI usage events write buffer reaches its cap, new rows are dropped and the client is not told. This MR makes POST /api/v4/usage_data/track_event and POST /api/v4/usage_data/track_events return 429 Too Many Requests with Retry-After: 180 before that point. The client then knows to resend.

  • A request is rejected only when it contains at least one AI usage event registered in Gitlab::Tracking::AiTracking. Other events are accepted as before.
  • Three write buffers are checked: the PostgreSQL buffers of Ai::UsageEvent and Ai::TokenDailyUsage, which are filled by the same AI tracking call, and the ClickHouse ai_usage_events buffer. The threshold is 80% of each buffer's own MAX_PENDING (write_buffer.class::MAX_PENDING for the PostgreSQL buffers, ClickHouse::WriteBuffer::MAX_PENDING for ClickHouse). Reaching it in any buffer rejects the request. The threshold follows the cap if the cap changes, and the space left below the cap is kept for internal writes.
  • The check runs before any event is processed, so a rejected track_events request records nothing and can be resent whole.
  • It fails open per buffer: if reading one buffer's depth raises a Redis error, that buffer does not reject the request and the error is tracked, as the cap check does since !270367 (merged). The other buffers are still checked.
  • The check runs before the existing per-user rate limit, so a rejected request is not charged to it and a resend after Retry-After is charged once.
  • Rejections increment the gitlab_ai_usage_events_admission_rejected_total counter.
  • The behaviour is behind the new gitlab_com_derisk flag ai_usage_events_buffer_admission, which is off by default. With the flag off, nothing changes.

The endpoints pass the event hashes to the helper rather than a list of names, which avoids a conflict between the Rails/Pluck and CodeReuse/ActiveRecord cops on params.

No changelog entry, because the behaviour is behind a flag that is off by default.

References

Screenshots or screen recordings

Not applicable: no UI change.

How to set up and validate locally

Not validated in a running GDK yet. Steps for a reviewer:

  1. In the Rails console, enable the flag and fill the PostgreSQL buffer past 80% of its cap:

    Feature.enable(:ai_usage_events_buffer_admission)
    limit = (Analytics::DatabaseWriteBuffer::MAX_PENDING * 0.8).to_i
    Ai::UsageEvent.write_buffer.add(Array.new(limit) { { filler: true } })
  2. Send an AI event and check for 429 and Retry-After: 180:

    curl --include --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
      --header "Content-Type: application/json" \
      --data '{"event": "code_suggestion_shown_in_ide"}' \
      "http://gdk.test:3000/api/v4/usage_data/track_event"
  3. Send a non-AI event with the same command and check for 200.

  4. Empty the buffer with Ai::UsageEvent.write_buffer.pop(limit) and disable the flag.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Chandra Saripaka

Merge request reports

Loading
Loading