-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathgg-execute.cc
380 lines (317 loc) · 11.1 KB
/
gg-execute.cc
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
/* -*-mode:c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
#include <iostream>
#include <string>
#include <memory>
#include <sys/fcntl.h>
#include <getopt.h>
#include <vector>
#include <unordered_set>
#include "execution/response.hh"
#include "net/requests.hh"
#include "storage/backend.hh"
#include "thunk/ggutils.hh"
#include "thunk/factory.hh"
#include "thunk/thunk_reader.hh"
#include "thunk/thunk_writer.hh"
#include "thunk/thunk.hh"
#include "util/child_process.hh"
#include "util/digest.hh"
#include "util/exception.hh"
#include "util/path.hh"
#include "util/temp_dir.hh"
#include "util/temp_file.hh"
#include "util/timelog.hh"
#include "util/util.hh"
using namespace std;
using namespace gg;
using namespace gg::thunk;
using ReductionResult = gg::cache::ReductionResult;
const bool sandboxed = ( getenv( "GG_SANDBOXED" ) != NULL );
const string temp_dir_template = "/tmp/thunk-execute";
const string temp_file_template = "/tmp/thunk-file";
vector<string> execute_thunk( const Thunk & original_thunk )
{
Thunk thunk = original_thunk;
// if ( not thunk.can_be_executed() ) {
// /* Let's see if we can redudce this thunk to an order one thunk by updating
// the infiles */
// for ( const Thunk::DataItem & dep_item : thunk.thunks() ) {
// /* let's check if we have a reduction of this infile */
// vector<ThunkOutput> new_outputs;
//
// auto result = gg::cache::check( dep_item.first );
//
// if ( not result.initialized() or
// gg::hash::type( result->hash ) == gg::ObjectType::Thunk ) {
// throw runtime_error( "thunk is not executable and cannot be "
// "reduced to an executable thunk" );
// }
//
// thunk.update_data( dep_item.first, result->hash );
// }
//
// thunk.set_hash( ThunkWriter::write( thunk ) );
//
// cerr << "thunk:" << original_thunk.hash() << " reduced to "
// << "thunk:" << thunk.hash() << "." << endl;
// }
/* when executing the thunk, we create a temp directory, and execute the thunk
in that directory. then we take the outfile, compute the hash, and move it
to the .gg directory. */
// PREPARING THE ENV
TempDirectory exec_dir { temp_dir_template };
roost::path exec_dir_path { exec_dir.name() };
roost::path outfile_path { "output" };
// EXECUTING THE THUNK
if ( not sandboxed ) {
ChildProcess process {
thunk.hash(),
[thunk, &exec_dir_path]() {
roost::create_directories( exec_dir_path );
CheckSystemCall( "chdir", chdir( exec_dir_path.string().c_str() ) );
return thunk.execute();
}
};
while ( not process.terminated() ) {
process.wait();
}
if ( process.exit_status() ) {
try {
process.throw_exception();
}
catch ( const exception & ex ) {
throw_with_nested( ExecutionError {} );
}
}
}
else {
auto allowed_files = thunk.get_allowed_files();
SandboxedProcess process {
"execute(" + thunk.hash().substr( 0, 5 ) + ")",
allowed_files,
[thunk]() {
return thunk.execute();
},
[&exec_dir_path] () {
roost::create_directories( exec_dir_path );
CheckSystemCall( "chdir", chdir( exec_dir_path.string().c_str() ) );
}
};
try {
process.execute();
}
catch( const exception & ex ) {
throw_with_nested( ExecutionError {} );
}
}
vector<string> output_hashes;
// GRABBING THE OUTPUTS & CREATING CACHE ENTRIES
for ( const string & output : thunk.outputs() ) {
roost::path outfile { exec_dir_path / output };
if ( not roost::exists( outfile ) ) {
throw ExecutionError {};
}
/* let's check if the output is a thunk or not */
string outfile_hash = gg::hash::file( outfile );
roost::path outfile_gg = gg::paths::blob( outfile_hash );
if ( not roost::exists( outfile_gg ) ) {
roost::move_file( outfile, outfile_gg );
} else {
roost::remove( outfile );
}
output_hashes.emplace_back( move( outfile_hash ) );
}
for ( size_t i = thunk.outputs().size() - 1; i < thunk.outputs().size(); i-- ) {
const string & output = thunk.outputs().at( i );
const string & outfile_hash = output_hashes.at( i );
gg::cache::insert( original_thunk.output_hash( output ), outfile_hash );
if ( original_thunk.hash() != thunk.hash() ) {
gg::cache::insert( thunk.output_hash( output ), outfile_hash );
}
if ( i == 0 ) {
gg::cache::insert( original_thunk.hash(), outfile_hash );
if ( original_thunk.hash() != thunk.hash() ) {
gg::cache::insert( thunk.hash(), outfile_hash );
}
}
}
return output_hashes;
}
void do_cleanup( const Thunk & thunk )
{
unordered_set<string> infile_hashes;
infile_hashes.emplace( thunk.hash() );
for ( const Thunk::DataItem & item : thunk.values() ) {
infile_hashes.emplace( item.first );
}
for ( const Thunk::DataItem & item : thunk.executables() ) {
infile_hashes.emplace( item.first );
}
for ( const string & blob : roost::list_directory( gg::paths::blobs() ) ) {
const roost::path path = gg::paths::blob( blob );
if ( ( not roost::is_directory( path ) ) and infile_hashes.count( blob ) == 0 ) {
roost::remove( path );
}
}
}
void fetch_dependencies( unique_ptr<StorageBackend> & storage_backend,
const Thunk & thunk )
{
try {
vector<storage::GetRequest> download_items;
bool executables = false;
auto check_dep =
[&download_items, &executables]( const Thunk::DataItem & item ) -> void
{
const auto target_path = gg::paths::blob( item.first );
if ( not roost::exists( target_path )
or roost::file_size( target_path ) != gg::hash::size( item.first ) ) {
if ( executables ) {
download_items.push_back( { item.first, target_path, 0544 } );
}
else {
download_items.push_back( { item.first, target_path, 0444 } );
}
}
};
for_each( thunk.values().cbegin(), thunk.values().cend(),
check_dep );
executables = true;
for_each( thunk.executables().cbegin(), thunk.executables().cend(),
check_dep );
if ( download_items.size() > 0 ) {
storage_backend->get( download_items );
}
}
catch ( const exception & ex ) {
throw_with_nested( FetchDependenciesError {} );
}
}
void upload_output( unique_ptr<StorageBackend> & storage_backend,
const vector<string> & output_hashes )
{
try {
vector<storage::PutRequest> requests;
for ( const string & output_hash : output_hashes ) {
requests.push_back( { gg::paths::blob( output_hash ), output_hash,
gg::hash::to_hex( output_hash ) } );
}
storage_backend->put( requests );
}
catch ( const exception & ex ) {
throw_with_nested( UploadOutputError {} );
}
}
void usage( const char * argv0 )
{
cerr << "Usage: " << argv0 << " [options] THUNK-HASH..." << endl
<< endl
<< "Options: " << endl
<< " -g, --get-dependencies Fetch the missing dependencies from the remote storage" << endl
<< " -p, --put-output Upload the output to the remote storage" << endl
<< " -C, --cleanup Remove unnecessary blobs in .gg dir" << endl
<< " -T, --timelog Produce timing log for this execution" << endl
<< endl;
}
int main( int argc, char * argv[] )
{
try {
if ( argc <= 0 ) {
abort();
}
if ( argc < 2 ) {
usage( argv[ 0 ] );
return to_underlying( JobStatus::OperationalFailure );
}
bool get_dependencies = false;
bool put_output = false;
bool cleanup = false;
Optional<TimeLog> timelog;
unique_ptr<StorageBackend> storage_backend;
const option command_line_options[] = {
{ "get-dependencies", no_argument, nullptr, 'g' },
{ "put-output", no_argument, nullptr, 'p' },
{ "cleanup", no_argument, nullptr, 'C' },
{ "timelog", no_argument, nullptr, 'T' },
{ nullptr, 0, nullptr, 0 },
};
while ( true ) {
const int opt = getopt_long( argc, argv, "gpCT", command_line_options, nullptr );
if ( opt == -1 ) {
break;
}
switch ( opt ) {
case 'g': get_dependencies = true; break;
case 'p': put_output = true; break;
case 'C': cleanup = true; break;
case 'T': timelog.reset(); break;
default:
throw runtime_error( "invalid option: " + string { argv[ optind - 1 ] } );
}
}
vector<string> thunk_hashes;
for ( int i = optind; i < argc; i++ ) {
thunk_hashes.push_back( argv[ i ] );
}
if ( thunk_hashes.size() == 0 ) {
usage( argv[ 0 ] );
return to_underlying( JobStatus::OperationalFailure );
}
gg::models::init();
for ( const string & thunk_hash : thunk_hashes ) {
/* take out an advisory lock on the thunk, in case
other gg-execute processes are running at the same time */
const string thunk_path = gg::paths::blob( thunk_hash ).string();
FileDescriptor raw_thunk { CheckSystemCall( "open( " + thunk_path + " )",
open( thunk_path.c_str(), O_RDONLY ) ) };
raw_thunk.block_for_exclusive_lock();
Thunk thunk = ThunkReader::read( thunk_path );
if ( timelog.initialized() ) { timelog->add_point( "read_thunk" ); }
if ( get_dependencies or put_output ) {
storage_backend = StorageBackend::create_backend( gg::remote::storage_backend_uri() );
}
if ( cleanup ) {
do_cleanup( thunk );
}
if ( timelog.initialized() ) { timelog->add_point( "do_cleanup" ); }
if ( get_dependencies ) {
fetch_dependencies( storage_backend, thunk );
}
if ( timelog.initialized() ) { timelog->add_point( "get_dependencies" ); }
vector<string> output_hashes = execute_thunk( thunk );
if ( timelog.initialized() ) { timelog->add_point( "execute" ); }
if ( put_output ) {
upload_output( storage_backend, output_hashes );
}
if ( timelog.initialized() ) { timelog->add_point( "upload_output" ); }
if ( timelog.initialized() and storage_backend != nullptr ) {
TempFile tmplog { "/tmp/timelog" };
tmplog.fd().write( timelog->str(), true );
tmplog.fd().close();
vector<storage::PutRequest> requests;
requests.emplace_back( tmplog.name(), "timelog/" + thunk_hash );
storage_backend->put( requests );
}
else if ( timelog.initialized() ) {
cout << timelog->str() << endl;
}
}
return to_underlying( JobStatus::Success );
}
catch ( const FetchDependenciesError & e ) {
print_nested_exception( e );
return to_underlying( JobStatus::FetchDependenciesFailure );
}
catch ( const ExecutionError & e ) {
print_nested_exception( e );
return to_underlying( JobStatus::ExecutionFailure );
}
catch ( const UploadOutputError & e ) {
print_nested_exception( e );
return to_underlying( JobStatus::UploadOutputFailure );
}
catch ( const exception & e ) {
print_exception( argv[ 0 ], e );
return to_underlying( JobStatus::OperationalFailure );
}
}