Skip to content

Commit 0567c2f

Browse files
committed
handle precondition failed for POST
1 parent 00536c4 commit 0567c2f

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

docs/runbook/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This page gathers the available guides to operate a Mithril network.
1010
|------------|------------|------------
1111
| **Genesis manually** | [manual-genesis](./genesis-manually/README.md) | Proceed to manual (re)genesis of the aggregator certificate chain.
1212
| **Era markers** | [era-markers](./era-markers/README.md) | Create and update era markers on the Cardano chain.
13+
| **Downloads statistics** | [downloads statistics](./statistics/README.md) | Display the number of downloads per day and per snapshot.
1314
| **Signer registrations monitoring** | [registrations-monitoring](./registrations-monitoring/README.md) | Gather aggregated data about signer registrations (versions, stake, ...).
1415
| **Update protocol parameters** | [protocol-parameters](./protocol-parameters/README.md) | Update the protocol parameters of a Mithril network.
1516
| **Recompute certificates hash** | [recompute-certificates-hash](./recompute-certificates-hash/README.md) | Recompute the certificates has of an aggregator.

docs/runbook/statistics/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Statistics
2+
3+
## Snapshot downloads per snapshot per day
4+
5+
```sh
6+
$> sqlite3 -table -batch \
7+
$DATA_STORES_DIRECTORY/monitoring.sqlite3 \
8+
< snapshot_downloads.sql
9+
```
10+
11+
The variable `$DATA_STORES_DIRECTORY` should point to the directory where the
12+
databases files are stored (see files in `mithril-aggregator/config` using the
13+
key `data_stores_directory` to know where they are).
14+

mithril-aggregator/src/http_server/routes/statistics_routes.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use mithril_common::MITHRIL_SIGNER_VERSION_HEADER;
21
use std::sync::Arc;
32
use warp::Filter;
43

@@ -17,9 +16,6 @@ fn post_statistics(
1716
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
1817
warp::path!("statistics" / "snapshot")
1918
.and(warp::post())
20-
.and(warp::header::optional::<String>(
21-
MITHRIL_SIGNER_VERSION_HEADER,
22-
))
2319
.and(warp::body::json())
2420
.and(middlewares::with_event_transmitter(
2521
dependency_manager.clone(),
@@ -37,23 +33,20 @@ mod handlers {
3733
use crate::http_server::routes::reply;
3834

3935
pub async fn post_snapshot_statistics(
40-
signer_node_version: Option<String>,
4136
snapshot_message: SnapshotMessage,
4237
event_transmitter: Arc<TransmitterService<EventMessage>>,
4338
) -> Result<impl warp::Reply, Infallible> {
44-
let headers: Vec<(&str, &str)> = signer_node_version
45-
.as_ref()
46-
.map(|v| ("signer-node-version", v.as_str()))
47-
.into_iter()
48-
.collect();
49-
let _ = event_transmitter.send_event_message(
39+
let headers: Vec<(&str, &str)> = Vec::new();
40+
41+
match event_transmitter.send_event_message(
5042
"HTTP::statistics",
5143
"snapshot_downloaded",
5244
&snapshot_message,
5345
headers,
54-
);
55-
56-
Ok(reply::empty(StatusCode::CREATED))
46+
) {
47+
Err(e) => Ok(reply::internal_server_error(e)),
48+
Ok(_) => Ok(reply::empty(StatusCode::CREATED)),
49+
}
5750
}
5851
}
5952

mithril-client/src/aggregator_client/http_client.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl AggregatorHTTPClient {
157157
}
158158

159159
/// Issue a POST HTTP request.
160+
#[async_recursion]
160161
async fn post(&self, url: &str, json: &str) -> Result<Response, AggregatorHTTPClientError> {
161162
let request_builder = Client::new().post(url.to_owned()).json(json);
162163
let current_api_version = self
@@ -168,13 +169,31 @@ impl AggregatorHTTPClient {
168169
let request_builder =
169170
request_builder.header(MITHRIL_API_VERSION_HEADER, current_api_version);
170171

171-
request_builder
172-
.send()
173-
.await
174-
.map_err(|e| AggregatorHTTPClientError::SubsystemError {
175-
message: format!("Error while posting data '{json}' to URL '{url}'."),
172+
let response = request_builder.send().await.map_err(|e| {
173+
AggregatorHTTPClientError::SubsystemError {
174+
message: format!("Error while POSTing data '{json}' to URL='{url}'."),
176175
error: e.into(),
177-
})
176+
}
177+
})?;
178+
179+
match response.status() {
180+
StatusCode::OK => Ok(response),
181+
StatusCode::PRECONDITION_FAILED => {
182+
if self.discard_current_api_version().await.is_some()
183+
&& !self.api_versions.read().await.is_empty()
184+
{
185+
return self.post(url, json).await;
186+
}
187+
188+
Err(self.handle_api_error(&response).await)
189+
}
190+
StatusCode::NOT_FOUND => Err(AggregatorHTTPClientError::RemoteServerLogical(format!(
191+
"Url='{url} not found"
192+
))),
193+
status_code => Err(AggregatorHTTPClientError::RemoteServerTechnical(format!(
194+
"Unhandled error {status_code}"
195+
))),
196+
}
178197
}
179198

180199
/// API version error handling

mithril-client/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ pub mod utils;
1616

1717
pub use entities::*;
1818
pub use message_adapters::{FromCertificateMessageAdapter, FromSnapshotMessageAdapter};
19+
20+
pub const MITHRIL_CLIENT_VERSION_HEADER: &str = "client-node-version";

openapi.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ paths:
362362
$ref: "#/components/schemas/Error"
363363
/statistics/snapshot:
364364
post:
365-
summary: Increments download statistics
366-
description: Increments snapshot download statistics
365+
summary: Records snapshot download event
366+
description: Records snapshot download event
367367
requestBody:
368368
description: Downloaded snapshot
369369
required: true
@@ -373,17 +373,17 @@ paths:
373373
$ref: "#/components/schemas/SnapshotMessage"
374374
responses:
375375
"201":
376-
description: Statistic succesfully posted
376+
description: Event successfully recorded
377377
"400":
378-
description: Increments download statistics bad request
378+
description: Record event bad request
379379
content:
380380
application/json:
381381
schema:
382382
$ref: "#/components/schemas/Error"
383383
"412":
384384
description: API version mismatch
385385
default:
386-
description: Snapshot download statistics error
386+
description: Record event error
387387
content:
388388
application/json:
389389
schema:

0 commit comments

Comments
 (0)