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
|
#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;
/**
* 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)
{
for (auto queue : mCommandQueues) {
const bool ret = connect(queue, &MessageQueue::messageReady, this, &Processor::process);
Q_UNUSED(ret);
}
}
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;
//We don't have a writeback yet, so we cleanup revisions immediately
//TODO: only cleanup once writeback is done
mPipeline->cleanupRevision(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()
{
//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;
};
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)
{
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);
mCommitQueueTimer.setInterval(100);
mCommitQueueTimer.setSingleShot(true);
QObject::connect(&mCommitQueueTimer, &QTimer::timeout, &mUserQueue, &MessageQueue::commit);
}
GenericResource::~GenericResource()
{
}
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);
});
}
#include "genericresource.moc"
|