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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "FileCas.h"
#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/memory.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/uid.h>
#include <spdlog/spdlog.h>
#include <gsl/gsl-lite.hpp>
#include <functional>
#include <unordered_map>
struct IUnknown; // Workaround for "combaseapi.h(229): error C2187: syntax error: 'identifier' was unexpected here" when using /permissive-
#include <atlfile.h>
#include <filesystem>
// Used for getting My Documents for default CAS
#include <ShlObj.h>
#pragma comment(lib, "shell32.lib")
//////////////////////////////////////////////////////////////////////////
namespace zen {
using namespace fmt::literals;
WideStringBuilderBase&
FileCasStrategy::MakeShardedPath(WideStringBuilderBase& ShardedPath, const IoHash& ChunkHash, size_t& OutShard2len)
{
ExtendableStringBuilder<96> HashString;
ChunkHash.ToHexString(HashString);
const char* str = HashString.c_str();
// Shard into a path with two directory levels containing 12 bits and 8 bits
// respectively.
//
// This results in a maximum of 4096 * 256 directories
//
// The numbers have been chosen somewhat arbitrarily but are large to scale
// to very large chunk repositories. It may or may not make sense to make
// this a configurable policy, and it would probably be a good idea to
// measure performance for different policies and chunk counts
ShardedPath.AppendAsciiRange(str, str + 3);
ShardedPath.Append('\\');
ShardedPath.AppendAsciiRange(str + 3, str + 5);
OutShard2len = ShardedPath.Size();
ShardedPath.Append('\\');
ShardedPath.AppendAsciiRange(str + 6, str + 64);
return ShardedPath;
}
CasStore::InsertResult
FileCasStrategy::InsertChunk(IoBuffer Chunk, const IoHash& ChunkHash)
{
// File-based chunks have special case handling whereby we move the file into
// place in the file store directory, thus avoiding unnecessary copying
IoBufferFileReference FileRef;
if (Chunk.IsWholeFile() && Chunk.GetFileReference(/* out */ FileRef))
{
size_t Shard2len = 0;
ExtendableWideStringBuilder<128> ShardedPath;
ShardedPath.Append(m_Config.RootDirectory.c_str());
ShardedPath.Append(std::filesystem::path::preferred_separator);
MakeShardedPath(ShardedPath, ChunkHash, /* out */ Shard2len);
auto DeletePayloadFileOnClose = [&] {
FILE_DISPOSITION_INFO Fdi{};
Fdi.DeleteFile = TRUE;
BOOL Success = SetFileInformationByHandle(FileRef.FileHandle, FileDispositionInfo, &Fdi, sizeof Fdi);
if (!Success)
{
spdlog::warn("Failed to flag temporary payload file for deletion: '{}'", PathFromHandle(FileRef.FileHandle));
}
};
// See if file already exists
//
// Future improvement: maintain Bloom filter to avoid expensive file system probes?
{
CAtlFile PayloadFile;
HRESULT hRes = PayloadFile.Create(ShardedPath.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING);
if (SUCCEEDED(hRes))
{
// If we succeeded in opening the file then we don't need to do anything else because it already exists and should contain
// the content we were about to insert
// We do need to ensure the file goes away on close, however
DeletePayloadFileOnClose();
return CasStore::InsertResult{.New = false};
}
}
std::filesystem::path FullPath(ShardedPath.c_str());
std::filesystem::path FilePath = FullPath.parent_path();
std::wstring FileName = FullPath.native();
const DWORD BufferSize = sizeof(FILE_RENAME_INFO) + gsl::narrow<DWORD>(FileName.size() * sizeof(WCHAR));
FILE_RENAME_INFO* RenameInfo = reinterpret_cast<FILE_RENAME_INFO*>(Memory::Alloc(BufferSize));
memset(RenameInfo, 0, BufferSize);
RenameInfo->ReplaceIfExists = FALSE;
RenameInfo->FileNameLength = gsl::narrow<DWORD>(FileName.size());
memcpy(RenameInfo->FileName, FileName.c_str(), FileName.size() * sizeof(WCHAR));
RenameInfo->FileName[FileName.size()] = 0;
// Try to move file into place
BOOL Success = SetFileInformationByHandle(FileRef.FileHandle, FileRenameInfo, RenameInfo, BufferSize);
if (!Success)
{
CAtlFile DirHandle;
auto InternalCreateDirectoryHandle = [&] {
return DirHandle.Create(FilePath.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS);
};
HRESULT hRes = InternalCreateDirectoryHandle();
if (FAILED(hRes))
{
zen::CreateDirectories(FilePath.c_str());
hRes = InternalCreateDirectoryHandle();
}
if (FAILED(hRes))
{
throw WindowsException(hRes, "Failed to open shard directory '{}'"_format(FilePath));
}
// Retry
Success = SetFileInformationByHandle(FileRef.FileHandle, FileRenameInfo, RenameInfo, BufferSize);
}
Memory::Free(RenameInfo);
if (Success)
{
return CasStore::InsertResult{.New = true};
}
spdlog::warn("rename of CAS payload file failed, falling back to regular write for {}", ChunkHash);
DeletePayloadFileOnClose();
}
return InsertChunk(Chunk.Data(), Chunk.Size(), ChunkHash);
}
CasStore::InsertResult
FileCasStrategy::InsertChunk(const void* const ChunkData, const size_t ChunkSize, const IoHash& ChunkHash)
{
size_t Shard2len = 0;
ExtendableWideStringBuilder<128> ShardedPath;
ShardedPath.Append(m_Config.RootDirectory.c_str());
ShardedPath.Append(std::filesystem::path::preferred_separator);
MakeShardedPath(ShardedPath, ChunkHash, /* out */ Shard2len);
// See if file already exists
//
// Future improvement: maintain Bloom filter to avoid expensive file system probes?
CAtlFile PayloadFile;
HRESULT hRes = PayloadFile.Create(ShardedPath.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING);
if (SUCCEEDED(hRes))
{
// If we succeeded in opening the file then we don't need to do anything else because it already exists and should contain the
// content we were about to insert
return CasStore::InsertResult{.New = false};
}
PayloadFile.Close();
RwLock::ExclusiveLockScope _(LockForHash(ChunkHash));
// For now, use double-checked locking to see if someone else was first
hRes = PayloadFile.Create(ShardedPath.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING);
if (SUCCEEDED(hRes))
{
// If we succeeded in opening the file then we don't need to do anything
// else because someone else managed to create the file before we did. Just return.
return {.New = false};
}
auto InternalCreateFile = [&] { return PayloadFile.Create(ShardedPath.c_str(), GENERIC_WRITE, FILE_SHARE_DELETE, CREATE_ALWAYS); };
hRes = InternalCreateFile();
if (hRes == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND))
{
// Ensure parent directories exist
std::filesystem::create_directories(std::wstring_view(ShardedPath.c_str(), Shard2len));
hRes = InternalCreateFile();
}
if (FAILED(hRes))
{
throw WindowsException(hRes, "Failed to open shard file");
}
size_t ChunkRemain = ChunkSize;
auto ChunkCursor = reinterpret_cast<const uint8_t*>(ChunkData);
while (ChunkRemain != 0)
{
uint32_t ByteCount = uint32_t(std::min<size_t>(1024 * 1024ull, ChunkRemain));
PayloadFile.Write(ChunkCursor, ByteCount);
ChunkCursor += ByteCount;
ChunkRemain -= ByteCount;
}
AtomicIncrement(m_Stats.PutCount);
AtomicAdd(m_Stats.PutBytes, ChunkSize);
return {.New = true};
}
IoBuffer
FileCasStrategy::FindChunk(const IoHash& ChunkHash)
{
size_t Shard2len = 0;
ExtendableWideStringBuilder<128> ShardedPath;
ShardedPath.Append(m_Config.RootDirectory.c_str());
ShardedPath.Append(std::filesystem::path::preferred_separator);
MakeShardedPath(ShardedPath, ChunkHash, /* out */ Shard2len);
RwLock::SharedLockScope _(LockForHash(ChunkHash));
auto Chunk = IoBufferBuilder::MakeFromFile(ShardedPath.c_str());
if (Chunk)
{
AtomicIncrement(m_Stats.GetCount);
AtomicAdd(m_Stats.GetBytes, Chunk.Size());
}
return Chunk;
}
/**
* Straightforward file-per-chunk CAS store implementation
*/
class FileCasImpl : public CasStore
{
public:
FileCasImpl() : m_Strategy(m_Config, m_Stats) {}
virtual ~FileCasImpl() = default;
void PickDefaultDirectory()
{
if (m_Config.RootDirectory.empty())
{
// Pick sensible default
WCHAR myDocumentsDir[MAX_PATH];
HRESULT hRes = SHGetFolderPathW(NULL,
CSIDL_PERSONAL /* My Documents */,
NULL,
SHGFP_TYPE_CURRENT,
/* out */ myDocumentsDir);
if (SUCCEEDED(hRes))
{
wcscat_s(myDocumentsDir, L"\\zen\\DefaultCAS");
m_Config.RootDirectory = myDocumentsDir;
}
}
}
virtual void Initialize(const CasStoreConfiguration& InConfig) override
{
m_Config = InConfig;
if (m_Config.RootDirectory.empty())
{
PickDefaultDirectory();
}
// Ensure root directory exists - create if it doesn't exist already
std::filesystem::create_directories(m_Config.RootDirectory);
std::filesystem::path filepath = m_Config.RootDirectory;
filepath /= ".cas_root";
CAtlFile marker;
HRESULT hRes = marker.Create(filepath.c_str(), GENERIC_READ, 0, OPEN_EXISTING);
if (FAILED(hRes))
{
ExtendableStringBuilder<128> manifest;
manifest.Append("CAS_ROOT");
hRes = marker.Create(filepath.c_str(), GENERIC_WRITE, 0, CREATE_ALWAYS);
if (SUCCEEDED(hRes))
marker.Write(manifest.c_str(), (DWORD)manifest.Size());
}
}
virtual CasStore::InsertResult InsertChunk(const void* chunkData, size_t chunkSize, const IoHash& chunkHash) override
{
return m_Strategy.InsertChunk(chunkData, chunkSize, chunkHash);
}
virtual CasStore::InsertResult InsertChunk(IoBuffer Chunk, const IoHash& chunkHash) override
{
return m_Strategy.InsertChunk(Chunk, chunkHash);
}
virtual IoBuffer FindChunk(const IoHash& chunkHash) override { return m_Strategy.FindChunk(chunkHash); }
private:
FileCasStrategy m_Strategy;
};
} // namespace zen
|