Saturday, July 19, 2014

qpid101

#!/usr/bin/env python
# Filename:                qpid101.py
# Description:             Qpid 101
# Supported Langauge(s):   Python 2.7.x
# Time-stamp:              <2014-07-19 01:04:29 someone> 
# -------------------------------------------------------
# * What's qpid?
# It's like a queue from data structures 101. How do you share
# the queue between two or more separate programs on two more
# more separate computers? Forget about passing pointers and
# sharing namespaces. Instead use a network queue server with  
# its own protocol. Then imagine special queues like how in data
# structures class there were special trees and make this server
# support those special extensions to a traditional queue. 
#
# * Could I use something other than a queue?
# Sure, you could use a DB or a RESTful web-service but DBs are 
# better at storing data, not passing it. A web-service between
# two programs doesn't allow for the message to be queued for
# later if the other program is down; i.e. it is not asynchronous.
# Why not make something new and focus on what it should do well? 
# See http://qpid.apache.org/overview.html for more justification.
# 
# * How how do I use it? 
# In a 101-level nutshell follow the examples below from:
# https://qpid.apache.org/releases/qpid-trunk/programming/book
# 
# * How do I install the Python library? 
# On Fedroa: 'yum install python-qpid python-qpid_messaging' 
#
# * Need a qpid server? 
# The swat-a-fly-with-a-telephone-pole-method...
# If you have an openstack install around, allow the client (to be
# written below) to connect to your openstack server on port 5672.
# -------------------------------------------------------
import sys
from qpid.messaging import *
broker =  "192.168.122.28:5672"
address = "amq.topic"
connection = Connection(broker)
try:
    # establish connection to message broker
    connection.open()
    # create a session object on which messages can be sent/recv'd
    session = connection.session()
    # create a sender that sends to the given address
    sender = session.sender(address)
    # create a receiver receives messages from the given address
    receiver = session.receiver(address)
    # send the message
    sender.send(Message("Hello world!"));
    # receive the message (only wait 1 second, else wait forver)
    message = receiver.fetch(timeout=1)
    print message.content
    # ack receipt of message during session
    session.acknowledge()
except MessagingError,m:
    print m
finally:
    connection.close()
# -------------------------------------------------------
# Not too exciting on the client: 
#     sh-4.2$ ./qpid101.py 
#     Hello world!
#     sh-4.2$ 
#
# Let's look at the server:
# * 'yum install qpid-tools'
# 
# * Run 'qpid-printevents' and re-run qpid101.py 
# ** The following is the output; all lines from one run; fields trimmed
# 
# 1. clientConnect broker=localhost:5672 rhost=192.168.122.28:5672-192.168.122.1:33254 user=anonymous
# 2. queueDeclare broker=localhost:5672 rhost=192.168.122.28:5672-192.168.122.1:33254 user=anonymous
#      qName=41a24690-c85b-4d4b-97c4-cdfc4ae0664a:0.0 durable=F excl=T autoDel=T args={} disp=created
# 3. bind broker=localhost:5672 rhost=192.168.122.28:5672-192.168.122.1:33254 user=anonymous exName=amq.topic
#      qName=41a24690-c85b-4d4b-97c4-cdfc4ae0664a:0.0 key=# args={}
# 4. subscribe broker=localhost:5672 rhost=192.168.122.28:5672-192.168.122.1:33254 user=anonymous
#      qName=41a24690-c85b-4d4b-97c4-cdfc4ae0664a:0.0 dest=0 excl=F args={}
# 5. unsubscribe broker=localhost:5672 rhost=192.168.122.28:5672-192.168.122.1:33254 user=anonymous dest=0
# 6. queueDelete broker=localhost:5672 rhost=192.168.122.28:5672-192.168.122.1:33254 user=anonymous
#      qName=41a24690-c85b-4d4b-97c4-cdfc4ae0664a:0.0
# 7. clientDisconnect broker=localhost:5672 rhost=192.168.122.28:5672-192.168.122.1:33254 user=anonymous
#
# Try flooding it with 'while [ 1 ]; do ./qpid101.py > /dev/null ; done' and watch it run
# 
# * Run 'qpid-queue-stats' 
# ** It's a ~150 line Python program and prints the enq/deq rate every 10 seconds
#
# * Let's look at qpid-stat
# ** It's a ~500 line Python program that displays msgIn/MsgOut per queue and other stats
# ** Try 'watch "qpid-stat -q -S msgIn"' to see the queues sorted by messages in
# ** run '/etc/init.d/qpidd restart' as root in another termainal and watch the msgIn reset
# 
# queue      dur  autoDel  excl  msg   msgIn  msgOut  bytes  bytesIn  bytesOut  cons  bind
# =========================================================================================
# q-plugin                          0  25.3k  25.3k      0   19.2m    19.2m        1     2
# 2dcb1680...              Y        0  15.3k  15.3k      0   5.70m    5.70m        1     2
# conductor                         0  7.66k  7.66k      0   8.68m    8.68m        1     2
# ...
#
# ** Try 'watch "qpid-stat -c -I"' to pick out who is connecting
# *** Use the first 10 lines of code via a Python REPL and wrap the send in loop
# *** Pick out your client address and note the change in idle, and msgIn
# 
# client-addr                 cproc          cpid   connected   idle    msgIn  msgOut
# ===================================================================================
# :5672-192.168.122.1:41724                  21772  8m 25s      3m 30s   120k  40.2k
# :5672-192.168.122.28:54254  cinder-volume  15803  9h 18m 17s  10s     4.72k  5.02k
# ...
# 

No comments: