Skip to content
This repository
Browse code

Update python client to do protocol negotiation.

  • Loading branch information...
commit a0396a42b48a88b7d6d75f120284d3ed62244be1 1 parent c4c27c2
jkreps jkreps authored

Showing 1 changed file with 16 additions and 7 deletions. Show diff stats Hide diff stats

  1. +16 7 clients/python/voldemort.py
23 clients/python/voldemort.py
@@ -103,6 +103,19 @@ def __init__(self, store_name, bootstrap_urls, reconnect_interval = 500, conflic
103 103 self.reconnect_interval = reconnect_interval
104 104 self.open = True
105 105
  106 + def _make_connection(self, host, port):
  107 + protocol = 'pb0'
  108 + logging.debug('Attempting to connect to ' + host + ':' + str(port))
  109 + connection = socket.socket()
  110 + connection.connect((host, port))
  111 + logging.debug('Connection succeeded, negotiating protocol')
  112 + connection.send(protocol)
  113 + resp = connection.recv(2)
  114 + if resp != 'ok':
  115 + raise VoldemortException('Server does not understand the protocol ' + protocol)
  116 + logging.debug('Protocol negotiation suceeded')
  117 + return connection
  118 +
106 119
107 120 ## Connect to a the next available node in the cluster
108 121 ## returns a tuple of (node_id, connection)
@@ -115,10 +128,7 @@ def _reconnect(self):
115 128 new_node = self.nodes[new_node_id]
116 129 connection = None
117 130 try:
118   - logging.debug('Attempting to connect to node ' + str(new_node_id))
119   - connection = socket.socket()
120   - connection.connect((new_node.host, new_node.socket_port))
121   - logging.debug('Connection succeeded')
  131 + connection = self._make_connection(new_node.host, new_node.socket_port)
122 132 self.request_count = 0
123 133 return new_node_id, connection
124 134 except socket.error, (err_num, message):
@@ -160,7 +170,6 @@ def _receive_response(self, connection):
160 170 size_bytes = connection.recv(4)
161 171 size = struct.unpack('>i', size_bytes)
162 172 return connection.recv(size[0])
163   -
164 173
165 174 ## Bootstrap cluster metadata from a list of urls of nodes in the cluster.
166 175 ## The urls are tuples in the form (host, port).
@@ -169,9 +178,9 @@ def _bootstrap_metadata(self, bootstrap_urls):
169 178 random.shuffle(bootstrap_urls)
170 179 for host, port in bootstrap_urls:
171 180 logging.debug('Attempting to bootstrap metadata from ' + host + ':' + str(port))
  181 + connection = None
172 182 try:
173   - connection = socket.socket()
174   - connection.connect((host, port))
  183 + connection = self._make_connection(host, port)
175 184 cluster_xmls = self._get_with_connection(connection, 'metadata', 'cluster.xml', should_route = False)
176 185 if len(cluster_xmls) != 1:
177 186 raise VoldemortException('Expected exactly one version of the metadata but found ' + str(cluster_xmls))

0 comments on commit a0396a4

Please sign in to comment.
Something went wrong with that request. Please try again.