icechunk 2.0.2

Transactional storage engine for Zarr designed for use on cloud object storage
Documentation
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Shuttle concurrency test for icechunk
//
// Only compiles when the `shuttle` feature is active:
//   cargo test -p icechunk --features shuttle --test test_shuttle -- --nocapture

// TODO: Make repo_info_num_updates tiny?

#![cfg(feature = "shuttle")]

use bytes::Bytes;
use chrono::{DateTime, Utc};
use futures::TryStreamExt;
use futures::future::try_join_all;
use icechunk::format::manifest::ChunkPayload;
use icechunk::format::repo_info::UpdateType;
use icechunk::format::snapshot::ArrayShape;
use icechunk::format::{ChunkIndices, Path, SnapshotId};
use icechunk::repository::VersionInfo;
use icechunk::{Repository, new_in_memory_storage};
use proptest::collection::vec;
use proptest::prelude::*;
use shuttle::future::{block_on, spawn};
use shuttle::{Config, Runner, scheduler};
use std::collections::HashSet;
use std::error::Error;
use std::sync::Arc;

fn assert_ops_log_invariants(log: &[(DateTime<Utc>, UpdateType, Option<String>)]) {
    // timestamps must be strictly decreasing (most recent first)
    log.windows(2).for_each(|window| {
        let (time_a, _, _) = &window[0];
        let (time_b, _, _) = &window[1];
        assert!(
            time_a > time_b,
            "ops log timestamps must be strictly decreasing: {time_a} should be > {time_b}"
        );
    });

    // all non-None backup paths must be unique
    let backup_paths: HashSet<_> =
        log[..log.len() - 1].iter().map(|(_, _, path)| path.as_ref()).collect();
    assert_eq!(backup_paths.len(), log.len() - 1);

    // last entry (earliest) should always be RepoInitializedUpdate
    if let Some((_, update_type, _backup_path)) = log.last() {
        assert!(
            matches!(update_type, UpdateType::RepoInitializedUpdate),
            "last ops log entry (earliest) should be RepoInitializedUpdate, got: {update_type:?}"
        );
        // initial commit has no backup
        // assert!(backup_path.is_none())
    } else {
        unreachable!();
    }
}

/// Like `shuttle::check_random` but with 8MB stack to handle
/// icechunk's commit path (zstd + flatbuffers serialization).
fn check_pct(f: impl Fn() + Send + Sync + 'static, iterations: usize, depth: usize) {
    let mut config = Config::default();
    config.stack_size = 0x80_0000; // 8MB
    let scheduler = scheduler::PctScheduler::new(depth, iterations);
    Runner::new(scheduler, config).run(f);
}

async fn mk_commit(
    repo: Arc<Repository>,
    path: Path,
    branch: &str,
    c: u32,
) -> Result<SnapshotId, Box<dyn Error + Send + Sync>> {
    let mut session = repo.writable_session(branch).await?;
    session
        .set_chunk_ref(
            path,
            ChunkIndices(vec![c]),
            Some(ChunkPayload::Inline("foo".into())),
        )
        .await?;
    Ok(session
        .commit("write chunk")
        .rebase(&icechunk::conflicts::detector::ConflictDetector, 3)
        .execute()
        .await?)
}

async fn mk_concurrent_commits_same_branch() -> Result<(), Box<dyn Error + Send + Sync>> {
    let storage = new_in_memory_storage().await?;
    let repo = Arc::new(
        Repository::create(None, storage, Default::default(), None, false).await?,
    );

    let mut session = repo.writable_session("main").await?;
    let shape = ArrayShape::new(vec![(10, 10)]).unwrap();
    let path: Path = "/array".try_into()?;
    session.add_array(path.clone(), shape, None, Bytes::new()).await?;

    let mut snaps = vec![];
    snaps.push(session.commit("init array").execute().await?);

    repo.create_branch("feature", &snaps[0]).await?;

    let repo1 = repo.clone();
    let path1 = path.clone();
    let handle1 = spawn(async move { mk_commit(repo1, path1, "main", 2).await });

    let repo2 = repo.clone();
    let path2 = path.clone();
    let handle2 = spawn(async move { mk_commit(repo2, path2, "feature", 1).await });

    for r in try_join_all([handle1, handle2]).await? {
        r?;
    }

    let (stream, _, _) = repo.ops_log().await?;
    let log: Vec<_> = stream.try_collect().await?;
    assert_ops_log_invariants(&log);

    Ok(())
}

// Regression test for https://github.com/earth-mover/icechunk/pull/1798
#[test]
fn concurrent_commits_same_branch() {
    check_pct(
        || {
            block_on(mk_concurrent_commits_same_branch()).unwrap();
        },
        100,
        3,
    );
}

