|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.mapreduce; |
| 19 | + |
| 20 | +import org.apache.commons.logging.Log; |
| 21 | +import org.apache.commons.logging.LogFactory; |
| 22 | +import org.apache.hadoop.conf.Configuration; |
| 23 | +import org.apache.hadoop.io.IntWritable; |
| 24 | +import org.apache.hadoop.util.ToolRunner; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.ArrayList; |
| 28 | + |
| 29 | +/** |
| 30 | + * A sleep job whose mappers create 1MB buffer for every record. |
| 31 | + */ |
| 32 | +public class GrowingSleepJob extends SleepJob { |
| 33 | + private static final Log LOG = LogFactory.getLog(GrowingSleepJob.class); |
| 34 | + |
| 35 | + public static class GrowingSleepMapper extends SleepMapper { |
| 36 | + private final int MB = 1024 * 1024; |
| 37 | + private ArrayList<byte[]> bytes = new ArrayList<>(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public void map(IntWritable key, IntWritable value, Context context) |
| 41 | + throws IOException, InterruptedException { |
| 42 | + super.map(key, value, context); |
| 43 | + long free = Runtime.getRuntime().freeMemory(); |
| 44 | + if (free > 32 * MB) { |
| 45 | + LOG.info("Free memory = " + free + |
| 46 | + " bytes. Creating 1 MB on the heap."); |
| 47 | + bytes.add(new byte[MB]); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) throws Exception { |
| 53 | + int res = ToolRunner.run(new Configuration(), new GrowingSleepJob(), args); |
| 54 | + System.exit(res); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Job createJob(int numMapper, int numReducer, |
| 59 | + long mapSleepTime, int mapSleepCount, |
| 60 | + long reduceSleepTime, int reduceSleepCount) |
| 61 | + throws IOException { |
| 62 | + Job job = super.createJob(numMapper, numReducer, mapSleepTime, |
| 63 | + mapSleepCount, reduceSleepTime, reduceSleepCount); |
| 64 | + job.setMapperClass(GrowingSleepMapper.class); |
| 65 | + job.setJobName("Growing sleep job"); |
| 66 | + return job; |
| 67 | + } |
| 68 | +} |
0 commit comments