-
Notifications
You must be signed in to change notification settings - Fork 4
/
job.h
447 lines (386 loc) · 13.7 KB
/
job.h
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* Job.h
*
* The job class.
*
* @author Toon Schoenmakers <[email protected]>
* @copyright 2015 - 2016 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include <phpcpp.h>
#include "connection.h"
#include "reducer.h"
#include "values.h"
#include "writer.h"
#include "directory.h"
#include "jobimpl.h"
#include "serialized.h"
#include "raceresult.h"
#include "mapreduceresult.h"
#include "error.h"
#include "taskresult.h"
#include <iostream>
/**
* Different error types
*/
using MapReduceError = Error<MapReduceResult>;
using RaceError = Error<RaceResult>;
using TaskError = Error<TaskResult>;
/**
* Class definition
*/
class Job : public Php::Base, public Php::Serializable
{
private:
/**
* The actual implementation of the job
*
* This member is either constructed by the __construct() or by the
* unserialize() method
*
* @var JobImpl
*/
std::unique_ptr<JobImpl> _impl;
public:
/**
* Constructor that creates an initial empty object
* The PHP __construct() method will be called right after this constructor)
*/
Job() {}
/**
* Destructor
*/
virtual ~Job() {}
/**
* Job PHP constructor
* Requires two PHP parameters: a Yothalot\\Connection object, and a
* Yothalot\\MapReduce object (or one of the other algorithms)
* @param params
*/
void __construct(Php::Parameters ¶ms)
{
// check number of parameters
if (params.size() < 2) throw Php::Exception("Yothalot\\Job constructor requires two parameters");
// extract the connection and map reduce params
Php::Value connection = params[0];
Php::Value algo = params[1];
// check type of parameters
if (!connection.instanceOf("Yothalot\\Connection")) throw Php::Exception("Connection is not an instance of Yothalot\\Connection");
if (!algo.instanceOf("Yothalot\\MapReduce") && !algo.instanceOf("Yothalot\\RecordReduce") && !algo.instanceOf("Yothalot\\Race") && !algo.instanceOf("Yothalot\\Task")) throw Php::Exception("Connection is not an instance of Yothalot\\MapReduce, Yothalot\\RecordReduce, Yothalot\\Race or Yothalot\\Task.");
// retrieve the underlying C++ Connection object
auto *con = (Connection*) connection.implementation();
// construct the implementation
_impl.reset(new JobImpl(con->rabbit(), con->cache(), algo));
}
/**
* Set the maximum amount of concurrent running processes
* @param params PHP input parameters
* @return Same object for chaining, or null on error
*/
Php::Value maxprocesses(Php::Parameters ¶ms)
{
// pass this to the implementation
if (!_impl->maxprocesses(std::max(1L, params[0].numericValue()))) return nullptr;
// allow chaining
return this;
}
/**
* Set the maximum amount of concurrent running mappers
* @param params PHP input parameters
* @return Same object for chaining or nullptr on error
*/
Php::Value maxmappers(Php::Parameters ¶ms)
{
// pass this to the implementations
if (!_impl->maxmappers(std::max(1L, params[0].numericValue()))) return nullptr;
// allow chaining
return this;
}
/**
* Set the maximum amount of concurrent running reducer
* @param params PHP input parameters
* @return Same object for chaining or nullptr on error
*/
Php::Value maxreducers(Php::Parameters ¶ms)
{
// pass this to the implementations
if (!_impl->maxreducers(std::max(1L, params[0].numericValue()))) return nullptr;
// allow chaining
return this;
}
/**
* Set the maximum amount of concurrent running finalizers
* @param params PHP input parameters
* @return Same object for chaining or nullptr on error
*/
Php::Value maxfinalizers(Php::Parameters ¶ms)
{
// pass this to the implementation
if (!_impl->maxfinalizers(std::max(0L, params[0].numericValue()))) return nullptr;
// allow chaining
return this;
}
/**
* Set the modulo
* @param params PHP input parameters
* @return Same object for chaining or nullptr on error
*/
Php::Value modulo(Php::Parameters ¶ms)
{
// pass this to the implementation
if (!_impl->modulo(std::max(1L, params[0].numericValue()))) return nullptr;
// allow chaining
return this;
}
/**
* Set the maximum amount of files
* @param params PHP input parameters
* @return NULL on failure, or the same object on success for chaining
*/
Php::Value maxfiles(Php::Parameters ¶ms)
{
// extract the number, old behavior is all the same value
int64_t mapperfiles = params[0].numericValue();
int64_t reducerfiles = params.size() >= 2 ? params[1].numericValue() : mapperfiles;
int64_t finalizerfiles = params.size() >= 3 ? params[2].numericValue() : mapperfiles;
// pass on to the implementation object
if (!_impl->maxfiles(mapperfiles, reducerfiles, finalizerfiles)) return nullptr;
// allow chaining
return this;
}
/**
* Limit the amount of bytes
* @param params PHP input parameters
* @return the same object for chaining or nullptr on failure
*/
Php::Value maxbytes(Php::Parameters ¶ms)
{
// extract the number, old behavior is all the same value
int64_t mapperbytes = params[0].numericValue();
int64_t reducerbytes = params.size() >= 2 ? params[1].numericValue() : mapperbytes;
int64_t finalizerbytes = params.size() >= 3 ? params[2].numericValue() : mapperbytes;
// pass on to the implementation object
if (!_impl->maxbytes(mapperbytes, reducerbytes, finalizerbytes)) return nullptr;
// allow chaining
return this;
}
/**
* Limit the amount of records
*
* @param params PHP input parameters
* @return Same object for chaining or nullptr on failure
*/
Php::Value maxrecords(Php::Parameters ¶ms)
{
// pass on to implementation object
if (!_impl->maxrecords(params[0])) return nullptr;
// success, allow chaining
return this;
}
/**
* Add data to this job
* @param params PHP input parameters
* @return Result is the object for chaining or nullptr on failure
*/
Php::Value add(Php::Parameters ¶ms)
{
// for mapreduce jobs we expect key/value pairs, for other jobs plain data
if(_impl->isMapReduce())
{
// we need two parameters
if (params.size() < 2) return nullptr;
// create the key and the value from the parameters
Tuple::Yothalot key(params[0]);
Tuple::Yothalot value(params[1]);
// get the possible server
const char* server = params.size() >= 3 ? params[2].rawValue() : nullptr;
// pass on to the implementation object
if (!_impl->add(key, value, server)) return nullptr;
// allow chaining
return this;
}
else
{
// it is a race and we only use the first parameter
// serialize and base64 encode the data to ensure that no null character appear in it
auto data = Php::call("base64_encode", Php::call("serialize", params[0])).stringValue();
// else it is a race job to which we can add data
if (!_impl->add(data)) return nullptr;
// allow chaining
return this;
}
}
/**
* Add data to this job
* @param params PHP input parameters
* @return Result is the object for chaining or nullptr on failure
*/
Php::Value map(Php::Parameters ¶ms)
{
// create the key and the value from the parameters
Tuple::Yothalot key(params[0]);
Tuple::Yothalot value(params[1]);
// get the possible server
const char* server = params.size() >= 3 ? params[2].rawValue() : nullptr;
// immediately redirect
if (!_impl->add(key, value, server)) return nullptr;
// allow chaining
return this;
}
/**
* Set the local property
* @param params PHP input parameters
* @return Result is the object for chaining or nullptr on failure
*/
Php::Value local(Php::Parameters ¶ms)
{
// pass on to the implementation object
if (!_impl->local(params[0].boolValue())) return nullptr;
// allow chaining
return this;
}
/**
* Flush the file, causing a new output file to be created.
* @return Result is the object for chaining or nullptr on failure
*/
Php::Value flush()
{
// pass on to the implementation object
if (!_impl->flush()) return nullptr;
// allow chaining
return this;
}
/**
* Add a file to this job
* @param params PHP input parameters
* @return the object for chaining or a nullptr on failure
*/
Php::Value file(Php::Parameters ¶ms)
{
// get the params in c++
const char *filename = params[0].rawValue();
int64_t start = params.size() >= 2 ? params[1].numericValue() : 0;
int64_t size = params.size() >= 3 ? params[2].numericValue() : 0;
bool remove = params.size() >= 4 ? params[3].boolValue() : false;
const char *server = params.size() >= 5 ? params[4].rawValue() : nullptr;
// call the file function in the implementation
if (!_impl->file(filename, (size_t)start, (size_t)size, remove, server)) return nullptr;
// allow chaining
return this;
}
/**
* Add a directory to this job
* @param params PHP input parameters
* @return the object for chaining or a nullptr on failure
*/
Php::Value directory(Php::Parameters ¶ms)
{
// if we don't have any parameters we act as a get operation
if (params.empty()) return _impl->directory();
// get the params in c++
auto dirname = params[0].rawValue();
auto remove = params.size() >= 1 ? params[1].boolValue() : false;
auto server = params.size() >= 2 ? params[2].rawValue() : "";
// call the file function in the implementation
if (!_impl->directory(dirname, remove, server)) return nullptr;
// allow chaining
return this;
}
/**
* Detach the job -- run it remotely but do not wait for the answer
* @return Result value (true or false)
*/
Php::Value detach()
{
// pass it on to the implementation object
return _impl->detach();
}
/**
* Execute this job, but do not detach from it (we can still wait for it)
* @return Result value (true or false)
*/
Php::Value start()
{
// pass it on to the implementation object
return _impl->start();
}
/**
* Wait for the job to be ready
* @return Result value (true or false)
*/
Php::Value wait()
{
// did the job execute successfully?
bool success = _impl->wait();
// what algorithm did we just wait for?
switch (_impl->algorithm())
{
case Algorithm::race:
if (success) return Php::Object{ "Yothalot\\RaceResult", new RaceResult { _impl->result() }};
else return Php::Object{ "Yothalot\\RaceError", new RaceError { _impl->result() }};
case Algorithm::mapreduce:
if (success) return Php::Object{ "Yothalot\\MapReduceResult", new MapReduceResult { _impl->result() }};
else return Php::Object{ "Yothalot\\MapReduceError", new MapReduceError { _impl->result() }};
case Algorithm::job:
if (success) return Php::Object{ "Yothalot\\TaskResult", new TaskResult { _impl->result() }};
else return Php::Object{ "Yothalot\\TaskError", new TaskError { _impl->result() }};
default: return nullptr;
}
}
/**
* Is the job ready yet?
* @return bool
*/
bool ready() const
{
// pass on to the implementation
return _impl->ready();
}
/**
* Serialize the object to a string
* @return std::string
*/
virtual std::string serialize() override
{
// tell the implementation object to serialize
Serialized result(_impl.get());
// done
return result;
}
/**
* Unserialize the object from a string
* @param buffer
* @param size
*/
virtual void unserialize(const char *buffer, size_t size) override
{
// exceptions might be thrown when unserializing, for example
// when serialized data holds no (valid) directory
try
{
// the serialized object also works the other way round
Serialized object(buffer, size);
// reset the _impl unique_ptr to a freshly created JobImpl
_impl.reset(new JobImpl(object));
}
catch (const std::runtime_error &error)
{
// turn the C++ error into a PHP error
throw Php::Exception(error.what());
}
}
/**
* The TCP handler that should be notified when the result is available
* @return TcpHandler
*/
TcpHandler *handler() const
{
return _impl->handler();
}
};