Styx Grid Services Tutorial: Hello World

In this first part of the tutorial we shall wrap the traditional "Hello World" program as a Styx Grid Service and execute it remotely. In order to do this we need to go through the following steps:

  1. Create a machine-readable description of the program (the configuration file).
  2. Create and run the SGS server, using this description.
  3. Run the service using the SGS client software.

1. Creating the configuration file

The first thing that we need to do is to create a configuration file that contains a complete description of the HelloWorld program and how to run it. The HelloWorld program reads no input files or command line arguments and simply prints a "Hello World" message to the console (i.e. to the standard output stream).

The configuration file for this service is therefore very simple but you don't have to write it yourself. The JStyx distribution contains not only a suitable configuration file, but also a HelloWorld program, implemented in Java. The provided configuration file (the SGSconfig.xml file in the conf directory of the distribution) contains descriptions of all the Styx Grid Services that are included in this tutorial but we are only interested in the part that describes the helloworld service for the moment:

<gridservice name="helloworld"
    command="JStyxRun uk.ac.rdg.resc.jstyx.gridservice.tutorial.HelloWorld"
    description="Prints Hello World to stdout">
  <outputs>
    <output type="stream" name="stdout"/>
  </outputs>
</gridservice>

This simply specifies that the HelloWorld program is run using the command "JStyxRun uk.ac.rdg.resc.jstyx.gridservice.tutorial.HelloWorld" and that it prints data to its standard output stream. (Important note for Windows users: the JStyxRun "program" is actually a batch file and so cannot be run directly with this command. You will need to change the command property for the helloworld SGS to "cmd.exe /C JStyxRun uk.ac. ...". This is because batch files are not executable on their own: they need to be run using the command prompt program.)

2. Run the server

We now need to run the SGS server. The GridServices script runs the server, passing in the SGSconfig.xml file. Assuming you have set your PATH correctly as described in the installation instructions, you can run the server simply by entering:

GridServices

at a command-line prompt. If all is well, you will see some debug messages printed to the console, including the line "Creating StyxGridService called helloworld". The final message will read something like "Started StyxGridServices, listening on port 9092". See below for instructions for changing this port number if the default port of 9092 is not acceptable for any reason.

Note that you should not run the SGS server as the root user for security reasons.

3. Run the service using the SGSRun program

We shall now execute the helloworld service. The SGS distribution includes a Java program called SGSRun, which is a generic client program for any Styx Grid Service. It is run from the command line like so:

SGSRun <hostname> <port> <servicename> [args]

Open a new command prompt or terminal window. Assuming that you are working on the same machine that is running the SGS server, you can run the helloworld service by entering:

SGSRun localhost 9092 helloworld

You should see the "Hello World!" message printed to the console window. The SGSRun program has connected to the server, created a new instance of the helloworld service, run the service and downloaded the output data, which in this case was just the "Hello World!" string.

4. Create a wrapper script

One of the most important features of the SGS system is the ability to run remote services exactly as if they were local programs. The SGSRun program gets us most of the way there but we still have to specify the location and port number of the server. Assuming that these are fixed, it is an easy task to create a script that wraps the SGSRun program.

On Windows, we can create a batch file called helloworld.bat with the following contents:

@echo off
SGSRun localhost 9092 helloworld

On Linux/Unix the file would be a shell script called helloworld:

#!/bin/sh
SGSRun localhost 9092 helloworld

The helloworld wrapper script can now be treated exactly as if it were the HelloWorld program itself.

5. Exercise: Provide your own Hello World program

For convenience, an example HelloWorld program (written in Java) is provided with the SGS distribution. This is what we have been using so far. As an exercise, you might like to write a HelloWorld program in your language of choice and run that as an SGS instead of the Java program. For example, if you write and compile a C program called helloworld and place it in the /usr/local/bin directory, you would change the relevant part of the configuration file to:

<gridservice name="helloworld"
    command="/usr/local/bin/helloworld"
    description="Prints Hello World to stdout">
  ...
</gridservice>

and stop and restart the server process.

Java programmers might like to know that the string in the command attribute of the <gridservice> tag is passed directly to Java's Runtime.getRuntime().exec() method. The main thing to watch out for is that .bat files under Windows cannot be executed directly: they must be passed to the cmd.exe (Windows 2000/XP) or command.com (Windows 9x) program and so the command attribute must read as something like:

command="cmd.exe /C C:\programs\myscript.bat"