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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
#include "genericresource.h"
#include "entitybuffer.h"
#include "pipeline.h"
#include "queuedcommand_generated.h"
#include "createentity_generated.h"
#include "domainadaptor.h"
#include "commands.h"
#include "index.h"
#include "log.h"
#include "definitions.h"
using namespace Akonadi2;
/**
* Replays changes from the storage one by one.
*
* Uses a local database to:
* * Remember what changes have been replayed already.
* * store a mapping of remote to local buffers
*/
class ChangeReplay : public QObject
{
Q_OBJECT
public:
typedef std::function<KAsync::Job<void>(const QByteArray &type, const QByteArray &key, const QByteArray &value)> ReplayFunction;
ChangeReplay(const QString &resourceName, const ReplayFunction &replayFunction)
: mStorage(storageLocation(), resourceName, Storage::ReadOnly),
mChangeReplayStore(storageLocation(), resourceName + ".changereplay", Storage::ReadWrite),
mReplayFunction(replayFunction)
{
}
qint64 getLastReplayedRevision()
{
qint64 lastReplayedRevision = 0;
auto replayStoreTransaction = mChangeReplayStore.createTransaction(Storage::ReadOnly);
replayStoreTransaction.openDatabase().scan("lastReplayedRevision", [&lastReplayedRevision](const QByteArray &key, const QByteArray &value) -> bool {
lastReplayedRevision = value.toLongLong();
return false;
}, [](const Storage::Error &) {
});
return lastReplayedRevision;
}
Q_SIGNALS:
void changesReplayed();
public Q_SLOTS:
void revisionChanged()
{
auto mainStoreTransaction = mStorage.createTransaction(Storage::ReadOnly);
auto replayStoreTransaction = mChangeReplayStore.createTransaction(Storage::ReadWrite);
qint64 lastReplayedRevision = 0;
replayStoreTransaction.openDatabase().scan("lastReplayedRevision", [&lastReplayedRevision](const QByteArray &key, const QByteArray &value) -> bool {
lastReplayedRevision = value.toLongLong();
return false;
}, [](const Storage::Error &) {
});
const qint64 topRevision = Storage::maxRevision(mainStoreTransaction);
if (lastReplayedRevision < topRevision) {
qint64 revision = lastReplayedRevision;
for (;revision <= topRevision; revision++) {
const auto uid = Storage::getUidFromRevision(mainStoreTransaction, revision);
const auto type = Storage::getTypeFromRevision(mainStoreTransaction, revision);
const auto key = Storage::assembleKey(uid, revision);
mainStoreTransaction.openDatabase(type + ".main").scan(key, [&lastReplayedRevision, type, this](const QByteArray &key, const QByteArray &value) -> bool {
mReplayFunction(type, key, value).exec();
//TODO make for loop async, and pass to async replay function together with type
Trace() << "Replaying " << key;
return false;
}, [key](const Storage::Error &) {
ErrorMsg() << "Failed to replay change " << key;
});
}
revision--;
replayStoreTransaction.openDatabase().write("lastReplayedRevision", QByteArray::number(revision));
replayStoreTransaction.commit();
Trace() << "Replayed until " << revision;
}
}
private:
Akonadi2::Storage mChangeReplayStore;
Akonadi2::Storage mStorage;
ReplayFunction mReplayFunction;
};
/**
* Drives the pipeline using the output from all command queues
*/
class Processor : public QObject
{
Q_OBJECT
public:
Processor(Akonadi2::Pipeline *pipeline, QList<MessageQueue*> commandQueues)
: QObject(),
mPipeline(pipeline),
mCommandQueues(commandQueues),
mProcessingLock(false)
{
mPipeline->startTransaction();
//FIXME Should be initialized to the current value of the change replay queue
mLowerBoundRevision = Storage::maxRevision(mPipeline->transaction());
mPipeline->commit();
for (auto queue : mCommandQueues) {
const bool ret = connect(queue, &MessageQueue::messageReady, this, &Processor::process);
Q_UNUSED(ret);
}
}
void setOldestUsedRevision(qint64 revision)
{
mLowerBoundRevision = revision;
}
signals:
void error(int errorCode, const QString &errorMessage);
private:
bool messagesToProcessAvailable()
{
for (auto queue : mCommandQueues) {
if (!queue->isEmpty()) {
return true;
}
}
return false;
}
private slots:
void process()
{
if (mProcessingLock) {
return;
}
mProcessingLock = true;
auto job = processPipeline().then<void>([this]() {
mProcessingLock = false;
if (messagesToProcessAvailable()) {
process();
}
}).exec();
}
KAsync::Job<qint64> processQueuedCommand(const Akonadi2::QueuedCommand *queuedCommand)
{
Log() << "Processing command: " << Akonadi2::Commands::name(queuedCommand->commandId());
//Throw command into appropriate pipeline
switch (queuedCommand->commandId()) {
case Akonadi2::Commands::DeleteEntityCommand:
return mPipeline->deletedEntity(queuedCommand->command()->Data(), queuedCommand->command()->size());
case Akonadi2::Commands::ModifyEntityCommand:
return mPipeline->modifiedEntity(queuedCommand->command()->Data(), queuedCommand->command()->size());
case Akonadi2::Commands::CreateEntityCommand:
return mPipeline->newEntity(queuedCommand->command()->Data(), queuedCommand->command()->size());
default:
return KAsync::error<qint64>(-1, "Unhandled command");
}
return KAsync::null<qint64>();
}
KAsync::Job<qint64, qint64> processQueuedCommand(const QByteArray &data)
{
flatbuffers::Verifier verifyer(reinterpret_cast<const uint8_t *>(data.constData()), data.size());
if (!Akonadi2::VerifyQueuedCommandBuffer(verifyer)) {
Warning() << "invalid buffer";
// return KAsync::error<void, qint64>(1, "Invalid Buffer");
}
auto queuedCommand = Akonadi2::GetQueuedCommand(data.constData());
const auto commandId = queuedCommand->commandId();
Trace() << "Dequeued Command: " << Akonadi2::Commands::name(commandId);
return processQueuedCommand(queuedCommand).then<qint64, qint64>(
[commandId](qint64 createdRevision) -> qint64 {
Trace() << "Command pipeline processed: " << Akonadi2::Commands::name(commandId);
return createdRevision;
}
,
[](int errorCode, QString errorMessage) {
//FIXME propagate error, we didn't handle it
Warning() << "Error while processing queue command: " << errorMessage;
}
);
}
//Process all messages of this queue
KAsync::Job<void> processQueue(MessageQueue *queue)
{
return KAsync::start<void>([this](){
mPipeline->startTransaction();
}).then(KAsync::dowhile(
[queue]() { return !queue->isEmpty(); },
[this, queue](KAsync::Future<void> &future) {
const int batchSize = 100;
queue->dequeueBatch(batchSize, [this](const QByteArray &data) {
return KAsync::start<void>([this, data](KAsync::Future<void> &future) {
processQueuedCommand(data).then<void, qint64>([&future, this](qint64 createdRevision) {
Trace() << "Created revision " << createdRevision;
future.setFinished();
}).exec();
});
}
).then<void>([&future, queue](){
future.setFinished();
},
[&future](int i, QString error) {
if (i != MessageQueue::ErrorCodes::NoMessageFound) {
Warning() << "Error while getting message from messagequeue: " << error;
}
future.setFinished();
}).exec();
}
)).then<void>([this]() {
mPipeline->commit();
});
}
KAsync::Job<void> processPipeline()
{
mPipeline->startTransaction();
Trace() << "Cleaning up from " << mPipeline->cleanedUpRevision() + 1 << " to " << mLowerBoundRevision;
for (qint64 revision = mPipeline->cleanedUpRevision() + 1; revision <= mLowerBoundRevision; revision++) {
mPipeline->cleanupRevision(revision);
}
mPipeline->commit();
//Go through all message queues
auto it = QSharedPointer<QListIterator<MessageQueue*> >::create(mCommandQueues);
return KAsync::dowhile(
[it]() { return it->hasNext(); },
[it, this](KAsync::Future<void> &future) {
auto queue = it->next();
processQueue(queue).then<void>([&future]() {
Trace() << "Queue processed";
future.setFinished();
}).exec();
}
);
}
private:
Akonadi2::Pipeline *mPipeline;
//Ordered by priority
QList<MessageQueue*> mCommandQueues;
bool mProcessingLock;
//The lowest revision we no longer need
qint64 mLowerBoundRevision;
};
GenericResource::GenericResource(const QByteArray &resourceInstanceIdentifier, const QSharedPointer<Pipeline> &pipeline)
: Akonadi2::Resource(),
mUserQueue(Akonadi2::storageLocation(), resourceInstanceIdentifier + ".userqueue"),
mSynchronizerQueue(Akonadi2::storageLocation(), resourceInstanceIdentifier + ".synchronizerqueue"),
mResourceInstanceIdentifier(resourceInstanceIdentifier),
mPipeline(pipeline ? pipeline : QSharedPointer<Akonadi2::Pipeline>::create(resourceInstanceIdentifier)),
mError(0),
mClientLowerBoundRevision(std::numeric_limits<qint64>::max())
{
mProcessor = new Processor(mPipeline.data(), QList<MessageQueue*>() << &mUserQueue << &mSynchronizerQueue);
QObject::connect(mProcessor, &Processor::error, [this](int errorCode, const QString &msg) { onProcessorError(errorCode, msg); });
QObject::connect(mPipeline.data(), &Pipeline::revisionUpdated, this, &Resource::revisionUpdated);
mSourceChangeReplay = new ChangeReplay(resourceInstanceIdentifier, [](const QByteArray &type, const QByteArray &key, const QByteArray &value) {
return KAsync::null<void>();
});
QObject::connect(mPipeline.data(), &Pipeline::revisionUpdated, mSourceChangeReplay, &ChangeReplay::revisionChanged);
QObject::connect(mSourceChangeReplay, &ChangeReplay::changesReplayed, this, &GenericResource::updateLowerBoundRevision);
mClientLowerBoundRevision = mPipeline->cleanedUpRevision();
mProcessor->setOldestUsedRevision(mSourceChangeReplay->getLastReplayedRevision());
mCommitQueueTimer.setInterval(100);
mCommitQueueTimer.setSingleShot(true);
QObject::connect(&mCommitQueueTimer, &QTimer::timeout, &mUserQueue, &MessageQueue::commit);
}
GenericResource::~GenericResource()
{
delete mProcessor;
delete mSourceChangeReplay;
}
void GenericResource::removeFromDisk(const QByteArray &instanceIdentifier)
{
Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier, Akonadi2::Storage::ReadWrite).removeFromDisk();
Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier + ".userqueue", Akonadi2::Storage::ReadWrite).removeFromDisk();
Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier + ".synchronizerqueue", Akonadi2::Storage::ReadWrite).removeFromDisk();
}
void GenericResource::onProcessorError(int errorCode, const QString &errorMessage)
{
Warning() << "Received error from Processor: " << errorCode << errorMessage;
mError = errorCode;
}
int GenericResource::error() const
{
return mError;
}
void GenericResource::enqueueCommand(MessageQueue &mq, int commandId, const QByteArray &data)
{
//TODO get rid of m_fbb member variable
m_fbb.Clear();
auto commandData = Akonadi2::EntityBuffer::appendAsVector(m_fbb, data.constData(), data.size());
auto buffer = Akonadi2::CreateQueuedCommand(m_fbb, commandId, commandData);
Akonadi2::FinishQueuedCommandBuffer(m_fbb, buffer);
mq.enqueue(m_fbb.GetBufferPointer(), m_fbb.GetSize());
}
void GenericResource::processCommand(int commandId, const QByteArray &data)
{
static int modifications = 0;
const int batchSize = 100;
mUserQueue.startTransaction();
enqueueCommand(mUserQueue, commandId, data);
modifications++;
if (modifications >= batchSize) {
mUserQueue.commit();
modifications = 0;
mCommitQueueTimer.stop();
} else {
mCommitQueueTimer.start();
}
}
static void waitForDrained(KAsync::Future<void> &f, MessageQueue &queue)
{
if (queue.isEmpty()) {
f.setFinished();
} else {
QObject::connect(&queue, &MessageQueue::drained, [&f]() {
f.setFinished();
});
}
};
KAsync::Job<void> GenericResource::processAllMessages()
{
//We have to wait for all items to be processed to ensure the synced items are available when a query gets executed.
//TODO: report errors while processing sync?
//TODO JOBAPI: A helper that waits for n events and then continues?
return KAsync::start<void>([this](KAsync::Future<void> &f) {
if (mCommitQueueTimer.isActive()) {
auto context = new QObject;
QObject::connect(&mCommitQueueTimer, &QTimer::timeout, context, [&f, context]() {
delete context;
f.setFinished();
});
} else {
f.setFinished();
}
}).then<void>([this](KAsync::Future<void> &f) {
waitForDrained(f, mSynchronizerQueue);
}).then<void>([this](KAsync::Future<void> &f) {
waitForDrained(f, mUserQueue);
});
}
void GenericResource::updateLowerBoundRevision()
{
mProcessor->setOldestUsedRevision(qMin(mClientLowerBoundRevision, mSourceChangeReplay->getLastReplayedRevision()));
}
void GenericResource::setLowerBoundRevision(qint64 revision)
{
mClientLowerBoundRevision = revision;
updateLowerBoundRevision();
}
#include "genericresource.moc"
|