Skip to content

Commit

Permalink
api: retry conflicts as a workaround
Browse files Browse the repository at this point in the history
This is a workaround for comment creation / updates conflicting with the `loom`
service that indexes threads objects concurrently. We will be fixing
this properly in the backend, but as a workaround we will retry
conflicts for now.
  • Loading branch information
mcobzarenco committed Feb 27, 2023
1 parent 1ebc746 commit c7011eb
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# v0.12.3

## Changed

- Retry conflicts as a workaround for comment creation / updates generating
spurious conflicts. We will be fixing this properly in the backend, but as a
workaround we will retry conflicts for now.

# v0.12.2

- Rename "triggers" to "streams" following the rename in the API
- Removed semantic url joins to support deployments within a subdirectory
- Added functionality to use moon forms both in `LabelDef`s and in `AnnotatedComments`s
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ The [api](/api) directory contains a Rust client library for reinfer which can b

Statically linked binaries with no dependencies are provided for selected platforms:

- [Linux (x86_64-unknown-linux-musl)](https://reinfer.io/public/cli/bin/x86_64-unknown-linux-musl/0.12.2/re)
- [macOS (x86_64-apple-darwin)](https://reinfer.io/public/cli/bin/x86_64-apple-darwin/0.12.2/re)
- [Windows (x86_64-pc-windows-gnu)](https://reinfer.io/public/cli/bin/x86_64-pc-windows-gnu/0.12.2/re.exe)
- [Linux (x86_64-unknown-linux-musl)](https://reinfer.io/public/cli/bin/x86_64-unknown-linux-musl/0.12.3/re)
- [macOS (x86_64-apple-darwin)](https://reinfer.io/public/cli/bin/x86_64-apple-darwin/0.12.3/re)
- [Windows (x86_64-pc-windows-gnu)](https://reinfer.io/public/cli/bin/x86_64-pc-windows-gnu/0.12.3/re.exe)

### Debian / Ubuntu

You can download a `.deb` package [here](https://reinfer.io/public/cli/debian/reinfer-cli_0.12.2_amd64.deb).
You can download a `.deb` package [here](https://reinfer.io/public/cli/debian/reinfer-cli_0.12.3_amd64.deb).

### From Source

Expand Down
2 changes: 1 addition & 1 deletion api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reinfer-client"
version = "0.12.2"
version = "0.12.3"
description = "API client for Re:infer, the conversational data intelligence platform"
homepage = "https://github.com/reinfer/cli"
readme = "README.md"
Expand Down
53 changes: 48 additions & 5 deletions api/src/retry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use reqwest::{blocking::Response, Result};
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
use std::thread::sleep;
use std::time::Duration;
use reqwest::{blocking::Response, Result, StatusCode};
use std::{
sync::atomic::{AtomicBool, Ordering::SeqCst},
thread::sleep,
time::Duration,
};

/// Strategy to use if retrying .
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -60,7 +62,7 @@ impl Retrier {
}

match send_request() {
Ok(response) if response.status().is_server_error() => {
Ok(response) if should_retry(&response) => {
warn_and_sleep!(format!("{} for {}", response.status(), response.url()))
}
Err(error) if error.is_timeout() => warn_and_sleep!(error),
Expand All @@ -74,6 +76,14 @@ impl Retrier {
}
}

fn should_retry(response: &Response) -> bool {
// TODO(mcobzarenco): we retry conflicts as a workaround for comment
// creation / updates conflicting with the `loom` service that indexes
// threads objects concurrently. We will be fixing this properly in the
// backend, but as a workaround we will retry conflicts for now.
response.status().is_server_error() || response.status() == StatusCode::CONFLICT
}

#[cfg(test)]
mod tests {
use super::{Retrier, RetryConfig, RetryStrategy};
Expand Down Expand Up @@ -201,4 +211,37 @@ mod tests {
.is_timeout());
timeout.assert();
}

#[test]
fn test_retry_conflicts() {
// TODO(mcobzarenco): we retry conflicts as a workaround for comment
// creation / updates conflicting with the `loom` service that indexes
// threads objects concurrently. We will be fixing this properly in the
// backend, but as a workaround we will retry conflicts for now.

let mut handler = Retrier::new(RetryConfig {
strategy: RetryStrategy::Always,
max_retry_count: 5,
base_wait: Duration::from_secs(0),
backoff_factor: 0.0,
});
let client = Client::builder().build().unwrap();

// Retries up to N times on timeout for non-first-requests.
for i_retry in 0..10 {
let err = mock("POST", "/")
.with_status(409)
.expect((i_retry + 1).into())
.create();
handler.config.max_retry_count = i_retry;
assert_eq!(
handler
.with_retries(|| client.post(format!("http://{}", server_address())).send())
.unwrap()
.status(),
409
);
err.assert();
}
}
}
4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reinfer-cli"
version = "0.12.2"
version = "0.12.3"
description = "Command line interface for Re:infer, the conversational data intelligence platform"
homepage = "https://github.com/reinfer/cli"
readme = "README.md"
Expand Down Expand Up @@ -34,7 +34,7 @@ serde_json = "1.0.87"
structopt = { version = "0.3.26", default-features = false }
url = { version = "2.3.1", features = ["serde"] }

reinfer-client = { version = "0.12.2", path = "../api" }
reinfer-client = { version = "0.12.3", path = "../api" }

[dev-dependencies]
pretty_assertions = "1.3.0"
Expand Down

0 comments on commit c7011eb

Please sign in to comment.