Skip to content

Commit ab8872a

Browse files
authored
Optimize CockroachDB setup for tests (96% reduction) (#493)
Optimizes setup time for CRDB-based tests by seeding + copying a seeded database, rather than populating the database anew during each test. This significantly cuts down on the setup time for any test which wants to use even a simple, single-node Cockroach instance.
1 parent 914924d commit ab8872a

File tree

9 files changed

+186
-69
lines changed

9 files changed

+186
-69
lines changed

.github/buildomat/jobs/build-and-test.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ set -o xtrace
1414
cargo --version
1515
rustc --version
1616

17+
banner clickhouse
18+
ptime -m ./tools/ci_download_clickhouse
19+
20+
banner cockroach
21+
ptime -m bash ./tools/ci_download_cockroachdb
22+
23+
#
24+
# Put "./cockroachdb/bin" and "./clickhouse" on the PATH for the test
25+
# suite.
26+
#
27+
export PATH="$PATH:$PWD/cockroachdb/bin:$PWD/clickhouse"
28+
1729
#
1830
# We build with:
1931
#
@@ -39,18 +51,6 @@ ptime -m cargo +'nightly-2021-11-24' build --locked --all-targets --verbose
3951
banner deploy-check
4052
ptime -m cargo run --bin omicron-package -- check
4153

42-
banner clickhouse
43-
ptime -m ./tools/ci_download_clickhouse
44-
45-
banner cockroach
46-
ptime -m bash ./tools/ci_download_cockroachdb
47-
48-
#
49-
# Put "./cockroachdb/bin" and "./clickhouse" on the PATH for the test
50-
# suite.
51-
#
52-
export PATH="$PATH:$PWD/cockroachdb/bin:$PWD/clickhouse"
53-
5454
#
5555
# NOTE: We're using using the same RUSTFLAGS and RUSTDOCFLAGS as above to avoid
5656
# having to rebuild here.

.github/workflows/rust.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ jobs:
108108
with:
109109
key: ${{ runner.os }}-clickhouse-binary-${{ hashFiles('tools/clickhouse_checksums') }}
110110
path: "clickhouse"
111+
- name: Download ClickHouse
112+
if: steps.cache-clickhouse.outputs.cache-hit != 'true'
113+
run: ./tools/ci_download_clickhouse
114+
- name: Download CockroachDB binary
115+
if: steps.cache-cockroachdb.outputs.cache-hit != 'true'
116+
run: bash ./tools/ci_download_cockroachdb
111117
- name: Build
112118
# We build with:
113119
# - RUSTFLAGS="-D warnings" RUSTDOCFLAGS="-D warnings": disallow warnings
@@ -119,13 +125,7 @@ jobs:
119125
# also gives us a record of which dependencies were used for each CI
120126
# run. Building with `--locked` ensures that the checked-in Cargo.lock
121127
# is up to date.
122-
run: RUSTFLAGS="-D warnings" RUSTDOCFLAGS="-D warnings" cargo build --locked --all-targets --verbose
123-
- name: Download ClickHouse
124-
if: steps.cache-clickhouse.outputs.cache-hit != 'true'
125-
run: ./tools/ci_download_clickhouse
126-
- name: Download CockroachDB binary
127-
if: steps.cache-cockroachdb.outputs.cache-hit != 'true'
128-
run: bash ./tools/ci_download_cockroachdb
128+
run: PATH="$PATH:$PWD/cockroachdb/bin:$PWD/clickhouse" RUSTFLAGS="-D warnings" RUSTDOCFLAGS="-D warnings" cargo build --locked --all-targets --verbose
129129
- name: Run tests
130130
# Use the same RUSTFLAGS and RUSTDOCFLAGS as above to avoid having to
131131
# rebuild here.

Cargo.lock

Lines changed: 40 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ default-members = [
2323
"common",
2424
"nexus",
2525
"nexus/src/db/db-macros",
26-
"nexus/test-utils",
2726
"package",
2827
"rpaths",
2928
"sled-agent",

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ example, on Helios, you'd want `/usr/bin` on your PATH.
8383
--
8484
. CockroachDB v21.1.10.
8585
+
86-
The test suite expects to be able to start a single-node CockroachDB cluster using the `cockroach` executable on your PATH.
86+
The build and test suite expects to be able to start a single-node CockroachDB cluster using the `cockroach` executable on your PATH.
8787
On illumos, MacOS, and Linux, you should be able to use the `tools/ci_download_cockroachdb` script to fetch the official CockroachDB binary. It will be put into `./cockroachdb/bin/cockroach`.
8888
Alternatively, you can follow the https://www.cockroachlabs.com/docs/stable/install-cockroachdb.html[official CockroachDB installation instructions for your platform].
8989

nexus/test-utils/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MPL-2.0"
88
anyhow = "1.0"
99
bytes = "1.0.1"
1010
chrono = { version = "0.4", features = [ "serde" ] }
11-
dropshot = { git = "https://github.com/oxidecomputer/dropshot", branch = "main" }
11+
dropshot = { git = "https://github.com/oxidecomputer/dropshot", branch = "main", features = [ "usdt-probes" ] }
1212
http = "0.2.5"
1313
hyper = "0.14"
1414
omicron-common = { path = "../../common" }
@@ -24,3 +24,8 @@ serde = { version = "1.0", features = [ "derive" ] }
2424
serde_json = "1.0"
2525
slog = { version = "2.7", features = [ "max_level_trace", "release_max_level_debug" ] }
2626
uuid = { version = "0.8", features = [ "serde", "v4" ] }
27+
28+
[build-dependencies]
29+
dropshot = { git = "https://github.com/oxidecomputer/dropshot", branch = "main", features = [ "usdt-probes" ] }
30+
omicron-test-utils = { path = "../../test-utils" }
31+
tokio = { version = "1.14" }

nexus/test-utils/build.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
use dropshot::{test_util::LogContext, ConfigLogging, ConfigLoggingLevel};
6+
use omicron_test_utils::dev::test_setup_database_seed;
7+
8+
// Creates a "pre-populated" CockroachDB storage directory, which
9+
// subsequent tests can copy instead of creating themselves.
10+
//
11+
// Is it critical this happens at build-time? No. However, it
12+
// makes it more convenient for tests to assume this seeded
13+
// directory exists, rather than all attempting to create it
14+
// concurrently.
15+
//
16+
// Refer to the documentation of [`test_setup_database_seed`] for
17+
// more context.
18+
#[tokio::main]
19+
async fn main() {
20+
println!("cargo:rerun-if-changed=build.rs");
21+
println!("cargo:rerun-if-changed=../../common/src/sql/dbinit.sql");
22+
println!("cargo:rerun-if-changed=../../tools/cockroachdb_checksums");
23+
println!("cargo:rerun-if-changed=../../tools/cockroachdb_version");
24+
25+
let logctx = LogContext::new(
26+
"crdb_seeding",
27+
&ConfigLogging::StderrTerminal { level: ConfigLoggingLevel::Info },
28+
);
29+
30+
test_setup_database_seed(&logctx.log).await;
31+
}

0 commit comments

Comments
 (0)