Skip to content

Commit

Permalink
Added information about the winner of a race job to result
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljar committed Nov 16, 2015
1 parent 270ee26 commit 532feb2
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ LINKER = g++

COMPILER_FLAGS = -Wall -c -O2 -std=c++11 -fpic -I. -g
LINKER_FLAGS = -shared
LINKER_DEPENDENCIES = -lphpcpp -lyothalot
LINKER_DEPENDENCIES = -lphpcpp -lyothalot -lamqpcpp

#
# Command to remove files, copy files and create directories.
Expand Down
22 changes: 21 additions & 1 deletion extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ extern "C" {
Php::Class<RaceResult> raceResult ("RaceResult");
Php::Class<Stats> stats ("Stats");
Php::Class<DataStats> datastats ("DataStats");
Php::Class<Winner> winner ("Winner");

// register writer functions
writer.method("emit", &Writer::emit, {
Expand Down Expand Up @@ -156,8 +157,12 @@ extern "C" {
.method("finalizers", &MapReduceResult::finalizers);

raceResult.method("started", &RaceResult::started)
.method("finished", &RaceResult::finished)
.method("runtime", &RaceResult::runtime)
.method("result", &RaceResult::result);
.method("processes", &RaceResult::processes)
.method("result", &RaceResult::result)
.method("winner", &RaceResult::winner);


// register stats methods
stats.method("first", &Stats::first)
Expand All @@ -174,6 +179,20 @@ extern "C" {
datastats.method("files", &DataStats::files)
.method("bytes", &DataStats::bytes);

// register winner methods
// register stats methods
winner.method("input", &Winner::input)
.method("output", &Winner::output)
.method("error", &Winner::error)
.method("server", &Winner::server)
.method("pid", &Winner::pid)
.method("signal", &Winner::signal)
.method("exit", &Winner::exit)
.method("started", &Winner::started)
.method("finished", &Winner::finished)
.method("runtime", &Winner::runtime);


// create the map reduce interface
Php::Interface mapreduce("MapReduce");

Expand Down Expand Up @@ -214,6 +233,7 @@ extern "C" {
ns.add(std::move(raceResult));
ns.add(std::move(stats));
ns.add(std::move(datastats));
ns.add(std::move(winner));

// add the init method for use on the command line to our namespace, this
// will result in `php -r "YothalotInit('mapper');"`
Expand Down
28 changes: 28 additions & 0 deletions raceresult.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Dependencies
*/
#include "stats.h"
#include "winner.h"

/**
* Class definition
Expand Down Expand Up @@ -57,6 +58,15 @@ class RaceResult : public Php::Base
{
return _json.decimal("started");
}

/**
* Get the time when the job is finished
* @return Php::Value
*/
Php::Value finished() const
{
return _json.decimal("finished");
}

/**
* Get the total runtime
Expand All @@ -66,6 +76,15 @@ class RaceResult : public Php::Base
{
return _json.decimal("runtime");
}

/**
* Get the number of processes during the race
* @return Php::Value
*/
Php::Value processes() const
{
return _json.integer("processes");
}

/**
* Get the result, only used for races and regular jobs
Expand All @@ -76,5 +95,14 @@ class RaceResult : public Php::Base
// unserialize the base64 encoded object from stdout
return Php::call("unserialize", Php::call("base64_decode", _json.object("winner").c_str("stdout")));
}

/**
* Get the winner class with all winner statistics
* @return Php::Value
*/
Php::Value winner()
{
return Php::Object("Yothalot\\Winner", new Winner(_json.object("winner")));
}
};

147 changes: 147 additions & 0 deletions winner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* Winner.h
*
* Statistics of the winner of the race job
*
* @author Aljar Meesters <[email protected]>
* @copyright Copernica BV 2015
*/

/**
* include guard
*/
#pragma once

/**
* dependencies
*/
#include <phpcpp.h>
#include "json/object.h"

/**
* class definition
*/
class Winner : public Php::Base
{
private:
/**
* json that holds the information
* @var JSON::Object
*/
JSON::Object _json;

public:
/**
* Constructor
* @param json
*/
Winner(const JSON::Object &json) : _json(json) {}

/**
* input that was send to the winner
* @return Php::Value
*/
Php::Value input() const
{
// Unserialize the result
auto completeObject = std::string(_json.c_str("stdin"));

// look for the \n\n separator
auto separator = completeObject.find("\n\n");

// should exist
if (separator == std::string::npos) throw std::runtime_error("missing separator between serialized data and input data");

// we now know where the rest of the data starts
auto data = completeObject.data() + separator + 2;

// Decode the data
return Php::call("base64_decode" , Php::Value(data));

// unserialize the first part of the stdin
// return unserialized(Php::call("unserialize", Php::call("base64_decode", Php::Value(rest.data()))));
}

/**
* output that was send to stdout by winner
* @return Php::Value
*/
Php::Value output() const
{
// Unserialize the result
return Php::call("unserialize", Php::call("base64_decode", _json.c_str("stdout")));

}

/**
* error that was send to stderr by winner
* @return Php::Value
*/
Php::Value error() const
{
return Php::call("unserialize", Php::call("base64_decode", _json.c_str("stderr")));
}

/**
* name of sever on which the winning job ran
* @return Php::Value
*/
Php::Value server() const
{
return _json.c_str("server");
}

/**
* Process id of the winner
* @return Php::Value
*/
Php::Value pid() const
{
return _json.integer("pid");
}

/**
* Signal if the winner was killed
* @return Php::Value
*/
Php::Value signal() const
{
return _json.integer("signal");
}

/**
* Exit code with which the winner exited
* @return Php::Value
*/
Php::Value exit() const
{
return _json.integer("exit");
}

/**
* Starting time of the winner
* @return Php::Value
*/
Php::Value started() const
{
return _json.decimal("started");
}

/**
* Finishing time of the winner
* @return Php::Value
*/
Php::Value finished() const
{
return _json.decimal("finished");
}

/**
* Runtime of the winner
* @return Php::Value
*/
Php::Value runtime() const
{
return _json.decimal("runtime");
}
};

0 comments on commit 532feb2

Please sign in to comment.