1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#ifndef CVMFS_SESSION_CONTEXT_H_ |
6 |
|
|
#define CVMFS_SESSION_CONTEXT_H_ |
7 |
|
|
|
8 |
|
|
#include <string> |
9 |
|
|
#include <vector> |
10 |
|
|
|
11 |
|
|
#include "pack.h" |
12 |
|
|
#include "repository_tag.h" |
13 |
|
|
#include "util/pointer.h" |
14 |
|
|
#include "util_concurrency.h" |
15 |
|
|
|
16 |
|
|
namespace upload { |
17 |
|
|
|
18 |
|
|
struct CurlSendPayload { |
19 |
|
|
const std::string* json_message; |
20 |
|
|
ObjectPackProducer* pack_serializer; |
21 |
|
|
size_t index; |
22 |
|
|
}; |
23 |
|
|
|
24 |
|
|
size_t SendCB(void* ptr, size_t size, size_t nmemb, void* userp); |
25 |
|
|
size_t RecvCB(void* buffer, size_t size, size_t nmemb, void* userp); |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* This class implements a context for a single publish operation |
29 |
|
|
* |
30 |
|
|
* The context is created at the start of a publish operation and |
31 |
|
|
* is supposed to live at least until the payload has been submitted |
32 |
|
|
* to the repo services. |
33 |
|
|
* |
34 |
|
|
* It is the GatewayUploader concrete class which handles the creation and |
35 |
|
|
* destruction of the SessionContext. A session should begin when the spooler |
36 |
|
|
* and uploaders are initialized and should last until the call to |
37 |
|
|
* Spooler::WaitForUpload(). |
38 |
|
|
*/ |
39 |
|
|
class SessionContextBase { |
40 |
|
|
public: |
41 |
|
|
SessionContextBase(); |
42 |
|
|
|
43 |
|
|
virtual ~SessionContextBase(); |
44 |
|
|
|
45 |
|
|
// By default, the maximum number of queued jobs is limited to 10, |
46 |
|
|
// representing 10 * 200 MB = 2GB max memory used by the queue |
47 |
|
|
bool Initialize(const std::string& api_url, const std::string& session_token, |
48 |
|
|
const std::string& key_id, const std::string& secret, |
49 |
|
|
uint64_t max_pack_size = ObjectPack::kDefaultLimit, |
50 |
|
|
uint64_t max_queue_size = 10); |
51 |
|
|
bool Finalize(bool commit, const std::string& old_root_hash, |
52 |
|
|
const std::string& new_root_hash, |
53 |
|
|
const RepositoryTag& tag); |
54 |
|
|
|
55 |
|
|
void WaitForUpload(); |
56 |
|
|
|
57 |
|
|
ObjectPack::BucketHandle NewBucket(); |
58 |
|
|
|
59 |
|
|
bool CommitBucket(const ObjectPack::BucketContentType type, |
60 |
|
|
const shash::Any& id, const ObjectPack::BucketHandle handle, |
61 |
|
|
const std::string& name = "", |
62 |
|
|
const bool force_dispatch = false); |
63 |
|
|
|
64 |
|
|
protected: |
65 |
|
|
virtual bool InitializeDerived(uint64_t max_queue_size) = 0; |
66 |
|
|
|
67 |
|
|
virtual bool FinalizeDerived() = 0; |
68 |
|
|
|
69 |
|
|
virtual bool Commit(const std::string& old_root_hash, |
70 |
|
|
const std::string& new_root_hash, |
71 |
|
|
const RepositoryTag& tag) = 0; |
72 |
|
|
|
73 |
|
|
virtual Future<bool>* DispatchObjectPack(ObjectPack* pack) = 0; |
74 |
|
|
|
75 |
|
|
int64_t NumJobsSubmitted() const; |
76 |
|
|
|
77 |
|
|
FifoChannel<Future<bool>*> upload_results_; |
78 |
|
|
|
79 |
|
|
std::string api_url_; |
80 |
|
|
std::string session_token_; |
81 |
|
|
std::string key_id_; |
82 |
|
|
std::string secret_; |
83 |
|
|
|
84 |
|
|
FifoChannel<bool> queue_was_flushed_; |
85 |
|
|
|
86 |
|
|
private: |
87 |
|
|
void Dispatch(); |
88 |
|
|
|
89 |
|
|
uint64_t max_pack_size_; |
90 |
|
|
|
91 |
|
|
std::vector<ObjectPack::BucketHandle> active_handles_; |
92 |
|
|
|
93 |
|
|
ObjectPack* current_pack_; |
94 |
|
|
pthread_mutex_t current_pack_mtx_; |
95 |
|
|
|
96 |
|
|
mutable atomic_int64 objects_dispatched_; |
97 |
|
|
uint64_t bytes_committed_; |
98 |
|
|
uint64_t bytes_dispatched_; |
99 |
|
|
}; |
100 |
|
|
|
101 |
✗✗✓ |
15 |
class SessionContext : public SessionContextBase { |
102 |
|
|
public: |
103 |
|
|
SessionContext(); |
104 |
|
|
|
105 |
|
|
protected: |
106 |
|
|
struct UploadJob { |
107 |
|
|
ObjectPack* pack; |
108 |
|
|
Future<bool>* result; |
109 |
|
|
}; |
110 |
|
|
|
111 |
|
|
virtual bool InitializeDerived(uint64_t max_queue_size); |
112 |
|
|
|
113 |
|
|
virtual bool FinalizeDerived(); |
114 |
|
|
|
115 |
|
|
virtual bool Commit(const std::string& old_root_hash, |
116 |
|
|
const std::string& new_root_hash, |
117 |
|
|
const RepositoryTag& tag); |
118 |
|
|
|
119 |
|
|
virtual Future<bool>* DispatchObjectPack(ObjectPack* pack); |
120 |
|
|
|
121 |
|
|
virtual bool DoUpload(const UploadJob* job); |
122 |
|
|
|
123 |
|
|
private: |
124 |
|
|
static void* UploadLoop(void* data); |
125 |
|
|
|
126 |
|
|
bool ShouldTerminate(); |
127 |
|
|
|
128 |
|
|
UniquePtr<FifoChannel<UploadJob*> > upload_jobs_; |
129 |
|
|
|
130 |
|
|
atomic_int32 worker_terminate_; |
131 |
|
|
pthread_t worker_; |
132 |
|
|
}; |
133 |
|
|
|
134 |
|
|
} // namespace upload |
135 |
|
|
|
136 |
|
|
#endif // CVMFS_SESSION_CONTEXT_H_ |