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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zenstore/cas.h>
#include "compactcas.h"
#include <zencore/compactbinarybuilder.h>
#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/memory.h>
#include <zencore/string.h>
#include <zencore/testing.h>
#include <zencore/testutils.h>
#include <zencore/thread.h>
#include <zencore/uid.h>
#include <zenstore/gc.h>
#include <filesystem>
#include <functional>
#include <gsl/gsl-lite.hpp>
#if ZEN_WITH_TESTS
# include <algorithm>
# include <random>
#endif
//////////////////////////////////////////////////////////////////////////
namespace zen {
CasContainerStrategy::CasContainerStrategy(const CasStoreConfiguration& Config, CasGc& Gc)
: GcStorage(Gc)
, m_Config(Config)
, m_Log(logging::Get("containercas"))
{
}
CasContainerStrategy::~CasContainerStrategy()
{
}
void
CasContainerStrategy::Initialize(const std::string_view ContainerBaseName, uint64_t Alignment, bool IsNewStore)
{
ZEN_ASSERT(IsPow2(Alignment));
ZEN_ASSERT(!m_IsInitialized);
m_ContainerBaseName = ContainerBaseName;
m_PayloadAlignment = Alignment;
OpenContainer(IsNewStore);
m_IsInitialized = true;
}
CasStore::InsertResult
CasContainerStrategy::InsertChunk(const void* ChunkData, size_t ChunkSize, const IoHash& ChunkHash)
{
{
RwLock::SharedLockScope _(m_LocationMapLock);
auto KeyIt = m_LocationMap.find(ChunkHash);
if (KeyIt != m_LocationMap.end())
{
return CasStore::InsertResult{.New = false};
}
}
// New entry
RwLock::ExclusiveLockScope _(m_InsertLock);
const uint64_t InsertOffset = m_CurrentInsertOffset;
m_SmallObjectFile.Write(ChunkData, ChunkSize, InsertOffset);
m_CurrentInsertOffset = (m_CurrentInsertOffset + ChunkSize + m_PayloadAlignment - 1) & ~(m_PayloadAlignment - 1);
RwLock::ExclusiveLockScope __(m_LocationMapLock);
const CasDiskLocation Location{InsertOffset, ChunkSize};
m_LocationMap[ChunkHash] = Location;
CasDiskIndexEntry IndexEntry{.Key = ChunkHash, .Location = Location};
m_TotalSize.fetch_add(static_cast<uint64_t>(ChunkSize));
m_CasLog.Append(IndexEntry);
return CasStore::InsertResult{.New = true};
}
CasStore::InsertResult
CasContainerStrategy::InsertChunk(IoBuffer Chunk, const IoHash& ChunkHash)
{
return InsertChunk(Chunk.Data(), Chunk.Size(), ChunkHash);
}
IoBuffer
CasContainerStrategy::FindChunk(const IoHash& ChunkHash)
{
RwLock::SharedLockScope _(m_LocationMapLock);
if (auto KeyIt = m_LocationMap.find(ChunkHash); KeyIt != m_LocationMap.end())
{
const CasDiskLocation& Location = KeyIt->second;
return IoBufferBuilder::MakeFromFileHandle(m_SmallObjectFile.Handle(), Location.GetOffset(), Location.GetSize());
}
// Not found
return IoBuffer();
}
bool
CasContainerStrategy::HaveChunk(const IoHash& ChunkHash)
{
RwLock::SharedLockScope _(m_LocationMapLock);
if (auto KeyIt = m_LocationMap.find(ChunkHash); KeyIt != m_LocationMap.end())
{
return true;
}
return false;
}
void
CasContainerStrategy::FilterChunks(CasChunkSet& InOutChunks)
{
// This implementation is good enough for relatively small
// chunk sets (in terms of chunk identifiers), but would
// benefit from a better implementation which removes
// items incrementally for large sets, especially when
// we're likely to already have a large proportion of the
// chunks in the set
InOutChunks.RemoveChunksIf([&](const IoHash& Hash) { return HaveChunk(Hash); });
}
void
CasContainerStrategy::Flush()
{
m_CasLog.Flush();
m_SmallObjectIndex.Flush();
m_SmallObjectFile.Flush();
}
void
CasContainerStrategy::Scrub(ScrubContext& Ctx)
{
const uint64_t WindowSize = 4 * 1024 * 1024;
uint64_t WindowStart = 0;
uint64_t WindowEnd = WindowSize;
const uint64_t FileSize = m_SmallObjectFile.FileSize();
std::vector<CasDiskIndexEntry> BigChunks;
std::vector<CasDiskIndexEntry> BadChunks;
// We do a read sweep through the payloads file and validate
// any entries that are contained within each segment, with
// the assumption that most entries will be checked in this
// pass. An alternative strategy would be to use memory mapping.
{
IoBuffer ReadBuffer{WindowSize};
void* BufferBase = ReadBuffer.MutableData();
RwLock::SharedLockScope _(m_LocationMapLock);
do
{
const uint64_t ChunkSize = Min(WindowSize, FileSize - WindowStart);
m_SmallObjectFile.Read(BufferBase, ChunkSize, WindowStart);
for (auto& Entry : m_LocationMap)
{
const uint64_t EntryOffset = Entry.second.GetOffset();
if ((EntryOffset >= WindowStart) && (EntryOffset < WindowEnd))
{
const uint64_t EntryEnd = EntryOffset + Entry.second.GetSize();
if (EntryEnd >= WindowEnd)
{
BigChunks.push_back({.Key = Entry.first, .Location = Entry.second});
continue;
}
const IoHash ComputedHash =
IoHash::HashBuffer(reinterpret_cast<uint8_t*>(BufferBase) + Entry.second.GetOffset() - WindowStart,
Entry.second.GetSize());
if (Entry.first != ComputedHash)
{
// Hash mismatch
BadChunks.push_back({.Key = Entry.first, .Location = Entry.second});
}
}
}
WindowStart += WindowSize;
WindowEnd += WindowSize;
} while (WindowStart < FileSize);
}
// Deal with large chunks
for (const CasDiskIndexEntry& Entry : BigChunks)
{
IoHashStream Hasher;
m_SmallObjectFile.StreamByteRange(Entry.Location.GetOffset(), Entry.Location.GetSize(), [&](const void* Data, uint64_t Size) {
Hasher.Append(Data, Size);
});
IoHash ComputedHash = Hasher.GetHash();
if (Entry.Key != ComputedHash)
{
BadChunks.push_back(Entry);
}
}
if (BadChunks.empty())
{
return;
}
ZEN_ERROR("Scrubbing found {} bad chunks in '{}'", BadChunks.size(), m_ContainerBaseName);
// Deal with bad chunks by removing them from our lookup map
std::vector<IoHash> BadChunkHashes;
for (const CasDiskIndexEntry& Entry : BadChunks)
{
BadChunkHashes.push_back(Entry.Key);
m_CasLog.Append({.Key = Entry.Key, .Location = Entry.Location, .Flags = CasDiskIndexEntry::kTombstone});
m_LocationMap.erase(Entry.Key);
}
// Let whomever it concerns know about the bad chunks. This could
// be used to invalidate higher level data structures more efficiently
// than a full validation pass might be able to do
Ctx.ReportBadCasChunks(BadChunkHashes);
}
void
CasContainerStrategy::CollectGarbage(GcContext& GcCtx)
{
namespace fs = std::filesystem;
// A naive garbage collection implementation that just copies evicted chunks
// into a new container file. We probably need to partition the container file
// into several parts to prevent needing to keep the entire container file during GC.
ZEN_INFO("collecting garbage from '{}'", m_Config.RootDirectory / m_ContainerBaseName);
RwLock::ExclusiveLockScope _(m_LocationMapLock);
Flush();
std::vector<IoHash> Candidates;
std::vector<IoHash> ChunksToKeep;
std::vector<IoHash> ChunksToDelete;
const uint64_t ChunkCount = m_LocationMap.size();
uint64_t TotalSize{};
Candidates.reserve(m_LocationMap.size());
for (auto& Entry : m_LocationMap)
{
Candidates.push_back(Entry.first);
TotalSize += Entry.second.GetSize();
}
ChunksToKeep.reserve(Candidates.size());
GcCtx.FilterCas(Candidates, [&ChunksToKeep, &ChunksToDelete](const IoHash& Hash, bool Keep) {
if (Keep)
{
ChunksToKeep.push_back(Hash);
}
else
{
ChunksToDelete.push_back(Hash);
}
});
if (m_LocationMap.empty() || ChunksToKeep.size() == m_LocationMap.size())
{
ZEN_INFO("garbage collect DONE, scanned #{} {} chunks from '{}', nothing to delete",
ChunkCount,
NiceBytes(TotalSize),
m_Config.RootDirectory / m_ContainerBaseName);
return;
}
const uint64_t NewChunkCount = ChunksToKeep.size();
uint64_t NewTotalSize = 0;
for (const IoHash& Key : ChunksToKeep)
{
const CasDiskLocation& Loc = m_LocationMap[Key];
NewTotalSize += Loc.GetSize();
}
std::error_code Error;
DiskSpace Space = DiskSpaceInfo(m_Config.RootDirectory, Error);
if (Error)
{
ZEN_ERROR("get disk space FAILED, reason '{}'", Error.message());
return;
}
if (Space.Free < NewTotalSize + (64 << 20))
{
ZEN_INFO("garbage collect from '{}' FAILED, required disk space {}, free {}",
m_Config.RootDirectory / m_ContainerBaseName,
NiceBytes(NewTotalSize),
NiceBytes(Space.Free));
return;
}
const bool CollectSmallObjects = GcCtx.IsDeletionMode() && GcCtx.CollectSmallObjects();
if (!CollectSmallObjects)
{
ZEN_INFO("garbage collect from '{}' DISABLED, found #{} {} chunks of total #{} {}",
m_Config.RootDirectory / m_ContainerBaseName,
ChunkCount - NewChunkCount,
NiceBytes(TotalSize - NewTotalSize),
ChunkCount,
NiceBytes(TotalSize));
return;
}
fs::path TmpSobsPath = m_Config.RootDirectory / (m_ContainerBaseName + ".gc.ucas");
fs::path TmpSlogPath = m_Config.RootDirectory / (m_ContainerBaseName + ".gc.ulog");
{
ZEN_DEBUG("creating temporary container cas '{}'...", TmpSobsPath);
TCasLogFile<CasDiskIndexEntry> TmpLog;
BasicFile TmpObjectFile;
bool IsNew = true;
TmpLog.Open(TmpSlogPath, IsNew);
TmpObjectFile.Open(TmpSobsPath, IsNew);
std::vector<uint8_t> Chunk;
uint64_t NextInsertOffset{};
for (const IoHash& Key : ChunksToKeep)
{
const auto Entry = m_LocationMap.find(Key);
const auto& Loc = Entry->second;
Chunk.resize(Loc.GetSize());
m_SmallObjectFile.Read(Chunk.data(), Chunk.size(), Loc.GetOffset());
const uint64_t InsertOffset = NextInsertOffset;
TmpObjectFile.Write(Chunk.data(), Chunk.size(), InsertOffset);
TmpLog.Append({.Key = Key, .Location = {InsertOffset, Chunk.size()}});
NextInsertOffset = (NextInsertOffset + Chunk.size() + m_PayloadAlignment - 1) & ~(m_PayloadAlignment - 1);
}
}
try
{
CloseContainer();
fs::path SobsPath = m_Config.RootDirectory / (m_ContainerBaseName + ".ucas");
fs::path SidxPath = m_Config.RootDirectory / (m_ContainerBaseName + ".uidx");
fs::path SlogPath = m_Config.RootDirectory / (m_ContainerBaseName + ".ulog");
fs::remove(SobsPath);
fs::remove(SidxPath);
fs::remove(SlogPath);
fs::rename(TmpSobsPath, SobsPath);
fs::rename(TmpSlogPath, SlogPath);
{
// Create a new empty index file
BasicFile SidxFile;
SidxFile.Open(SidxPath, true);
}
OpenContainer(false /* IsNewStore */);
GcCtx.DeletedCas(ChunksToDelete);
ZEN_INFO("garbage collect from '{}' DONE, collected #{} {} chunks of total #{} {}",
m_Config.RootDirectory / m_ContainerBaseName,
ChunkCount - NewChunkCount,
NiceBytes(TotalSize - NewTotalSize),
ChunkCount,
NiceBytes(TotalSize));
}
catch (std::exception& Err)
{
ZEN_ERROR("garbage collection FAILED, reason '{}'", Err.what());
// Something went wrong, try create a new container
OpenContainer(true /* IsNewStore */);
GcCtx.DeletedCas(ChunksToDelete);
GcCtx.DeletedCas(ChunksToKeep);
}
}
void
CasContainerStrategy::MakeSnapshot()
{
RwLock::SharedLockScope _(m_LocationMapLock);
std::vector<CasDiskIndexEntry> Entries{m_LocationMap.size()};
uint64_t EntryIndex = 0;
for (auto& Entry : m_LocationMap)
{
CasDiskIndexEntry& IndexEntry = Entries[EntryIndex++];
IndexEntry.Key = Entry.first;
IndexEntry.Location = Entry.second;
}
m_SmallObjectIndex.Write(Entries.data(), Entries.size() * sizeof(CasDiskIndexEntry), 0);
}
void
CasContainerStrategy::OpenContainer(bool IsNewStore)
{
std::filesystem::path SobsPath = m_Config.RootDirectory / (m_ContainerBaseName + ".ucas");
std::filesystem::path SidxPath = m_Config.RootDirectory / (m_ContainerBaseName + ".uidx");
std::filesystem::path SlogPath = m_Config.RootDirectory / (m_ContainerBaseName + ".ulog");
m_SmallObjectFile.Open(SobsPath, IsNewStore);
m_SmallObjectIndex.Open(SidxPath, IsNewStore);
m_CasLog.Open(SlogPath, IsNewStore);
// TODO: should validate integrity of container files here
m_CurrentInsertOffset = 0;
m_CurrentIndexOffset = 0;
m_TotalSize = 0;
m_LocationMap.clear();
uint64_t MaxFileOffset = 0;
m_CasLog.Replay([&](const CasDiskIndexEntry& Record) {
if (Record.Flags & CasDiskIndexEntry::kTombstone)
{
m_TotalSize.fetch_sub(Record.Location.GetSize());
}
else
{
m_TotalSize.fetch_add(Record.Location.GetSize());
m_LocationMap[Record.Key] = Record.Location;
MaxFileOffset = std::max<uint64_t>(MaxFileOffset, Record.Location.GetOffset() + Record.Location.GetSize());
}
});
m_CurrentInsertOffset = (MaxFileOffset + m_PayloadAlignment - 1) & ~(m_PayloadAlignment - 1);
m_CurrentIndexOffset = m_SmallObjectIndex.FileSize();
}
void
CasContainerStrategy::CloseContainer()
{
m_SmallObjectFile.Close();
m_SmallObjectIndex.Close();
m_CasLog.Close();
}
//////////////////////////////////////////////////////////////////////////
#if ZEN_WITH_TESTS
TEST_CASE("cas.compact.gc")
{
ScopedTemporaryDirectory TempDir;
CasStoreConfiguration CasConfig;
CasConfig.RootDirectory = TempDir.Path();
CreateDirectories(CasConfig.RootDirectory);
const int kIterationCount = 1000;
std::vector<IoHash> Keys(kIterationCount);
{
CasGc Gc;
CasContainerStrategy Cas(CasConfig, Gc);
Cas.Initialize("test", 16, true);
for (int i = 0; i < kIterationCount; ++i)
{
CbObjectWriter Cbo;
Cbo << "id" << i;
CbObject Obj = Cbo.Save();
IoBuffer ObjBuffer = Obj.GetBuffer().AsIoBuffer();
const IoHash Hash = HashBuffer(ObjBuffer);
Cas.InsertChunk(ObjBuffer, Hash);
Keys[i] = Hash;
}
for (int i = 0; i < kIterationCount; ++i)
{
IoBuffer Chunk = Cas.FindChunk(Keys[i]);
CHECK(!!Chunk);
CbObject Value = LoadCompactBinaryObject(Chunk);
CHECK_EQ(Value["id"].AsInt32(), i);
}
}
// Validate that we can still read the inserted data after closing
// the original cas store
{
CasGc Gc;
CasContainerStrategy Cas(CasConfig, Gc);
Cas.Initialize("test", 16, false);
for (int i = 0; i < kIterationCount; ++i)
{
IoBuffer Chunk = Cas.FindChunk(Keys[i]);
CHECK(!!Chunk);
CbObject Value = LoadCompactBinaryObject(Chunk);
CHECK_EQ(Value["id"].AsInt32(), i);
}
GcContext Ctx;
Cas.CollectGarbage(Ctx);
}
}
TEST_CASE("cas.compact.totalsize")
{
std::random_device rd;
std::mt19937 g(rd());
const auto CreateChunk = [&](uint64_t Size) -> IoBuffer {
const size_t Count = static_cast<size_t>(Size / sizeof(uint32_t));
std::vector<uint32_t> Values;
Values.resize(Count);
for (size_t Idx = 0; Idx < Count; ++Idx)
{
Values[Idx] = static_cast<uint32_t>(Idx);
}
std::shuffle(Values.begin(), Values.end(), g);
return IoBufferBuilder::MakeCloneFromMemory(Values.data(), Values.size() * sizeof(uint32_t));
};
ScopedTemporaryDirectory TempDir;
CasStoreConfiguration CasConfig;
CasConfig.RootDirectory = TempDir.Path();
CreateDirectories(CasConfig.RootDirectory);
const uint64_t kChunkSize = 1024;
const int32_t kChunkCount = 16;
{
CasGc Gc;
CasContainerStrategy Cas(CasConfig, Gc);
Cas.Initialize("test", 16, true);
for (int32_t Idx = 0; Idx < kChunkCount; ++Idx)
{
IoBuffer Chunk = CreateChunk(kChunkSize);
const IoHash Hash = HashBuffer(Chunk);
auto InsertResult = Cas.InsertChunk(Chunk, Hash);
ZEN_ASSERT(InsertResult.New);
}
const uint64_t TotalSize = Cas.StorageSize().DiskSize;
CHECK_EQ(kChunkSize * kChunkCount, TotalSize);
}
{
CasGc Gc;
CasContainerStrategy Cas(CasConfig, Gc);
Cas.Initialize("test", 16, false);
const uint64_t TotalSize = Cas.StorageSize().DiskSize;
CHECK_EQ(kChunkSize * kChunkCount, TotalSize);
}
}
#endif
void
compactcas_forcelink()
{
}
} // namespace zen
|