Skip to content

Commit

Permalink
Update python client to do protocol negotiation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkreps committed Jun 21, 2009
1 parent c4c27c2 commit a0396a4
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions clients/python/voldemort.py
Expand Up @@ -103,6 +103,19 @@ def __init__(self, store_name, bootstrap_urls, reconnect_interval = 500, conflic
self.reconnect_interval = reconnect_interval self.reconnect_interval = reconnect_interval
self.open = True 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 ## Connect to a the next available node in the cluster
## returns a tuple of (node_id, connection) ## returns a tuple of (node_id, connection)
Expand All @@ -115,10 +128,7 @@ def _reconnect(self):
new_node = self.nodes[new_node_id] new_node = self.nodes[new_node_id]
connection = None connection = None
try: try:
logging.debug('Attempting to connect to node ' + str(new_node_id)) connection = self._make_connection(new_node.host, new_node.socket_port)
connection = socket.socket()
connection.connect((new_node.host, new_node.socket_port))
logging.debug('Connection succeeded')
self.request_count = 0 self.request_count = 0
return new_node_id, connection return new_node_id, connection
except socket.error, (err_num, message): except socket.error, (err_num, message):
Expand Down Expand Up @@ -160,7 +170,6 @@ def _receive_response(self, connection):
size_bytes = connection.recv(4) size_bytes = connection.recv(4)
size = struct.unpack('>i', size_bytes) size = struct.unpack('>i', size_bytes)
return connection.recv(size[0]) return connection.recv(size[0])



## Bootstrap cluster metadata from a list of urls of nodes in the cluster. ## Bootstrap cluster metadata from a list of urls of nodes in the cluster.
## The urls are tuples in the form (host, port). ## The urls are tuples in the form (host, port).
Expand All @@ -169,9 +178,9 @@ def _bootstrap_metadata(self, bootstrap_urls):
random.shuffle(bootstrap_urls) random.shuffle(bootstrap_urls)
for host, port in bootstrap_urls: for host, port in bootstrap_urls:
logging.debug('Attempting to bootstrap metadata from ' + host + ':' + str(port)) logging.debug('Attempting to bootstrap metadata from ' + host + ':' + str(port))
connection = None
try: try:
connection = socket.socket() connection = self._make_connection(host, port)
connection.connect((host, port))
cluster_xmls = self._get_with_connection(connection, 'metadata', 'cluster.xml', should_route = False) cluster_xmls = self._get_with_connection(connection, 'metadata', 'cluster.xml', should_route = False)
if len(cluster_xmls) != 1: if len(cluster_xmls) != 1:
raise VoldemortException('Expected exactly one version of the metadata but found ' + str(cluster_xmls)) raise VoldemortException('Expected exactly one version of the metadata but found ' + str(cluster_xmls))
Expand Down

0 comments on commit a0396a4

Please sign in to comment.