#[derive(Clone, Debug, PartialEq)]
enum Action {
    Commit,
    AddBranch,
    DeleteBranch,
    AddTag,
    DeleteTag,
    Amend,
    ResetBranch,
}

#[derive(Debug, Clone)]
enum ActionResult {
    Commit(String, SnapshotId),
    AddBranch(String, SnapshotId),
    DeleteBranch { branch: String, previous_snap: SnapshotId },
    AddTag(String, SnapshotId),
    DeleteTag { tag: String, previous_snap: SnapshotId },
    Amend { branch: String, new_snap: SnapshotId, previous_snap: SnapshotId },
    ResetBranch { branch: String, to_snap: SnapshotId, previous_snap: SnapshotId },
}

fn actions(
    range: impl Into<proptest::collection::SizeRange>,
) -> impl Strategy<Value = Vec<Action>> {
    use Action::*;
    vec(
        proptest::sample::select(vec![
            Commit,
            AddBranch,
            DeleteBranch,
            AddTag,
            DeleteTag,
            Amend,
            ResetBranch,
        ]),
        range,
    )
}

async fn setup_branches(
    repo: Arc<Repository>,
    actions: &[Action],
    branches: &[String],
) -> Result<(), Box<dyn Error + Send + Sync>> {
    let path: Path = "/array".try_into()?;
    for (action, branch) in actions.iter().zip(branches.iter()) {
        if *action != Action::AddBranch {
            repo.create_branch(branch, &repo.lookup_branch("main").await?).await?;
        }
        match action {
            Action::AddBranch => {}
            Action::Amend => {
                mk_commit(repo.clone(), path.clone(), branch, 3).await?;
            }
            Action::ResetBranch => {
                mk_commit(repo.clone(), path.clone(), branch, 2).await?;
                mk_commit(repo.clone(), path.clone(), branch, 3).await?;
            }
            _ => {
                repo.create_tag(
                    &format!("tag-to-delete-{branch}"),
                    &repo.lookup_branch("main").await?,
                )
                .await?;
            }
        }
    }
    Ok(())
}

async fn execute_action(
    repo: Arc<Repository>,
    action: Action,
    branch: String,
) -> Result<ActionResult, Box<dyn Error + Send + Sync>> {
    use Action::*;

    let res = match action {
        Commit => {
            let snap = mk_commit(repo, "/array".try_into()?, &branch, 0).await?;
            ActionResult::Commit(branch, snap)
        }
        AddBranch => {
            let snap = repo.lookup_branch("main").await?;
            repo.create_branch(&branch, &snap).await?;
            ActionResult::AddBranch(branch, snap)
        }
        DeleteBranch => {
            let previous_snap = repo.lookup_branch(&branch).await?;
            repo.delete_branch(&branch).await?;
            ActionResult::DeleteBranch { branch, previous_snap }
        }
        AddTag => {
            let snap = repo.lookup_branch(&branch).await?;
            // stick `branch` in to avoid conflicts
            let tag = format!("tag-to-create-{branch}");
            repo.create_tag(&tag, &snap).await?;
            ActionResult::AddTag(tag, snap)
        }
        DeleteTag => {
            // stick `branch` in to avoid conflicts
            let tag = format!("tag-to-delete-{branch}");
            let previous_snap = repo.lookup_tag(&tag).await?;
            repo.delete_tag(&tag).await?;
            ActionResult::DeleteTag { tag, previous_snap }
        }
        Amend => {
            let previous_snap = repo.lookup_branch(&branch).await?;
            let mut session = repo.writable_session(&branch).await?;
            session
                .set_chunk_ref(
                    "/array".try_into()?,
                    ChunkIndices(vec![4]),
                    Some(ChunkPayload::Inline("amend".into())),
                )
                .await?;
            let new_snap = session.commit("amend commit").amend().execute().await?;
            ActionResult::Amend { branch, new_snap, previous_snap }
        }
        ResetBranch => {
            let previous_snap = repo.lookup_branch(&branch).await?;
            let to_snap = repo.lookup_branch("main").await?;
            repo.reset_branch(&branch, &to_snap, None).await?;
            ActionResult::ResetBranch { branch, to_snap, previous_snap }
        }
    };

    Ok(res)
}

