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
|
#include "genericresource.h"
#include "entitybuffer.h"
#include "pipeline.h"
#include "queuedcommand_generated.h"
#include "createentity_generated.h"
#include "domainadaptor.h"
#include "commands.h"
#include "clientapi.h"
#include "index.h"
#include "log.h"
#include <assert.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<void> 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:
//mPipeline->removedEntity
return KAsync::null<void>();
case Akonadi2::Commands::ModifyEntityCommand:
//mPipeline->modifiedEntity
return KAsync::null<void>();
case Akonadi2::Commands::CreateEntityCommand:
return mPipeline->newEntity(queuedCommand->command()->Data(), queuedCommand->command()->size());
default:
return KAsync::error<void>(-1, "Unhandled command");
}
return KAsync::null<void>();
}
//Process all messages of this queue
KAsync::Job<void> processQueue(MessageQueue *queue)
{
//TODO use something like:
//KAsync::foreach("pass iterator here").each("process value here").join();
//KAsync::foreach("pass iterator here").parallel("process value here").join();
return KAsync::dowhile(
[this, queue](KAsync::Future<bool> &future) {
if (queue->isEmpty()) {
future.setValue(false);
future.setFinished();
return;
}
queue->dequeue(
[this, &future](void *ptr, int size, std::function<void(bool success)> messageQueueCallback) {
auto callback = [messageQueueCallback, &future](bool success) {
messageQueueCallback(success);
future.setValue(!success);
future.setFinished();
};
flatbuffers::Verifier verifyer(reinterpret_cast<const uint8_t *>(ptr), size);
if (!Akonadi2::VerifyQueuedCommandBuffer(verifyer)) {
Warning() << "invalid buffer";
callback(false);
return;
}
auto queuedCommand = Akonadi2::GetQueuedCommand(ptr);
Trace() << "Dequeued Command: " << Akonadi2::Commands::name(queuedCommand->commandId());
//TODO JOBAPI: job lifetime management
//Right now we're just leaking jobs. In this case we'd like jobs that are heap allocated and delete
//themselves once done. In other cases we'd like jobs that only live as long as their handle though.
//FIXME this job is stack allocated and thus simply dies....
processQueuedCommand(queuedCommand).then<void>(
[callback]() {
callback(true);
},
[callback](int errorCode, QString errorMessage) {
Warning() << "Error while processing queue command: " << errorMessage;
callback(false);
}
).exec();
},
[&future](const MessageQueue::Error &error) {
Warning() << "Error while getting message from messagequeue: " << error.message;
future.setValue(false);
future.setFinished();
}
);
}
);
}
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 &resourceIdentifier)
: Akonadi2::Resource(),
mUserQueue(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde." + resourceIdentifier + ".userqueue"),
mSynchronizerQueue(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde." + resourceIdentifier + ".synchronizerqueue"),
mError(0)
{
}
GenericResource::~GenericResource()
{
}
void GenericResource::configurePipeline(Akonadi2::Pipeline *pipeline)
{
//TODO figure out lifetime of the processor
mProcessor = new Processor(pipeline, QList<MessageQueue*>() << &mUserQueue << &mSynchronizerQueue);
QObject::connect(mProcessor, &Processor::error, [this](int errorCode, const QString &msg) { onProcessorError(errorCode, msg); });
}
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, uint size, Akonadi2::Pipeline *pipeline)
{
//TODO instead of copying the command including the full entity first into the command queue, we could directly
//create a new revision, only pushing a handle into the commandqueue with the relevant changeset (for changereplay).
//The problem is that we then require write access from multiple threads (or even processes to avoid sending the full entity over the wire).
enqueueCommand(mUserQueue, commandId, data);
}
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 (mSynchronizerQueue.isEmpty()) {
f.setFinished();
} else {
QObject::connect(&mSynchronizerQueue, &MessageQueue::drained, [&f]() {
f.setFinished();
});
}
}).then<void>([this](KAsync::Future<void> &f) {
if (mUserQueue.isEmpty()) {
f.setFinished();
} else {
QObject::connect(&mUserQueue, &MessageQueue::drained, [&f]() {
f.setFinished();
});
}
});
}
#include "genericresource.moc"
|