external IP allocations read the whole ip_pool_range table

オープン
#11,869 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
rust

調査の方向性

Start with the allocation queries in nexus/db-queries/src/db/queries/external_ip.rs and external_multicast_group.rs, then use EXPLAIN ANALYZE to confirm whether the updated_pool_range CTE scans the whole table. Try the proposed join form and verify that it constrains the update to the selected range; the issue describes three other scan patterns in ip_pool.rs and external_subnet.rs that may also need investigation. Done when the affected queries avoid unnecessary full scans and the plans confirm the expected lookups.

索引モデルが issue の本文から書いたものです。

説明

(filing this for now as a followup to https://github.com/oxidecomputer/omicron/pull/11851. Plan to write this up properly as part of investigating it in the future)

robot text

Every external IP allocation currently reads all of ip_pool_range, because CockroachDB plans one step of the allocation query as a full scan of the table. Three other queries on the same table also scan more than they need to. I found these while checking index usage with EXPLAIN ANALYZE.

The allocation query is the most significant of the four because it's on a hot path. The sections below are in rough priority order.

1. External IP allocation

The allocation queries all have an updated_pool_range CTE that bumps rcgen on the range the new address came from:

UPDATE ip_pool_range
SET rcgen = rcgen + 1
WHERE id = (SELECT ip_pool_range_id FROM next_external_ip) AND time_deleted IS NULL
RETURNING id

CockroachDB 22.1 doesn't use the scalar subquery to constrain the scan, so it plans this as a full scan of the primary key rather than a lookup of one row. A read-only copy of the same predicate read about 24k rows in a table of 25.8k.

This affects next_automatic_floating_ip, next_explicit_floating_ip, next_instance_snat_ip, next_omicron_zone_snat_ip, and both multicast group allocation queries (which have the same CTE, reading from next_external_multicast_group). The queries are built in nexus/db-queries/src/db/queries/external_ip.rs and external_multicast_group.rs.

A possible fix is to express the id as a join that the optimizer can use, for example:

UPDATE ip_pool_range
SET rcgen = rcgen + 1
FROM next_external_ip
WHERE ip_pool_range.id = next_external_ip.ip_pool_range_id
  AND ip_pool_range.time_deleted IS NULL
RETURNING ip_pool_range.id

I haven't tried this, so it needs to be confirmed with EXPLAIN.

2. Looking up the pool that contains an address

lookup_ip_pool_containing_address_on_connection in nexus/db-queries/src/db/datastore/ip_pool.rs (called by ip_pool_fetch_containing_address_in_current_silo and ip_pool_fetch_containing_address_for_services) filters on first_address <= X AND last_address >= X. No index can serve that as a two-sided bound, so the query is a full scan. It read all 25.8k rows in the test, even when X was in no range.

Live ranges never overlap, so the range that contains X, if there is one, is the range with the largest first_address that is at most X. That means the lookup can be written as:

SELECT ... FROM ip_pool_range
WHERE first_address <= X AND time_deleted IS NULL
ORDER BY first_address DESC
LIMIT 1

followed by a check that last_address >= X. This is a single seek on the existing lookup_pool_range_by_first_address index, which already stores last_address.

Two things to be careful about in the rewrite:

  • The pool, silo, pool type and IP version filters have to be applied after the LIMIT 1, not before it. If they're applied before, and the range that contains X is filtered out, the query returns a different range that doesn't contain X.
  • The rewrite relies on ranges not overlapping. That is an application-level invariant, enforced across all pools at insert time by the filter_overlapping_ip_ranges query, and not a database constraint. The reliance should be documented at the query.
3. Overlap checks when adding a range or subnet pool member

filter_overlapping_ip_ranges (nexus/db-queries/src/db/queries/ip_pool.rs) and insert_subnet_pool_member (nexus/db-queries/src/db/queries/external_subnet.rs) each have subqueries that ask whether an existing range contains an endpoint of the candidate, of the form $1 >= first_address AND $2 <= last_address or $1 BETWEEN first_address AND last_address. These are full scans for the same reason as in section 2.

The same approach likely applies here: find the range with the largest first_address at or below the endpoint, and check its last_address. I haven't worked through this one, so it needs checking.

4. ip_pool_delete's check for remaining ranges (minor)

ip_pool_delete checks whether a pool still has ranges with WHERE ip_pool_id = X AND time_deleted IS NULL LIMIT 1. This is planned as a scan of the primary key that stops at the first match, and it read about 1k rows in the test. A seek on lookup_pool_range_by_pool_and_first_address would read one row. The optimizer only picks the index when table statistics say the pool is empty.

This is low priority, because pool deletion is rare and the scan stops early.

How this was measured

I ran EXPLAIN ANALYZE on a single-node CockroachDB v22.1 with about 25.8k ranges across 12 pools and 6.2k external IPs, after collecting table statistics.

This came up while adding the lookup_pool_range_by_pool_and_first_address index on (ip_pool_id, first_address) for pagination. That index doesn't help any of the queries above.

主要言語
Rust
スター
630
フォーク
110
平均マージ
2日 2時間
マージ済み PR(30日)
160

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

oxidecomputer/omicron のほかの issue

oxidecomputer/omicron の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。