Skip to content

Instantly share code, notes, and snippets.

@rystsov
Last active September 7, 2015 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rystsov/7f8db599d44ee635655f to your computer and use it in GitHub Desktop.
Save rystsov/7f8db599d44ee635655f to your computer and use it in GitHub Desktop.
def _read_write_read(self, test, val, due):
n = self._get_n(0)
resps = net.send(self.nodes, lambda x: x.prepare(n), due-now())
try:
# Waiting for q (majority) succeeded responses.
ok = resps.where(lambda x: isinstance(x.msg, Accepted)).wait(self.q)
last = ok.max(lambda x: x.msg.n).msg.val
if test != None and test(last):
candidate = val
else:
candidate = last
acks = net.send(ok.nodes, lambda x: x.accept(n, candidate), due-now())
resps += acks
acks.where(lambda x: isinstance(x.msg, OK)).wait(self.q)
if test != None and not test(last): return Conflict(val=last)
return OK(val=candidate)
except CantResolveWait:
return NetworkError()
finally:
resps = resps.abort() # abort the connections and
# pick a ballot number greater than any of already known
for x in resps.where(lambda x: isinstance(x.msg, Conflict)):
self._get_n(x.msg.promise)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment