Ticket #1191: mockingtests.dpatch.txt

File mockingtests.dpatch.txt, 14.6 KB (added by zooko, at 2010-09-08T14:03:35Z)
Line 
1[01;34mhunk[00m ./src/allmydata/immutable/downloader/fetcher.py 74
2         eventually(self.loop)
3 
4     def no_more_shares(self):
5+        log.msg("aaa %s.no_more_shares()" % (self,))
6         # ShareFinder tells us it's reached the end of its list
7         self._no_more_shares = True
8         eventually(self.loop)
9[01;34mhunk[00m ./src/allmydata/immutable/downloader/fetcher.py 104
10             self._node.fetch_failed(self, f)
11             return
12 
13-        #print "LOOP", self._blocks.keys(), "active:", self._active_share_map, "overdue:", self._overdue_share_map, "unused:", self._shares
14+        log.msg( "aaa %s.loop(); blocks: %s, active: %s, overdue: %s, unused: %s" % (self, self._blocks.keys(), self._active_share_map, self._overdue_share_map, self._shares))
15         # Should we sent out more requests?
16         while len(set(self._blocks.keys())
17                   | set(self._active_share_map.keys())
18[01;34mhunk[00m ./src/allmydata/immutable/downloader/fetcher.py 130
19             # progress
20             self._ask_for_more_shares()
21             if self._no_more_shares:
22+                self._no_shares_error() # this calls self.stop()
23                 # But there are no more shares to be had. If we're going to
24                 # succeed, it will be with the shares we've already seen.
25                 # Will they be enough?
26[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 52
27         self._lp = log.msg(format="ShareFinder[si=%(si)s] starting",
28                            si=self._si_prefix,
29                            level=log.NOISY, parent=logparent, umid="2xjj2A")
30+        log.msg("xxx %s.__init__(%s, %s, %s, %s, %s, %s)" % (self, storage_broker, verifycap, node, download_status, logparent, max_outstanding_requests))
31 
32     def update_num_segments(self):
33[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 55
34+        log.msg("xxx %s.update_num_segments()" % (self,))
35         (numsegs, authoritative) = self.node.get_num_segments()
36         assert authoritative
37         for cs in self._commonshares.values():
38[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 62
39             cs.set_authoritative_num_segments(numsegs)
40 
41     def start_finding_servers(self):
42+        log.msg("xxx %s.start_finding_servers()" % (self,))
43         # don't get servers until somebody uses us: creating the
44         # ImmutableFileNode should not cause work to happen yet. Test case is
45         # test_dirnode, which creates us with storage_broker=None
46[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 78
47         return log.msg(*args, **kwargs)
48 
49     def stop(self):
50+        log.msg("xxx %s.stop()" % (self,))
51         self.running = False
52         while self.overdue_timers:
53             req,t = self.overdue_timers.popitem()
54[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 86
55 
56     # called by our parent CiphertextDownloader
57     def hungry(self):
58+        log.msg("xxx %s.hungry()" % (self,))
59         self.log(format="ShareFinder[si=%(si)s] hungry",
60                  si=self._si_prefix, level=log.NOISY, umid="NywYaQ")
61         self.start_finding_servers()
62[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 95
63 
64     # internal methods
65     def loop(self):
66+        log.msg("xxx %s.loop()" % (self,))
67         pending_s = ",".join([idlib.shortnodeid_b2a(rt.peerid)
68                               for rt in self.pending_requests]) # sort?
69         self.log(format="ShareFinder loop: running=%(running)s"
70[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 123
71             self.send_request(server)
72             # we loop again to get parallel queries. The check above will
73             # prevent us from looping forever.
74+            log.msg("xxx %s.loop() => loop again to get parallel queries" % (self,))
75             eventually(self.loop)
76             return
77 
78[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 140
79         self.share_consumer.no_more_shares()
80 
81     def send_request(self, server):
82+        log.msg("xxx %s.send_request(%s)" % (self, server))
83         peerid, rref = server
84         req = RequestToken(peerid)
85         self.pending_requests.add(req)
86[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 163
87         d.addCallback(incidentally, eventually, self.loop)
88 
89     def _request_retired(self, req):
90+        log.msg("xxx %s._request_retired(%s)" % (self, req))
91         self.pending_requests.discard(req)
92         self.overdue_requests.discard(req)
93         if req in self.overdue_timers:
94[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 171
95             del self.overdue_timers[req]
96 
97     def overdue(self, req):
98+        log.msg("xxx %s.overdue(%s)" % (self, req))
99         del self.overdue_timers[req]
100         assert req in self.pending_requests # paranoia, should never be false
101         self.overdue_requests.add(req)
102[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 179
103 
104     def _got_response(self, buckets, server_version, peerid, req, d_ev,
105                       time_sent, lp):
106+        log.msg("xxx %s._got_response(%s, %s, %s, %s, %s, %s, %s)" % (self, buckets, server_version, peerid, req, d_ev, time_sent, lp))
107         shnums = sorted([shnum for shnum in buckets])
108         time_received = now()
109         d_ev.finished(shnums, time_received)
110[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 201
111         self._deliver_shares(shares)
112 
113     def _create_share(self, shnum, bucket, server_version, peerid, dyhb_rtt):
114+        log.msg("xxx %s._create_share(%s, %s, %s, %s, %s)" % (self, shnum, bucket, server_version, peerid, dyhb_rtt))
115         if shnum in self._commonshares:
116             cs = self._commonshares[shnum]
117         else:
118[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 230
119         return s
120 
121     def _deliver_shares(self, shares):
122+        log.msg("xxx %s._deliver_shares(%s)" % (self, shares))
123         # they will call hungry() again if they want more
124         self._hungry = False
125         shares_s = ",".join([str(sh) for sh in shares])
126[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 239
127         eventually(self.share_consumer.got_shares, shares)
128 
129     def _got_error(self, f, peerid, req, d_ev, lp):
130+        log.msg("xxx %s._got_error(%s, %s, %s, %s, %s)" % (self, f, peerid, req, d_ev, lp))
131         d_ev.finished("error", now())
132         self.log(format="got error from [%(peerid)s]",
133                  peerid=idlib.shortnodeid_b2a(peerid), failure=f,
134[01;34mhunk[00m ./src/allmydata/immutable/downloader/finder.py 244
135                  level=log.UNUSUAL, parent=lp, umid="zUKdCw")
136-
137-
138[01;34mhunk[00m ./src/allmydata/immutable/downloader/node.py 224
139 
140     # called by our child ShareFinder
141     def got_shares(self, shares):
142+        log.msg("xxx %s.got_shares(%s)" % (self, shares))
143         self._shares.update(shares)
144         if self._active_segment:
145             self._active_segment.add_shares(shares)
146[01;34mhunk[00m ./src/allmydata/immutable/downloader/node.py 229
147     def no_more_shares(self):
148+        log.msg("xxx %s.no_more_shares() ; _active_segment: %s" % (self, self._active_segment))
149         self._no_more_shares = True
150         if self._active_segment:
151             self._active_segment.no_more_shares()
152[01;34mhunk[00m ./src/allmydata/immutable/downloader/share.py 86
153 
154         self._requested_blocks = [] # (segnum, set(observer2..))
155         ver = server_version["http://allmydata.org/tahoe/protocols/storage/v1"]
156+        log.msg( "zzz ver: %r" % (ver,))
157         self._overrun_ok = ver["tolerates-immutable-read-overrun"]
158         # If _overrun_ok and we guess the offsets correctly, we can get
159         # everything in one RTT. If _overrun_ok and we guess wrong, we might
160[01;34mhunk[00m ./src/allmydata/test/test_immutable.py 4
161 from allmydata.test import common
162 from allmydata.interfaces import NotEnoughSharesError
163 from allmydata.util.consumer import download_to_data
164+from allmydata import uri
165 from twisted.internet import defer
166 from twisted.trial import unittest
167[01;34mhunk[00m ./src/allmydata/test/test_immutable.py 7
168+from allmydata.immutable.downloader.common import COMPLETE
169 import random
170 
171[01;34mhunk[00m ./src/allmydata/test/test_immutable.py 10
172+from foolscap.api import eventually
173+from allmydata.util import log
174+
175+from allmydata.immutable.downloader import fetcher, finder, share
176+
177+import mock
178+
179+class MockNode(object):
180+    def __init__(self, check_reneging, check_fetch_failed):
181+        self.got = 0
182+        self.finished_d = defer.Deferred()
183+        self.segment_size = 78
184+        self.guessed_segment_size = 78
185+        self._no_more_shares = False
186+        self.check_reneging = check_reneging
187+        self.check_fetch_failed = check_fetch_failed
188+        self._si_prefix='aa'
189+        self.have_UEB = True
190+        self.share_hash_tree = mock.Mock()
191+        self.share_hash_tree.needed_hashes.return_value = False
192+        self.on_want_more_shares = None
193+
194+    def when_finished(self):
195+        return self.finished_d
196+    def get_num_segments(self):
197+        return (5, True)
198+    def _calculate_sizes(self, guessed_segment_size):
199+        return {'block_size': 4, 'num_segments': 5}
200+    def no_more_shares(self):
201+        self._no_more_shares = True
202+    def got_shares(self, shares):
203+        if self.check_reneging:
204+            if self._no_more_shares:
205+                self.finished_d.errback(unittest.FailTest("The node was told by the share finder that it is destined to remain hungry, then was given another share."))
206+                return
207+        self.got += len(shares)
208+        log.msg("yyy 3 %s.got_shares(%s) got: %s" % (self, shares, self.got))
209+        if self.got == 3:
210+            self.finished_d.callback(True)
211+    def get_desired_ciphertext_hashes(self, *args, **kwargs):
212+        return iter([])
213+    def fetch_failed(self, *args, **kwargs):
214+        if self.check_fetch_failed:
215+            if self.finished_d:
216+                self.finished_d.errback(unittest.FailTest("The node was told by the segment fetcher that the download failed."))
217+                self.finished_d = None
218+    def want_more_shares(self):
219+        if self.on_want_more_shares:
220+            self.on_want_more_shares()
221+    def process_blocks(self, *args, **kwargs):
222+        if self.finished_d:
223+            self.finished_d.callback(None)
224+
225+class TestSegmentFetcher(unittest.TestCase):
226+    def test_be_satisfied_with_ill_distributed_shares(self):
227+        mocknode = MockNode(check_reneging=False, check_fetch_failed=True)
228+        sf = fetcher.SegmentFetcher(mocknode, 1, 3, None)
229+        mocknode.on_want_more_shares = lambda: sf.no_more_shares
230+
231+        rcap = uri.CHKFileURI('a'*32, 'a'*32, 3, 99, 100)
232+        vcap = rcap.get_verify_cap()
233+        server_version = {
234+                    'http://allmydata.org/tahoe/protocols/storage/v1': {
235+                        "tolerates-immutable-read-overrun": True
236+                        }
237+                    }
238+        ss = []
239+        mockcommonshare = mock.Mock()
240+        mockcommonshare.get_desired_block_hashes.return_value = iter([])
241+
242+        mockds0 = mock.Mock()
243+        share0 = share.Share(mock.Mock(), server_version, vcap,
244+                            mockcommonshare, mocknode, mockds0, 's1', 0, 0,
245+                            None)
246+        mockds0.add_request_sent.side_effect = \
247+            eventually(sf._block_request_activity, share0, 0, COMPLETE)
248+        ss.append(share0)
249+
250+        mockds1 = mock.Mock()
251+        share1 = share.Share(mock.Mock(), server_version, vcap,
252+                            mockcommonshare, mocknode, mockds1, 's1', 1, 0,
253+                            None)
254+        mockds1.add_request_sent.side_effect = \
255+            eventually(sf._block_request_activity, share1, 1, COMPLETE)
256+        ss.append(share1)
257+
258+        mockds2 = mock.Mock()
259+        share2 = share.Share(mock.Mock(), server_version, vcap,
260+                            mockcommonshare, mocknode, mockds2, 's2', 2, 0,
261+                            None)
262+        mockds2.add_request_sent.side_effect = \
263+            eventually(sf._block_request_activity, share2, 2, COMPLETE)
264+        ss.append(share2)
265+
266+        sf.add_shares(ss)
267+        sf.no_more_shares()
268+
269+        return mocknode.when_finished()
270+
271+class TestShareFinder(unittest.TestCase):
272+    def test_no_reneging_on_no_more_shares_ever(self):
273+        # ticket #1191
274+
275+        # Suppose that K=3 and you send two DYHB requests, the first
276+        # response offers two shares, and then the last offers one
277+        # share. If you tell your share consumer "no more shares,
278+        # ever", and then immediately tell them "oh, and here's
279+        # another share", then you lose.
280+
281+        rcap = uri.CHKFileURI('a'*32, 'a'*32, 3, 99, 100)
282+        vcap = rcap.get_verify_cap()
283+
284+        class MockServer(object):
285+            def __init__(self, buckets):
286+                self.version = {
287+                    'http://allmydata.org/tahoe/protocols/storage/v1': {
288+                        "tolerates-immutable-read-overrun": True
289+                        }
290+                    }
291+                self.buckets = buckets
292+                self.d = defer.Deferred()
293+                self.s = None
294+            def callRemote(self, methname, *args, **kwargs):
295+                log.msg("yyy 2 %s.callRemote(%s, %s, %s)" % (self, methname, args, kwargs))
296+                d = defer.Deferred()
297+
298+                # Even after the 3rd answer we're still hungry because
299+                # we're interested in finding a 3rd server so we don't
300+                # have to download more than one share from the first
301+                # server. This is actually necessary to trigger the
302+                # bug.
303+                def _give_buckets_and_hunger_again():
304+                    d.callback(self.buckets)
305+                    self.s.hungry()
306+                eventually(_give_buckets_and_hunger_again)
307+                return d
308+
309+        mockserver1 = MockServer({1: mock.Mock(), 2: mock.Mock()})
310+        mockserver2 = MockServer({})
311+        mockserver3 = MockServer({3: mock.Mock()})
312+        mockstoragebroker = mock.Mock()
313+        mockstoragebroker.get_servers_for_index.return_value = [ ('ms1', mockserver1), ('ms2', mockserver2), ('ms3', mockserver3), ]
314+        mockdownloadstatus = mock.Mock()
315+        mocknode = MockNode(check_reneging=True, check_fetch_failed=True)
316+
317+        s = finder.ShareFinder(mockstoragebroker, vcap, mocknode, mockdownloadstatus)
318+
319+        mockserver1.s = s
320+        mockserver2.s = s
321+        mockserver3.s = s
322+
323+        s.hungry()
324+
325+        return mocknode.when_finished()
326+
327 class Test(common.ShareManglingMixin, common.ShouldFailMixin, unittest.TestCase):
328     def test_test_code(self):
329         # The following process of stashing the shares, running