1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al,...

35
1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen

Transcript of 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al,...

Page 1: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

1

Distribuerede systemer – 12. februar 2001

Presentation based on slides by Coulouris et al,modified by Jens B Jorgensen

Page 2: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

2

Chapter 4: Interprocess Communication

From Coulouris, Dollimore and Kindberg

Distributed Systems: Concepts and Design

Edition 3, © Addison-Wesley 2001

Page 3: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 3

Interprocess communication - basics

Integrating communication into a programming language paradigm.

TCP and UDP provide application program interfaces to communication.

RMI - Remote method invocation – allows an object to invoke a method in an object in a remote process.

RPC – Remote procedure call – allows a client to call a procedure in a remote server.

Page 4: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 4

Interprocess communication - middleware layers

Applications, services

Middlewarelayers

request-reply protocol

marshalling and external data representation

UDP and TCP

Thischapter

RMI and RPC

Page 5: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 5

Interprocess communication – how?

Message passing between a pair of processes can be supported by two message communication operations: send and receive.

Involves: Communication of data. Synchronization of the two processes (possibly).

Queue associated with each message destination.Senders cause messages to be added to remote

queues.Receivers remove messages from local queues. Issues: Reliability and ordering.

Page 6: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 6

Interprocess communication – communication patterns

Synchronous communication: The sending and receiving processes synchronize at every message, the send and receive operations are blocking.

Asynchronous communication: The send operation is non-blocking, the receive operation may be either blocking or non-blocking.

Page 7: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 7

Interprocess communication – ports and sockets (1)

Messages are sent to (Internet address, local port) pairs.

Local port: Message destination within a computer, specified as an integer.

Socket: Endpoint for communication between processes.

Interprocess communication: Transmission of a message between a socket in one process and a socket in another process.

Sockets are bound to ports.

Page 8: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 8

Interprocess communication – ports and sockets (2)

message

agreed portany port

socketsocket

Internet address = 138.37.88.249Internet address = 138.37.94.248

other ports

client server

• Any process may use multiple ports to receive messages.• A process cannot share ports with other processes on the same computer.• Each socket is associated with either UDP or TCP.

Page 9: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 9

UDP – User Datagram Protocol

A datagram sent by UDP is transmitted from a sending process to a receiving process without acknowledgements or retries.

A process which will apply UDP must create a socket and bind it to a local port.

A receiving process needs to specify an array of bytes in which to receive a message.

Page 10: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 10

UDP – properties

Non-blocking send operation.Blocking receive operation.Timeouts may be applied.Failure model:

Omission failures. Ordering.

Examples of use: DNS.

Page 11: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 11

UDP – Java API

DatagramPacket: Array of bytes containing message. Length of message. Internet address. Port number. Methods: new, getData, getLength, getPort, getAddress.

DatagramSocket: Methods: new, send, receive, …

Page 12: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 12

UDP - client sends a message to the server and gets a reply

import java.net.*;import java.io.*;public class UDPClient{ public static void main(String args[]){

// args give message contents and server hostname try {

DatagramSocket aSocket = new DatagramSocket(); byte [] m = args[0].getBytes();InetAddress aHost = InetAddress.getByName(args[1]);int serverPort = 6789; DatagramPacket request = new DatagramPacket(m, args[0].length(), aHost, serverPort);aSocket.send(request); byte[] buffer = new byte[1000];DatagramPacket reply = new DatagramPacket(buffer, buffer.length);aSocket.receive(reply);System.out.println("Reply: " + new String(reply.getData()));aSocket.close();

}catch (SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e){System.out.println("IO: " + e.getMessage());}

} }

Page 13: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 13

UDP - server repeatedly receives a request and sends it back to the client

import java.net.*;import java.io.*;public class UDPServer{

public static void main(String args[]){ try{ DatagramSocket aSocket = new DatagramSocket(6789);

byte[] buffer = new byte[1000]; while(true){ DatagramPacket request = new DatagramPacket(buffer, buffer.length); aSocket.receive(request); DatagramPacket reply = new DatagramPacket(request.getData(),

request.getLength(), request.getAddress(), request.getPort()); aSocket.send(reply);}

}catch (SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e) {System.out.println("IO: " + e.getMessage());}

}}

Page 14: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 14

TCP – Transmission Control Protocol

TCP provides the abstraction of a stream of bytes to which data may be written and from which data may be read.

TCP uses: connect operation. accept operations.

The client role involves creating a stream socket bound to any port and then making a connect.

The server role involves creating a listening socket bound to a server port.

Upon accept, a connection between client and server established using a new socket.

Page 15: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 15

TCP - properties

TCP hides: Message sizes. Lost messages. Flow control. Message duplication and ordering. Message destinations.

Failure model: Ensures (almost always) integrity and reliability. Cannot survive all situations.

Examples of use: HTTP, FTP, Telnet, SMTP.

Page 16: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 16

External data representation and marshalling - basics

Motivation: Information in programs represented as data structures, information in messages consists of sequences of bytes.

Issues: Character sets - ASCII vs. Unicode. Byte order – big-endian vs. little-endian.

Two metods for data exchange: Convert values to agreed external format before

transmission. Transmit values in sender’s format.

Page 17: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 17

External data representation and marshalling - definitions

External data representation: An agreed standard for the representation of data structures and primitive values.

Marshalling: The process of taking a collection of data items and assembling them into a form suitable for transmission in a message.

Unmarshalling: The process of disassembling data on arrival to produce an equivalent collection of data items at the destination.

Page 18: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 18

External data representation and marshalling - approaches

CORBA’s common data representation (CDR).Java’s object serialization.Marshalling and unmarshalling activities intended to

be carried out by a middleware layer without any involvement on the part of the application programmer.

Page 19: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 19

CORBA CDR

Type Representation

sequence length (unsigned long) followed by elements in orderstring length (unsigned long) followed by characters in order (can also

can have wide characters)array array elements in order (no length specified because it is fixed)struct in the order of declaration of the componentsenumerated unsigned long (the values are specified by the order declared)union type tag followed by the selected member

Constructed types:

15 primitive types: short, long, unsigned short, …char, boolean, …

CDR can represent all of the data types that can be used asarguments and return values in remote invocations in CORBA.

Page 20: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 20

CORBA CDR message

The flattened form represents a Person struct with value: {‘Smith’, ‘London’, 1934}

0–34–78–1112–15

16–19

20-23

24–27

5

"Smit""h___"

6"Lond""on__"

1934

index in sequence of bytes 4 bytes

notes on representation

length of string

‘Smith’

length of string

‘London’

unsigned long

Marshalling operations can be generated automatically fromthe spec of the types of data items to be transmitted; typesDescribed by CORBA IDL (interface definition language).

Page 21: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 21

Java object serialization

In Java RMI, both objects and primitive data values may be passed as arguments and results of method invocation.

Serialization: Flattening an object or a connected set of objects into a serial form.

Unserialization: The reverse. Info about class of each object necessary.References serialized as handles.Reflection makes it possible to do serialization and

unserialization in a generic manner.

Page 22: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 22

Request-reply protocol - basics

Client-server communication in terms of the send and receive operations in the Java API for UDP datagrams.

Uses remote object references, identifiers for a remote object that is valid throughout a distributed system.

Communication primitives: doOperation. getRequest. sendReply.

Page 23: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 23

Request-reply protocol - operations

public byte[] doOperation (RemoteObjectRef o, int methodId, byte[] arguments)sends a request message to the remote object and returns the reply. The arguments specify the remote object, the method to be invoked and the arguments of that method.

public byte[] getRequest ();acquires a client request via the server port.

public void sendReply (byte[] reply, InetAddress clientHost, int clientPort); sends the reply message reply to the client at its Internet address and port.

Page 24: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 24

Request-reply protocol - communication

Request

ServerClient

doOperation

(wait)

(continuation)

Replymessage

getRequest

execute

method

messageselect object

sendReply

Page 25: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 25

Request-reply protocol - message structure

messageType

requestId

objectReference

methodId

arguments

int (0=Request, 1= Reply)

int

RemoteObjectRef

int or Method

array of bytes

Page 26: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 26

Request-reply protocol - failure model

Problems: Omission failures. No guarantee of delivery in order.

Timeouts.Discarding duplicate request messages.Lost reply messages.History.

Page 27: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 27

HTTP – HyperText Transfer Protocol

HTTP used by web browsers to make requests to web servers and to receive replies from them.

Client requests specify a URL that includes the DNS hostname of a web server, an optional port number and an identification of a resource.

HTTP specifies: Messages. Methods. Arguments. Results. Rules for representing data in messages.

Page 28: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 28

HTTP - methods

GET: Requests the resource whose URL is given as argument.

HEAD: Similar to GET, but no data returned.POST: Can deal with data supplied with the request.PUT: Can be used to store data.DELETE: Server deletes resource.OPTIONS: Server supplied client with list of

methods.TRACE: Server sends the request back.

Page 29: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 29

HTTP - request message and reply message

GET //www.dcs.qmw.ac.uk/index.html HTTP/ 1.1

URL or pathnamemethod HTTP version headers message body

HTTP/1.1 200 OK resource data

HTTP version status code reason headers message body

Request:

Reply:

Page 30: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 30

Group communication - basics

Multicast operation: An operation that sends a single message from one process to each of the members of a group of processes.

Useful for: Fault tolerance based on replicated services. Finding the discovery servers in spontaneous networking. Better performance through replicated data. Propagation of event notification.

Page 31: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 31

Group communication – IP multicast

IP multicast is built on top of IP. IP multicast allows the sender to transmit a single IP

packet to a set of computers that form a multicast group.

A multicast group is specified by a class D internet address.

At the application level, IP multicast is available only via UDP.

Failure model: Unreliable.

Page 32: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 32

IP multicast - Java API

MulticastSocket (subclass of DatagramSocket), adding capability to join and leave multicast groups.

Page 33: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 33

IP multicast - multicast peer joins a group and sends and receives datagrams (1)

import java.net.*;import java.io.*;public class MulticastPeer{

public static void main(String args[]){ // args give message contents & destination multicast group (e.g. "228.5.6.7")

try { InetAddress group = InetAddress.getByName(args[1]); MulticastSocket s = new MulticastSocket(6789); s.joinGroup(group);

byte [] m = args[0].getBytes(); DatagramPacket messageOut =

new DatagramPacket(m, m.length, group, 6789); s.send(messageOut);

// this figure continued on the next slide

Page 34: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 34

IP multicast - multicast peer joins a group and sends and receives datagrams (2 - continued)

// get messages from others in group byte[] buffer = new byte[1000];

for(int i=0; i< 3; i++) { DatagramPacket messageIn =

new DatagramPacket(buffer, buffer.length); s.receive(messageIn); System.out.println("Received:" + new String(messageIn.getData())); }

s.leaveGroup(group); }catch (SocketException e){System.out.println("Socket: " + e.getMessage());

}catch (IOException e){System.out.println("IO: " + e.getMessage());} } }

Page 35: 1 Distribuerede systemer – 12. februar 2001 Presentation based on slides by Coulouris et al, modified by Jens B Jorgensen.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3

© Addison-Wesley Publishers 2000 35

Summary

Introduction to interprocess communication.Ports and sockets.UDP.TCP.External data representation, marshalling; CORBA

CDR, Java object serialization.Request-reply protocol.HTTP.Group communication.