The Artima Developer Community
Sponsored Link

Observing JavaSpace-Based Systems, Part II
How to Log and Analyze a Space-Based Distributed System
by Philip Bishop & Nigel Warren
October 14, 2002

<<  Page 4 of 6  >>

Advertisement

CSV Logging Strategy

A simple strategy for logging space-based operations is to write them to a CSV file. This lets us easily view the log file by opening it in a spreadsheet application. Most spreadsheets will open CSV files, use the comma delimiter to determine where each column ends/starts, and parse a carriage return as a marker for the end of a row.

Previously, we implemented the toString() method in the SpaceOperation class to return a CSV-formatted string, so we can quickly develop a basic LoggingStrategy implementation as shown below:

public class CSVStrategy
        implements LoggingStrategy {

    public synchronized void logOperation(SpaceOperation op) {
        try {
            String msg = op.toString();
            RandomAccessFile raf =
                    new RandomAccessFile("spacelog.csv", "rw");
            FileOutputStream fos =
                    new FileOutputStream(raf.getFD());
            raf.seek(raf.length());
            PrintWriter pw = new PrintWriter(fos);
            pw.println(msg);
            pw.flush();
            fos.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Although our simple implementation will work, we must synchronize the logOperation() method to avoid corrupting the log file or miss writing data to the file due to IOExceptions. This implementation's obvious problem is lock contention caused by multiple clients (or threads in the same client) blocking the wait to write to the log file.

A simple, yet effective, solution to reduce the lock contention is to use a guarded queue to store the SpaceOperation objects as they arrive. In addition, we have a separate thread processing the head of the queue, thus writing the SpaceOperation object out to disk.

Using a guarded queue only causes contention when we add or remove data from the queue, which in almost all cases is shorter than the length of time it takes to write the data to the file. If you're not familiar with how a guarded queue works, then you can download the source code for this article and other examples in our book JavaSpaces in Practice (see Resources).

Below is a revised version of the CSVStrategy class using a queue. The dump() method has the same code as the previous version's logOperation() method:

public class CSVStrategy
        implements LoggingStrategy, Runnable {
    private jsip.util.Queue _queue
            = new jsip.util.Queue();

    public CSVStrategy() {
        Thread t = new Thread(this);
        t.start();
    }

    public void logOperation(SpaceOperation op) {
        _queue.add(op);
    }

    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            SpaceOperation op =
                    (SpaceOperation) _queue.getNext();
            //Write the SpaceOperation to disk
            dump(op);
        }
    }
}

Now that we have a LoggingStrategy implementation, we can start to assemble the final pieces of the jigsaw: the proxy and service starter classes.

<<  Page 4 of 6  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us