Skip to content

BUGFIX: full table scan preventing looking up bgp configs by name - #6477

Merged
rcgoodfellow merged 5 commits into
mainfrom
bugfix-6471
Sep 23, 2024
Merged

rcgoodfellow merged 5 commits into
mainfrom
bugfix-6471

Conversation

@internet-diglett

Copy link
Copy Markdown
Contributor

Related

resolves #6471

@david-crespo

Copy link
Copy Markdown
Contributor

Why didn’t a test hit this? Not enough rows in the table to trigger it?

@bnaecker

Copy link
Copy Markdown
Collaborator

@david-crespo The size of the table doesn't matter, but I think you need to run a query looking for a record by name. Any tests we have must be looking up by primary key.

@bnaecker

Copy link
Copy Markdown
Collaborator

But speaking of, @internet-diglett it might be worth writing a regression test in this PR.

@internet-diglett

Copy link
Copy Markdown
Contributor Author

@bnaecker yeah, I was looking into beefing up our tests across the board for networking APIs, but some of that work also overlaps with another PR I'm working on so I don't know which I'm going to put it in at the moment 😂

@elaine-oxide

Copy link
Copy Markdown
Contributor

Please check if this will also fix #6619

Thank you!

@rcgoodfellow

Copy link
Copy Markdown
Contributor

I don't think this PR will fix 6619. The error in that issue is

DatabaseError(Unknown, "query `SELECT COUNT(*) FROM \\"bgp_config\\" WHERE (\\"bgp_config\\".\\"bgp_announce_set_id\\" = $1)` contains a full table/index scan which is explicitly disallowed")

but this PR is adding an index on the bgp_config name column, not the bgp_announce_set_id column.

@rcgoodfellow

Copy link
Copy Markdown
Contributor

We weren't missing an index for looking up BGP configs by name, what we were missing was the time_deleted NOT NULL filter on queries looking BGP configs up by name. There were some other indexes we were missing though, those have now been added.

I added a test that covers the API calls used in #6471 and #6619. Both of those issues are now addressed by this PR.

The test I wrote also surfaced the fact that the diesel count method was being used incorrectly in a few places that has now been fixed.

@elaine-oxide elaine-oxide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments. Reviewed for consistency.

Comment thread nexus/db-model/src/schema_versions.rs Resolved
Comment on lines +1007 to +1008
async fn test_delete_bgp_config_delete_by_name() {
let logctx = dev::test_setup_log("test_delete_bgp_config_by_name");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should rename for clarity, since this deletes both bgp config and announce set.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread schema/crdb/dbinit.sql
CREATE INDEX IF NOT EXISTS lookup_bgp_config_by_bgp_announce_set_id ON omicron.public.bgp_config (
bgp_announce_set_id
) WHERE
time_deleted IS NULL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double check if needed for line 2802

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is an index on a different field that is not already in the primary key so both the index and time deleted constraint are needed.

@elaine-oxide elaine-oxide Sep 23, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so it is not needed for line 2802 because there it is already in the primary key (although I am not understanding how this relates to line 2832 and 2797)?

@david-crespo david-crespo Sep 23, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not needed for 2802 because that table does not have a time_deleted column. Strictly speaking, I believe this would work without the WHERE clause, but it would make the index much bigger because it would be indexing a bunch of deleted rows which will never be queried.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thank you!

CREATE INDEX IF NOT EXISTS lookup_bgp_config_by_bgp_announce_set_id ON omicron.public.bgp_config (
bgp_announce_set_id
) WHERE
time_deleted IS NULL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double check if this is needed for schema/crdb/lookup-bgp-config-indexes/up02.sql

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above, yes this is needed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See follow up comment above.

.select(sps_bgp_peer_config::bgp_config_id)
.count()
.execute_async(&conn)
.get_result_async(&conn)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to select all the rows if you just want to check whether there are any. Something like this should do it:

let configs_exist = diesel::dsl::select(diesel::dsl::exists(
  sps_bgp_peer_config_dsl::switch_port_settings_bgp_peer_config
    .filter(sps_bgp_peer_config::bgp_config_id.eq(id))
))
.get_result_async::<bool>(&conn)
.await?;

Compare:

let has_children = diesel::dsl::select(diesel::dsl::exists(
external_ip::table
.filter(external_ip::dsl::ip_pool_id.eq(pool_id))
.filter(external_ip::dsl::ip_pool_range_id.eq(range_id))
.filter(external_ip::dsl::time_deleted.is_null()),
))
.get_result_async::<bool>(&*conn)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

.filter(bgp_config::time_deleted.is_null())
.count()
.execute_async(&conn)
.get_result_async(&conn)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same deal here

let in_use = diesel::dsl::select(diesel::dsl::exists(
  bgp_config_dsl::bgp_config
    .filter(bgp_config::bgp_announce_set_id.eq(id))
    .filter(bgp_config::time_deleted.is_null())
))
.get_result_async::<bool>(&conn)
.await?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@rcgoodfellow

Copy link
Copy Markdown
Contributor

Thanks for the feedback, @elaine-oxide and @david-crespo. I believe I've addressed all the issues identified.

@rcgoodfellow
rcgoodfellow merged commit 2461d17 into main Sep 23, 2024
@rcgoodfellow
rcgoodfellow deleted the bugfix-6471 branch September 23, 2024 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants