The Artima Developer Community
Sponsored Link

Make Room for JavaSpaces, Part V
Make Your Compute Server Robust and Scalable
by Susan Hupfer
First Published in JavaWorld, June 2000

<<  Page 6 of 8  >>

Advertisement

Master Maps Tasks to Spaces

The first thing the revamped Master code does is to make use of the getSpaces method to obtain access to the available spaces. Here's a sketch of the Master code, illustrating the ways in which it has changed:



public class Master {
    private ServiceItem[] spaceServices;
    private int numSpaces;
    . . .
    private void startComputing() {
        ServiceMatches serviceMatches = SpaceAccessor.getSpaces();
        spaceServices = serviceMatches.items;
        numSpaces = serviceMatches.totalMatches;
        . . .
        generateTasks();
        collectResults();
    }



private void generateTasks() {
        ServiceItem spaceService;
        JavaSpace space;
        boolean written;
        Integer num;
        for (int i = 0; i < 10; i++) {
            spaceService = spaceServices[i % numSpaces];
            space = (JavaSpace)spaceService.service;
            num = new Integer(i);
            written = false;            
            while (!written) {
                written = writeTask(space, new AddTask(num, num));
            }
            written = false;
            while (!written) {
                written = writeTask(space, new MultTask(num, num));
        }

    }
    private boolean writeTask(JavaSpace space, Command task) {
        // write the task to the specified space
        . . .
    }
private void collectResults() {
        ResultEntry result;
        for (int i = 0; i < 10; i++) {
            // try to retrieve two results, one for addition
            // and one for multiplication
            for (int j = 0; j < 2; j++) {
                result = null;
                while (result == null) {
                    result = takeResult(i);
                }
            }
        }
    }
    protected ResultEntry takeResult(int i) {
        . . .
        ServiceItem spaceService = spaceServices[i % numSpaces];
        JavaSpace space = (JavaSpace)spaceService.service;
        ResultEntry template = new ResultEntry(i);
        ResultEntry result;
        // . . . within try/catch clauses:
        result = (ResultEntry)space.take(template, txn, Long.MAX_VALUE);
    }

After the call to getSpaces in startComputing, which returns a ServiceMatches result, you extract both the array of matching services and the count of how many there are from the result. Then you generate tasks and collect results, as described before.

The generateTasks method has changed a bit. Each time the method iterates through its loop, it determines which space to write the ith task to. Here the mapping is simple: the addition task i and multiplication task i are both written to the space (i % numSpaces) in the array of spaces. The writeTask method now takes a JavaSpace parameter, so it can write the task to the appropriate space.

The collectResults method has changed slightly too. It is now structured to have a nested loop and to remove addition results i and multiplication results i from the space. In fact, the takeResult method now takes an integer argument that is used to determine where to look for the result. To find the result of a task i, the method will look in the space (i % numSpaces) in the array of spaces, since that's where the task is written and where a worker writes the result.

<<  Page 6 of 8  >>


Sponsored Links



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