summaryrefslogtreecommitdiff
path: root/buildscripts/emr/emr.java
blob: 0f01d6d0e2d8cf92406a80ba226f4224a1966eee (plain)
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
// emr.java

import java.io.*;
import java.util.*;
import java.net.*;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.fs.*;


public class emr {

    static class MongoSuite {
        String mongo;
        String code;
        String workingDir;

        String suite;

        void copy( MongoSuite c ) {
            mongo = c.mongo;
            code = c.code;
            workingDir = c.workingDir;
            
            suite = c.suite;
            
        }

        void downloadTo( File localDir ) 
            throws IOException {
            IOUtil.download( mongo , localDir );
            IOUtil.download( code , localDir );
        }

        boolean runTest() 
            throws IOException {

            // mkdir
            File dir = new File( workingDir , suite );
            dir.mkdirs();
            
            // download
            System.out.println( "going to download" );
            downloadTo( dir );
            
            
            // explode
            System.out.println( "going to explode" );
            IOUtil.runCommand( "tar zxvf " + IOUtil.urlFileName( code ) , dir );
            String[] res = IOUtil.runCommand( "tar zxvf " + IOUtil.urlFileName( mongo ) , dir );
            for ( String x : res[0].split( "\n" ) ) {
                if ( x.indexOf( "/bin/" ) < 0 )
                    continue;
                File f = new File( dir.toString() , x );
                if ( ! f.renameTo( new File( dir , IOUtil.urlFileName( x ) ) ) )
                    throw new RuntimeException( "rename failed" );
            }
            
            List<String> cmd = new ArrayList<String>();
            cmd.add( "/usr/bin/python" );
            cmd.add( "buildscripts/smoke.py" );
            
            File log_config = new File( dir , "log_config.py" );
            System.out.println( "log_config: " + log_config.exists() );
            if ( log_config.exists() ) {

                java.util.Map<String,Object> properties = IOUtil.readPythonSettings( log_config );

                cmd.add( "--buildlogger-builder" );
                cmd.add( properties.get( "name" ).toString() );
                
                cmd.add( "--buildlogger-buildnum" );
                cmd.add( properties.get( "number" ).toString() );
                
                cmd.add( "--buildlogger-credentials" );
                cmd.add( "log_config.py" );
                
                cmd.add( "--buildlogger-phase" );
                {
                    int idx = suite.lastIndexOf( "/" );
                    if ( idx < 0 ) 
                        cmd.add( suite );
                    else
                        cmd.add( suite.substring( 0 , idx ) );
                }

            }

            cmd.add( suite );
            
            System.out.println( cmd );

            Process p = Runtime.getRuntime().exec( cmd.toArray( new String[cmd.size()] ) , new String[]{} , dir );

            List<Thread> threads = new ArrayList<Thread>();
            threads.add( new IOUtil.PipingThread( p.getInputStream() , System.out ) );
            threads.add( new IOUtil.PipingThread( p.getErrorStream() , System.out ) );

            for ( Thread t : threads ) 
                t.start();

            try {
                for ( Thread t : threads ) {
                    t.join();
                }
                int rc = p.waitFor();
                return rc == 0;
            }
            catch ( InterruptedException ie ) {
                ie.printStackTrace();
                throw new RuntimeException( "sad" , ie );
            }
            
        }

        public void readFields( DataInput in ) 
            throws IOException {
            mongo = in.readUTF();
            code = in.readUTF();
            workingDir = in.readUTF();
            
            suite = in.readUTF();
        }

        public void write( final DataOutput out ) 
            throws IOException {
            out.writeUTF( mongo );            
            out.writeUTF( code );
            out.writeUTF( workingDir );

            out.writeUTF( suite );
        }

        public String toString() {
            return "mongo: " + mongo + " code: " + code + " suite: " + suite + " workingDir: " + workingDir;
        }
    }

    public static class Map implements Mapper<Text, MongoSuite, Text, IntWritable> {

        public void map( Text key, MongoSuite value, OutputCollector<Text,IntWritable> output, Reporter reporter ) 
            throws IOException {
            
            FileLock lock = new FileLock( "mapper" );
            try {
                lock.lock();

                System.out.println( "key: " + key );
                System.out.println( "value: " + value );
                
                long start = System.currentTimeMillis();
                boolean passed = value.runTest();
                long end = System.currentTimeMillis();
                
                output.collect( new Text( passed ? "passed" : "failed" ) , new IntWritable( 1 ) );
                output.collect( new Text( key.toString() + "-time-seconds" ) , new IntWritable( (int)((end-start)/(1000)) ) );
                output.collect( new Text( key.toString() + "-passed" ) , new IntWritable( passed ? 1 : 0 ) );
                
                String ip = IOUtil.readStringFully( new URL( "http://myip.10gen.com/" ).openConnection().getInputStream() );
                ip = ip.substring( ip.indexOf( ":" ) + 1 ).trim();
                output.collect( new Text( ip ) , new IntWritable(1) );
            }
            catch ( RuntimeException re ) {
                re.printStackTrace();
                throw re;
            }
            catch ( IOException ioe ) {
                ioe.printStackTrace();
                throw ioe;
            }
            finally {
                lock.unlock();
            }
            
        }

