sui_data_ingestion_core/
util.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use object_store::aws::AmazonS3ConfigKey;
use object_store::gcp::GoogleConfigKey;
use object_store::path::Path;
use object_store::{ClientOptions, ObjectStore, RetryConfig};
use std::str::FromStr;
use std::time::Duration;
use sui_types::messages_checkpoint::CheckpointSequenceNumber;
use url::Url;

pub fn create_remote_store_client(
    url: String,
    remote_store_options: Vec<(String, String)>,
    timeout_secs: u64,
) -> Result<Box<dyn ObjectStore>> {
    let retry_config = RetryConfig {
        max_retries: 0,
        retry_timeout: Duration::from_secs(timeout_secs + 1),
        ..Default::default()
    };
    let client_options = ClientOptions::new()
        .with_timeout(Duration::from_secs(timeout_secs))
        .with_allow_http(true);
    let url = Url::parse(&url)?;
    let mut scheme = url.scheme();
    if url.host_str().unwrap_or_default().starts_with("s3") {
        scheme = "s3";
    }
    match scheme {
        "http" | "https" => {
            let http_store = object_store::http::HttpBuilder::new()
                .with_url(url)
                .with_client_options(client_options)
                .with_retry(retry_config)
                .build()?;
            Ok(Box::new(http_store))
        }
        "gs" => {
            let mut builder = object_store::gcp::GoogleCloudStorageBuilder::new()
                .with_url(url.as_str())
                .with_retry(retry_config)
                .with_client_options(client_options);
            for (key, value) in remote_store_options {
                builder = builder.with_config(GoogleConfigKey::from_str(&key)?, value);
            }
            Ok(Box::new(builder.build()?))
        }
        "s3" => {
            let mut builder = object_store::aws::AmazonS3Builder::new()
                .with_url(url.as_str())
                .with_retry(retry_config)
                .with_client_options(client_options);
            for (key, value) in remote_store_options {
                builder = builder.with_config(AmazonS3ConfigKey::from_str(&key)?, value);
            }
            Ok(Box::new(builder.build()?))
        }
        "file" => Ok(Box::new(
            object_store::local::LocalFileSystem::new_with_prefix(url.path())?,
        )),
        _ => Err(anyhow::anyhow!("Unsupported URL scheme: {}", url.scheme())),
    }
}

pub async fn end_of_epoch_data(
    url: String,
    remote_store_options: Vec<(String, String)>,
    timeout_secs: u64,
) -> Result<Vec<CheckpointSequenceNumber>> {
    let client = create_remote_store_client(url, remote_store_options, timeout_secs)?;
    let response = client.get(&Path::from("epochs.json")).await?;
    Ok(serde_json::from_slice(response.bytes().await?.as_ref())?)
}