2 Middleware RPC RMI
2 Middleware RPC RMI
Michael Eichberg
Middleware (Technologies)
Remoting - RPC and RMI
Middleware
Remoting? (A Denition)
Remoting allows a computer program to cause a subroutine or procedure to execute in another address space (commonly on another computer on a shared network) without the programmer explicitly coding the details for this remote interaction.
Middleware
What is Middleware?
Middleware
What is Middleware?
Middleware is a class of software technologies designed to help (I) manage the complexity and (II) heterogeneity inherent in distributed systems.
void error(char *msg){perror(msg); exit(1);} int main(int argc, char *argv[]){ int sockfd, newsockfd, portno, clilen; char buffer[256]; int n; struct sockaddr_in serv_addr, cli_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); // socket() returns a socket descriptor if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); // bzero() sets all values in a buffer to zero. portno = atoi(argv[1]); // atoi() converts str into an integer ...
void error(char *msg){ perror(msg);exit(0);} int main(int argc, char *argv[]){ int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); ...
Middleware
establishing a channel and of all errors that might happen during this
process
Middleware
10
establishing a channel and of all errors that might happen during this We want:
improved language integration, and ... establishing a protocol generation of most of the (Who sends what, when, in which order and what answer is
expected.)
process
networking code
Middleware
11
Distributed Application
Distributed Application
Operating System API Operating System (Processes, Communication, Memory Management) Network
Middleware
12
Programming abstractions offered by middleware mask some of the heterogeneity and handles some of the complexity programmers of a distributed application must deal with:
Middleware
13
Middleware provides transparency with respect to one or more of the following dimensions:
Middleware
14
An operating system (OS) is the software that makes the hardware useable
(A bare computer without an OS could be programmed with great difculty.)
Middleware
15
Middleware as Infrastructure
The additional software layers increase the size and complexity of the
infrastructure necessary to use the new abstractions
Middleware
16
Middleware as Infrastructure
The infrastructure is also intended to support additional functionality that makes development, maintenance, and monitoring easier and less costly:
logging, recovery, advanced transaction models (e.g. transactional RPC), language primitives for transactional demarcation, transactional le system, etc.
Middleware
17
Middleware as Infrastructure
The infrastructure also takes care of (all) the non-functional properties typically ignored by data models, programming models, and programming languages:
Middleware
18
Middleware as Infrastructure
Conceptual Model
client process client code development environment IDL server process server code
IDL sources
IDL compiler
RPC protocols
security service
distributed le service
thread service
[Alonso; Web services: Concepts, Architectures and Applications; Springer Verlag, 2004]
Middleware
19
g in m n m io ra ct og tra Pr s
Understanding Middleware
Intended to hide low level details of hardware, networks, and distribution Continuing trend towards increasingly more powerful primitives that
without changing the basic concept of RPC have additional properties or allow more exibility in the use of the concept
Ab
RPC and C CORBA and C++ RMI (Corba) and Java Classical Web Services and XML RESTful Web Services and JSON
Middleware
20
Understanding Middleware
In
Comprehensive platform for developing and running complex distributed
systems
ru st fra u ct re
21
22
Emphasis is on hiding network communication by allowing a process to call a procedure of which the implementation is located on a remote machine:
Removing the need for the distributed systems programmer to worry about
all the details of network programming (i.e., no more sockets)
23
Procedural languages (mainly C) were dominant In procedural languages, the basic module is the
procedure (A procedure implements a particular function or service that can be used anywhere within the program.)
24
Request
Machine B (Client)
Communication is by sending
messages (no shared memory, no shared disks, etc.)
Response
25
How to make the service invocation part of the language in a more or less
transparent manner?
26
Different ways to order bytes: Intel CPUs are little-endian Sun Sparc CPUs are big-endian
27
Problem:
Information in running programs is stored in high-level data structures; Information in messages passed over network consists of sequences of bytes
Complex data types must be attened: Marshalling - the process of assembling data items into a form suitable
for transmission in a message
28
Problem:
Information in running programs is stored in high-level data structures; Information in messages passed over network consists of sequences of bytes
Two methods for exchanging data values: Values converted to an agreed external format before transmission
and converted to the local form on receipt
29
How to nd and bind the service one actually wants among a potentially
large collection of services and servers? The goal is that the client does not necessarily need to know where the server resides or even which server provides the service (location transparency).
30
How to deal with errors in the service invocation in a more or less elegant
manner:
31
A. The stack before the call B. The stack while the called procedure is active
How to identify and address entities? Pointers cannot be passed as parameters Each machine has a different address space
A Main program's variables bytes buf fd return address "read's" local variables B
32
Interface description language (IDL) Directory and naming services Dynamic binding Marshalling and unmarshalling Opaque references
Opaque references need to be used by the client to refer to the same data structure or entity at the server across different calls. (The server is responsible for providing these opaque references.)
33
Client stub
Implementation of the interface on the local machine through which the remote functionality can be invoked
Server stub
Implementation of the interface on the server side passing control to the remote procedure that implements the actual functionality
34
client process
server process
communication module
[Alonso; Web services: Concepts, Architectures and Applications; Springer Verlag, 2004]
35
SUN RPC (see RFC 1831, 1014) uses XDR CORBAs IDL is called IDL (ISO 14750) JAXRPC uses XML ... The interfaces are then compiled by an IDL compiler which generates the
necessary stubs (and skeletons)
36
IDL sources
IDL compiler
interface headers
[Alonso; Web services: Concepts, Architectures and Applications; Springer Verlag, 2004]
37
38
39
main(int argc, char ** argv) { CLIENT *clientHandle; char *serverName = "coffee"; readargs a; Data *data; /* creates socket and a client handle */ clientHandle= clnt_create(serverName, FILEREADWRITE, VERSION, "udp"); if (clientHandle != NULL){ a.f = 10; a.position = 100; a.length = 1000; /* call to remote read procedure */ data = read_2(&a, clientHandle); clnt_destroy(clientHandle); /* closes socket */ } }
e or m s! et No ck so
40
Binding in RPC
Overview
Binding is the process whereby a client creates a local association for (i.e.,
a handle to) a given server in order to invoke a remote procedure (Form of handle depends on environment. Today, usually an IP address and port number. Alternatives: an Ethernet address, an X.500 address, etc.)
Static binding (local binding): the handle to the server where the
procedure resides is hard-coded in client stub
41
This service must be reachable by all participants; possible alternatives: predened location environment variables e.g., Apples Bonjour broadcasting to all nodes looking for the binder Uses IDL specications to perform bindings
42
client procedure call client stub bind marshal serialize 2. nd 5. send 3. query for server implementing the procedure
server procedure server stub 0. register unmarshal deserialize 7. receive 6. invoke procedure
communication module
[Alonso; Web services: Concepts, Architectures and Applications; Springer Verlag, 2004]
43
Disadvantages Client and server tightly coupled If the server changes location, client
must be changed
44
After a time-out expires, the client may decide to resend the request If after several tries there is no success, what may have happened
depends on the call semantics:
45
46
At least-once
The procedure will be executed if the server does not fail, but it is possible that it is executed more than once
This may happen, for instance, if the client re-sends the request after a
time-out
If the server is designed so that service calls are idempotent (each call
results in the same outcome given the same input), this might be acceptable
At most-once
The procedure will be executed either once or not at all. Re-sending the request will not result in the procedure executing several times
47
Exactly once
The system guarantees the local semantics assuming that a server machine that crashes will eventually restart. It keeps track of orphan calls, that is, calls on server machines that have crashed, and allows them to be later adopted by a new server.
48
Asynchronous RPC
client wait for result request A reply server call local procedure and return results time
A. The interconnection between client and server in a traditional RPC note that blocking occurs the client waits
client
B. The interaction using asynchronous RPC no blocking; useful when the client doesnt need or expect a result
accept request
49
client
interrupt client
accept request
50
Stored Procedures
Based Upon RPCs
To add exibility to their servers, (database) software vendors added to them the possibility of programming procedures that will run inside the server and that could be invoked through RPC. Turned out to be very useful for databases:
(Stored) procedures could be used to hide the schema and the SQL
programming from the clients
51
Distributed Environments
Based Upon RPCs
Common aspects: Security Services for Authentication / Authorization, Replication, Logging, Transaction Handling,...
52
Client and server are separate entities The server encapsulates and hides the details of the back end systems
(such as databases)
53
RPC allows designers to build distributed systems but does not solve many
of the problems distribution creates (In that regard, it is only a low-level construct.)
54
55
Allows an object running in one Java Virtual Machine (VM) to invoke methods on an object running in another Java VM...
Remote objects can be treated similarly to local objects Handles marshalling, transportation, and garbage collection of the remote
objects
56
RMI is Middleware
57
RMI is Middleware
58
server
RPC mechanism
foo ...
59
remote invocation
:F
60
remote invocation
:C
local invocations :E
local invocation :D
remote invocation
:F
61
Client computer
Client
an object
implements the
remote interface
Client invokes method The same interface
marshals requests
and unmarshals results
Proxy Skeleton Client OS Server OS
forwards request
Marshalled invocation is passed across network
62
an object
unmarshals requests
and marshals results
invokes method in
remote object
Proxy
Skeleton
Client OS
Server OS
63
Can be passed freely between processes (e.g. as a parameter) The implementation of the remote object references is hidden by the
middleware (Opaque references.)
64
Interface Language
65
RMI Compiler (rmic) generates stub and skeleton classes Either static binding or dynamic binding via Naming.lookup Dynamic binding is supported by the rmiregistry
Starts a registry process for registering server objects Enables binding of objects to names
66
An Example Application
Clients invoke a method getTime() on a remote object The server returns a java.util.Date instance representing the current
server time
67
1. Dene the remote interface 2. Implement the RMI server 3. (Before Java 5) Generate RMI stubs (and if you use Java 1.1 skeletons) 4. Implement a server registrar 5. Implement an RMI client
68
69
70
71
72
73
1. Start rmiregistry tool on server 2. Start TimeRegistrar on server 3. Start TimeClient on client 4. The Time interface need to be available on both server and client (Code downloading is - by default - not enabled.)
74
RMI uses a reference-counting garbage collection algorithm When a remote object is not referenced by any client, the RMI runtime
refers to it using a weak reference (Allows the JVM's GC to discard the object (if no other local references exist.)
75
Failure Handling
(All methods in the remote interface have to declare that they may throw this exception.)
Short-Term Persistence
76
How is the data transfered between the client and the server?
(I.e., what happens when we sent a Date object from the server to the client?)
Short-Term Persistence
77
seller
bidder auctions bids item successful bid bids Bid oat amount Date datetime
categories items
Short-Term Persistence
Java Serialization
Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time. The Java Serialization API provides a standard mechanism for developers to handle object serialization.
Short-Term Persistence
79
Java Serialization
The API denes a default protocol, which can also be customized... by implementing
readObject(ObjectInputStream in)
and
writeObject(ObjectOutputStream out),
Short-Term Persistence
80
Serializing an AuctionItem
Java Serialization - Example
Short-Term Persistence
81
Serializing an AuctionItem
Java Serialization - Example
public class SerializationTest { } private AuctionItem item; void saveItem() throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream("AuctionItem.ser"); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(item); out.flush(); } finally { if (fos != null) fos.close(); } } ...
Short-Term Persistence
82
Deserializing an AuctionItem
Java Serialization - Example
public class SerializationTest { ... } AuctionItem loadItem() throws IOException, ClassNotFoundException { } ... FileInputStream fin = null; try { fin = new FileInputStream("AuctionItem.ser"); ObjectInputStream in = new ObjectInputStream(fin); item = (AuctionItem) in.readObject(); } finally { if (fin != null) fin.close(); } return item;
Short-Term Persistence
83
Java Serialization
Summary
Pros:
Built into the Java language no libraries necessary Ability to persist graph of objects, not just single ones Can also handle reference circles automatically Low implementation effort: just use the marker interface Serializable mark non-persistent elds as transient Default protocol can be overwritten (customized)
Short-Term Persistence
84
Java Serialization
Summary
Cons:
Usually, stored in binary format other (non-Java) applications cannot
access the data; not recommended for long term storage (if a class evolves the le format will break)
Short-Term Persistence
85
Java Serialization
Summary
Conclusions:
Serialization can do a good job for persisting data only needed by a single
application which always needs the full data set and where the data will not be stored for long
Short-Term Persistence
86
Short-Term Persistence
87
Short-Term Persistence
88
object CustomersClient extends App { import java.rmi.Naming.lookup val host = "localhost" // args(0) val ca = lookup("rmi://" + host + "/Customers").asInstanceOf[CustomerAccess]; println("customers: " + ca.customers) }
89
Both primitive data types and objects can be serialized The receiver has no prior knowledge of the types of the objects in the
serialized form
Supports reection (the ability to enquire about the properties of a class) Objects are prepared for transport with Java serialization using
java.io.Serializable
No problems with conversions - advantage of being Java-only Overhead: accounts for ~25%-50% of the time for a remote invocation
90
Marshalling of Objects
91
RMI Server
RMI skeleton
Internet Protocol
Data transfer contained in IP packets (lowest level)
92
RMI Interface
Denes what operations are made available to clients; the only class the client must know
RMI Client
RMI interface RMI stub
RMI Server
RMI Stub
Implements the interface; takes calls from the client; performs marshaling / unmarshaling
RMI skeleton
RMI Skeleton
Calls the server methods; performs unmarshaling / marshaling
93
RMI Client
RMI Server
RMI Server
Implements the interface; actually provides dened functionality
RMI skeleton
RMI Client
Some application relying on the interface; only needs to have the interface in its class path
94
RMI Capabilities
Parameter passing either by reference or by value: Primitive values and serializable objects are passed by value Remote objects are passed by reference Dynamic loading of unknown classes
A serialized object is automatically annotated with a descriptor (e.g., URL) where the byte code of its class can be downloaded.
95
RMI Packages
java.rmi
Core package
java.rmi.server
Core package for server-side classes and interfaces
java.rmi.registry
For interfacing with the RMI lookup service
java.rmi.activation
Classes and interfaces for implementing server objects that can be activated upon client request
java.rmi.dgc
Distributed garbage collection
96
97
98
Subtype polymorphism can be used to deal with implementation variants With RPC, basically only primitive types can be passed RMI enables behavior mobility
(Both data and code can be transferred from client to server and vice versa.)
99
TP-Monitors
Transactional RPC
Asynchronous RPC
Specialized RPC, typically with additional functionality or properties but almost always running on RPC platforms. Remote Procedure Call: hides communication details behind a procedure call and helps bridge heterogeneous platforms. Sockets: operating system level interface to the underlying communication protocols TCP, UDP: User Datagram Protocol (UDP) transports data packets without guarantees. Transmission Control Protocol (TCP) veries correct delivery of data streams. Internet Protocol (IP): moves a packet of data from one node to another
Sockets
TCP, UDP
Middleware
100
Historic Development
DNA enthalten in COM+ COM DCOM MTS TPM Mainframe TPM Server Mom Server ORB Server Grundlage fr CORBA Standard J2EE Standard
.NET
}
1998
zusammengefasst zu
1960
1970
1980
1990
1995
2000
2001
2002