        public void configure(JobConf job) {}
        public void close(){}
    }

    public static class Reduce implements Reducer<Text, IntWritable, Text, IntWritable> {

        public void reduce( Text key, Iterator<IntWritable> values, OutputCollector<Text,IntWritable> output , Reporter reporter ) 
            throws IOException {

            int sum = 0;
            while ( values.hasNext() ) {
                sum += values.next().get();
            }
            output.collect( key , new IntWritable( sum ) );
        }

        public void configure(JobConf job) {}
        public void close(){}
    }

    public static class MySplit implements InputSplit , Writable {
        
        public MySplit(){
        }
        
        MySplit( MongoSuite config , int length ) {
            _config = config;
            _length = length;
        }

        public long getLength() {
            return _length;
        }
        
        public String[] getLocations() {
            return new String[0];
        }

        public void readFields( DataInput in ) 
            throws IOException {
            _config = new MongoSuite();
            _config.readFields( in );
            _length = in.readInt();
        }

        public void write( final DataOutput out ) 
            throws IOException {
            _config.write( out );
            out.writeInt( _length );
        }

        MongoSuite _config;
        int _length;
    }

    public static class InputMagic implements InputFormat<Text,MongoSuite> {

        public RecordReader<Text,MongoSuite> getRecordReader( InputSplit split, JobConf job , Reporter reporter ){
            final MySplit s = (MySplit)split;
            return new RecordReader<Text,MongoSuite>() {
                
                public void close(){}
                
                public Text createKey() {
                    return new Text();
                }
                
                public MongoSuite createValue() {
                    return new MongoSuite();
                }

                public long getPos() {
                    return _seen ? 1 : 0;
                }

                public float getProgress() {
                    return getPos();
                }
                
                public boolean next( Text key , MongoSuite value ) {
                    key.set( s._config.suite );
                    value.copy( s._config );

                    
                    boolean x = _seen;
                    _seen = true;
                    return !x;
                }
                
                boolean _seen = false;
            };
        }
        
        public InputSplit[] getSplits( JobConf job , int numSplits ){
            String[] pcs = job.get( "suites" ).split(",");
            InputSplit[] splits = new InputSplit[pcs.length];
            for ( int i=0; i<splits.length; i++ ) {
                MongoSuite c = new MongoSuite();
                c.suite = pcs[i];
                
                c.mongo = job.get( "mongo" );
                c.code = job.get( "code" );
                c.workingDir = job.get( "workingDir" );

                splits[i] = new MySplit( c , 100 /* XXX */);
            }
            return splits;
        }
        
        public void validateInput(JobConf job){}

        
    }

    /**
     * args
     *   mongo tgz
     *   code tgz
     *   output path
     *   tests to run ?
     */

    public static void main( String[] args ) throws Exception{

        JobConf conf = new JobConf();
        conf.setJarByClass(emr.class);
        
        String workingDir = "/data/db/emr/";
        

        // parse args

        int pos = 0;
        for ( ; pos < args.length; pos++ ) {
            if ( ! args[pos].startsWith( "--" ) )
                break;
            
            String arg = args[pos].substring(2);
            if ( arg.equals( "workingDir" ) ) {
                workingDir = args[++pos];
            }
            else {
                System.err.println( "unknown arg: " + arg );
                throw new RuntimeException( "unknown arg: " + arg );
            }
        }
        
        String mongo = args[pos++];
        String code = args[pos++];
        String output = args[pos++];
        
        String suites = "";
        for ( ; pos < args.length; pos++ ) {
            if ( suites.length() > 0 )
                suites += ",";
            suites += args[pos];
        }
        
        if ( suites.length() == 0 )
            throw new RuntimeException( "no suites" );
        
        System.out.println( "workingDir:\t" + workingDir );
        System.out.println( "mongo:\t" + mongo );
        System.out.println( "code:\t " + code );
        System.out.println( "output\t: " + output );
        System.out.println( "suites\t: " + suites );

        if ( false ) {
            MongoSuite s = new MongoSuite();
            s.mongo = mongo;
            s.code = code;
            s.workingDir = workingDir;
            s.suite = suites;
            s.runTest();
            return;
        }

        // main hadoop set
        conf.set( "mongo" , mongo );
        conf.set( "code" , code );
        conf.set( "workingDir" , workingDir );
        conf.set( "suites" , suites );

        conf.set( "mapred.map.tasks" , "1" );
        conf.setLong( "mapred.task.timeout" , 4 * 3600 * 1000 /* 4  hours */);

        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(IntWritable.class);
        
        conf.setMapperClass(Map.class);
        conf.setReducerClass(Reduce.class);
        
        conf.setInputFormat(InputMagic.class);
        conf.setOutputFormat(TextOutputFormat.class);
        
        FileOutputFormat.setOutputPath(conf, new Path(output) );

        //  actually run

        JobClient.runJob( conf );
    }
}