Skip to content

Commit

Permalink
ref(proguard): Use Chunked<ProguardMapping> for proguard upload
Browse files Browse the repository at this point in the history
  • Loading branch information
szokeasaurusrex committed Dec 4, 2024
1 parent 2a3565e commit 87444d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 47 deletions.
13 changes: 13 additions & 0 deletions src/utils/chunks/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ where
}
}

impl<T> Assemblable for &T
where
T: Assemblable,
{
fn name(&self) -> Cow<str> {
(*self).name()
}

fn debug_id(&self) -> Option<DebugId> {
(*self).debug_id()
}
}

impl<T> Assemblable for Chunked<T>
where
T: Assemblable,
Expand Down
16 changes: 15 additions & 1 deletion src/utils/proguard/mapping.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use symbolic::common::ByteView;
use std::borrow::Cow;

use symbolic::common::{ByteView, DebugId};
use thiserror::Error;
use uuid::Uuid;

use crate::utils::chunks::Assemblable;

#[derive(Debug, Error)]
pub enum ProguardMappingError {
#[error("Proguard mapping does not contain line information")]
Expand Down Expand Up @@ -62,3 +66,13 @@ impl AsRef<[u8]> for ProguardMapping<'_> {
self.bytes.as_ref()
}
}

impl Assemblable for ProguardMapping<'_> {
fn name(&self) -> Cow<str> {
format!("proguard/{}.txt", self.uuid).into()
}

fn debug_id(&self) -> Option<DebugId> {
None
}
}
55 changes: 9 additions & 46 deletions src/utils/proguard/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
//! Proguard mappings, while we work on a more permanent solution, which will
//! work for all different types of debug files.
use std::borrow::Cow;
use std::thread;
use std::time::{Duration, Instant};

use anyhow::Result;
use indicatif::ProgressStyle;
use sha1_smol::Digest;

use crate::api::{Api, ChunkUploadOptions, ChunkedDifRequest, ChunkedFileState};
use crate::utils::chunks::{self, Chunk};
use crate::utils::fs::get_sha1_checksums;
use crate::api::{Api, ChunkUploadOptions, ChunkedFileState};
use crate::utils::chunks;
use crate::utils::chunks::Chunked;
use crate::utils::proguard::ProguardMapping;

/// How often to poll the server for the status of the assembled mappings.
Expand All @@ -25,43 +23,6 @@ const ASSEMBLE_POLL_INTERVAL: Duration = Duration::from_secs(1);
// usually was almost instantaneous, so this should probably be enough time.
const ASSEMBLE_POLL_TIMEOUT: Duration = Duration::from_secs(120);

struct ChunkedMapping {
raw_data: Vec<u8>,
hash: Digest,
chunk_hashes: Vec<Digest>,
file_name: String,
chunk_size: usize,
}

impl ChunkedMapping {
fn try_from_mapping(mapping: &ProguardMapping, chunk_size: u64) -> Result<Self> {
let raw_data = mapping.as_ref().to_vec();
let file_name = format!("/proguard/{}.txt", mapping.uuid());

let (hash, chunk_hashes) = get_sha1_checksums(&raw_data, chunk_size as usize)?;
Ok(Self {
raw_data,
hash,
chunk_hashes,
file_name,
chunk_size: chunk_size.try_into()?,
})
}

fn chunks(&self) -> impl Iterator<Item = Chunk<'_>> {
self.raw_data
.chunks(self.chunk_size)
.zip(self.chunk_hashes.iter())
.map(|(chunk, hash)| Chunk((*hash, chunk)))
}
}

impl<'a> From<&'a ChunkedMapping> for ChunkedDifRequest<'a> {
fn from(value: &'a ChunkedMapping) -> Self {
ChunkedDifRequest::new(Cow::from(&value.file_name), &value.chunk_hashes, value.hash)
}
}

/// Uploads a set of Proguard mappings to Sentry.
/// Blocks until the mappings have been assembled (up to ASSEMBLE_POLL_TIMEOUT).
/// Returns an error if the mappings fail to assemble, or if the timeout is reached.
Expand All @@ -71,17 +32,19 @@ pub fn chunk_upload(
org: &str,
project: &str,
) -> Result<()> {
let chunked_mappings: Vec<ChunkedMapping> = mappings
let chunked_mappings = mappings
.iter()
.map(|mapping| ChunkedMapping::try_from_mapping(mapping, chunk_upload_options.chunk_size))
.collect::<Result<_>>()?;
.map(|mapping| Chunked::from(mapping, chunk_upload_options.chunk_size as usize))
.collect::<Result<Vec<_>>>()?;

let progress_style = ProgressStyle::default_bar().template(
"Uploading Proguard mappings...\
\n{wide_bar} {bytes}/{total_bytes} ({eta})",
);

let chunks = chunked_mappings.iter().flat_map(|mapping| mapping.chunks());
let chunks = chunked_mappings
.iter()
.flat_map(|mapping| mapping.iter_chunks());

chunks::upload_chunks(
&chunks.collect::<Vec<_>>(),
Expand Down

0 comments on commit 87444d5

Please sign in to comment.