-
Notifications
You must be signed in to change notification settings - Fork 28
/
PullBackupReactor.cpp
216 lines (199 loc) · 8.38 KB
/
PullBackupReactor.cpp
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
#include "PullBackupReactor.h"
#include "blob_client/src/lib.rs.h"
#include "DatabaseManager.h"
namespace comm {
namespace network {
namespace reactor {
PullBackupReactor::PullBackupReactor(const backup::PullBackupRequest *request)
: ServerWriteReactorBase<
backup::PullBackupRequest,
backup::PullBackupResponse>(request) {
}
void PullBackupReactor::initializeGetReactor(const std::string &holder) {
if (this->backupItem == nullptr) {
throw std::runtime_error(
"get reactor cannot be initialized when backup item is missing");
}
get_client_initialize_cxx(rust::String(holder));
this->clientInitialized = true;
}
void PullBackupReactor::initialize() {
// we make sure that the blob client's state is flushed to the main memory
// as there may be multiple threads from the pool taking over here
const std::lock_guard<std::mutex> lock(this->reactorStateMutex);
if (this->request.userid().empty()) {
throw std::runtime_error("no user id provided");
}
if (this->request.backupid().empty()) {
throw std::runtime_error("no backup id provided");
}
this->backupItem = database::DatabaseManager::getInstance().findBackupItem(
this->request.userid(), this->request.backupid());
if (this->backupItem == nullptr) {
throw std::runtime_error(
"no backup found for provided parameters: user id [" +
this->request.userid() + "], backup id [" + this->request.backupid() +
"]");
}
this->logs = database::DatabaseManager::getInstance().findLogItemsForBackup(
this->request.backupid());
}
std::unique_ptr<grpc::Status>
PullBackupReactor::writeResponse(backup::PullBackupResponse *response) {
// we make sure that the blob client's state is flushed to the main memory
// as there may be multiple threads from the pool taking over here
const std::lock_guard<std::mutex> lock(this->reactorStateMutex);
response->set_attachmentholders("");
response->set_backupid("");
size_t extraBytesNeeded = 0;
if (this->state == State::COMPACTION) {
response->set_backupid(this->backupItem->getBackupID());
extraBytesNeeded += database::BackupItem::FIELD_BACKUP_ID.size();
extraBytesNeeded += this->backupItem->getBackupID().size();
if (!this->clientInitialized) {
extraBytesNeeded += database::BackupItem::FIELD_ATTACHMENT_HOLDERS.size();
extraBytesNeeded += this->backupItem->getAttachmentHolders().size();
response->set_attachmentholders(this->backupItem->getAttachmentHolders());
this->initializeGetReactor(this->backupItem->getCompactionHolder());
}
std::string dataChunk;
if (this->internalBuffer.size() < this->chunkLimit) {
rust::Vec<unsigned char> responseVec = get_client_blocking_read_cxx(
this->backupItem->getCompactionHolder().c_str());
dataChunk = (responseVec.empty())
? ""
: std::string(reinterpret_cast<char *>(responseVec.data()));
dataChunk.resize(responseVec.size());
}
if (!dataChunk.empty() ||
this->internalBuffer.size() + extraBytesNeeded >= this->chunkLimit) {
dataChunk =
this->prepareDataChunkWithPadding(dataChunk, extraBytesNeeded);
response->set_compactionchunk(dataChunk);
return nullptr;
}
this->state = State::LOGS;
if (!this->internalBuffer.empty()) {
response->set_compactionchunk(std::move(this->internalBuffer));
return nullptr;
}
}
if (this->state == State::LOGS) {
// TODO make sure logs are received in correct order regardless their size
if (this->logs.empty()) {
// this means that there are no logs at all so we just terminate with
// the compaction
return std::make_unique<grpc::Status>(grpc::Status::OK);
}
if (this->currentLogIndex == this->logs.size()) {
if (!this->internalBuffer.empty()) {
response->set_logid(this->previousLogID);
response->set_logchunk(std::move(this->internalBuffer));
return nullptr;
}
return std::make_unique<grpc::Status>(grpc::Status::OK);
}
if (this->currentLogIndex > this->logs.size()) {
// we went out of the scope of the logs collection, this should never
// happen and should be perceived as an error
throw std::runtime_error("log index out of bound");
}
// this means that we're not reading anything between invocations of
// writeResponse
// it is only not null when we read data in chunks
if (this->currentLog == nullptr) {
this->currentLog = this->logs.at(this->currentLogIndex);
extraBytesNeeded += database::LogItem::FIELD_LOG_ID.size();
extraBytesNeeded += this->currentLog->getLogID().size();
response->set_attachmentholders(this->currentLog->getAttachmentHolders());
extraBytesNeeded += database::LogItem::FIELD_ATTACHMENT_HOLDERS.size();
extraBytesNeeded += this->currentLog->getAttachmentHolders().size();
if (this->currentLog->getPersistedInBlob()) {
// if the item is stored in the blob, we initialize the get reactor
// and proceed
this->initializeGetReactor(this->currentLog->getValue());
this->currentLogHolder =
std::make_unique<std::string>(this->currentLog->getValue());
} else {
// if the item is persisted in the database, we just take it, send the
// data to the client and reset currentLog so the next invocation of
// writeResponse will take another one from the collection
response->set_logid(this->currentLog->getLogID());
response->set_logchunk(this->currentLog->getValue());
this->nextLog();
return nullptr;
}
} else {
extraBytesNeeded += database::LogItem::FIELD_LOG_ID.size();
extraBytesNeeded += this->currentLog->getLogID().size();
}
response->set_backupid(this->currentLog->getBackupID());
response->set_logid(this->currentLog->getLogID());
// we want to read the chunks from the blob through the get client until
// we get an empty chunk - a sign of "end of chunks"
std::string dataChunk;
if (this->internalBuffer.size() < this->chunkLimit && !this->endOfQueue) {
rust::Vec<unsigned char> responseVec =
get_client_blocking_read_cxx(this->currentLogHolder->c_str());
dataChunk = (responseVec.empty())
? ""
: std::string(reinterpret_cast<char *>(responseVec.data()));
dataChunk.resize(responseVec.size());
}
this->endOfQueue = this->endOfQueue || (dataChunk.size() == 0);
dataChunk = this->prepareDataChunkWithPadding(dataChunk, extraBytesNeeded);
// if we get an empty chunk, we reset the currentLog so we can read the
// next one from the logs collection.
// If there's data inside, we write it to the client and proceed.
if (dataChunk.empty()) {
this->nextLog();
} else {
response->set_logchunk(dataChunk);
}
return nullptr;
}
throw std::runtime_error("unhandled state");
}
void PullBackupReactor::nextLog() {
++this->currentLogIndex;
this->previousLogID = this->currentLog->getLogID();
this->currentLog = nullptr;
this->endOfQueue = false;
if (this->currentLogHolder != nullptr) {
get_client_terminate_cxx(this->currentLogHolder->c_str());
this->currentLogHolder = nullptr;
}
}
std::string PullBackupReactor::prepareDataChunkWithPadding(
const std::string &dataChunk,
size_t padding) {
if (dataChunk.size() > this->chunkLimit) {
throw std::runtime_error(std::string(
"received data chunk bigger than the chunk limit: " +
std::to_string(dataChunk.size()) + "/" +
std::to_string(this->chunkLimit)));
}
std::string chunk = std::move(this->internalBuffer) + dataChunk;
const size_t realSize = chunk.size() + padding;
if (realSize <= this->chunkLimit) {
return chunk;
}
const size_t bytesToStash = realSize - this->chunkLimit;
this->internalBuffer = std::string(chunk.end() - bytesToStash, chunk.end());
chunk.resize(chunk.size() - bytesToStash);
if (chunk.size() > this->chunkLimit) {
throw std::runtime_error("new data chunk incorrectly calculated");
}
return chunk;
}
void PullBackupReactor::terminateCallback() {
const std::lock_guard<std::mutex> lock(this->reactorStateMutex);
get_client_terminate_cxx(this->backupItem->getCompactionHolder().c_str());
if (!this->getStatusHolder()->getStatus().ok()) {
throw std::runtime_error(
this->getStatusHolder()->getStatus().error_message());
}
}
} // namespace reactor
} // namespace network
} // namespace comm