async fn assert_action_postcondition(
    repo: Arc<Repository>,
    action: ActionResult,
) -> Result<(), Box<dyn Error + Send + Sync>> {
    use ActionResult::*;
    match action {
        Commit(branch, snap) => {
            let anc: HashSet<SnapshotId> = repo
                .ancestry(&VersionInfo::BranchTipRef(branch.clone()))
                .await?
                .map_ok(|info| info.id)
                .try_collect()
                .await?;
            assert!(anc.contains(&snap));
        }
        AddBranch(branch, snap) => {
            assert!(repo.list_branches().await?.contains(&branch));
            assert_eq!(repo.lookup_branch(&branch).await?, snap);
        }
        DeleteBranch { branch, .. } => {
            assert!(!repo.list_branches().await?.contains(&branch));
        }
        AddTag(tag, snap) => {
            assert!(repo.list_tags().await?.contains(&tag));
            assert_eq!(repo.lookup_tag(&tag).await?, snap);
        }
        DeleteTag { tag, .. } => {
            assert!(!repo.list_tags().await?.contains(&tag));
        }
        Amend { branch, new_snap, previous_snap } => {
            let tip = repo.lookup_branch(&branch).await?;
            assert_eq!(tip, new_snap, "amend snapshot should be branch tip for {branch}");
            let anc: HashSet<SnapshotId> = repo
                .ancestry(&VersionInfo::BranchTipRef(branch.clone()))
                .await?
                .map_ok(|info| info.id)
                .try_collect()
                .await?;
            assert!(anc.contains(&new_snap));
            assert!(!anc.contains(&previous_snap));
        }
        ResetBranch { branch, to_snap, .. } => {
            let tip = repo.lookup_branch(&branch).await?;
            assert_eq!(tip, to_snap, "branch {branch} should be reset to {to_snap:?}");
        }
    };
    Ok(())
}

fn assert_ops_log_contains(
    entries: &[(DateTime<Utc>, UpdateType, Option<String>)],
    result: &ActionResult,
) {
    use ActionResult::*;
    let found = entries.iter().any(|(_, update_type, _)| match (result, update_type) {
        (
            Commit(branch, snap),
            UpdateType::NewCommitUpdate { branch: b, new_snap_id },
        ) => b == branch && new_snap_id == snap,
        (AddBranch(branch, _), UpdateType::BranchCreatedUpdate { name }) => {
            name == branch
        }
        (
            DeleteBranch { branch, previous_snap },
            UpdateType::BranchDeletedUpdate { name, previous_snap_id },
        ) => name == branch && previous_snap_id == previous_snap,
        (AddTag(tag, _), UpdateType::TagCreatedUpdate { name }) => name == tag,
        (
            DeleteTag { tag, previous_snap },
            UpdateType::TagDeletedUpdate { name, previous_snap_id },
        ) => name == tag && previous_snap_id == previous_snap,
        (
            Amend { branch, new_snap, previous_snap },
            UpdateType::CommitAmendedUpdate { branch: b, new_snap_id, previous_snap_id },
        ) => b == branch && new_snap_id == new_snap && previous_snap_id == previous_snap,
        (
            ResetBranch { branch, previous_snap, .. },
            UpdateType::BranchResetUpdate { name, previous_snap_id },
        ) => name == branch && previous_snap_id == previous_snap,
        _ => false,
    });
    assert!(found, "no ops log entry found for action result {result:?}");
}

async fn execute_concurrent_actions(
    actions: Vec<Action>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
    let branches: Vec<String> =
        (0..actions.len()).map(|i| format!("branch-{i}")).collect();
    let storage = new_in_memory_storage().await?;
    let repo = Arc::new(
        Repository::create(None, storage, Default::default(), None, false).await?,
    );

    let mut session = repo.writable_session("main").await?;
    let shape = ArrayShape::new(vec![(10, 10)]).unwrap();
    let path: Path = "/array".try_into()?;
    session.add_array(path.clone(), shape, None, Bytes::new()).await?;
    session.commit("foo").execute().await?;

    setup_branches(repo.clone(), &actions, &branches).await?;

    let (stream, _, _) = repo.ops_log().await?;
    let ops_count_before = stream.try_collect::<Vec<_>>().await?.len();

    let handles = actions
        .iter()
        .zip(branches.iter())
        .map(|(action, branch)| {
            spawn({
                let repo = repo.clone();
                let branch = branch.clone();
                let action = action.clone();
                async move { execute_action(repo, action, branch).await }
            })
        })
        .collect::<Vec<_>>();

    let results: Vec<_> =
        try_join_all(handles).await?.into_iter().collect::<Result<Vec<_>, _>>()?;

    let (stream, _, _) = repo.ops_log().await?;
    let log: Vec<_> = stream.try_collect().await?;

    let new_entries: Vec<_> = log[..log.len() - ops_count_before].to_vec();
    assert_eq!(new_entries.len(), actions.len());
    assert_ops_log_invariants(&log);

    for r in &results {
        assert_ops_log_contains(&new_entries, r);
    }

    for r in results {
        assert_action_postcondition(repo.clone(), r).await?;
    }
    Ok(())
}

proptest! {
    #[test]
    fn concurrent_actions(acts in actions(3..=5)) {
        let acts = acts.clone();
        check_pct(move || {
            let acts = acts.clone();
            block_on(execute_concurrent_actions(acts)).unwrap();
        }, 100, 3);
    }
}