Professional Documents
Culture Documents
Client-Server Programming in Java-RMI
Client-Server Programming in Java-RMI
in
Java-RMI
http://wiki.duke.edu/display/scsc
scsc@duke.edu
Outline
Client-Server Parallelism
Java RMI
Performance Issues/Hints
1
Client-Server Parallelism
Client 1
Server
The Net
Client 2 DBase
No inter-client
Client 3
communication
2
Client-Server Parallelism, Examples
3
Pros/Cons of Client-Server Parallelism
Cons:
You must be able to fit each client-job onto a single machines
Memory could be a limiting factor
It will still take X hours for any one client to finish, its just that
you will get back 10 or 100 results every X hours
Sometimes (fast) partial results can be helpful
Scalability is a function of the (coarse) problem size
Pros:
It doesnt require real parallel programming!
This can be one of the most efficient forms of parallelism
It provides automatic load-balancing
Automatic Load-Balancing
client1 Job 6
1
Server
client2 Job 2
Job 1
Job 2
client3 Job 5
3
Job 3
(fast)
Job 4
Job 5
client4 Job 4
Job 6
4
The Java Programming Language
Java-RMI
5
Java-RMI Classes
WorkUnit.java ResultUnit.java
RmiInterface.java
Client.java Server.java
6
Java-RMI, Server Example
package ClientServer;
import java.rmi.*;
import java.rmi.server.*;
class Server extends UnicastRemoteObject implements RmiInterface {
private double next_alpha = 0.0;
private double next_beta = 100.0;
private double delta = 0.1;
Part of Java-RMI
Part of Java
public class WorkUnit implements Serializable {
double alpha,beta;
// needed for Serializable interface (needed by RMI)
private static final long serialVersionUID = 1L;
}
7
What happens in the Example Code?
WorkUnit3
WorkUnit1
WorkUnit1
Client
(1) ResultUnit1
ResultUnit3
WorkUnit2
WorkUnit3
WorkUnit4
Server
WorkUnit4
WorkUnit2
ResultUnit4
Client
(2) ResultUnit2
ResultUnit4
ResultUnit3
ResultUnit2
ResultUnit1
Some Caveats
8
Building the Client
Directory structure:
./ClientServer/:
WorkUnit.java
ResultUnit.java
RmiInterface.java
Client.java
Server.java
makefile
The Server build is also fairly straightforward ... with one extra step
We need the WorkUnit, ResultUnit, and RmiInterface classes built
first (and in that order)
jbp@hostname [ 36 ] % ls
Client.class RmiInterface.class Server_Stub.class
Client.java RmiInterface.java WorkUnit.class
ResultUnit.class Server.class WorkUnit.java
ResultUnit.java Server.java makefile
9
Running the Server
10
Running the Client and Server Together
rmirun is a home-grown Perl script that will launch the registry, one
server, and (N-1) clients
jbp@hostname [ 1 ] % rmirun -n 8 Server.class Client.class
#$ -cwd -o output.txt
#$ -pe low* 4-20
rmirun -S -n $NSLOTS -m $TMPDIR/machines Server.class Client.class
11
High-Performance, Multi-Threaded Server
The server can be an obvious bottleneck -- all the clients need to talk
to it, and if the server is slowed down then all clients will feel it
Luckily, a Java-RMI server is automatically a multi-threaded
server that can take advantage of multiple CPUs on the machine!
. . .
class Server extends UnicastRemoteObject implements RmiInterface {
. . .
public synchronized WorkUnit GetNextWorkUnitEven() throws RemoteException {
WorkUnit wu = new WorkUnit();
wu.alpha = next_alpha;
wu.beta = next_beta;
next_alpha += 2.0*delta;
next_beta -= 2.0*delta;
return( wu );
}
public synchronized WorkUnit GetNextWorkUnitOdd() throws RemoteException {
WorkUnit wu = new WorkUnit();
wu.alpha = next_alpha + delta;
wu.beta = next_beta + delta;
next_alpha += 2.0*delta;
next_beta -= 2.0*delta;
return( wu );
}
public WorkUnit GetNextWorkUnit( int clnum ) throws RemoteException {
if( (clnum%2) == 0 ) {
wu = GetNextWorkUnitEven();
} else {
wu = GetNextWorkUnitOdd();
}
return( wu );
}
12
Handling Errors
UnknownHostException (Naming.lookup)
Yes, its the obvious cause
ConnectException (Naming.lookup or server.function)
The connection was refused by the remote machine, i.e. the
network is ok, but the server is refusing to hand out any data
Or a network time-out or other network problem ... retry?
ConnectIOException
Possible network problems ... retry?
MarshalException (server.function)
UnmarshalException (server.function)
Problems when marshalling the data to the remote machine ...
possibly just a problem with the Serialization of your data-type;
but could also be an RMI-version mismatch
This is unlikely, but very bad when it happens (the data MIGHT
have gotten to the server and been processed, but you cant tell)
13
Network Errors, Client, contd
ServerError (server.function)
ServerException (server.function)
ServerRuntimeException (server.function)
The server encountered an error in processing the remote method
... could be bad parameters were sent; could be that the server code
is bad ... retry?
NoSuchObjectException (server.function)
The server is no longer running ... restart the server and try again
But restart the server is an off-line process
Aside from the network-badness errors (just like the client might see),
the server might also see:
AlreadyBoundException (Naming.rebind)
Someone else is using the same server-name ... so try another?
But your client is probably expecting the server at a given URL
If youre sharing the server machine with others (like in the
DSCR), and youve shared your code with others, then someone
else beat you to that service name
Might be worth thinking about NOT hard-coding the name into your
clients or server (yet another command-line argument?)
14
Results Checking
Given all the weird errors that can creep in, you may want to allow
clients to re-send a result several times, to make sure it gets
through
15
Famous Last Words
If youre running the client and server (both) on a cluster, then you
probably dont have to worry much about network errors or bad results
packets from hackers
Probably a good idea to still catch the errors and print them, just
for debugging
Actually, Java requires that you catch them
What if other people are using your cool, new, super-fast, parallel
client-server app?
Theyre all going to start the same named servers ... bad!
You may want to have a scheme for re-naming the server if the
usual name is already in use
Prepend your NetID or $JOBID; append a 1, then 2, ...
16
Next Steps
JDBC+SQLite
JDBC is a set of connector-functions to a real database
SQLite is a mostly-full-featured SQL server
Used in many production apps (Mac OS X)
Database will store data to disk
So server failure wont be catastrophic
Client1 Client2
// prepare message
. . .
// async msg arrival PutMessage( 1, m1 );
. . . . . .
m1
m1 = GetMessage();
// process m1
. . . . . .
PutMessage( 2, m2 ); m2 = GetMessage();
. . . // process m2
m2
. . .
m2
m1
17