Permalink
Browse files

Update python client to do protocol negotiation.

  • Loading branch information...
1 parent c4c27c2 commit a0396a42b48a88b7d6d75f120284d3ed62244be1 @jkreps jkreps committed Jun 21, 2009
Showing with 16 additions and 7 deletions.
  1. +16 −7 clients/python/voldemort.py
View
23 clients/python/voldemort.py
@@ -103,6 +103,19 @@ def __init__(self, store_name, bootstrap_urls, reconnect_interval = 500, conflic
self.reconnect_interval = reconnect_interval
self.open = True
+ def _make_connection(self, host, port):
+ protocol = 'pb0'
+ logging.debug('Attempting to connect to ' + host + ':' + str(port))
+ connection = socket.socket()
+ connection.connect((host, port))
+ logging.debug('Connection succeeded, negotiating protocol')
+ connection.send(protocol)
+ resp = connection.recv(2)
+ if resp != 'ok':
+ raise VoldemortException('Server does not understand the protocol ' + protocol)
+ logging.debug('Protocol negotiation suceeded')
+ return connection
+
## Connect to a the next available node in the cluster
## returns a tuple of (node_id, connection)
@@ -115,10 +128,7 @@ def _reconnect(self):
new_node = self.nodes[new_node_id]
connection = None
try:
- logging.debug('Attempting to connect to node ' + str(new_node_id))
- connection = socket.socket()
- connection.connect((new_node.host, new_node.socket_port))
- logging.debug('Connection succeeded')
+ connection = self._make_connection(new_node.host, new_node.socket_port)
self.request_count = 0
return new_node_id, connection
except socket.error, (err_num, message):
@@ -160,7 +170,6 @@ def _receive_response(self, connection):
size_bytes = connection.recv(4)
size = struct.unpack('>i', size_bytes)
return connection.recv(size[0])
-
## Bootstrap cluster metadata from a list of urls of nodes in the cluster.
## The urls are tuples in the form (host, port).
@@ -169,9 +178,9 @@ def _bootstrap_metadata(self, bootstrap_urls):
random.shuffle(bootstrap_urls)
for host, port in bootstrap_urls:
logging.debug('Attempting to bootstrap metadata from ' + host + ':' + str(port))
+ connection = None
try:
- connection = socket.socket()
- connection.connect((host, port))
+ connection = self._make_connection(host, port)
cluster_xmls = self._get_with_connection(connection, 'metadata', 'cluster.xml', should_route = False)
if len(cluster_xmls) != 1:
raise VoldemortException('Expected exactly one version of the metadata but found ' + str(cluster_xmls))

0 comments on commit a0396a4

Please sign in to comment.