Skip to content

Commit

Permalink
Include some entropy when choosing the storage bucket for a file (#7048)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 12, 2024
1 parent af9a4fc commit 5e27262
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions backend/canisters/storage_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Update the canister creation fee to 0.5T ([#6700](https://github.com/open-chat-labs/open-chat/pull/6700))
- Expose size of each virtual stable memory in metrics ([#6981](https://github.com/open-chat-labs/open-chat/pull/6981))
- Use `GroupedTimerJobQueue` to sync events to storage buckets ([#7045](https://github.com/open-chat-labs/open-chat/pull/7045))
- Include some entropy when choosing the storage bucket for a file ([#7048](https://github.com/open-chat-labs/open-chat/pull/7048))

## [[2.0.1419](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1419-storage_index)] - 2024-10-28

Expand Down
6 changes: 4 additions & 2 deletions backend/canisters/storage_index/impl/src/model/buckets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ impl Buckets {
}
}

pub fn allocate(&self, blob_hash: Hash) -> Option<CanisterId> {
pub fn allocate(&self, blob_hash: Hash, entropy: u64) -> Option<CanisterId> {
let bucket_count = self.active_buckets.len();
if bucket_count == 0 {
None
} else {
let usize_from_hash = u64::from_le_bytes(blob_hash[..8].try_into().unwrap()) as usize;
let mut bucket_allocation_hash = blob_hash;
bucket_allocation_hash.rotate_left((entropy % 32) as usize);
let usize_from_hash = u64::from_le_bytes(bucket_allocation_hash[..8].try_into().unwrap()) as usize;

// Use a modified modulo of the hash to slightly favour the first bucket
// so that they don't all run out of space at the same time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ fn allocated_bucket_impl(args: Args, state: &RuntimeState) -> Response {
});
}

let now = state.env.now();
let bucket = state
.data
.files
.bucket_for_blob(args.file_hash)
.or_else(|| state.data.buckets.allocate(args.file_hash));
.or_else(|| state.data.buckets.allocate(args.file_hash, now));

if let Some(canister_id) = bucket {
let now = state.env.now();

Success(SuccessResult {
canister_id,
file_id: generate_file_id(
Expand Down

0 comments on commit 5e27262

Please sign in to comment.