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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include <zenbase/refcount.h>
#include <zencore/iohash.h>
#include <zencore/logging.h>
#include <zencore/thread.h>
#include <zenhttp/httpclient.h>
#include <zenhttp/httpserver.h>
#include <atomic>
#include <chrono>
#include <list>
#include <memory>
#include <set>
#include <vector>
struct ZenCacheValue;
namespace cpr {
class Session;
}
namespace zen {
class CbObjectView;
class CloudCacheClient;
class IoBuffer;
struct IoHash;
/**
* Cached access token, for use with `Authorization:` header
*/
struct CloudCacheAccessToken
{
using Clock = std::chrono::system_clock;
using TimePoint = Clock::time_point;
static constexpr int64_t ExpireMarginInSeconds = 30;
std::string Value;
TimePoint ExpireTime;
bool IsValid() const
{
return Value.empty() == false &&
ExpireMarginInSeconds < std::chrono::duration_cast<std::chrono::seconds>(ExpireTime - Clock::now()).count();
}
};
struct CloudCacheResult
{
IoBuffer Response;
uint64_t SentBytes{};
uint64_t ReceivedBytes{};
double ElapsedSeconds{};
int32_t ErrorCode{};
std::string Reason;
bool Success = false;
};
struct PutRefResult : CloudCacheResult
{
std::vector<IoHash> Needs;
IoHash RawHash;
};
struct FinalizeRefResult : CloudCacheResult
{
std::vector<IoHash> Needs;
};
struct CloudCacheExistsResult : CloudCacheResult
{
std::set<IoHash> Needs;
};
struct GetObjectReferencesResult : CloudCacheResult
{
std::set<IoHash> References;
};
/**
* Context for performing Jupiter operations
*
* Maintains an HTTP connection so that subsequent operations don't need to go
* through the whole connection setup process
*
*/
class CloudCacheSession
{
public:
CloudCacheSession(CloudCacheClient* CacheClient);
~CloudCacheSession();
CloudCacheResult Authenticate();
CloudCacheResult GetRef(std::string_view Namespace, std::string_view BucketId, const IoHash& Key, ZenContentType RefType);
CloudCacheResult GetBlob(std::string_view Namespace, const IoHash& Key);
CloudCacheResult GetCompressedBlob(std::string_view Namespace, const IoHash& Key, std::filesystem::path TempFolderPath = {});
CloudCacheResult GetObject(std::string_view Namespace, const IoHash& Key);
CloudCacheResult GetInlineBlob(std::string_view Namespace,
std::string_view BucketId,
const IoHash& Key,
IoHash& OutPayloadHash,
std::filesystem::path TempFolderPath = {});
PutRefResult PutRef(std::string_view Namespace, std::string_view BucketId, const IoHash& Key, IoBuffer Ref, ZenContentType RefType);
CloudCacheResult PutBlob(std::string_view Namespace, const IoHash& Key, IoBuffer Blob);
CloudCacheResult PutCompressedBlob(std::string_view Namespace, const IoHash& Key, IoBuffer Blob);
CloudCacheResult PutCompressedBlob(std::string_view Namespace, const IoHash& Key, const CompositeBuffer& Blob);
CloudCacheResult PutObject(std::string_view Namespace, const IoHash& Key, IoBuffer Object);
FinalizeRefResult FinalizeRef(std::string_view Namespace, std::string_view BucketId, const IoHash& Key, const IoHash& RefHah);
CloudCacheResult RefExists(std::string_view Namespace, std::string_view BucketId, const IoHash& Key);
GetObjectReferencesResult GetObjectReferences(std::string_view Namespace, const IoHash& Key);
CloudCacheResult BlobExists(std::string_view Namespace, const IoHash& Key);
CloudCacheResult CompressedBlobExists(std::string_view Namespace, const IoHash& Key);
CloudCacheResult ObjectExists(std::string_view Namespace, const IoHash& Key);
CloudCacheExistsResult BlobExists(std::string_view Namespace, const std::set<IoHash>& Keys);
CloudCacheExistsResult CompressedBlobExists(std::string_view Namespace, const std::set<IoHash>& Keys);
CloudCacheExistsResult ObjectExists(std::string_view Namespace, const std::set<IoHash>& Keys);
std::vector<IoHash> Filter(std::string_view Namespace, std::string_view BucketId, const std::vector<IoHash>& ChunkHashes);
CloudCacheClient& Client() { return *m_CacheClient; };
private:
inline LoggerRef Log() { return m_Log; }
CloudCacheResult CacheTypeExists(std::string_view Namespace, std::string_view TypeId, const IoHash& Key);
CloudCacheExistsResult CacheTypeExists(std::string_view Namespace, std::string_view TypeId, const std::set<IoHash>& Keys);
LoggerRef m_Log;
RefPtr<CloudCacheClient> m_CacheClient;
};
/**
* Access token provider interface
*/
class CloudCacheTokenProvider
{
public:
virtual ~CloudCacheTokenProvider() = default;
virtual CloudCacheAccessToken AcquireAccessToken() = 0;
static std::unique_ptr<CloudCacheTokenProvider> CreateFromStaticToken(CloudCacheAccessToken Token);
struct OAuthClientCredentialsParams
{
std::string_view Url;
std::string_view ClientId;
std::string_view ClientSecret;
};
static std::unique_ptr<CloudCacheTokenProvider> CreateFromOAuthClientCredentials(const OAuthClientCredentialsParams& Params);
static std::unique_ptr<CloudCacheTokenProvider> CreateFromCallback(std::function<CloudCacheAccessToken()>&& Callback);
};
struct CloudCacheClientOptions
{
std::string_view Name;
std::string_view ServiceUrl;
std::string_view DdcNamespace;
std::string_view BlobStoreNamespace;
std::string_view ComputeCluster;
std::chrono::milliseconds ConnectTimeout{5000};
std::chrono::milliseconds Timeout{};
bool AssumeHttp2 = false;
bool AllowResume = false;
uint8_t RetryCount = 0;
};
/**
* Jupiter upstream cache client
*/
class CloudCacheClient : public RefCounted
{
public:
CloudCacheClient(const CloudCacheClientOptions& Options, std::unique_ptr<CloudCacheTokenProvider> TokenProvider);
~CloudCacheClient();
std::string_view DefaultDdcNamespace() const { return m_DefaultDdcNamespace; }
std::string_view DefaultBlobStoreNamespace() const { return m_DefaultBlobStoreNamespace; }
std::string_view ComputeCluster() const { return m_ComputeCluster; }
std::string_view ServiceUrl() const { return m_HttpClient.GetBaseUri(); }
LoggerRef Logger() { return m_Log; }
private:
LoggerRef m_Log;
const std::string m_DefaultDdcNamespace;
const std::string m_DefaultBlobStoreNamespace;
const std::string m_ComputeCluster;
const std::unique_ptr<CloudCacheTokenProvider> m_TokenProvider;
HttpClient m_HttpClient;
friend class CloudCacheSession;
};
} // namespace zen
|