Showing posts with label concurrency. Show all posts
Showing posts with label concurrency. Show all posts

Friday, 24 June 2011

Naming threads created with the ExecutorService

When profiling applications it sometimes becomes a pain to understand where so many threads come from and what the hell they are doing.






If you’re like me and have a dependency on java.util.concurrent‘s API’s for anything concurrency-related, then you’ve certainly noticed that default thread names aren’t particularly helpful with the aforementioned problem.
Here’s a quick & dirty implementation of a ThreadFactory (based on Executors.DefaultThreadFactory) but with support for your own thread name prefixes.

public class NamedThreadFactory implements ThreadFactory {

// constants -----------------------------------------------------------------

private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);

// internal vars -------------------------------------------------------------

private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;

// constructors --------------------------------------------------------------

public NamedThreadFactory() {
this("ThreadPool(" + POOL_NUMBER.getAndIncrement() + "-thread-");
}

public NamedThreadFactory(String namePrefix) {
SecurityManager s = System.getSecurityManager();
this.group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
this.namePrefix = namePrefix + "(pool" +
POOL_NUMBER.getAndIncrement() + "-thread-";
}

// ThreadFactory -------------------------------------------------------------

public Thread newThread(Runnable r) {
Thread t = new Thread(this.group, r, this.namePrefix +
this.threadNumber.getAndIncrement() + ")", 0L);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}
Tip: If you’re using Spring, you can just use CustomizableThreadFactory and save yourself the trouble.

Friday, 17 June 2011

What is map reduce?



MapReduce is a parallel programming technique made popular by Google. It is used for processing very very large amounts of data. Such processing can be completed in a reasonable amount of time only by distributing the work to multiple machines in parallel. Each machine processes a small subset of the data. MapReduce is a programming model that lets developers focus on the writing code that processes their data without having to worry about the details of parallel execution.

MapReduce requires modeling the data to be processed as key,value pairs. The developer codes a map function and a reduce function.

The MapReduce runtime calls the map function for each key,value pair. The map function takes as input a key,value pair and produces an output which is another key,value pair.

The MapReduce runtime sorts and groups the output from the map functions by key. It then calls the reduce function passing it a key and a list of values associated with the key. The reduce function is called for each key. The output from the reduce function is a key,value pair. The value is generally an aggregate or something calculated by processing the list of values that were passed in for the input key. The reduce function is called for each intermediate key produced by the map function. The output from the reduce function is the required result.

As an example , let us say you have a large number of log files that contain audit logs for some event such as access to an account. You need to find out how many times each account was accessed in the last 10 years.
Assume each line in the log file is a audit record. We are processing log files line by line.The map and reduce functions would look like this:

map(key , value) {

// key = byte offset in log file
// value = a line in the log file
if ( value is an account access audit log) {
account number = parse account from value
output key = account number, value = 1
}
}
reduce(key, list of values) {
// key = account number
// list of values {1,1,1,1.....}
for each value
count = count + value

output key , count
}
The map function is called for each line in each log file. Lines that are not relevant are ignored. Account number is parsed out of relevant lines and output with a value 1. The MapReduce runtime sorts and groups the output by account number. The reduce function is called for each account. The reduce function aggregates the values for each account, which is the required result.

MapReduce jobs are generally executed on a cluster of machines. Each machine executes a task which is either a map task or reduce task. Each task is processing a subset of the data. In the above example, let us say we start with a set of large input files. The MapReduce runtime breaks the input data into partitions called splits or shards. Each split or shard is processed by a map task on a machine. The output from each map task is sorted and partitioned by key. The outputs from all the maps are merged to create partitions that are input to the reduce tasks.

There can be multiple machines each running a reduce task. Each reduce task gets a partition to process. The partition can have multiple keys. But all the data for each key is in 1 partition. In other words each key can processed by 1 reduce task only.

The number of machines , the number of map tasks , number of reduce tasks and several other things are configurable.

MapReduce is useful for problems that require some processing of large data sets. The algorithm can be broken into map and reduce functions. MapReduce runtime takes care of distributing the processing to multiple machines and aggregating the results.

Apache Hadoop is an open source Java implementation of mapreduce. Stay tuned for future blog / tutorial on mapreduce using hadoop.