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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "zenstore/cidstore.h"
#include <zencore/compress.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/stats.h>
#include <zencore/string.h>
#include <zenstore/scrubcontext.h>
#include "cas.h"
#include <filesystem>
namespace zen {
struct CidStore::Impl
{
explicit Impl(CasStore& InCasStore) : m_CasStore(InCasStore) {}
CasStore& m_CasStore;
void Initialize(const CidStoreConfiguration& Config) { m_CasStore.Initialize(Config); }
CidStore::InsertResult AddChunk(const IoBuffer& ChunkData, const IoHash& RawHash, CidStore::InsertMode Mode)
{
metrics::RequestStats::Scope $(m_AddChunkOps, ChunkData.GetSize());
#if ZEN_BUILD_DEBUG
IoHash VerifyRawHash;
uint64_t _;
ZEN_ASSERT(CompressedBuffer::ValidateCompressedHeader(ChunkData, VerifyRawHash, _) && RawHash == VerifyRawHash);
#endif
IoBuffer Payload(ChunkData);
Payload.SetContentType(ZenContentType::kCompressedBinary);
CasStore::InsertResult Result = m_CasStore.InsertChunk(Payload, RawHash, static_cast<CasStore::InsertMode>(Mode));
if (Result.New)
{
m_WriteCount++;
}
return {.New = Result.New};
}
std::vector<CidStore::InsertResult> AddChunks(std::span<IoBuffer> ChunkDatas, std::span<IoHash> RawHashes, CidStore::InsertMode Mode)
{
if (ChunkDatas.size() == 1)
{
std::vector<CidStore::InsertResult> Result(1);
Result[0] = AddChunk(ChunkDatas[0], RawHashes[0], Mode);
return Result;
}
ZEN_ASSERT(ChunkDatas.size() == RawHashes.size());
std::vector<IoBuffer> Chunks;
Chunks.reserve(ChunkDatas.size());
#if ZEN_BUILD_DEBUG
size_t Offset = 0;
#endif
uint64_t TotalSize = 0;
for (const IoBuffer& ChunkData : ChunkDatas)
{
TotalSize += ChunkData.GetSize();
#if ZEN_BUILD_DEBUG
IoHash VerifyRawHash;
uint64_t _;
ZEN_ASSERT(CompressedBuffer::ValidateCompressedHeader(ChunkData, VerifyRawHash, _) && RawHashes[Offset++] == VerifyRawHash);
#endif
Chunks.push_back(ChunkData);
Chunks.back().SetContentType(ZenContentType::kCompressedBinary);
}
metrics::RequestStats::Scope $(m_AddChunkOps, TotalSize);
std::vector<CasStore::InsertResult> CasResults =
m_CasStore.InsertChunks(Chunks, RawHashes, static_cast<CasStore::InsertMode>(Mode));
ZEN_ASSERT(CasResults.size() == ChunkDatas.size());
std::vector<CidStore::InsertResult> Result;
for (const CasStore::InsertResult& CasResult : CasResults)
{
if (CasResult.New)
{
m_WriteCount++;
}
Result.emplace_back(CidStore::InsertResult{.New = CasResult.New});
}
return Result;
}
IoBuffer FindChunkByCid(const IoHash& DecompressedId)
{
metrics::RequestStats::Scope StatsScope(m_FindChunkOps, 0);
IoBuffer Result = m_CasStore.FindChunk(DecompressedId);
if (Result)
{
m_HitCount++;
StatsScope.SetBytes(Result.GetSize());
}
else
{
m_MissCount++;
}
return Result;
}
bool ContainsChunk(const IoHash& DecompressedId)
{
// metrics::RequestStats::Scope $(m_ContainChunkOps);
return m_CasStore.ContainsChunk(DecompressedId);
}
void FilterChunks(HashKeySet& InOutChunks)
{
InOutChunks.RemoveHashesIf([&](const IoHash& Hash) { return ContainsChunk(Hash); });
}
bool IterateChunks(std::span<IoHash> DecompressedIds,
const std::function<bool(size_t Index, const IoBuffer& Payload)>& AsyncCallback,
WorkerThreadPool* OptionalWorkerPool,
uint64_t LargeSizeLimit)
{
return m_CasStore.IterateChunks(DecompressedIds, AsyncCallback, OptionalWorkerPool, LargeSizeLimit);
}
void Flush() { m_CasStore.Flush(); }
#if ZEN_WITH_TESTS
void Scrub(ScrubContext& Ctx) { m_CasStore.Scrub(Ctx); }
#endif // ZEN_WITH_TESTS
CidStoreStats Stats()
{
return CidStoreStats{
.HitCount = m_HitCount,
.MissCount = m_MissCount,
.WriteCount = m_WriteCount,
.AddChunkOps = m_AddChunkOps.Snapshot(),
.FindChunkOps = m_FindChunkOps.Snapshot()
// .ContainChunkOps = m_ContainChunkOps.Snapshot()
};
}
void ReportMetrics(StatsMetrics& Statsd)
{
const CidStoreStats Now = Stats();
const CidStoreStats& Old = m_LastReportedMetrics;
Statsd.Meter("zen.cas_hits", Now.HitCount - Old.HitCount);
Statsd.Meter("zen.cas_misses", Now.MissCount - Old.MissCount);
Statsd.Meter("zen.cas_writes", Now.WriteCount - Old.WriteCount);
m_LastReportedMetrics = Now;
}
std::atomic_uint64_t m_HitCount{};
std::atomic_uint64_t m_MissCount{};
std::atomic_uint64_t m_WriteCount{};
metrics::RequestStats m_AddChunkOps;
metrics::RequestStats m_FindChunkOps;
CidStoreStats m_LastReportedMetrics;
uint64_t m_LastScrubTime = 0;
};
//////////////////////////////////////////////////////////////////////////
CidStore::CidStore(GcManager& Gc) : m_CasStore(CreateCasStore(Gc)), m_Impl(std::make_unique<Impl>(*m_CasStore))
{
}
CidStore::~CidStore()
{
}
void
CidStore::Initialize(const CidStoreConfiguration& Config)
{
m_Impl->Initialize(Config);
}
CidStore::InsertResult
CidStore::AddChunk(const IoBuffer& ChunkData, const IoHash& RawHash, InsertMode Mode)
{
return m_Impl->AddChunk(ChunkData, RawHash, Mode);
}
std::vector<CidStore::InsertResult>
CidStore::AddChunks(std::span<IoBuffer> ChunkDatas, std::span<IoHash> RawHashes, InsertMode Mode)
{
return m_Impl->AddChunks(ChunkDatas, RawHashes, Mode);
}
IoBuffer
CidStore::FindChunkByCid(const IoHash& DecompressedId)
{
return m_Impl->FindChunkByCid(DecompressedId);
}
bool
CidStore::ContainsChunk(const IoHash& DecompressedId)
{
return m_Impl->ContainsChunk(DecompressedId);
}
bool
CidStore::IterateChunks(std::span<IoHash> DecompressedIds,
const std::function<bool(size_t Index, const IoBuffer& Payload)>& AsyncCallback,
WorkerThreadPool* OptionalWorkerPool,
uint64_t LargeSizeLimit)
{
return m_Impl->IterateChunks(DecompressedIds, AsyncCallback, OptionalWorkerPool, LargeSizeLimit);
}
void
CidStore::FilterChunks(HashKeySet& InOutChunks)
{
return m_Impl->FilterChunks(InOutChunks);
}
void
CidStore::Flush()
{
m_Impl->Flush();
}
#if ZEN_WITH_TESTS
void
CidStore::Scrub(ScrubContext& Ctx)
{
m_Impl->Scrub(Ctx);
}
#endif // ZEN_WITH_TESTS
CidStoreSize
CidStore::TotalSize() const
{
return m_Impl->m_CasStore.TotalSize();
}
CidStoreStats
CidStore::Stats() const
{
return m_Impl->Stats();
}
void
CidStore::ReportMetrics(StatsMetrics& Statsd)
{
return m_Impl->ReportMetrics(Statsd);
}
} // namespace zen
|