Prof. Dr.-Ig. Stefa Deßloch AG Heterogee Iformatiossysteme Geb. 36, Raum 329 Tel. 0631/205 3275 dessloch@iformatik.ui-kl.de Chapter 4 Remote Procedure Calls ad Distributed Trasactios
Outlie Remote Procedure Call cocepts IDL, priciples, bidig variatios remote method ivocatio example: Java RMI stored procedures Distributed Trasactio Processig trasactioal RPC X/Ope DTP Summary Prof.Dr.-Ig. Stefa Deßloch 2 Middleware for Heterogeeous ad Distributed Iformatio Systems
Commuicatio ad Distributed Processig Distributed (Iformatio) System cosists of (possibly autoomous) subsystems joitly workig i a coordiated maer How do subsystems commuicate? Remote Procedure Calls (RPC) trasparetly ivoke procedures located o other machies Peer-To-Peer-Messagig Message Queuig Trasactioal Support (ACID properties) for distributed processig Server/system compoets are Resource Maagers (Trasactioal) Remote Procedure Calls (TRPC) Distributed Trasactio Processig Prof.Dr.-Ig. Stefa Deßloch 3 Middleware for Heterogeeous ad Distributed Iformatio Systems
Remote Procedure Call (RPC) Goal: Simple programmig model for distributed applicatios based o procedure as a ivocatio mechaism for distributed compoets Core mechaism i almost every form of middleware Distributed programs ca iteract (trasparetly) i heterogeeous eviromets etwork protocols programmig laguages operatig systems hardware platforms Importat cocepts Iterface Defiitio Laguage (IDL) IDL Compiler Proxy (Cliet Stub) Skeleto (Server Stub) Iterface Defiitio Laguage (IDL) Header files Proxy Skeleto Prof.Dr.-Ig. Stefa Deßloch 4 Middleware for Heterogeeous ad Distributed Iformatio Systems
How RPC Works Defie a iterface for the remote procedure usig a IDL abstract represetatio of procedure iput ad output parameters ca be idepedet of programmig laguages Compile the iterface usig IDL-compiler, resultig i cliet stub (proxy) server stub (skeleto) auxiliary files (header files, ) Cliet stub (proxy) compiled ad liked with cliet program cliet program ivokes remote procedure by ivokig the (local) cliet stub implemets everythig to iteract with the server remotely Server stub (skeleto) implemets the server portio of the ivocatio compiled ad liked with server code calls the actual procedure implemeted at the server Prof.Dr.-Ig. Stefa Deßloch 5 Middleware for Heterogeeous ad Distributed Iformatio Systems
RPC Cliet Server Applicatio Proxy RPC rutime RPC rutime Skeleto Applicatio Call order PACK ARGUMENT SEND CALL RECEIVE UNPACK PACKET ARGUMENT order (1) Cliet calls the local proxy (2) Cliet proxy marshals (packs) argumets to order (3) Cliet rutime system seds the call packet (argumets ad procedure ame) (4) Server rutime receives the message ad calls the right stub (5) Server stub upacks the argumets ad calls the server program (6) The order program rus as if it were called locally. Results flow back to the caller by reversig the procedure. WORK (cotiue) UNPACK RESULT RECEIVE RETURN PACKET SEND PACK RETURN Prof.Dr.-Ig. Stefa Deßloch 6 Middleware for Heterogeeous ad Distributed Iformatio Systems
Bidig i RPC Before performig RPC, the cliet must first locate ad bid to the server create/obtai a (eviromet-specific) hadle to the server ecapsulates iformatio such as IP address, port umber, Etheret address, Static bidig hadle is "hard-coded" ito the cliet stub at compile-time advatages: simple ad efficiet disadvatages: cliet ad server are tightly coupled server locatio chage requires recompilatio dyamic load balacig across multiple (redudat) servers is ot possible Dyamic bidig utilizes a ame ad directory service based o logical ames, sigatures of procedures server registers available procedure with the N&D server cliet asks for server hadle, uses it to perform RPC requires lookup protocol/api may be performed iside the cliet stub (automatic bidig) or outside opportuities for load balacig, more sophisticated selectio (traders) Locatio trasparecy usually meas that a remote procedure is ivoked just like a local procedure Bidig process for remote ad local procedures usually differ Prof.Dr.-Ig. Stefa Deßloch 7 Middleware for Heterogeeous ad Distributed Iformatio Systems
RPC Variatio 1: Distributed Objects Basic Idea: Evolve RPC cocept for objects applicatio cosists of distributed object compoets object services are ivoked usig Remote Method Ivocatio (RMI) Utilizes/matches advatages of object-orieted computig object idetity ecapsulatio: object maipulated oly through methods iheritace, polymorphism iterface vs. implemetatio reusability Prof.Dr.-Ig. Stefa Deßloch 8 Middleware for Heterogeeous ad Distributed Iformatio Systems
Distributed Objects with Java RMI Mechaism for commuicatio betwee Java programs betwee Java programs ad applets ruig i differet JVMs, possibly o differet odes Capabilities fidig remote objects trasparet commuicatio with remote objects loadig byte code for remote objects Cliet RMI RMI Directory Service RMI Server Prof.Dr.-Ig. Stefa Deßloch 9 Middleware for Heterogeeous ad Distributed Iformatio Systems
Java RMI Developmet Java is used as the IDL ad developmet programmig laguage Developmet steps 1. Defiig a remote iterface (e.g., Order ) methods capable of throwig RemoteExceptios 2. Implemetig server object class (e.g., OrderImpl, which implemets Order ) oly applicatio logic; commuicatio ifrastructure ot "visible" 3. Implemet cliet object, ivocatio of remote (server) object locate the remote object usig the RMI registry ivoke methods o remote object usig the remote iterface hadle RemoteExceptios 4. Provide server code for creatig a server object (istatiate server object class) exportig ad registerig the server object with the RMI registry Prof.Dr.-Ig. Stefa Deßloch 10 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example - Class ad Iterface Relatioships 'marker' iterface Remote class providig remote server object 'ifrastructure' exteds UicastRemoteObject stub variable declaratio Order implemets export object OrderCliet call via stub OrderImpl create OrderServer cliet server Prof.Dr.-Ig. Stefa Deßloch 11 Middleware for Heterogeeous ad Distributed Iformatio Systems
Java RMI Deploymet ad Rutime Deploymet geerate stub usig Java compiler ivoke server code for creatig ad registerig the server object Rutime ru the cliet applicatio issuig a server object lookup i the cliet applicatio will result i trasferrig a cliet stub object (implemetig the remote iterface) to the cliet applicatio stub class eeds to be loaded ito JVM o the cliet, either through local class path or dyamically over the etwork ivokig methods o the remote iterface will be carried out usig stubs/skeletos as discussed earlier Iterface Defiitio Laguage (Java) Java Compiler Stub/Proxy Prof.Dr.-Ig. Stefa Deßloch 12 Middleware for Heterogeeous ad Distributed Iformatio Systems
RPC Variatio 2: Stored Procedures Named persistet code to be ivoked i SQL, executed by the DBMS SQL CALL statemet Created directly i a DB schema Stored Procedure creatio requires header (sigature): cosists of a ame ad a (possibly empty) list of parameters. may specify parameter mode: IN, OUT, INOUT may retur result sets body (implemetatio): usig SQL procedural extesios or exteral programmig laguage (e.g., Java) Ivocatio of stored procedures usig CALL statemet through the usual DB access approaches (e.g., JDBC see CallableStatemet ) locatio-trasparecy is provided, but RPC itself is ot trasparet! geeric ivocatio mechaism, o stubs/skeletos ivolved) i the scope of a existig DB coectio, active trasactio Prof.Dr.-Ig. Stefa Deßloch 13 Middleware for Heterogeeous ad Distributed Iformatio Systems
RPCs ad Trasactios cliet Example sceario for T: debit/credit T ivokes debit procedure (ST1), modifyig DB1 T performs credit operatio o DBS2, modifyig DB2 Need trasactioal guaratees for T Program structure of T BOT CALL debit( ) CONNECT (DB2) UPDATE ACCOUNTS SET DISCONNECT EOT Requires coordiatio of distributed trasactio based o 2PC distributed TA ST1 DBS1 DB1 P T DBS2 DB2 presetatio applicatio logic resource maagemet Prof.Dr.-Ig. Stefa Deßloch 14 Middleware for Heterogeeous ad Distributed Iformatio Systems
Trasactioal RPC (TRPC) RPCs are issued i the cotext of a trasactio demarcatio (BOT, EOT) usually happes o the cliet TRPC-Stub like RPC-Stub additioal resposibilities for TA-orieted commuicatio TRPC requires the followig additioal steps bidig of RPC to trasactios usig TRID otifyig TA-Mgr about RM-Calls if performed through RPC (register participat of TA) bidig processes to trasactios: failures (crashes) resultig i process termiatio should be commuicated to the TA-Mgr Prof.Dr.-Ig. Stefa Deßloch 15 Middleware for Heterogeeous ad Distributed Iformatio Systems
X/OPEN Stadard for Distributed TA Processig Resource Maager recoverable supports exteral coordiatio of TAs usig 2PC protocol (XA-compliat) TA-Mgr coordiates, cotrols RMs Applicatio Program demarcates TA (TA-brackets) Applicatio ivokes RM services (AP) e.g., SQL-statemets i distributed eviromet: performs (T)RPCs TX-Iterface Trasactioal Cotext TRID geerated by TA-Mgr at BEGIN established at the cliet passed alog (trasitively) with RM-requests, RPCs begi commit/rollback request local eviromet register TA-Mgr (TM) prepare commit/rollback XA- Iterface Resource-Mgr (RM) Prof.Dr.-Ig. Stefa Deßloch 16 Middleware for Heterogeeous ad Distributed Iformatio Systems
Iteractios i a Local Eviromet 1. AP -> TM: begi establishes trasactio cotext, global id (TRID) for ToC 1. TM -> RM static registratio TM otifies frequetly used RMs about the ew global trasactio, RM ca associate future AP requests from ToC with the TRID 2. AP -> RM: request the RM 1. first dyamically registers with the TM to joi the global trasactio for ToC (uless RM uses static registratio), the 2. processes the AP request 3. AP -> TM: commit, rollback TM will iteract with RMs to complete the trasactio usig the 2PC protocol (prepare, commit/rollback) Thread of Cotrol (ToC) etity, with all its cotext (resources, etc.) that is i cotrol of the processor (e.g., a OS-thread) is associated with at most oe TRID at a time (but a global TA ca have multiple ToCs). A AP request is implicitly associated with a TRID through the curret ToC. Prof.Dr.-Ig. Stefa Deßloch 17 Middleware for Heterogeeous ad Distributed Iformatio Systems
X/OPEN DTP Distributed Eviromet "superior" "subordiate" begi commit/rollback TA-Mgr (TM) 2PC maage outgoig maage icomig TA-Mgr (TM) 2PC maage TA associatio Applicatio (AP) TRPC Comm.-Mgr (CM) Comm.-Mgr (CM) TRPC Server AP register register request request Resource-Mgr (RM) Resource-Mgr (RM) Commuicatio Maager (CM) provides trasactioal RPC support works with TMs o both sides to help maage global trasactios Remote TM assumes role of subordiate TM hierarchical 2PC processig Prof.Dr.-Ig. Stefa Deßloch 18 Middleware for Heterogeeous ad Distributed Iformatio Systems
Iteractios i a Distributed Eviromet Applicatio issues a (trasactioal) RPC (TRPC) 1. local CM iteracts with local TM to maage the outgoig TA TM otes TRID as extedig to the remote system TM otes local CM to be icluded i commit processig (2PC) local TM becomes the "superior" TA-Maager (later iitiates 2PC) 2. local CM commuicates RPC ad trasactioal cotext to remote CM 3. remote CM iteracts with remote TM to maage the icomig TA CM establishes TRID with remote TM, which becomes the "subordiate" for this TRID CM asks TM to associate TRID with ToC i which (remote) server applicatio will ru TM performs static registratio with remote RMs for TRID CM (server stub) ivokes server AP 4. server AP may issue (remote) RM requests (might cause dyamic RM registratio as usual) Applicatio issues a commit (or rollback) 1. superior TM drives hierarchical 2PC processig, ivolvig local CM just like RMs 2. local CM relays prepare/commit/rollback calls to remote CM, which i tur relays them to subordiate (remote) TM 3. subordiate TM coordiates with remote RMs for each phase Prof.Dr.-Ig. Stefa Deßloch 19 Middleware for Heterogeeous ad Distributed Iformatio Systems
Summary Remote Procedure Call importat core cocept for distributed IS RPC model is based o iterface defiitios usig IDL cliet stub (proxy), server stub (skeleto) for trasparet ivocatio of remote procedure bidig mechaism RPC Variatios Remote Method Ivocatio supported i object-based middleware (e.g., CORBA, Eterprise Java) Stored Procedures Trasactio support for RPCs distributed trasactio processig guaratees atomicity of global TA trasactioal RPC X/Ope DTP as foudatio for stadardized DTP variatios/ehacemets appear i object-based middleware (CORBA OTS, Java JTA/JTS) Prof.Dr.-Ig. Stefa Deßloch 20 Middleware for Heterogeeous ad Distributed Iformatio Systems
Appedix JAVA RMI EXAMPLE Prof.Dr.-Ig. Stefa Deßloch 21 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example Sceario: Pizza-Service Pizza id: OID ame: Strig price: float getprice setprice pizzas * * Order id: OID orderdate: Date deliverydate: Date create additem deliver cacel totalprice Item id: OID cout: it create delete * * 1 orderitems * igrediets orders * 1 Igrediet id: OID ame: Strig stock: it * * Customer id: OID ame: Strig create delete curretorder totalallorders 1 address 1 Address id: OID zip: it city: Strig street: Strig address 1 1 Supplier id: OID ame: Strig Prof.Dr.-Ig. Stefa Deßloch 22 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example Remote Service Iterface import java.rmi.*; import java.util.date; public iterface Order exteds Remote { public void additem(it pizzaid, it umber) throws RemoteExceptio; public Date getdeliverydate() throws RemoteExceptio; public Date setdeliverydate (Date ewdate) throws RemoteExceptio; } Prof.Dr.-Ig. Stefa Deßloch 23 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example Server Class Implemetatio...... import java.rmi.*; import java.rmi.registry.locateregistry import java.rmi.registry.registry import java.rmi.server.uicastremoteobject; import java.util.*; public class OrderImpl implemets Order { private Vector fitems; private Date fdeliverydate; public OrderImpl(Strig ame) throws RemoteExceptio { 'export' Order object for acceptig requests register with ame server Prof.Dr.-Ig. Stefa Deßloch super(); try { Order stub = (Order) UicastRemoteObject.exportObject(this, 0); Registry Namig = LocateRegistry.getRegistry(); Namig.rebid(ame, stub); fitems = ew Vector(); fdeliverydate = ull; } catch (Exceptio e) { System.err.pritl( Output: + e.getmessage()); e.pritstacktrace(); } } 24 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example Server Class (cotiued)... public void additem(it pizzaid, it umber ) throws RemoteExceptio { // assumig class Item is kow Item item = ew Item(pizzaId, umber); fitems.addelemet(item); }... // Impl. of other methods } Prof.Dr.-Ig. Stefa Deßloch 25 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example Server... remote object import java.rmi.*; ame (later used import java.server.*; i cliet lookup) public class OrderServer { public static void mai(strig args[]) { try { OrderImpl order = ew OrderImpl( my_order ); System.out.pritl( Order server is ruig ); } catch (Exceptio e) { System.err.pritl( Exceptio: + e.getmessage()); e.pritstacktrace(); } } } Prof.Dr.-Ig. Stefa Deßloch 26 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example Cliet Program... import java.rmi.*; public class OrderCliet { public static void Mai(Strig args[]) { try { returs a istace of the stub class (geerated from the remote Order iterface) Registry Namig = Locate Registry.getRegistry(args[0]); Order order = (Order) Namig.lookup("my_order"); it pizzaid = Iteger.parseIt(args[0]); it umber = Iteger.parseIt(args[1]); order.additem(pizzaid, umber); } catch (Exceptio e) { System.err.pritl( system error: + e); } } } Prof.Dr.-Ig. Stefa Deßloch 27 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example Compile, Geerate Stub, Ru Compile will also geerate stubs: javac Order.java OrderImpl.java OrderCliet.java OrderServer.java Admiistrative steps: Start directory server: rmiregistry Start RMI-Servers: java OrderServer Ru cliets: java OrderCliet Prof.Dr.-Ig. Stefa Deßloch 28 Middleware for Heterogeeous ad Distributed Iformatio Systems