Download Programming in RMI

Document related concepts
no text concepts found
Transcript
RemoteProcedureCalls
1
Contents
• Remoteprocedurecall
– Fundamentals
– RPCinC(underUnix/Linux)
• RemoteMethodInvocation
– Fundamentals
– JavaRMI
Distributed Systems: Core
2
REMOTEPROCEDURECALLS(RPC)
3
RPC:Fundamentals
• Problem:client-serverinteractionishandledthroughthe
OSprimitivesforI/Oè difficulttodevelopapplications
• Idea(SunMicrosystemsintheearly80s):enableremote
accessthroughthewell-knownprocedurecall
programmingmodel
program
P
begin
…
p1();
…
…
p2();
…
…
end
procP1
begin
…
end
procP2
begin
…
end
program
P
begin
…
p1();
…
…
p2();
…
…
end
Client
Distributed Systems: Core
communication facilities
RPC
procP1
begin
…
end
procP2
begin
…
end
Server
4
RPC:Howdoes it work
procedure execution
application
code
local
call
invocation
return
application
code
result
transfer
client stub
(middleware)
server stub
(middleware)
data
transfer
system
call
data
transfer
system
call
network transport
services
network transport
services
reply message
invocation message
Distributed Systems: Core
communication facilities
procedure
call
5
Parameterpassing:Marshallingandserialization
• Passingaparameterposestwoproblems:
– Structureddata(e.g.,structs/records,objects)mustbeultimately
flattenedinabytestream
• Calledserialization (or pickling, inthecontextof OODBMSs)
– Hostsmayusedifferentdatarepresentations(e.g.,littleendianvs.big
endian,EBCDICvs.ASCII)andproperconversionsareneeded
• Calledmarshalling
• Middlewareprovidesautomatedsupport:
– Themarshallingandserializationcodeisautomaticallygeneratedfrom
andbecomespartofthestubs
– Enabledby:
• Alanguage/platform independent representation oftheprocedure’s
signature, writtenusing anInterfaceDefinition Language (IDL)
• Adatarepresentationformattobeusedduring communication
Distributed Systems: Core
6
TheroleofIDL
• TheInterfaceDefinitionLanguage (IDL)raisesthelevelof
abstractionoftheservicedefinition
– Itseparatestheserviceinterface fromitsimplementation
– Thelanguagecomeswith“mappings” ontotargetlanguages(e.g.,C,
Pascal,Python…)
• Advantages:
– Enablesthedefinitionofservicesinalanguage-independentfashion
– Beingdefinedformally,anIDLdescriptioncanbeusedto
automaticallygeneratetheserviceinterfacecodeinthetarget
language
Distributed Systems: Core
7
SunRPC(ONCRPC)
• SunMicrosystems’ RPC(alsocalledOpenNetworkComputing
RPC,ONCRPC)isthedefacto standardovertheInternet
– AtthecoreofNFS,andmanyotherservices
– FoundinmodernUnixsystems(e.g.,Linux)
• DataformatspecifiedbyXDR(eXternalDataRepresentation)
– Initiallyonlyfordatarepresentation,thenextendedinaproperIDL
• TransportcanuseeitherTCPorUDP
• Parameterpassing:
– Onlypassbycopyisallowed(nopointers)
– Onlyoneinputandoneoutputparameter
• ProvisionforDESsecurity
Distributed Systems: Core
communication facilities
8
SunRPC:Developmentcycle
server procedures
rdate_proc.c
server program
gcc
server stub
rdate_svc.c
service specification
rdate.x
client code
rpcgen
include file
rdate.h
Distributed Systems: Core
RPC
libraries
client stub
rdate_clnt.c
rdate_client.c
gcc
9
rdate_svc
client program
rdate_client
REMOTEMETHODINVOCATION
(RMI)
10
Towards RMI…
• Thebasicideaissimple
– Aclientexecutesarequest.This
requesttravelsoverthenetwork
towardsaspecificServer.
– Theserverprocessestherequests
andsendsbackaresult.
– If we were toworkwithsockets
we would have toexplicitly deal
withmessage formatsand
connections.
11
Towards RMI…
• Wewantamechanismthroughwhichthedevelopercan
simplyexecuteamethodcall.
– Withouthavingtodealwithnetworkissues!
• Thetechnicalsolutionistoinstallaproxyontheclientside
andhavetheproxyhideawayallthecomplexity.
– Theproxyappearstotheclientasaregularobject.
• Atthesametimetheserver-sidedeveloperalsodoesnot
wanttohavetodealwithlow-levelnetworktechnicalities.
– He/She toocaninstallaproxy…
– Thiscreatesanewlevelofabstraction
12
Towards RMI…
13
TheBasicsofRMI
• RemoteObject
– Objectswhose methods canbecalled fromaJVMthat is different
fromtheone they exist in.
• RemoteInterface
– Theinterface defines themethods that canbeinvoked froma
different JVM.
• Server
– TheServeris asetof“one ormore”remoteobjects that offer
resources (dataandcomputation),through remoteinterfaces,to
external machines that aredistributed onthenetwork.
• RemoteMethodInvocation (RMI)
– Invocation ofamethod offered byaremoteobject that implements a
remoteinterface.Thesyntax is identical toalocal method invocation.
14
RMIStubs
• RMIworkflow
– Theserverinstantiates remoteobjects andmakes them visible
– Theclientsobtain references totheremoteobjects called stubs.
– Through these stubs they make their calls.
But how doI
know the
stubs???
15
RMIRegistry
• TheRMIRegistry provides thestubs totheclients
– During registration theservermustprovide acanonical name forthe
remoteobject.This is aunique identifier.
– Theclientmustknow this canonical name before hand.
16
Downloadingthestub
• TheRMIRegistry cansend theclientthestub indifferent
ways…
– If thetwo JVMs reside onthesame machinethey canusethelocal
filesystem
– If they reside ondifferent machines they mustuseanexternal and
well-identified HTTPserver.
17
Summary
• Clientside
1.
2.
3.
Requestthestubneededtomaketheremotemethodcall
Serialize theparametersyouneedtopasstotheremoteobject
Sendtherequesttotheserver
• Serverside
1.
2.
3.
4.
Theserverreceivestheserializeddataandlocalizestheremote
objectthatneedstobeinvoked
Theservercallsthedesiredmethodpassingitthede-serializedinput
parameters
Theservercapturesthereturnparameterandanyeventual
exceptions
Theserversendseverythingbacktotheclient
18
RunningExample
• We will implement thewell-known Warehouse example
• Thewarehouse contains asetofproducts andeach product
has:
– Astring identifier which is tobeunique
– Aprice
19
Shared interface
• TheWarehouse <<interface>>clarifies what methods are
being exposed
• It extends theRemote<<interface>>
• All methods need tothrow aRemoteException
20
Let’simplementthesharedinterface
21
Shared Interface
import java.rmi.*;
public interface Warehouse extends Remote{
double getPrice(String description) throws RemoteException;
}
22
WarehouseServer
• Ilserverimplemental’interfacciaremota
• EstendelaclasseUnicastRemoteObjectcherendel’oggetto
accessibiledaremoto
23
Implementiamo il componente server-side
24
Warehouse Server-sideimplementation
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
Makes theobject remotely accessible
public class WarehouseImpl extends UnicastRemoteObject implements Warehouse{
private Map<String, Double> prices;
public WarehouseImpl() throws RemoteException
{
prices = new HashMap<String, Double>();
prices.put("BlackwellToaster", 24.95);
prices.put("ZapXpressMicrowaveOven", 49.95);
}
public double getPrice(String description) throws RemoteException
{
Double price = prices.get(description);
return price == null ? 0 : price;
}
}
25
AlternativeServer-sideimplementation
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class WarehouseImpl implements Warehouse{
public WarehouseImpl() throws RemoteException
{
prices = new HashMap<String, Double>();
prices.put("BlackwellToaster", 24.95);
prices.put("ZapXpressMicrowaveOven", 49.95);
UnicastRemoteObject.exportObject(this,0);
}
public double getPrice(String description) throws RemoteException
{
Double price = prices.get(description);
return price == null ? 0 : price;
}
private Map<String, Double> prices;
}
26
Publishingtheremoteobject (1)
• TheserverstartsbypublishingtheremoteobjecttotheRMI
Registry
• Theregistry mustbeonlinebefore this occurs
– Wewillseehowtodothis.
Assumes theregistry is inthedefault
location ->localhost:1099
WarehouseImpl centralWarehouse = new WarehouseImpl();
Registryregistry=LocateRegistry.getRegistry();
registry.bind("central_warehouse", centralWarehouse);
binding thename “central_warehouse”totheremoteobject
centralWarehouse
27
Let’spublishit…
28
Publishingtheremoteobject (2)
import java.rmi.*;
import javax.naming.*;
public class WarehouseServer
{
public static void main(String[] args) throws RemoteException, NamingException
{
System.out.println("Constructingserverimplementation...");
WarehouseImpl centralWarehouse = new WarehouseImpl();
System.out.println("Bindingserverimplementationtoregistry...");
Registryregistry= LocateRegistry.getRegistry();
registry.bind(“central_warehouse", centralWarehouse);
System.out.println("Waitingforinvocationsfromclients...");
}
}
29
Technicalnotesonbind()
• Forsecurityreasonsbindings,unbindings orrebindings can
onlybeperformedbyanapplicationthatisrunningonthe
sameregistryhost.
• Thisallowsustoavoidmaliciousbehaviour.
• Lookupsareopentoexternalclients…
String[]remoteObjects= registry.list();
30
TechnicalnotesontheRegistry
• Theregistry is also aRemoteObject
• Thebind()method is partoftheobject’s remoteinterface
void bind(String name, Remote obj)
throws RemoteException, AlreadyBoundException, AccessException;
• Theparameters will need tobeserialized/deserialized
• TheRegistry will dynamically downloadthedefinition ofthe
remoteinterface we areusing tobeable toserialize the
object we arepassing.
31
WarehouseClient
• This is how we get areference toastub ontheclientside:
– Registry mustbeonline
– Theobject needs tobealready ontheserver
String remoteObjectName = "central_warehouse";
Warehouse centralWarehouse = (Warehouse)
registry.lookup(remoteObjectName);
CastingtoWarehouse andnot WarehouseImpl beacuse this is all the
clientknows
32
Let’simplementtheclientcode…
33
WarehouseClient
import java.rmi.*;
import java.util.*;
import javax.naming.*;
public class WarehouseClient
{
public static void main(String[] args) throws NamingException, RemoteException
{
Registry registry= LocateRegistry.getRegistry();
System.out.print("RMIregistry bindings:");
String[] e = registry.list();
for (int i=0; i<e.legth; i++)
System.out.println(e[i]);
String remoteObjectName = "central_warehouse";
Warehouse centralWarehouse = (Warehouse) registry.lookup(remoteObjectName);
}
}
String descr = "Blackwell Toaster";
double price = centralWarehouse.getPrice(descr);
System.out.println(descr + ":" + price);
34
Howtodeploy theRMIApplication
• What dowe need?
1.
2.
3.
4.
Launch theHTTPserver.
Launch theRMIRegistry
Launch theServer
Launch theClient
35
Setup
• OntheServerside:
server/
WarehouseServer.class
WarehouseImpl.class
download/
Warehouse.class
• Ontheclientside:
client/
WarehouseClient.class
36
Setup
• Let’slaunchtheserveronlocalhost:8080
– Thisisthelocationwewillneedtodeclareour
java.rmi.server.codebase forthedynamicdownloading.
• LinuxandOSX
$rmiregistry –J-Djava.rmi.server.codebase=http://localhost:8080/Warehouse
• Windows
$startrmiregistry –J-Djava.rmi.server.codebase=http://localhost:8080/Warehouse
• The rmiregistry is distributed withJava
37
Setup
• Let’sgototheserverdir andtype
$javaWarehouseServer
• Whatdowesee?
Constructing serverimplementation...
Bindingserverimplementationtoregistry...
Exceptioninthread"main"javax.naming.CommunicationException [Rootexceptionis
java.rmi.ServerException:RemoteExceptionoccurredinserverthread;nestedexceptionis:
java.rmi.UnmarshalException:errorunmarshalling arguments;nestedexceptionis:
java.lang.ClassNotFoundException: Warehouse]
atcom.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:143)
atcom.sun.jndi.toolkit.url.GenericURLContext.bind(GenericURLContext.java:226)
atjavax.naming.InitialContext.bind(InitialContext.java:419)
atWarehouseServer.main(WarehouseServer.java:13)
$java-Djava.rmi.server.codebase=http://localhost:8080/Warehouse/ WarehouseServer
38
Logging theapplication
• Logging canbevery important inadistributed application.
• Theeasiest waytodoit id:
– -Djava.rmi.server.logCalls=true
– AllRMIcallsandexceptionsaresavedtoSystem.err.
• Formorecomplexneeds
– http://download.oracle.com/javase/1.4.2/docs/guide/rmi/logging.htm
l
39
PASSINGPARAMETERS
40
RemoteandNon-remoteobjects
• Anon-remote object (passed orreceived as amethod
parameter)is passed bycopy
– It is serialized andputinto thestream
– It is then deserializzed ontheother sideinto anew copy
– Changes madetoanon-remoteobject have noeffect ontheoriginal
copy
• Aremote object(already exported,or passed orreceived as a
method parameter)is passed using astub
– Aremoteobjectpassedasaparametercanonlyprovidearemote
interface
41
Let’simplementanewmethodthatdoesthis…
42
WarehouseV2
import java.rmi.*;
import java.util.*;
public interface Warehouse extends Remote
{
double getPrice(String description) throws RemoteException;
Product getProduct(List<String> keywords) throws RemoteException;
}
43
Product
import java.io.*;
public class Product implements Serializable{
private String description;
private double price;
private Warehouse location;
}
public Product(String description, double price){
this.description = description;
this.price = price;
}
public String getDescription(){
return description;
}
public double getPrice(){
return price;
}
public Warehouse getLocation(){
return location;
}
public void setLocation(Warehouse location){
this.location = location;
}
44
NewProjectStructure
• Threedifferent compilationunits
• Theserverandtheclientboth depend onthecommon
interface artifacts
server/
WarehouseServer.class
WarehouseImpl.class
Book.class
<<dependency>>
download/
Warehouse.class
Product.class
client/
<<dependency>>
WarehouseClient.class
45
ApplicationWorkflow
Usealistof
stringsasasetof
keywords
TheBookisa
subclassof
Product, witha
different
implementation
ofgetDescription
Thereturned
product canhave
areference to
thewarehouse
46
Book
public class Book extends Product{
public Book(String title, String isbn, double price){
super(title, price);
this.isbn = isbn;
}
public String getDescription(){
return super.getDescription() + "" + isbn;
}
}
private String isbn;
47
DynamicClassLoading
• ThankstoDynamicClassLoadinginJavaitispossibletoload
Javaclassdefinitionsatruntime.
– Theclientcanreceive previously unknown classes fromtheserver
• Theservercommunicates thecodebase URLtotheclient
– java.rmi.server.codebase
• Theclientcontacts theHTTPserverandgets theBook.class
filesothat its codecanactually beexecuted.
– This is all transparent
48
SecurityPolicy
• Dynamic ClassLoading requires securitypolicies
– Executing anexternally defined piece ofcodeis never nice
• Themost simple example…
grant{
permission java.security.AllPermission "","";
};
• This policyneeds tobeused ontheRMIRegistry,ontheserver
sothat it canaccess thermi registry
49
Let’s implement thecode
50
WarehouseImpl
public class WarehouseImpl extends UnicastRemoteObject implements Warehouse{
private Map<String, Product> products;
private Product backup;
public WarehouseImpl(Product backup) throws RemoteException{
products = new HashMap<String, Product>();
this.backup = backup;
}
public void add(String keyword, Product product){
product.setLocation(this);
products.put(keyword, product);
}
public double getPrice(String description) throws RemoteException{
for (Product p : products.values())
if (p.getDescription().equals(description)) return p.getPrice();
if (backup == null) return 0;
else return backup.getPrice(description);
}
public Product getProduct(List<String> keywords) throws RemoteException{
for (String keyword : keywords){
Product p = products.get(keyword);
if (p != null) return p;
}
return backup;
}//getProduct
}//class
51
WarehouseServer
import java.rmi.*;
import javax.naming.*;
public class WarehouseServer{
public static void main(String[] args) throws RemoteException, NamingException{
System.out.println("Constructingserverimplementation...");
WarehouseImpl centralWarehouse = new WarehouseImpl(
new Book(“BackupBook", “123456", 66.99));
centralWarehouse.add("toaster", new Product("BlackwellToaster", 23.95));
System.out.println("Bindingserverimplementationtoregistry...");
Registryregistry= LocateRegistry.getRegistry();
registry.bind(“central_warehouse", centralWarehouse);
}
System.out.println("Waitingforinvocationsfromclients...");
}
52
WarehouseClient
import java.rmi.*;
import java.util.*;
import javax.naming.*;
import java.util.ArrayList;
public class WarehouseClient
{
public static void main(String[] args) throws NamingException, RemoteException
{
…
Warehouse centralWarehouse = (Warehouse) registry.lookup (“central_warehouse”);
ArrayList<String> l=new ArrayList<String>();
l.add(”pluto");
Product p=centralWarehouse.getProduct(l);
System.out.println("Description:" + p.getDescription());
}
}
Stringlocation=null;
try{
location=p.getWarehouse().getLocation();
System.out.println("Product download from->"+location);
}catch(RemoteException e){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
53
ACTIVATABLE OBJECTS
54
Activatable Objects
• Sometimeswedon’twanttohavetheserverremoteobjects
justsittingtherewaitingtobecalled
• Itwouldbemoreefficientifwecouldsetthemupsothat
they
– Automaticallygetinstantiatedeverytimeoneisneeded.
– Automaticallygettorndownwhennobodyelseneedsthem
• RMIprovidesasolutionforthis.
– TheRMIdaemon
55
Howdoesitwork?
• WiththeintroductionoftheclassActivatable andtheRMI
daemon,rmid,programscanbewrittentoregister
informationaboutremoteobjectimplementationsthat
shouldbecreatedandexecute"ondemand“,ratherthan
runningallthetime
• TheRMIdaemon,rmid,providesaJavavirtualmachinefrom
whichotherJVMinstancesmaybespawned
Remote
Requests
Active/Inactive
notifications
Activation
Monitor
Activation Group
Activation
Requests
Forward
Requests
Active Object
Activator
Activation
Procedures
Inactive
Object
56
Codechanges?
• Nochangesinthecommonpart…
• Nochangesintheclientpart…
• Therearefourstepsontheserver-side…
–
–
–
–
Maketheappropriateimportsintheimplementationclass
Extendyourclassfromjava.rmi.activation.Activatable
Declareatwo-argumentconstructorintheimplementationclass
Implementtheremoteinterfacemethods(youshouldalreadyhave
this)
57
Let’simplementtheserver-sideremoteobject…
58
Activatable Server-side
publicclassActiveWarehouseImpl extendsActivatable implementsActiveWarehouse {
privateMap<String,ActiveProduct>products;
privateStringloc;
publicActiveWarehouseImpl(ActivationID id,MarshalledObject<?>data)throws
RemoteException {
super(id,0);
//addextracodeforinitializationfromincomingdata
products=newHashMap<String,ActiveProduct>();
this.loc ="Servernumber1";
}
59
TheSetupClass
•
Thejobofthe"setup"classistocreatealltheinformationnecessaryfor
theactivatable class
• withoutnecessarilycreatinganinstanceoftheremoteobject
•
Thesetupclass
• Makestheappropriateimports
• Installsasecuritymanager
• CreatesanActivationGroup instance
• CreatesanActivationDesc instance
• Declaresaninstanceofyourremoteinterfaceandregistersitwith
rmid
• Bindsthestubtoanameinthermiregistry
rmid -J-Djava.security.policy=myrmi.policy -J-Djava.rmi.server.codebase=http://localhost:8888/…/
60
Let’simplementtheSetupClass
61
TheSetupClass
Propertiesprops=newProperties();
props.put("java.security.policy","myrmi.policy");
ActivationGroupDesc groupDesc =newActivationGroupDesc(props,null);
try{
ActivationGroupID gid =ActivationGroup.getSystem().registerGroup(groupDesc);
Stringlocation="http://localhost:8888/Warehouse/";
MarshalledObject<ActiveWarehouseConfig>data=null;
ActivationDesc activationDescription =newActivationDesc(gid,
"it.polimi.guinea.mw.rmi.warehouse.server.ActiveWarehouseImpl",location,data);
ActiveWarehouse activeWarehouse =
(ActiveWarehouse)Activatable.register(activationDescription);
System.out.println("Stub createdfortheactivatable object...");
Naming.rebind("warehouse", activeWarehouse);
}
catch(RemoteException |MalformedURLException |ActivationException e){ …}
62