aboutsummaryrefslogtreecommitdiff
path: root/zenserver/upstream/jupiter.h
blob: f05e059bdd6f50efb4777bdfd88d7bbf4e4924d4 (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/logging.h>
#include <zencore/refcount.h>
#include <zencore/thread.h>
#include <zenhttp/httpserver.h>

#include <atomic>
#include <chrono>
#include <list>
#include <memory>
#include <vector>

struct ZenCacheValue;

namespace zen {
namespace detail {
	struct CloudCacheSessionState;
}

class IoBuffer;
class CloudCacheClient;
struct IoHash;
class CbObjectView;

/**
 * Cached access token, for use with `Authorization:` header
 */
struct CloudCacheAccessToken
{
	static constexpr int64_t ExpireMarginInSeconds = 30;

	std::string							  Value;
	std::chrono::steady_clock::time_point ExpireTime;

	bool IsValid() const
	{
		return !Value.empty() &&
			   ExpireMarginInSeconds <
				   std::chrono::duration_cast<std::chrono::seconds>(ExpireTime - std::chrono::steady_clock::now()).count();
	}
};

struct CloudCacheResult
{
	IoBuffer	Response;
	int64_t		Bytes		   = {};
	double		ElapsedSeconds = {};
	int32_t		ErrorCode	   = {};
	std::string Reason;
	bool		Success = false;
	bool		Exists	= false;
};

/**
 * 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 GetDerivedData(std::string_view BucketId, std::string_view Key);
	CloudCacheResult GetDerivedData(std::string_view BucketId, const IoHash& Key);
	CloudCacheResult GetRef(std::string_view BucketId, const IoHash& Key, ZenContentType RefType);
	CloudCacheResult GetBlob(const IoHash& Key);
	CloudCacheResult GetCompressedBlob(const IoHash& Key);
	CloudCacheResult GetObject(const IoHash& Key);

	CloudCacheResult PutDerivedData(std::string_view BucketId, std::string_view Key, IoBuffer DerivedData);
	CloudCacheResult PutDerivedData(std::string_view BucketId, const IoHash& Key, IoBuffer DerivedData);
	CloudCacheResult PutRef(std::string_view BucketId, const IoHash& Key, IoBuffer Ref, ZenContentType RefType);
	CloudCacheResult PutBlob(const IoHash& Key, IoBuffer Blob);
	CloudCacheResult PutCompressedBlob(const IoHash& Key, IoBuffer Blob);
	CloudCacheResult PutObject(const IoHash& Key, IoBuffer Object);

	CloudCacheResult DerivedDataExists(std::string_view BucketId, std::string_view Key);
	CloudCacheResult DerivedDataExists(std::string_view BucketId, const IoHash& Key);
	CloudCacheResult RefExists(std::string_view BucketId, const IoHash& Key);
	CloudCacheResult BlobExists(const IoHash& Key);
	CloudCacheResult CompressedBlobExists(const IoHash& Key);
	CloudCacheResult ObjectExists(const IoHash& Key);

	std::vector<IoHash> Filter(std::string_view BucketId, const std::vector<IoHash>& ChunkHashes);

private:
	inline spdlog::logger&		 Log() { return m_Log; }
	const CloudCacheAccessToken& GetAccessToken();
	bool						 VerifyAccessToken(long StatusCode);

	spdlog::logger&					m_Log;
	RefPtr<CloudCacheClient>		m_CacheClient;
	detail::CloudCacheSessionState* m_SessionState;
};

struct CloudCacheClientOptions
{
	std::string_view ServiceUrl;
	std::string_view DdcNamespace;
	std::string_view BlobStoreNamespace;
	std::string_view OAuthProvider;
	std::string_view OAuthClientId;
	std::string_view OAuthSecret;
	bool			 UseLegacyDdc = false;
};

/**
 * Jupiter upstream cache client
 */
class CloudCacheClient : public RefCounted
{
public:
	CloudCacheClient(const CloudCacheClientOptions& Options);
	~CloudCacheClient();

	CloudCacheAccessToken AcquireAccessToken();
	std::string_view	  DdcNamespace() const { return m_DdcNamespace; }
	std::string_view	  BlobStoreNamespace() const { return m_BlobStoreNamespace; }
	std::string_view	  ServiceUrl() const { return m_ServiceUrl; }
	bool				  IsValid() const { return m_IsValid; }

	spdlog::logger& Logger() { return m_Log; }

private:
	spdlog::logger& m_Log;
	std::string		m_ServiceUrl;
	std::string		m_OAuthDomain;
	std::string		m_OAuthUriPath;
	std::string		m_OAuthFullUri;
	std::string		m_DdcNamespace;
	std::string		m_BlobStoreNamespace;
	std::string		m_OAuthClientId;
	std::string		m_OAuthSecret;
	bool			m_IsValid = false;

	RwLock									   m_SessionStateLock;
	std::list<detail::CloudCacheSessionState*> m_SessionStateCache;

	detail::CloudCacheSessionState* AllocSessionState();
	void							FreeSessionState(detail::CloudCacheSessionState*);

	friend class CloudCacheSession;
};

}  // namespace zen