-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* -*-mode:c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
|
||
#include "engine_vhive.hh" | ||
|
||
#include <stdexcept> | ||
|
||
#include "response.hh" | ||
#include "net/http_response.hh" | ||
#include "thunk/ggutils.hh" | ||
#include "util/optional.hh" | ||
#include "util/system_runner.hh" | ||
#include "util/units.hh" | ||
#include "util/base64.hh" | ||
|
||
using namespace std; | ||
using namespace gg; | ||
using namespace gg::thunk; | ||
|
||
VHiveExecutionEngine::VHiveExecutionEngine( const size_t max_jobs, const string & url ) | ||
: ExecutionEngine( max_jobs ), parsed_url_( url ), address_( parsed_url_.host, parsed_url_.protocol ), | ||
final_url_( parsed_url_.host ) | ||
{ | ||
// If a port is specified, we treat the url and address differently | ||
uint16_t port = parsed_url_.port.get(); | ||
|
||
if ( port ) { | ||
string port_string = to_string( port ); | ||
final_url_ = parsed_url_.host + ":" + port_string; | ||
address_ = Address( parsed_url_.host, port_string ); | ||
} | ||
} | ||
|
||
HTTPRequest VHiveExecutionEngine::generate_request( const Thunk & thunk ) | ||
{ | ||
string payload = Thunk::execution_payload( thunk ); | ||
HTTPRequest request; | ||
request.set_first_line( "POST / HTTP/1.1" ); | ||
request.add_header( HTTPHeader{ "Host", final_url_ } ); | ||
request.add_header( HTTPHeader{ "Content-Length", to_string( payload.size() ) } ); | ||
request.add_header( HTTPHeader{ "Content-Type", "application/json" } ); | ||
request.add_header( HTTPHeader{ "Accept", "*/*" } ); | ||
request.done_with_headers(); | ||
|
||
request.read_in_body( payload ); | ||
assert( request.state() == COMPLETE ); | ||
|
||
return request; | ||
} | ||
|
||
void VHiveExecutionEngine::force_thunk( const Thunk & thunk, | ||
ExecutionLoop & exec_loop ) | ||
{ | ||
HTTPRequest request = generate_request( thunk ); | ||
exec_loop.make_http_request<TCPConnection>( thunk.hash(), | ||
address_, request, | ||
[this] ( const uint64_t, const string & thunk_hash, | ||
const HTTPResponse & http_response ) -> bool | ||
{ | ||
running_jobs_--; | ||
|
||
if ( http_response.status_code() != "200" ) { | ||
failure_callback_( thunk_hash, JobStatus::InvocationFailure ); | ||
return false; | ||
} | ||
|
||
ExecutionResponse response = ExecutionResponse::parse_message( http_response.body() ); | ||
|
||
/* print the output, if there's any */ | ||
if ( response.stdout.length() ) { | ||
cerr << response.stdout << endl; | ||
} | ||
|
||
switch ( response.status ) { | ||
case JobStatus::Success: | ||
{ | ||
if ( response.thunk_hash != thunk_hash ) { | ||
cerr << http_response.str() << endl; | ||
throw runtime_error( "expected output for " + | ||
thunk_hash + ", got output for " + | ||
response.thunk_hash ); | ||
} | ||
|
||
for ( const auto & output : response.outputs ) { | ||
gg::cache::insert( gg::hash::for_output( response.thunk_hash, output.tag ), output.hash ); | ||
|
||
if ( output.data.length() ) { | ||
roost::atomic_create( base64::decode( output.data ), | ||
gg::paths::blob( output.hash ) ); | ||
} | ||
} | ||
|
||
gg::cache::insert( response.thunk_hash, response.outputs.at( 0 ).hash ); | ||
|
||
vector<ThunkOutput> thunk_outputs; | ||
for ( auto & output : response.outputs ) { | ||
thunk_outputs.emplace_back( move( output.hash ), move( output.tag ) ); | ||
} | ||
|
||
success_callback_( response.thunk_hash, move( thunk_outputs ), 0 ); | ||
|
||
break; | ||
} | ||
|
||
default: /* in case of any other failure */ | ||
failure_callback_( thunk_hash, response.status ); | ||
} | ||
|
||
return false; | ||
}, | ||
[this] ( const uint64_t, const string & thunk_hash ) | ||
{ | ||
failure_callback_( thunk_hash, JobStatus::SocketFailure ); | ||
} | ||
); | ||
|
||
running_jobs_++; | ||
} | ||
|
||
size_t VHiveExecutionEngine::job_count() const | ||
{ | ||
return running_jobs_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* -*-mode:c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
|
||
#ifndef ENGINE_VHIVE_HH | ||
#define ENGINE_VHIVE_HH | ||
|
||
#include "engine.hh" | ||
#include "net/http_request.hh" | ||
#include "thunk/thunk.hh" | ||
#include "util/uri.hh" | ||
|
||
class VHiveExecutionEngine : public ExecutionEngine | ||
{ | ||
private: | ||
ParsedURI parsed_url_; | ||
Address address_; | ||
std::string final_url_; | ||
|
||
size_t running_jobs_ { 0 }; | ||
|
||
HTTPRequest generate_request( const gg::thunk::Thunk & thunk ); | ||
|
||
public: | ||
VHiveExecutionEngine( const size_t max_jobs, const std::string & address ); | ||
|
||
void force_thunk( const gg::thunk::Thunk & thunk, | ||
ExecutionLoop & exec_loop ) override; | ||
size_t job_count() const override; | ||
|
||
bool is_remote() const { return true; } | ||
std::string label() const override { return "remote"; } | ||
bool can_execute( const gg::thunk::Thunk & ) const { return true; } | ||
}; | ||
|
||
#endif /* ENGINE_VHIVE_HH */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters