ECE761: Networking and Programming

Download Report

Transcript ECE761: Networking and Programming

Message-Oriented Persistent
Communication
© C Xu, 1998,2007
Organization of Comm System
Examples:
Email --- Client/Outbound Mail Server/Inbound Server
MPI --- Message Passing Layer
Difference??
© C Xu, 1998,2007
Another Example of Persistent Comm
 Persistent
communication of letters back in the
days of the Pony Express.
Although the means of transportation and the means by which letters
are sorted have changed, the mail principle remains the same
© C Xu, 1998,2007
Persistence and Synchronicity
 Persistent
vs Transient Communication
– Persistent: submitted messages are stored by the comm.
system as long as it takes to deliver it the receiver
– Transient: messages are stored by the comm system as
long as the sending and receiving app. are executing.
 Asynchronous
vs synchronous
– Sync: a client is blocked until its message is stored in a
local buffer at the receiving host, or actually delivered
to the receiver
– Async: A sender continues immediately after it has
submitted its message for transmission. (The message
is stored in a local buffer at the sending host, or
otherwise at the first communication server.
© C Xu, 1998,2007
a)
Persistent asynchronous communication
E.g. Email
b)
Persistent synchronous communication (??) (a
weaker form: when the sender is blocked until its
message is stored at receiver-side comm server )
© C Xu, 1998,2007
2-22.2
c)
d)
Transient asynchronous communication (e.g. UDP,
asynchronous RPC). Transmission fails if the receiver
is not executing at the time the msg arrives.
Receipt-based transient synchronous communication .
Sender is blocked until the msg is stored in a local
buffer at the receiving host. (Example?)
© C Xu, 1998,2007
e)
f)
Delivery-based transient synchronous communication at
message delivery. (Sender is blocked until the msg is delivered
to the receiver)
Response-based transient synchronous communication (e.g.
RPC and RMI) (Sender is blocked until it receives a reply msg
from the receiver)
© C Xu, 1998,2007
Different Forms of Communication
 Persistent
Asynchronous
 Persistent Synchronous
 Transient Asynchronous
 Transient Synchronous
– Receipt-based (weakest, e.g. async. RPC)
– Delivery-based
– Response-based (strongest, e.g. RPC)
 Need
for persistent comm. in middleware, largescale system
– Components of a large scale distributed system may
not always be immediately accessible
– Failure should be masked with a recovery procedure
– But, persistent comm. incurs long delays
© C Xu, 1998,2007
Message-Oriented Transient Comm.


Socket is a communication endpoint to which an appl can write data to
and from which incoming data can be read
Socket primitives for TCP/IP.
Primitive
Meaning
Socket
Create a new communication endpoint
Bind
Attach a local address to a socket
Listen
Announce willingness to accept connections
Accept
Block caller until a connection request arrives
Connect
Actively attempt to establish a connection
Send
Send some data over the connection
Receive
Receive some data over the connection
Close
Release the connection
© C Xu, 1998,2007
Berkeley Sockets

Connection-oriented communication pattern using sockets.
© C Xu, 1998,2007
MPI: Another Transient Comm

Basic message-passing primitives of MPI.
Primitive
Meaning
MPI_bsend
Append outgoing message to a local send buffer of MPI runtime
MPI_send
Send a message and wait until copied to local or remote buffer
MPI_ssend
Send a message and wait until receipt starts
MPI_sendrecv
Send a message and wait for reply
MPI_isend
Pass reference to outgoing message, and continue
MPI_issend
Pass reference to outgoing message, and wait until receipt starts
MPI_recv
Receive a message; block if there are none
MPI_irecv
Check if there is an incoming message, but do not block
© C Xu, 1998,2007
Synchrony in MPI
MPI_bsend  Transient synchronous
 MPI_send (blocking send)

– Block the caller until the msg be copied to MPI runtime
system at the receiver’s side;  Receipt-based
transient comm
– or until the receiver has initiated a receive operation
delivery-based transient comm
MPI_ssend  delivery-based
 MPI_sendrecv  response-based

© C Xu, 1998,2007
Message Queuing Model
MQ: a software-engineering component used for
interprocess comm. It utilizes a queue for
messaging—the passing of control or of content.
[Wikipedia]
 MQ provides a Persistent Asynchronous comm
protocol

– Sender and receiver do not need to connect to the msg
queue at the same time; msgs placed on the queue are
stored until the recipient retrieves them.
– Most msg queues have set limits on the size of data that
can be transmitted in a single msg. Those with no such
limits are often referred to as mailboxes.
© C Xu, 1998,2007
MQ Model
2-26

Four combinations for loosely-coupled comm. using queues
(sender and receiver can execute completely independently of
each other)
© C Xu, 1998,2007
MQ: Interface to a Queue
One-way Communication Primitives:
Primitive
Meaning
Put
Append a message to a specified queue (non-blocking)
Get
Block until the specified queue is nonempty, and remove the first message
Poll
Check a specified queue for messages, and remove the first. Never block.
Notify
Install a handler to be called when a message is put into the specified
queue.
• Addressing: System wide unique destination queue
• Active message: The handler to be executed on arrival at the dest.
• Mobile object: messages in principle contains any data. How
about objects?
© C Xu, 1998,2007
Msg Oriented Middleware (MOM)
 Many
impl function internally in OS or appl
– In UNIX: msgctrl(), msgget(), msgsnd()
 Distributed
impl across different systems for msg
passing
– Enhanced fault resilient: msgs don’t get lost even in the
event of a system failure.
 Examples:
–
–
–
–
–
IBM’s WebSphere MQ (MQ series),
Oracle Advaced Queueing (AQ)
Microsoft’s MSMQ
Sun’s Java API: Java Message Service (JMS) since 2005
Amazon’s Simple Queue Service for building automated
workflow (aws.amazon.com/sqs)
© C Xu, 1998,2007
MOM (Cont’)
Msgs are forwarded over comm servers and
eventually delivered to dest, even if it was down
when the msg was sent.
 Each app has its own queue and a queue can be read
only by its assoc. app.
 Multiple apps can share a single queue
 Guaranteed that a msg will eventually be inserted in
the recipient’s queue, but no guarantee about when,
or if the msg will actually be read.

– Immediate-term storage capacity for messages

Messaging domain:
– Point-to-point vs publish/subscribe
© C Xu, 1998,2007
General Architecture:

Source Queue, Destination Queue, or 3rd-party messaging service (Amazon)
Mapping of queue-level addressing to network-level addressing. (analogous
to DNS in email system)
Queue manager

Special manager working as routers: forward msg between queue managers


© C Xu, 1998,2007
Queue Manager, Router, and Overlay Network
2-29
• Static vs dynamic routing with routers
• Multicasting with routers
© C Xu, 1998,2007
Message Brokers



(e.g. Amazon’s SQS
Integration of legend applications with new ones to form a single coherent
distributed information system
Message broker acts as an appl-level gateway to convert incoming msg to
a format that can be understood by the dest app.
The general organization of a message broker in a msg-queuing system.
© C Xu, 1998,2007
Amazon Simple Queue Service
Amazon’s SQS offers a reliable, highly scalable,
hosted queue for storing messages as they travel
between computers. (aws.amazon.com/sqs)
 By using Amazon SQS, developers can simply move
data between distributed components of their
applications that perform different tasks, without
losing messages or requiring each component to be
always available.
 Make it easy to build an automated workflow
 Pricing: $0.01 for 10K SQS requests.

© C Xu, 1998,2007
Basic Queue Requests








CreateQueue: Create queues for use with your AWS account.
ListQueues: List your existing queues.
DeleteQueue: Delete one of your queues.
SendMessage: Add any data entries to a specified queue.
ReceiveMessage: Return one or more messages from a specified queue.
DeleteMessage: Remove a previously received message from a
specified queue.
SetQueueAttributes: Control queue settings like the amount of time
that messages are locked after being read so they cannot be read again.
GetQueueAttributes: See information about a queue like the number of
messages in it.
© C Xu, 1998,2007
Example: IBM WebSphere MQ




Launched in 1992, previously known as MQ series
De-facto standard for messaging across platforms
Queue Manager: handles storage, timing issues, triggering, and all
other functions not directly actual movement of data
QM comm with outside via local Binding, or remote Client connection
© C Xu, 1998,2007
Message Channels


MC is a unidirectional, reliable connection between a
sending and a receiving queue manager
Each end is managed by a Message Channel Agent (MCA)
– check send queue, wrap messages into transport-level packets, send
to associated receiving MCA
– listening for incoming packets, unwrap them, store into queues
Attribute
Description
Transport type
Determines the transport protocol to be used
FIFO delivery
Indicates that messages are to be delivered in the order they are sent
Message length
Maximum length of a single message
Setup retry
count
Specifies maximum number of retries to start up the remote MCA
Delivery retries
Maximum times MCA will try to put received message into queue
© C Xu, 1998,2007
Message Transfer




Address: queue manager + destination queue;
Each queue manager has a system-wide unique id; Local aliases
for queue manager names
Routing tables: (Dest, SendQueue)
Transfer along a channel can take place only if both MCAs are
up and runing
© C Xu, 1998,2007
Message Queue Interface

Primitives available in an MQSeries MQI
Primitive
Description
MQopen
Open a (possibly remote) queue
MQclose
Close a queue
MQput
Put a message into an opened queue
MQget
Get a message from a (local) queue
MQSerie also provides facilities to signal applications
when msgs arrive. For more info, see
http://www.redbooks.ibm.com/abstracts/sg247128.html
© C Xu, 1998,2007
Messaging Domain
 Point-to-Point
 Publish/Subscribe
– Clients address messages to a topic
© C Xu, 1998,2007
Publish/Subscribe Messaging

Request/Reply (RPC, MRI) model has limitations in two
types of applications
– Update of state info that changes over time
» Send requests regularly for update of the new info. NOT efficient
– Action in response to event occurrence:
» Send event info to ALL nodes who are interested in

Publish/Subscribe messaging
– Any number of customers of info can receive messages that are
provided by one or more producers of the info
– Publisher: producer of the info
– Subscriber: consumer of the info
– Topic (or named logical channel) to be subscribed by consumers to
register their interests
– Publish/subscribe broker: track subscriptions to individual topics and
provide facilitates for a publisher to publish msgs on a given topic

Topic-based vs content-based vs hybrid systems
© C Xu, 1998,2007
JMS Programming


Available in Java System Message Queue V3.6
Interfaces:
– ConnectionFactory
» Administrated object used to create a connection to JMS provider
» Defined by administrator, rather than programmer
– Connection
» A conn represents a comm link between app and msg server
» Administrated object
– Destination: where msg is delivered and consumed
» Queue or topic
–
–
–
–

MessageConsumer
MessageProducer
Message
Session: Single threaded context for sending and receiving messages
http://www.sun.com/software/products/message_queue/index.xml
© C Xu, 1998,2007
JMS Programming Model
© C Xu, 1998,2007
Producer (1/3)
injects resources for a connection factory, queue, and topic:
@Resource(mappedName="jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName="jms/Queue")
private static Queue queue;
@Resource(mappedName="jms/Topic")
private static Topic topic;
Assigns either the queue or topic to a destination object, based on dest type:
Destination dest = null;
try {
if (destType.equals("queue")) {
dest = (Destination) queue;
} else {
dest = (Destination) topic;
}
} catch (Exception e) {}
© C Xu, 1998,2007
Producer (2/3)
Creates a Connection and a Session:
Connection connection =
connectionFactory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Creates a MessageProducer and a TextMessage:
MessageProducer producer = session.createProducer(dest);
TextMessage message = session.createTextMessage();
Sends one or more messages to the destination:
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " + message.getText());
producer.send(message);
}
© C Xu, 1998,2007
Producer (3/3)
Sends an empty control message to indicate the end of the message stream:
producer.send(session.createMessage());
Sending an empty message of no specified type is a convenient way to
indicate to the consumer that the final message has arrived.
Closes the connection in a finally block, automatically closing the
session and MessageProducer:
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
© C Xu, 1998,2007
Async Consumer
1. injects resources for a connection factory, queue, and topic.
2. Assigns either the queue or topic to a destination object, based on the specified dest type.
3. Creates a Connection and a Session.
4. Creates a MessageConsumer.
5. Creates an instance of the TextListener class and registers it as the message listener
for the MessageConsumer:
listener = new TextListener();
consumer.setMessageListener(listener);
6. Starts the connection, causing message delivery to begin.
7. Listens for the messages published to the destination, stopping when the user types the
character q or Q:
System.out.println("To end program, type Q or q, " + "then <return>");
inputStreamReader = new InputStreamReader(System.in);
while (!((answer == 'q') || (answer == 'Q'))) {
try {
answer = (char) inputStreamReader.read();
} catch (IOException e) { }
}
8. Closes the connection, which automatically closes the session and MessageConsumer.
© C Xu, 1998,2007
Async Consumer (2/2)
When a message arrives, the onMessage method is called automatically.
The onMessage method converts the incoming message to a TextMessage and
displays its content. If the message is not a text message, it reports this fact:
public void onMessage(Message message) {
TextMessage msg = null;
try {
if (message instanceof TextMessage) {
msg = (TextMessage) message;
System.out.println("Reading message: " + msg.getText());
} else {
System.out.println("Message is not a " + "TextMessage");
}
} catch (JMSException e) } catch (Throwable t) { }
}
© C Xu, 1998,2007
Email vs Message Queue

Email is a special case of Message Queue
– Email aims at providing direct support for end users
– Email makes direct use of the underlying transport
services (SMTP over TCP); routing is left out
– General MQ can satisfy a different set of requirements:
» guaranteed message delivery,
» message priorities,
» logging facilities,
» efficient multicasting,
» load balancing,
» fault tolerance, etc.
© C Xu, 1998,2007
Msg-Queuing Model in Naplet ?
© C Xu, 1998,2007