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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use std::{
collections::{BTreeMap, VecDeque},
ffi::OsStr,
ops::Deref,
path::{Path, PathBuf},
sync::{
atomic::{AtomicU16, AtomicUsize, Ordering},
Arc,
},
time::SystemTime,
};
use crate::store::{handle, types, RefreshMode};
pub(crate) struct Snapshot {
pub(crate) indices: Vec<handle::IndexLookup>,
pub(crate) loose_dbs: Arc<Vec<crate::loose::Store>>,
pub(crate) marker: types::SlotIndexMarker,
}
mod error {
use std::path::PathBuf;
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
#[error("The objects directory at '{0}' is not an accessible directory")]
Inaccessible(PathBuf),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Alternate(#[from] crate::alternate::Error),
#[error("The slotmap turned out to be too small with {} entries, would need {} more", .current, .needed)]
InsufficientSlots { current: usize, needed: usize },
#[error(
"Would have overflown amount of max possible generations of {}",
super::Generation::MAX
)]
GenerationOverflow,
}
}
pub use error::Error;
use crate::store::types::{Generation, IndexAndPacks, MutableIndexAndPack, SlotMapIndex};
impl super::Store {
pub(crate) fn load_one_index(
&self,
refresh_mode: RefreshMode,
marker: types::SlotIndexMarker,
) -> Result<Option<Snapshot>, Error> {
let index = self.index.load();
if !index.is_initialized() {
return self.consolidate_with_disk_state(false );
}
if marker.generation != index.generation || marker.state_id != index.state_id() {
Ok(Some(self.collect_snapshot()))
} else {
if self.load_next_index(index) {
Ok(Some(self.collect_snapshot()))
} else {
match refresh_mode {
RefreshMode::Never => Ok(None),
RefreshMode::AfterAllIndicesLoaded => {
self.consolidate_with_disk_state(true )
}
}
}
}
}
fn load_next_index(&self, mut index: arc_swap::Guard<Arc<SlotMapIndex>>) -> bool {
'retry_with_changed_index: loop {
let previous_state_id = index.state_id();
'retry_with_next_slot_index: loop {
match index
.next_index_to_load
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |current| {
(current != index.slot_indices.len()).then(|| current + 1)
}) {
Ok(slot_map_index) => {
let _ongoing_operation = IncOnNewAndDecOnDrop::new(&index.num_indices_currently_being_loaded);
let slot = &self.files[index.slot_indices[slot_map_index]];
let _lock = slot.write.lock();
if slot.generation.load(Ordering::SeqCst) > index.generation {
continue 'retry_with_next_slot_index;
}
let mut bundle = slot.files.load_full();
let bundle_mut = Arc::make_mut(&mut bundle);
if let Some(files) = bundle_mut.as_mut() {
let _loaded_count = IncOnDrop(&index.loaded_indices);
match files.load_index(self.object_hash) {
Ok(_) => {
slot.files.store(bundle);
break 'retry_with_next_slot_index;
}
Err(_) => {
slot.files.store(bundle);
continue 'retry_with_next_slot_index;
}
}
}
}
Err(_nothing_more_to_load) => {
let num_load_operations = index.num_indices_currently_being_loaded.deref();
while num_load_operations.load(Ordering::Relaxed) != 0 {
std::thread::yield_now()
}
break 'retry_with_next_slot_index;
}
}
}
if previous_state_id == index.state_id() {
let potentially_new_index = self.index.load();
if Arc::as_ptr(&potentially_new_index) == Arc::as_ptr(&index) {
return false;
} else {
index = potentially_new_index;
continue 'retry_with_changed_index;
}
} else {
return true;
}
}
}
pub(crate) fn consolidate_with_disk_state(&self, load_new_index: bool) -> Result<Option<Snapshot>, Error> {
let index = self.index.load();
let previous_index_state = Arc::as_ptr(&index) as usize;
let write = self.write.lock();
let objects_directory = &self.path;
let index = self.index.load();
if previous_index_state != Arc::as_ptr(&index) as usize {
return Ok(Some(self.collect_snapshot()));
}
let was_uninitialized = !index.is_initialized();
self.num_disk_state_consolidation.fetch_add(1, Ordering::Relaxed);
let db_paths: Vec<_> = std::iter::once(objects_directory.to_owned())
.chain(crate::alternate::resolve(objects_directory)?)
.collect();
let loose_dbs = if was_uninitialized
|| db_paths.len() != index.loose_dbs.len()
|| db_paths
.iter()
.zip(index.loose_dbs.iter().map(|ldb| &ldb.path))
.any(|(lhs, rhs)| lhs != rhs)
{
Arc::new(
db_paths
.iter()
.map(|path| crate::loose::Store::at(path, self.object_hash))
.collect::<Vec<_>>(),
)
} else {
Arc::clone(&index.loose_dbs)
};
let indices_by_modification_time = Self::collect_indices_and_mtime_sorted_by_size(
db_paths,
index.slot_indices.len().into(),
self.use_multi_pack_index.then(|| self.object_hash),
)?;
let mut idx_by_index_path: BTreeMap<_, _> = index
.slot_indices
.iter()
.filter_map(|&idx| {
let f = &self.files[idx];
Option::as_ref(&f.files.load()).map(|f| (f.index_path().to_owned(), idx))
})
.collect();
let mut new_slot_map_indices = Vec::new();
let mut index_paths_to_add = was_uninitialized
.then(|| VecDeque::with_capacity(indices_by_modification_time.len()))
.unwrap_or_default();
let mut num_loaded_indices = 0;
for (index_info, mtime) in indices_by_modification_time.into_iter().map(|(a, b, _)| (a, b)) {
match idx_by_index_path.remove(index_info.path()) {
Some(slot_idx) => {
let slot = &self.files[slot_idx];
let files_guard = slot.files.load();
let files =
Option::as_ref(&files_guard).expect("slot is set or we wouldn't know it points to this file");
if index_info.is_multi_index() && files.mtime() != mtime {
index_paths_to_add.push_back((index_info, mtime, Some(slot_idx)));
if files.index_is_loaded() {
num_loaded_indices += 1;
}
} else {
if Self::assure_slot_matches_index(&write, slot, index_info, mtime, index.generation) {
num_loaded_indices += 1;
}
new_slot_map_indices.push(slot_idx);
}
}
None => index_paths_to_add.push_back((index_info, mtime, None)),
}
}
let needs_stable_indices = self.maintain_stable_indices(&write);
let mut next_possibly_free_index = index
.slot_indices
.iter()
.max()
.map(|idx| (idx + 1) % self.files.len())
.unwrap_or(0);
let mut num_indices_checked = 0;
let mut needs_generation_change = false;
let mut slot_indices_to_remove: Vec<_> = idx_by_index_path.into_iter().map(|(_, v)| v).collect();
while let Some((mut index_info, mtime, move_from_slot_idx)) = index_paths_to_add.pop_front() {
'increment_slot_index: loop {
if num_indices_checked == self.files.len() {
return Err(Error::InsufficientSlots {
current: self.files.len(),
needed: index_paths_to_add.len() + 1,
});
}
let slot_index = next_possibly_free_index;
let slot = &self.files[slot_index];
next_possibly_free_index = (next_possibly_free_index + 1) % self.files.len();
num_indices_checked += 1;
match move_from_slot_idx {
Some(move_from_slot_idx) => {
debug_assert!(index_info.is_multi_index(), "only set for multi-pack indices");
if slot_index == move_from_slot_idx {
continue 'increment_slot_index;
}
match Self::try_set_index_slot(
&write,
slot,
index_info,
mtime,
index.generation,
needs_stable_indices,
) {
Ok(dest_was_empty) => {
slot_indices_to_remove.push(move_from_slot_idx);
new_slot_map_indices.push(slot_index);
if !dest_was_empty {
needs_generation_change = true;
}
break 'increment_slot_index;
}
Err(unused_index_info) => index_info = unused_index_info,
}
}
None => {
match Self::try_set_index_slot(
&write,
slot,
index_info,
mtime,
index.generation,
needs_stable_indices,
) {
Ok(dest_was_empty) => {
new_slot_map_indices.push(slot_index);
if !dest_was_empty {
needs_generation_change = true;
}
break 'increment_slot_index;
}
Err(unused_index_info) => index_info = unused_index_info,
}
}
}
}
}
assert_eq!(
index_paths_to_add.len(),
0,
"By this time we have assigned all new files to slots"
);
let generation = if needs_generation_change {
index.generation.checked_add(1).ok_or(Error::GenerationOverflow)?
} else {
index.generation
};
let index_unchanged = index.slot_indices == new_slot_map_indices;
if generation != index.generation {
assert!(
!index_unchanged,
"if the generation changed, the slot index must have changed for sure"
);
}
if !index_unchanged || loose_dbs != index.loose_dbs {
let new_index = Arc::new(SlotMapIndex {
slot_indices: new_slot_map_indices,
loose_dbs,
generation,
next_index_to_load: index_unchanged
.then(|| Arc::clone(&index.next_index_to_load))
.unwrap_or_default(),
loaded_indices: index_unchanged
.then(|| Arc::clone(&index.loaded_indices))
.unwrap_or_else(|| Arc::new(num_loaded_indices.into())),
num_indices_currently_being_loaded: Default::default(),
});
self.index.store(new_index);
}
for slot in slot_indices_to_remove.into_iter().map(|idx| &self.files[idx]) {
let _lock = slot.write.lock();
let mut files = slot.files.load_full();
let files_mut = Arc::make_mut(&mut files);
if needs_stable_indices {
if let Some(files) = files_mut.as_mut() {
files.trash();
}
} else {
*files_mut = None;
};
slot.files.store(files);
if !needs_stable_indices {
slot.generation.store(generation, Ordering::SeqCst);
}
}
let new_index = self.index.load();
Ok(if index.state_id() == new_index.state_id() {
None
} else {
if load_new_index {
self.load_next_index(new_index);
}
Some(self.collect_snapshot())
})
}
pub(crate) fn collect_indices_and_mtime_sorted_by_size(
db_paths: Vec<PathBuf>,
initial_capacity: Option<usize>,
multi_pack_index_object_hash: Option<git_hash::Kind>,
) -> Result<Vec<(Either, SystemTime, u64)>, Error> {
let mut indices_by_modification_time = Vec::with_capacity(initial_capacity.unwrap_or_default());
for db_path in db_paths {
let packs = db_path.join("pack");
let entries = match std::fs::read_dir(&packs) {
Ok(e) => e,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => continue,
Err(err) => return Err(err.into()),
};
let indices = entries
.filter_map(Result::ok)
.filter_map(|e| e.metadata().map(|md| (e.path(), md)).ok())
.filter(|(_, md)| md.file_type().is_file())
.filter(|(p, _)| {
let ext = p.extension();
(ext == Some(OsStr::new("idx")) && p.with_extension("pack").is_file())
|| (multi_pack_index_object_hash.is_some() && ext.is_none() && is_multipack_index(p))
})
.map(|(p, md)| md.modified().map_err(Error::from).map(|mtime| (p, mtime, md.len())))
.collect::<Result<Vec<_>, _>>()?;
let multi_index_info = multi_pack_index_object_hash.and_then(|hash| {
indices.iter().find_map(|(p, a, b)| {
is_multipack_index(p)
.then(|| {
git_pack::multi_index::File::at(p)
.ok()
.filter(|midx| midx.object_hash() == hash)
.map(|midx| (midx, *a, *b))
})
.flatten()
})
});
if let Some((multi_index, mtime, flen)) = multi_index_info {
let index_names_in_multi_index: Vec<_> =
multi_index.index_names().iter().map(|p| p.as_path()).collect();
let mut indices_not_in_multi_index: Vec<(Either, _, _)> = indices
.into_iter()
.filter_map(|(path, a, b)| {
(path != multi_index.path()
&& !index_names_in_multi_index
.contains(&Path::new(path.file_name().expect("file name present"))))
.then(|| (Either::IndexPath(path), a, b))
})
.collect();
indices_not_in_multi_index.insert(0, (Either::MultiIndexFile(Arc::new(multi_index)), mtime, flen));
indices_by_modification_time.extend(indices_not_in_multi_index);
} else {
indices_by_modification_time.extend(
indices
.into_iter()
.filter_map(|(p, a, b)| (!is_multipack_index(&p)).then(|| (Either::IndexPath(p), a, b))),
)
}
}
indices_by_modification_time.sort_by(|l, r| l.2.cmp(&r.2).reverse());
Ok(indices_by_modification_time)
}
#[allow(clippy::too_many_arguments)]
fn try_set_index_slot(
lock: &parking_lot::MutexGuard<'_, ()>,
dest_slot: &MutableIndexAndPack,
index_info: Either,
mtime: SystemTime,
current_generation: Generation,
needs_stable_indices: bool,
) -> Result<bool, Either> {
let (dest_slot_was_empty, generation) = match &**dest_slot.files.load() {
Some(bundle) => {
if bundle.index_path() == index_info.path() || (bundle.is_disposable() && needs_stable_indices) {
return Err(index_info);
}
(false, current_generation + 1)
}
None => {
(true, current_generation)
}
};
Self::set_slot_to_index(lock, dest_slot, index_info, mtime, generation);
Ok(dest_slot_was_empty)
}
fn set_slot_to_index(
_lock: &parking_lot::MutexGuard<'_, ()>,
slot: &MutableIndexAndPack,
index_info: Either,
mtime: SystemTime,
generation: Generation,
) {
let _lock = slot.write.lock();
let mut files = slot.files.load_full();
let files_mut = Arc::make_mut(&mut files);
slot.generation.store(generation, Ordering::SeqCst);
*files_mut = Some(index_info.into_index_and_packs(mtime));
slot.files.store(files);
}
fn assure_slot_matches_index(
_lock: &parking_lot::MutexGuard<'_, ()>,
slot: &MutableIndexAndPack,
index_info: Either,
mtime: SystemTime,
current_generation: Generation,
) -> bool {
match Option::as_ref(&slot.files.load()) {
Some(bundle) => {
assert_eq!(
bundle.index_path(),
index_info.path(),
"Parallel writers cannot change the file the slot points to."
);
if bundle.is_disposable() {
let _lock = slot.write.lock();
let mut files = slot.files.load_full();
let files_mut = Arc::make_mut(&mut files)
.as_mut()
.expect("BUG: cannot change from something to nothing, would be race");
files_mut.put_back();
debug_assert_eq!(
files_mut.mtime(),
mtime,
"BUG: we can only put back files that didn't obviously change"
);
slot.generation.store(current_generation, Ordering::SeqCst);
slot.files.store(files);
} else {
}
bundle.index_is_loaded()
}
None => {
unreachable!("BUG: a slot can never be deleted if we have it recorded in the index WHILE changing said index. There shouldn't be a race")
}
}
}
fn maintain_stable_indices(&self, _guard: &parking_lot::MutexGuard<'_, ()>) -> bool {
self.num_handles_stable.load(Ordering::SeqCst) > 0
}
pub(crate) fn collect_snapshot(&self) -> Snapshot {
let index = self.index.load();
let indices = if index.is_initialized() {
index
.slot_indices
.iter()
.map(|idx| (*idx, &self.files[*idx]))
.filter_map(|(id, file)| {
let lookup = match (&**file.files.load()).as_ref()? {
types::IndexAndPacks::Index(bundle) => handle::SingleOrMultiIndex::Single {
index: bundle.index.loaded()?.clone(),
data: bundle.data.loaded().cloned(),
},
types::IndexAndPacks::MultiIndex(multi) => handle::SingleOrMultiIndex::Multi {
index: multi.multi_index.loaded()?.clone(),
data: multi.data.iter().map(|f| f.loaded().cloned()).collect(),
},
};
handle::IndexLookup { file: lookup, id }.into()
})
.collect()
} else {
Vec::new()
};
Snapshot {
indices,
loose_dbs: Arc::clone(&index.loose_dbs),
marker: index.marker(),
}
}
}
fn is_multipack_index(path: &Path) -> bool {
path.file_name() == Some(OsStr::new("multi-pack-index"))
}
struct IncOnNewAndDecOnDrop<'a>(&'a AtomicU16);
impl<'a> IncOnNewAndDecOnDrop<'a> {
pub fn new(v: &'a AtomicU16) -> Self {
v.fetch_add(1, Ordering::SeqCst);
Self(v)
}
}
impl<'a> Drop for IncOnNewAndDecOnDrop<'a> {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::SeqCst);
}
}
struct IncOnDrop<'a>(&'a AtomicUsize);
impl<'a> Drop for IncOnDrop<'a> {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}
pub(crate) enum Either {
IndexPath(PathBuf),
MultiIndexFile(Arc<git_pack::multi_index::File>),
}
impl Either {
fn path(&self) -> &Path {
match self {
Either::IndexPath(p) => p,
Either::MultiIndexFile(f) => f.path(),
}
}
fn into_index_and_packs(self, mtime: SystemTime) -> IndexAndPacks {
match self {
Either::IndexPath(path) => IndexAndPacks::new_single(path, mtime),
Either::MultiIndexFile(file) => IndexAndPacks::new_multi_from_open_file(file, mtime),
}
}
fn is_multi_index(&self) -> bool {
matches!(self, Either::MultiIndexFile(_))
}
}
impl Eq for Either {}
impl PartialEq<Self> for Either {
fn eq(&self, other: &Self) -> bool {
self.path().eq(other.path())
}
}
impl PartialOrd<Self> for Either {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.path().partial_cmp(other.path())
}
}
impl Ord for Either {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.path().cmp(other.path())
}
}