From c81881f0225713dfd4a5774074801a0d6e3a1bf3 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 09:21:21 -0400 Subject: [PATCH 01/17] attempt to cache rust builds --- .github/workflows/release.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8170c9dd..3baa1cec 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -38,6 +38,7 @@ jobs: target: ${{ matrix.target }} if: startsWith(matrix.os, 'ubuntu') - uses: rui314/setup-mold@v1 + - uses: Swatinem/rust-cache@v2 - uses: taiki-e/upload-rust-binary-action@v1 with: bin: corrosion From 60fb2958226db11a93c7e00e34b7a9d90507dfba Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 09:24:49 -0400 Subject: [PATCH 02/17] key rust cache per os + target --- .github/workflows/release.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3baa1cec..dc2eec10 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,8 +37,13 @@ jobs: with: target: ${{ matrix.target }} if: startsWith(matrix.os, 'ubuntu') + - uses: rui314/setup-mold@v1 + - uses: Swatinem/rust-cache@v2 + with: + key: ${{ matrix.os }} ${{ matrix.target }} + - uses: taiki-e/upload-rust-binary-action@v1 with: bin: corrosion From fb30bb83a1053500368d8d91259a3d9eee42188b Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 09:41:30 -0400 Subject: [PATCH 03/17] run tests in ci --- .github/workflows/ci.yml | 45 +++++++++++++++++++++++++++++++++++ .github/workflows/release.yml | 3 ++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..46a08ffc --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,45 @@ +on: + push: + branches: [ main ] + pull_request: + branches: + - main + +name: CI +env: + RUSTFLAGS: -D warnings + CARGO_TERM_COLOR: always + +jobs: + build: + name: Test + strategy: + matrix: + include: + - target: x86_64-apple-darwin + os: macos-latest + - target: aarch64-apple-darwin + os: macos-latest + - target: x86_64-unknown-linux-gnu + os: ubuntu-latest + - target: aarch64-unknown-linux-gnu + os: ubuntu-latest + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v3 + + - uses: dtolnay/rust-toolchain@stable + with: + targets: [ "${{ matrix.target }}" ] + + - uses: Swatinem/rust-cache@v2 + with: + key: ${{ matrix.os }} ${{ matrix.target }} + + - name: Install latest nextest release + uses: taiki-e/install-action@nextest + + - name: Test with latest nextest release + run: cargo nextest run --workspace \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dc2eec10..37e9e315 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -32,12 +32,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 + - name: Install cross-compilation tools uses: taiki-e/setup-cross-toolchain-action@v1 with: target: ${{ matrix.target }} if: startsWith(matrix.os, 'ubuntu') - + - uses: rui314/setup-mold@v1 - uses: Swatinem/rust-cache@v2 From 2739b3c5818c35e75dcaed8cf8b346382809ffd4 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 09:42:29 -0400 Subject: [PATCH 04/17] my bad, targets is comma separated --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 46a08ffc..2f79de45 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,7 @@ jobs: - uses: dtolnay/rust-toolchain@stable with: - targets: [ "${{ matrix.target }}" ] + targets: ${{ matrix.target }} - uses: Swatinem/rust-cache@v2 with: From 8547aab18430d2481c68c44c15159c882baef15b Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 09:56:15 -0400 Subject: [PATCH 05/17] try a different cache key to see if it fixes the 500 from gh cache service. run tests for specific target --- .github/workflows/ci.yml | 4 ++-- .github/workflows/release.yml | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f79de45..b5a27d6f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,10 +36,10 @@ jobs: - uses: Swatinem/rust-cache@v2 with: - key: ${{ matrix.os }} ${{ matrix.target }} + key: ${{ matrix.os }}-${{ matrix.target }} - name: Install latest nextest release uses: taiki-e/install-action@nextest - name: Test with latest nextest release - run: cargo nextest run --workspace \ No newline at end of file + run: cargo nextest run --workspace --target ${{ matrix.target }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 37e9e315..569d870e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,6 +8,10 @@ on: tags: - v[0-9]+.* +env: + RUSTFLAGS: -D warnings + CARGO_TERM_COLOR: always + jobs: create-release: runs-on: ubuntu-latest @@ -32,7 +36,7 @@ jobs: runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - + - name: Install cross-compilation tools uses: taiki-e/setup-cross-toolchain-action@v1 with: @@ -43,7 +47,7 @@ jobs: - uses: Swatinem/rust-cache@v2 with: - key: ${{ matrix.os }} ${{ matrix.target }} + key: ${{ matrix.os }}-${{ matrix.target }} - uses: taiki-e/upload-rust-binary-action@v1 with: From e5c2f5e5f8c0183543660323da353939d436cf8b Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 10:03:28 -0400 Subject: [PATCH 06/17] use cross-compilation tools even for tests --- .github/workflows/ci.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b5a27d6f..933858a1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,7 @@ jobs: name: Test strategy: matrix: + fail-fast: false include: - target: x86_64-apple-darwin os: macos-latest @@ -30,9 +31,13 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: dtolnay/rust-toolchain@stable + - name: Install cross-compilation tools + uses: taiki-e/setup-cross-toolchain-action@v1 with: - targets: ${{ matrix.target }} + target: ${{ matrix.target }} + if: startsWith(matrix.os, 'ubuntu') + + - uses: rui314/setup-mold@v1 - uses: Swatinem/rust-cache@v2 with: From f274845da5ec6c633aa3ce5b8ccfb619a0f5a6b5 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 10:05:04 -0400 Subject: [PATCH 07/17] wrong spot for fail-fast --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 933858a1..1665b2a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,8 +14,8 @@ jobs: build: name: Test strategy: + fail-fast: false matrix: - fail-fast: false include: - target: x86_64-apple-darwin os: macos-latest From d136c00cf251f444d4b3a6e8da18b956e678292c Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 10:10:11 -0400 Subject: [PATCH 08/17] try cross-compilation tools even for macos --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1665b2a5..da1dcb73 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,7 +35,7 @@ jobs: uses: taiki-e/setup-cross-toolchain-action@v1 with: target: ${{ matrix.target }} - if: startsWith(matrix.os, 'ubuntu') + # if: startsWith(matrix.os, 'ubuntu') - uses: rui314/setup-mold@v1 From 266ba13b58587f12453b41e514be390e44497b67 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 10:31:16 -0400 Subject: [PATCH 09/17] use ipv4 if that's what the gossip server listens on --- crates/corro-agent/src/api/peer.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/corro-agent/src/api/peer.rs b/crates/corro-agent/src/api/peer.rs index 85f9e535..8f231365 100644 --- a/crates/corro-agent/src/api/peer.rs +++ b/crates/corro-agent/src/api/peer.rs @@ -1,5 +1,6 @@ use std::cmp; use std::collections::HashMap; +use std::net::SocketAddr; use std::ops::RangeInclusive; use std::sync::Arc; @@ -273,7 +274,12 @@ async fn build_quinn_client_config(config: &GossipConfig) -> eyre::Result eyre::Result { let client_config = build_quinn_client_config(config).await?; - let mut client = quinn::Endpoint::client("[::]:0".parse()?)?; + + let client_bind_addr = match config.bind_addr { + SocketAddr::V4(_) => "0.0.0.0:0".parse()?, + SocketAddr::V6(_) => "[::]:0".parse()?, + }; + let mut client = quinn::Endpoint::client(client_bind_addr)?; client.set_default_client_config(client_config); Ok(client) From 1412618b11c6bf6de577a11728f61bfceddba953 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 10:37:47 -0400 Subject: [PATCH 10/17] try sccache --- .github/workflows/ci.yml | 7 ++++--- .github/workflows/release.yml | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index da1dcb73..885d37af 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,6 +9,8 @@ name: CI env: RUSTFLAGS: -D warnings CARGO_TERM_COLOR: always + SCCACHE_GHA_ENABLED: "true" + RUSTC_WRAPPER: "sccache" jobs: build: @@ -39,9 +41,8 @@ jobs: - uses: rui314/setup-mold@v1 - - uses: Swatinem/rust-cache@v2 - with: - key: ${{ matrix.os }}-${{ matrix.target }} + - name: Run sccache-cache + uses: mozilla-actions/sccache-action@v0.0.3 - name: Install latest nextest release uses: taiki-e/install-action@nextest diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 569d870e..674d0888 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,6 +11,8 @@ on: env: RUSTFLAGS: -D warnings CARGO_TERM_COLOR: always + SCCACHE_GHA_ENABLED: "true" + RUSTC_WRAPPER: "sccache" jobs: create-release: @@ -45,9 +47,8 @@ jobs: - uses: rui314/setup-mold@v1 - - uses: Swatinem/rust-cache@v2 - with: - key: ${{ matrix.os }}-${{ matrix.target }} + - name: Run sccache-cache + uses: mozilla-actions/sccache-action@v0.0.3 - uses: taiki-e/upload-rust-binary-action@v1 with: From 6a9dca2e793b301f3c8aa0a99d77cbe6c0502695 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 11:22:31 -0400 Subject: [PATCH 11/17] go back to rust-cache to cache more stuff, maybe fix cross-compiled tests --- .github/workflows/ci.yml | 27 ++++++++++++++++----------- .github/workflows/release.yml | 11 ++++------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 885d37af..d14b037b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,20 +32,25 @@ jobs: steps: - uses: actions/checkout@v3 - - - name: Install cross-compilation tools - uses: taiki-e/setup-cross-toolchain-action@v1 + + - uses: rui314/setup-mold@v1 + + - name: Install Rust stable + uses: dtolnay/rust-toolchain@stable with: + toolchain: ${{ env.rust_stable }} target: ${{ matrix.target }} - # if: startsWith(matrix.os, 'ubuntu') - - uses: rui314/setup-mold@v1 - - - name: Run sccache-cache - uses: mozilla-actions/sccache-action@v0.0.3 - - - name: Install latest nextest release - uses: taiki-e/install-action@nextest + - name: Install cargo-nextest + uses: taiki-e/install-action@v2 + with: + tool: cargo-nextest + + - uses: taiki-e/setup-cross-toolchain-action@v1 + with: + target: ${{ matrix.target }} + + - uses: Swatinem/rust-cache@v2 - name: Test with latest nextest release run: cargo nextest run --workspace --target ${{ matrix.target }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 674d0888..41aef9b8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -39,16 +39,13 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Install cross-compilation tools - uses: taiki-e/setup-cross-toolchain-action@v1 + - uses: rui314/setup-mold@v1 + + - uses: taiki-e/setup-cross-toolchain-action@v1 with: target: ${{ matrix.target }} - if: startsWith(matrix.os, 'ubuntu') - - - uses: rui314/setup-mold@v1 - - name: Run sccache-cache - uses: mozilla-actions/sccache-action@v0.0.3 + - uses: Swatinem/rust-cache@v2 - uses: taiki-e/upload-rust-binary-action@v1 with: From 7ea199d9113c4c57090b45ef345e1b4a1a411034 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 11:24:05 -0400 Subject: [PATCH 12/17] mis-use of dtolnay/rust-toolchain --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d14b037b..6e1166b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,7 +38,6 @@ jobs: - name: Install Rust stable uses: dtolnay/rust-toolchain@stable with: - toolchain: ${{ env.rust_stable }} target: ${{ matrix.target }} - name: Install cargo-nextest From edf15934570e2e0eceaba79e83a156321e1a9aff Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 11:24:36 -0400 Subject: [PATCH 13/17] remove sccache wrapper env --- .github/workflows/ci.yml | 2 -- .github/workflows/release.yml | 2 -- 2 files changed, 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6e1166b7..cdd9cb97 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,8 +9,6 @@ name: CI env: RUSTFLAGS: -D warnings CARGO_TERM_COLOR: always - SCCACHE_GHA_ENABLED: "true" - RUSTC_WRAPPER: "sccache" jobs: build: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 41aef9b8..855e2a86 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,8 +11,6 @@ on: env: RUSTFLAGS: -D warnings CARGO_TERM_COLOR: always - SCCACHE_GHA_ENABLED: "true" - RUSTC_WRAPPER: "sccache" jobs: create-release: From 348708854a71e7f5413b5aa9ffa0299f5b915081 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 11:45:22 -0400 Subject: [PATCH 14/17] still cache on failure --- .github/workflows/ci.yml | 2 ++ .github/workflows/release.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cdd9cb97..26ab4ddc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,6 +48,8 @@ jobs: target: ${{ matrix.target }} - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true - name: Test with latest nextest release run: cargo nextest run --workspace --target ${{ matrix.target }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 855e2a86..7e1e4494 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -44,6 +44,8 @@ jobs: target: ${{ matrix.target }} - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true - uses: taiki-e/upload-rust-binary-action@v1 with: From dd26b9846b901552e784111ab5caf634d40cef9a Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 12:18:46 -0400 Subject: [PATCH 15/17] relax tests, don't use cross for release from macos x86 to aarch64 --- .github/workflows/ci.yml | 8 -------- .github/workflows/release.yml | 1 + 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 26ab4ddc..615a9785 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,12 +19,8 @@ jobs: include: - target: x86_64-apple-darwin os: macos-latest - - target: aarch64-apple-darwin - os: macos-latest - target: x86_64-unknown-linux-gnu os: ubuntu-latest - - target: aarch64-unknown-linux-gnu - os: ubuntu-latest runs-on: ${{ matrix.os }} @@ -43,10 +39,6 @@ jobs: with: tool: cargo-nextest - - uses: taiki-e/setup-cross-toolchain-action@v1 - with: - target: ${{ matrix.target }} - - uses: Swatinem/rust-cache@v2 with: cache-on-failure: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7e1e4494..3341b5fe 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,6 +42,7 @@ jobs: - uses: taiki-e/setup-cross-toolchain-action@v1 with: target: ${{ matrix.target }} + if: startsWith(matrix.os, 'ubuntu') - uses: Swatinem/rust-cache@v2 with: From 22401eae51811eb5229574b1efdc52008295d791 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 14:34:19 -0400 Subject: [PATCH 16/17] don't install tracing subscriber in corro sync command, already installed --- crates/corrosion/src/command/consul/sync.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/corrosion/src/command/consul/sync.rs b/crates/corrosion/src/command/consul/sync.rs index c269da56..6e44ed86 100644 --- a/crates/corrosion/src/command/consul/sync.rs +++ b/crates/corrosion/src/command/consul/sync.rs @@ -21,8 +21,6 @@ pub async fn run>( api_addr: SocketAddr, db_path: P, ) -> eyre::Result<()> { - tracing_subscriber::fmt::init(); - let (mut tripwire, tripwire_worker) = tripwire::Tripwire::new_signals(); let node: &'static str = Box::leak( From c265cf9aec55bffd4e3fd8d7b58107f2b92d2aab Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 21 Aug 2023 15:19:31 -0400 Subject: [PATCH 17/17] don't build the schema from corro-consul, leave that to corrosion --- Cargo.lock | 1 + crates/corro-api-types/src/lib.rs | 27 +++++ crates/corro-types/src/config.rs | 5 - crates/corrosion/Cargo.toml | 1 + crates/corrosion/src/command/consul/sync.rs | 127 ++++++++++++-------- 5 files changed, 103 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 10b8c429..84b94d90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -830,6 +830,7 @@ dependencies = [ "consul-client", "corro-admin", "corro-agent", + "corro-api-types", "corro-client", "corro-tests", "corro-tpl", diff --git a/crates/corro-api-types/src/lib.rs b/crates/corro-api-types/src/lib.rs index 145ad730..c40a1212 100644 --- a/crates/corro-api-types/src/lib.rs +++ b/crates/corro-api-types/src/lib.rs @@ -168,6 +168,33 @@ impl ColumnType { _ => return None, }) } + + pub fn from_str(s: &str) -> Option { + Some(match s { + "INTEGER" => Self::Integer, + "REAL" => Self::Float, + "TEXT" => Self::Text, + "BLOB" => Self::Blob, + _ => return None, + }) + } +} + +impl FromSql for ColumnType { + fn column_result(value: ValueRef<'_>) -> rusqlite::types::FromSqlResult { + match value { + ValueRef::Text(s) => Ok(match String::from_utf8_lossy(s).as_ref() { + "INTEGER" => Self::Integer, + "REAL" => Self::Float, + "TEXT" => Self::Text, + "BLOB" => Self::Blob, + _ => { + return Err(FromSqlError::InvalidType); + } + }), + _ => Err(FromSqlError::InvalidType), + } + } } #[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)] diff --git a/crates/corro-types/src/config.rs b/crates/corro-types/src/config.rs index 08ceb01b..4c709b0b 100644 --- a/crates/corro-types/src/config.rs +++ b/crates/corro-types/src/config.rs @@ -273,10 +273,5 @@ pub enum LogFormat { #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "kebab-case")] pub struct ConsulConfig { - #[serde(default)] - pub extra_services_columns: Vec, - #[serde(default)] - pub extra_statements: Vec, - pub client: consul_client::Config, } diff --git a/crates/corrosion/Cargo.toml b/crates/corrosion/Cargo.toml index 4f4e4da5..7035813c 100644 --- a/crates/corrosion/Cargo.toml +++ b/crates/corrosion/Cargo.toml @@ -20,6 +20,7 @@ corro-admin = { path = "../corro-admin" } corro-agent = { path = "../corro-agent" } corro-client = { path = "../corro-client" } corro-types = { path = "../corro-types" } +corro-api-types = { path = "../corro-api-types" } crc32fast = { workspace = true } eyre = { workspace = true } fallible-iterator = { workspace = true } diff --git a/crates/corrosion/src/command/consul/sync.rs b/crates/corrosion/src/command/consul/sync.rs index 6e44ed86..e370aa65 100644 --- a/crates/corrosion/src/command/consul/sync.rs +++ b/crates/corrosion/src/command/consul/sync.rs @@ -1,4 +1,5 @@ use consul_client::{AgentCheck, AgentService, Client}; +use corro_api_types::ColumnType; use corro_client::CorrosionClient; use corro_types::{api::Statement, config::ConsulConfig}; use metrics::{histogram, increment_counter}; @@ -35,9 +36,7 @@ pub async fn run>( info!("Setting up corrosion for consul sync"); setup( - &corrosion, - config.extra_services_columns.as_slice(), - config.extra_statements.as_slice(), + &corrosion ) .await?; @@ -118,12 +117,9 @@ pub async fn run>( async fn setup( corrosion: &CorrosionClient, - extra_services_columns: &[String], - extra_statements: &[String], ) -> eyre::Result<()> { + let mut conn = corrosion.pool().get().await?; { - let mut conn = corrosion.pool().get().await?; - let tx = conn.transaction()?; info!("Creating internal tables"); @@ -143,51 +139,51 @@ async fn setup( tx.commit()?; } info!("Ensuring schema..."); - corrosion - .schema(&build_schema(extra_services_columns, extra_statements)) - .await?; - Ok(()) -} -fn build_schema(extra_services_columns: &[String], extra_statements: &[String]) -> Vec { - let extra = extra_services_columns.join(","); - let mut statements = vec![ - Statement::Simple(format!("CREATE TABLE consul_services ( - node TEXT NOT NULL, - id TEXT NOT NULL, - name TEXT NOT NULL DEFAULT '', - tags TEXT NOT NULL DEFAULT '[]', - meta TEXT NOT NULL DEFAULT '{{}}', - port INTEGER NOT NULL DEFAULT 0, - address TEXT NOT NULL DEFAULT '', - updated_at INTEGER NOT NULL DEFAULT 0, - - {} - - PRIMARY KEY (node, id) - ) WITHOUT ROWID;", if extra.is_empty() { String::new() } else {format!("{extra},")})), - Statement::Simple("CREATE INDEX consul_services_node_id_updated_at ON consul_services (node, id, updated_at);".to_string()), - - Statement::Simple("CREATE TABLE consul_checks ( - node TEXT NOT NULL, - id TEXT NOT NULL, - service_id TEXT NOT NULL DEFAULT '', - service_name TEXT NOT NULL DEFAULT '', - name TEXT NOT NULL DEFAULT '', - status TEXT NOT NULL DEFAULT '', - output TEXT NOT NULL DEFAULT '', - updated_at INTEGER NOT NULL DEFAULT 0, - PRIMARY KEY (node, id) - ) WITHOUT ROWID;".to_string()), - Statement::Simple("CREATE INDEX consul_checks_node_id_updated_at ON consul_checks (node, id, updated_at);".to_string()), - Statement::Simple("CREATE INDEX consul_checks_node_service_id ON consul_checks (node, service_id);".to_string()), + struct ColumnInfo { + name: String, + kind: corro_api_types::ColumnType + } + + let col_infos: Vec = conn.prepare("PRAGMA table_info(consul_services)")?.query_map([], |row| Ok(ColumnInfo { name: row.get(1)?, kind: row.get(2)? })).map_err(|e| eyre::eyre!("could not query consul_services' table_info: {e}"))?.collect::, _>>()?; + + let expected_cols = [ + ("node", ColumnType::Text), + ("id", ColumnType::Text), + ("name", ColumnType::Text), + ("tags", ColumnType::Text), + ("meta", ColumnType::Text), + ("port", ColumnType::Integer), + ("address", ColumnType::Text), + ("updated_at", ColumnType::Integer), + ]; + + for (name, kind) in expected_cols { + if col_infos.iter().find(|info| info.name == name && info.kind == kind ).is_none() { + eyre::bail!("expected a column consul_services.{name} w/ type {kind:?}"); + } + } + + let col_infos: Vec = conn.prepare("PRAGMA table_info(consul_checks)")?.query_map([], |row| Ok(ColumnInfo { name: row.get(1)?, kind: row.get(2)? })).map_err(|e| eyre::eyre!("could not query consul_checks' table_info: {e}"))?.collect::, _>>()?; + + let expected_cols = [ + ("node", ColumnType::Text), + ("id", ColumnType::Text), + ("service_id", ColumnType::Text), + ("service_name", ColumnType::Text), + ("name", ColumnType::Text), + ("status", ColumnType::Text), + ("output", ColumnType::Text), + ("updated_at", ColumnType::Integer), ]; - for s in extra_statements { - statements.push(Statement::Simple(s.clone())); + for (name, kind) in expected_cols { + if col_infos.iter().find(|info| info.name == name && info.kind == kind ).is_none() { + eyre::bail!("expected a column consul_checks.{name} w/ type {kind:?}"); + } } - statements + Ok(()) } #[derive(Debug, Serialize, Deserialize)] @@ -582,10 +578,39 @@ mod tests { _ = tracing_subscriber::fmt::try_init(); let (tripwire, tripwire_worker, tripwire_tx) = Tripwire::new_simple(); - let ta1 = launch_test_agent(|conf| conf.build(), tripwire.clone()).await?; + let tmpdir = tempfile::TempDir::new()?; + tokio::fs::write(tmpdir.path().join("consul.sql"), b" + CREATE TABLE consul_services ( + node TEXT NOT NULL, + id TEXT NOT NULL, + name TEXT NOT NULL DEFAULT '', + tags TEXT NOT NULL DEFAULT '[]', + meta TEXT NOT NULL DEFAULT '{}', + port INTEGER NOT NULL DEFAULT 0, + address TEXT NOT NULL DEFAULT '', + updated_at INTEGER NOT NULL DEFAULT 0, + app_id INTEGER AS (CAST(JSON_EXTRACT(meta, '$.app_id') AS INTEGER)), + + PRIMARY KEY (node, id) + ); + + CREATE TABLE consul_checks ( + node TEXT NOT NULL, + id TEXT NOT NULL, + service_id TEXT NOT NULL DEFAULT '', + service_name TEXT NOT NULL DEFAULT '', + name TEXT NOT NULL DEFAULT '', + status TEXT NOT NULL DEFAULT '', + output TEXT NOT NULL DEFAULT '', + updated_at INTEGER NOT NULL DEFAULT 0, + PRIMARY KEY (node, id) + ); + ").await?; + + let ta1 = launch_test_agent(|conf| conf.add_schema_path(tmpdir.path().display().to_string()).build(), tripwire.clone()).await?; let ta2 = launch_test_agent( |conf| { - conf.bootstrap(vec![ta1.agent.gossip_addr().to_string()]) + conf.bootstrap(vec![ta1.agent.gossip_addr().to_string()]).add_schema_path(tmpdir.path().display().to_string()) .build() }, tripwire.clone(), @@ -596,8 +621,6 @@ mod tests { setup( &ta1_client, - &["app_id INTEGER AS (CAST (JSON_EXTRACT (meta, '$.app_id') AS INTEGER))".into()], - &[], ) .await?; @@ -653,8 +676,6 @@ mod tests { setup( &ta2_client, - &["app_id INTEGER AS (CAST (JSON_EXTRACT (meta, '$.app_id') AS INTEGER))".into()], - &[], ) .await?;