3 from const import reactor
6 from hash import intify
7 from knode import KNode as Node
8 from ktable import KTable, K
11 """ base class for some long running asynchronous proccesses like finding nodes or values """
12 def __init__(self, table, target, callback):
15 self.num = intify(target)
19 self.callback = callback
23 def sort(a, b, num=self.num):
24 """ this function is for sorting nodes relative to the ID we are looking for """
25 x, y = num ^ a.num, num ^ b.num
33 def goWithNodes(self, t):
38 FIND_NODE_TIMEOUT = 15
40 class FindNode(ActionBase):
41 """ find node action merits it's own class as it is a long running stateful process """
42 def handleGotNodes(self, dict):
44 sender = dict["sender"]
45 sender = Node().initWithDict(sender)
46 sender.conn = self.table.airhook.connectionForAddr((sender.host, sender.port))
47 self.table.table.insertNode(sender)
48 if self.finished or self.answered.has_key(sender.id):
49 # a day late and a dollar short
51 self.outstanding = self.outstanding - 1
52 self.answered[sender.id] = 1
54 n = Node().initWithDict(node)
55 n.conn = self.table.airhook.connectionForAddr((n.host, n.port))
56 if not self.found.has_key(n.id):
62 send messages to new peers, if necessary
66 l = self.found.values()
69 if node.id == self.target:
71 return self.callback([node])
72 if (not self.queried.has_key(node.id)) and node.id != self.table.node.id:
73 #xxxx t.timeout = time.time() + FIND_NODE_TIMEOUT
74 df = node.findNode(self.target, self.table.node.senderDict())
75 df.addCallbacks(self.handleGotNodes, self.makeMsgFailed(node))
76 self.outstanding = self.outstanding + 1
77 self.queried[node.id] = 1
78 if self.outstanding >= const.CONCURRENT_REQS:
80 assert(self.outstanding) >=0
81 if self.outstanding == 0:
84 reactor.callFromThread(self.callback, l[:K])
86 def makeMsgFailed(self, node):
87 def defaultGotNodes(err, self=self, node=node):
88 self.table.table.nodeFailed(node)
89 self.outstanding = self.outstanding - 1
91 return defaultGotNodes
93 def goWithNodes(self, nodes):
95 this starts the process, our argument is a transaction with t.extras being our list of nodes
96 it's a transaction since we got called from the dispatcher
99 if node.id == self.table.node.id:
102 self.found[node.id] = node
107 GET_VALUE_TIMEOUT = 15
108 class GetValue(FindNode):
109 """ get value task """
110 def handleGotNodes(self, dict):
111 sender = dict["sender"]
112 sender = Node().initWithDict(sender)
113 sender.conn = self.table.airhook.connectionForAddr((sender.host, sender.port))
114 self.table.table.insertNode(sender)
115 if self.finished or self.answered.has_key(sender.id):
116 # a day late and a dollar short
118 self.outstanding = self.outstanding - 1
119 self.answered[sender.id] = 1
121 # if we have any closer than what we already got, query them
122 if dict.has_key('nodes'):
123 for node in dict['nodes']:
124 n = Node().initWithDict(node)
125 n.conn = self.table.airhook.connectionForAddr((n.host, n.port))
126 if not self.found.has_key(n.id):
128 elif dict.has_key('values'):
129 def x(y, z=self.results):
135 v = filter(None, map(x, dict['values']))
137 reactor.callFromThread(self.callback, v)
144 l = self.found.values()
148 if (not self.queried.has_key(node.id)) and node.id != self.table.node.id:
149 #xxx t.timeout = time.time() + GET_VALUE_TIMEOUT
150 df = node.findValue(self.target, self.table.node.senderDict())
151 df.addCallback(self.handleGotNodes)
152 df.addErrback(self.makeMsgFailed(node))
153 self.outstanding = self.outstanding + 1
154 self.queried[node.id] = 1
155 if self.outstanding >= const.CONCURRENT_REQS:
157 assert(self.outstanding) >=0
158 if self.outstanding == 0:
159 ## all done, didn't find it!!
161 reactor.callFromThread(self.callback,[])
164 def goWithNodes(self, nodes, found=None):
170 if node.id == self.table.node.id:
173 self.found[node.id] = node
178 class StoreValue(ActionBase):
179 def __init__(self, table, target, value, callback):
180 ActionBase.__init__(self, table, target, callback)
184 def storedValue(self, t, node):
185 self.outstanding -= 1
186 self.table.insertNode(node)
189 self.stored.append(t)
190 if len(self.stored) >= const.STORE_REDUNDANCY:
192 self.callback(self.stored)
194 if not len(self.stored) + self.outstanding >= const.STORE_REDUNDANCY:
197 def storeFailed(self, t, node):
198 self.table.nodeFailed(node)
199 self.outstanding -= 1
207 num = const.CONCURRENT_REQS - self.outstanding
208 if num > const.STORE_REDUNDANCY:
209 num = const.STORE_REDUNDANCY
212 node = self.nodes.pop()
214 if self.outstanding == 0:
216 self.callback(self.stored)
218 if not node.id == self.table.node.id:
219 self.outstanding += 1
220 df = node.storeValue(self.target, self.value, self.table.node.senderDict())
221 df.addCallback(self.storedValue, node=node)
222 df.addErrback(self.storeFailed, node=node)
224 def goWithNodes(self, nodes):
226 self.nodes.sort(self.sort)
231 def __init__(self, store):
233 reactor.callLater(const.KEINITIAL_DELAY, self.doExpire)
236 self.cut = "%0.6f" % (time() - const.KE_AGE)
240 c = self.store.cursor()
241 s = "delete from kv where time < '%s';" % self.cut
243 reactor.callLater(const.KE_DELAY, self.doExpire)