add more download-status data, fix tests
authorBrian Warner <warner@lothar.com>
Wed, 29 Jun 2011 22:25:55 +0000 (15:25 -0700)
committerBrian Warner <warner@lothar.com>
Wed, 29 Jun 2011 22:25:55 +0000 (15:25 -0700)
src/allmydata/immutable/downloader/status.py
src/allmydata/test/test_web.py
src/allmydata/web/status.py

index 0bf3f715e664b27349296025da1c946b7c8b994c..cbe9969a71e4cf8c5c5a281ca22310e6a48b19a4 100644 (file)
@@ -245,8 +245,7 @@ class DownloadStatus:
         # a download is considered active if it has at least one outstanding
         # read() call
         for r_ev in self.read_events:
-            (ign1, ign2, ign3, finishtime, ign4, ign5, ign6) = r_ev
-            if finishtime is None:
+            if r_ev["finish_time"] is None:
                 return True
         return False
 
index 915cba8a053777d8a52724af977e7721112323eb..951f5541beb6c95d2852b0ec07a9ced6318fc6be 100644 (file)
@@ -19,7 +19,7 @@ from allmydata.nodemaker import NodeMaker
 from allmydata.unknown import UnknownNode
 from allmydata.web import status, common
 from allmydata.scripts.debug import CorruptShareOptions, corrupt_share
-from allmydata.util import fileutil, base32
+from allmydata.util import fileutil, base32, hashutil
 from allmydata.util.consumer import download_to_data
 from allmydata.util.netstring import split_netstring
 from allmydata.util.encodingutil import to_str
@@ -78,34 +78,39 @@ def build_one_ds():
     ds = DownloadStatus("storage_index", 1234)
     now = time.time()
 
-    ds.add_segment_request(0, now)
-    # segnum, when, start,len, decodetime
-    ds.add_segment_delivery(0, now+1, 0, 100, 0.5)
-    ds.add_segment_request(1, now+2)
-    ds.add_segment_error(1, now+3)
+    serverid_a = hashutil.tagged_hash("foo", "serverid_a")[:20]
+    serverid_b = hashutil.tagged_hash("foo", "serverid_b")[:20]
+    storage_index = hashutil.storage_index_hash("SI")
+    e0 = ds.add_segment_request(0, now)
+    e0.activate(now+0.5)
+    e0.deliver(now+1, 0, 100, 0.5) # when, start,len, decodetime
+    e1 = ds.add_segment_request(1, now+2)
+    e1.error(now+3)
     # two outstanding requests
-    ds.add_segment_request(2, now+4)
-    ds.add_segment_request(3, now+5)
+    e2 = ds.add_segment_request(2, now+4)
+    e3 = ds.add_segment_request(3, now+5)
+    del e2,e3 # hush pyflakes
 
     # simulate a segment which gets delivered faster than a system clock tick (ticket #1166)
-    ds.add_segment_request(4, now)
-    ds.add_segment_delivery(4, now, 0, 140, 0.5)
+    e = ds.add_segment_request(4, now)
+    e.activate(now)
+    e.deliver(now, 0, 140, 0.5)
 
-    e = ds.add_dyhb_sent("serverid_a", now)
+    e = ds.add_dyhb_request(serverid_a, now)
     e.finished([1,2], now+1)
-    e = ds.add_dyhb_sent("serverid_b", now+2) # left unfinished
+    e = ds.add_dyhb_request(serverid_b, now+2) # left unfinished
 
     e = ds.add_read_event(0, 120, now)
     e.update(60, 0.5, 0.1) # bytes, decrypttime, pausetime
     e.finished(now+1)
     e = ds.add_read_event(120, 30, now+2) # left unfinished
 
-    e = ds.add_request_sent("serverid_a", 1, 100, 20, now)
+    e = ds.add_block_request(serverid_a, 1, 100, 20, now)
     e.finished(20, now+1)
-    e = ds.add_request_sent("serverid_a", 1, 120, 30, now+1) # left unfinished
+    e = ds.add_block_request(serverid_a, 1, 120, 30, now+1) # left unfinished
 
     # make sure that add_read_event() can come first too
-    ds1 = DownloadStatus("storage_index", 1234)
+    ds1 = DownloadStatus(storage_index, 1234)
     e = ds1.add_read_event(0, 120, now)
     e.update(60, 0.5, 0.1) # bytes, decrypttime, pausetime
     e.finished(now+1)
@@ -556,10 +561,24 @@ class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixi
         def _check_dl(res):
             self.failUnless("File Download Status" in res, res)
         d.addCallback(_check_dl)
-        d.addCallback(lambda res: self.GET("/status/down-%d?t=json" % dl_num))
+        d.addCallback(lambda res: self.GET("/status/down-%d/event_json" % dl_num))
         def _check_dl_json(res):
             data = simplejson.loads(res)
             self.failUnless(isinstance(data, dict))
+            self.failUnless("read" in data)
+            self.failUnlessEqual(data["read"][0]["length"], 120)
+            self.failUnlessEqual(data["segment"][0]["segment_length"], 100)
+            self.failUnlessEqual(data["segment"][2]["segment_number"], 2)
+            self.failUnlessEqual(data["segment"][2]["finish_time"], None)
+            phwr_id = base32.b2a(hashutil.tagged_hash("foo", "serverid_a")[:20])
+            cmpu_id = base32.b2a(hashutil.tagged_hash("foo", "serverid_b")[:20])
+            # serverids[] keys are strings, since that's what JSON does, but
+            # we'd really like them to be ints
+            self.failUnlessEqual(data["serverids"]["0"], "phwr")
+            self.failUnlessEqual(data["serverids"]["1"], "cmpu")
+            self.failUnlessEqual(data["server_info"][phwr_id]["short"], "phwr")
+            self.failUnlessEqual(data["server_info"][cmpu_id]["short"], "cmpu")
+            self.failUnless("dyhb" in data)
         d.addCallback(_check_dl_json)
         d.addCallback(lambda res: self.GET("/status/up-%d" % ul_num))
         def _check_ul(res):
index 7f1937f2b02e04375319287b6bc63bfb847fa865..c146912ce861997c0e872ab92cd934fd98b90b42 100644 (file)
@@ -344,8 +344,8 @@ class DownloadStatusPage(DownloadResultsRendererMixin, rend.Page):
     def relative_time(self, t):
         if t is None:
             return t
-        if self.download_status.started is not None:
-            return t - self.download_status.started
+        if self.download_status.first_timestamp is not None:
+            return t - self.download_status.first_timestamp
         return t
     def short_relative_time(self, t):
         t = self.relative_time(t)
@@ -353,48 +353,150 @@ class DownloadStatusPage(DownloadResultsRendererMixin, rend.Page):
             return ""
         return "+%.6fs" % t
 
-    def renderHTTP(self, ctx):
-        req = inevow.IRequest(ctx)
-        t = get_arg(req, "t")
-        if t == "json":
-            return self.json(req)
-        return rend.Page.renderHTTP(self, ctx)
-
-    def json(self, req):
-        req.setHeader("content-type", "text/plain")
-        data = {}
-        dyhb_events = []
-        for serverid,requests in self.download_status.dyhb_requests.iteritems():
-            for req in requests:
-                dyhb_events.append( (base32.b2a(serverid),) + req )
-        dyhb_events.sort(key=lambda req: req[1])
-        data["dyhb"] = dyhb_events
-        request_events = []
-        for serverid,requests in self.download_status.requests.iteritems():
-            for req in requests:
-                request_events.append( (base32.b2a(serverid),) + req )
-        request_events.sort(key=lambda req: (req[4],req[1]))
-        data["requests"] = request_events
-        data["segment"] = self.download_status.segment_events
-        data["read"] = self.download_status.read_events
+    def _find_overlap(self, events, start_key, end_key):
+        # given a list of event dicts, return a new list in which each event
+        # has an extra "row" key (an int, starting at 0). This is a hint to
+        # our JS frontend about how to overlap the parts of the graph it is
+        # drawing.
+
+        # we must always make a copy, since we're going to be adding "row"
+        # keys and don't want to change the original objects. If we're
+        # stringifying serverids, we'll also be changing the serverid keys.
+        new_events = []
+        rows = []
+        for ev in events:
+            ev = ev.copy()
+            if "serverid" in ev:
+                ev["serverid"] = base32.b2a(ev["serverid"])
+            # find an empty slot in the rows
+            free_slot = None
+            for row,finished in enumerate(rows):
+                if finished is not None:
+                    if ev[start_key] > finished:
+                        free_slot = row
+                        break
+            if free_slot is None:
+                free_slot = len(rows)
+                rows.append(ev[end_key])
+            else:
+                rows[free_slot] = ev[end_key]
+            ev["row"] = free_slot
+            new_events.append(ev)
+        return new_events
+
+    def _find_overlap_requests(self, events):
+        """We compute a three-element 'row tuple' for each event: (serverid,
+        shnum, row). All elements are ints. The first is a mapping from
+        serverid to group number, the second is a mapping from shnum to
+        subgroup number. The third is a row within the subgroup.
+
+        We also return a list of lists of rowcounts, so renderers can decide
+        how much vertical space to give to each row.
+        """
+
+        serverid_to_group = {}
+        groupnum_to_rows = {} # maps groupnum to a table of rows. Each table
+                              # is a list with an element for each row number
+                              # (int starting from 0) that contains a
+                              # finish_time, indicating that the row is empty
+                              # beyond that time. If finish_time is None, it
+                              # indicate a response that has not yet
+                              # completed, so the row cannot be reused.
+        new_events = []
+        for ev in events:
+            # DownloadStatus promises to give us events in temporal order
+            ev = ev.copy()
+            ev["serverid"] = base32.b2a(ev["serverid"])
+            if ev["serverid"] not in serverid_to_group:
+                groupnum = len(serverid_to_group)
+                serverid_to_group[ev["serverid"]] = groupnum
+            groupnum = serverid_to_group[ev["serverid"]]
+            if groupnum not in groupnum_to_rows:
+                groupnum_to_rows[groupnum] = []
+            rows = groupnum_to_rows[groupnum]
+            # find an empty slot in the rows
+            free_slot = None
+            for row,finished in enumerate(rows):
+                if finished is not None:
+                    if ev["start_time"] > finished:
+                        free_slot = row
+                        break
+            if free_slot is None:
+                free_slot = len(rows)
+                rows.append(ev["finish_time"])
+            else:
+                rows[free_slot] = ev["finish_time"]
+            ev["row"] = (groupnum, free_slot)
+            new_events.append(ev)
+        # maybe also return serverid_to_group, groupnum_to_rows, and some
+        # indication of the highest finish_time
+        #
+        # actually, return the highest rownum for each groupnum
+        highest_rownums = [len(groupnum_to_rows[groupnum])
+                           for groupnum in range(len(serverid_to_group))]
+        return new_events, highest_rownums
+
+    def child_event_json(self, ctx):
+        inevow.IRequest(ctx).setHeader("content-type", "text/plain")
+        data = { } # this will be returned to the GET
+        ds = self.download_status
+
+        data["read"] = self._find_overlap(ds.read_events,
+                                          "start_time", "finish_time")
+        data["segment"] = self._find_overlap(ds.segment_events,
+                                             "start_time", "finish_time")
+        data["dyhb"] = self._find_overlap(ds.dyhb_requests,
+                                          "start_time", "finish_time")
+        data["block"],data["block_rownums"] = self._find_overlap_requests(ds.block_requests)
+
+        servernums = {}
+        serverid_strings = {}
+        for d_ev in data["dyhb"]:
+            if d_ev["serverid"] not in servernums:
+                servernum = len(servernums)
+                servernums[d_ev["serverid"]] = servernum
+                #title= "%s: %s" % ( ",".join([str(shnum) for shnum in shnums]))
+                serverid_strings[servernum] = d_ev["serverid"][:4]
+        data["server_info"] = dict([(serverid, {"num": servernums[serverid],
+                                                "color": self.color(base32.a2b(serverid)),
+                                                "short": serverid_strings[servernums[serverid]],
+                                                })
+                                   for serverid in servernums.keys()])
+        data["num_serverids"] = len(serverid_strings)
+        # we'd prefer the keys of serverids[] to be ints, but this is JSON,
+        # so they get converted to strings. Stupid javascript.
+        data["serverids"] = serverid_strings
+        data["bounds"] = {"min": ds.first_timestamp,
+                          "max": ds.last_timestamp,
+                          }
+        # for testing
+        ## data["bounds"]["max"] = tfmt(max([d_ev["finish_time"]
+        ##                                   for d_ev in data["dyhb"]
+        ##                                   if d_ev["finish_time"] is not None]
+        ##                                  ))
         return simplejson.dumps(data, indent=1) + "\n"
 
+    def _rate_and_time(self, bytes, seconds):
+        time_s = self.render_time(None, seconds)
+        if seconds != 0:
+            rate = self.render_rate(None, 1.0 * bytes / seconds)
+            return T.span(title=rate)[time_s]
+        return T.span[time_s]
+
     def render_events(self, ctx, data):
         if not self.download_status.storage_index:
             return
         srt = self.short_relative_time
         l = T.div()
-        
+
         t = T.table(align="left", class_="status-download-events")
         t[T.tr[T.th["serverid"], T.th["sent"], T.th["received"],
                T.th["shnums"], T.th["RTT"]]]
-        dyhb_events = []
-        for serverid,requests in self.download_status.dyhb_requests.iteritems():
-            for req in requests:
-                dyhb_events.append( (serverid,) + req )
-        dyhb_events.sort(key=lambda req: req[1])
-        for d_ev in dyhb_events:
-            (serverid, sent, shnums, received) = d_ev
+        for d_ev in self.download_status.dyhb_requests:
+            serverid = d_ev["serverid"]
+            sent = d_ev["start_time"]
+            shnums = d_ev["response_shnums"]
+            received = d_ev["finish_time"]
             serverid_s = idlib.shortnodeid_b2a(serverid)
             rtt = None
             if received is not None:
@@ -406,89 +508,89 @@ class DownloadStatusPage(DownloadResultsRendererMixin, rend.Page):
                  T.td[",".join([str(shnum) for shnum in shnums])],
                  T.td[self.render_time(None, rtt)],
                  ]]]
-        
+
         l[T.h2["DYHB Requests:"], t]
         l[T.br(clear="all")]
-        
+
         t = T.table(align="left",class_="status-download-events")
         t[T.tr[T.th["range"], T.th["start"], T.th["finish"], T.th["got"],
                T.th["time"], T.th["decrypttime"], T.th["pausedtime"],
                T.th["speed"]]]
         for r_ev in self.download_status.read_events:
-            (start, length, requesttime, finishtime, bytes, decrypt, paused) = r_ev
-            if finishtime is not None:
-                rtt = finishtime - requesttime - paused
+            start = r_ev["start"]
+            length = r_ev["length"]
+            bytes = r_ev["bytes_returned"]
+            decrypt_time = ""
+            if bytes:
+                decrypt_time = self._rate_and_time(bytes, r_ev["decrypt_time"])
+            speed, rtt = "",""
+            if r_ev["finish_time"] is not None:
+                rtt = r_ev["finish_time"] - r_ev["start_time"] - r_ev["paused_time"]
                 speed = self.render_rate(None, compute_rate(bytes, rtt))
                 rtt = self.render_time(None, rtt)
-                decrypt = self.render_time(None, decrypt)
-                paused = self.render_time(None, paused)
-            else:
-                speed, rtt, decrypt, paused = "","","",""
+            paused = self.render_time(None, r_ev["paused_time"])
+
             t[T.tr[T.td["[%d:+%d]" % (start, length)],
-                   T.td[srt(requesttime)], T.td[srt(finishtime)],
-                   T.td[bytes], T.td[rtt], T.td[decrypt], T.td[paused],
+                   T.td[srt(r_ev["start_time"])], T.td[srt(r_ev["finish_time"])],
+                   T.td[bytes], T.td[rtt],
+                   T.td[decrypt_time], T.td[paused],
                    T.td[speed],
                    ]]
-        
+
         l[T.h2["Read Events:"], t]
         l[T.br(clear="all")]
-        
+
         t = T.table(align="left",class_="status-download-events")
-        t[T.tr[T.th["type"], T.th["segnum"], T.th["when"], T.th["range"],
+        t[T.tr[T.th["segnum"], T.th["start"], T.th["active"], T.th["finish"],
+               T.th["range"],
                T.th["decodetime"], T.th["segtime"], T.th["speed"]]]
-        reqtime = (None, None)
         for s_ev in self.download_status.segment_events:
-            (etype, segnum, when, segstart, seglen, decodetime) = s_ev
-            if etype == "request":
-                t[T.tr[T.td["request"],
-                    T.td["seg%d" % segnum],
-                    T.td[srt(when)],
-                    T.td["-"],
-                    T.td["-"],
-                    T.td["-"],
-                    T.td["-"]]]
-                    
-                reqtime = (segnum, when)
-            elif etype == "delivery":
-                if reqtime[0] == segnum:
-                    segtime = when - reqtime[1]
+            range_s = "-"
+            segtime_s = "-"
+            speed = "-"
+            decode_time = "-"
+            if s_ev["finish_time"] is not None:
+                if s_ev["success"]:
+                    segtime = s_ev["finish_time"] - s_ev["active_time"]
+                    segtime_s = self.render_time(None, segtime)
+                    seglen = s_ev["segment_length"]
+                    range_s = "[%d:+%d]" % (s_ev["segment_start"], seglen)
                     speed = self.render_rate(None, compute_rate(seglen, segtime))
-                    segtime = self.render_time(None, segtime)
+                    decode_time = self._rate_and_time(seglen, s_ev["decode_time"])
                 else:
-                    segtime, speed = "", ""
-                t[T.tr[T.td["delivery"], T.td["seg%d" % segnum],
-                       T.td[srt(when)],
-                       T.td["[%d:+%d]" % (segstart, seglen)],
-                       T.td[self.render_time(None,decodetime)],
-                       T.td[segtime], T.td[speed]]]
-            elif etype == "error":
-                t[T.tr[T.td["error"], T.td["seg%d" % segnum]]]
-                
+                    # error
+                    range_s = "error"
+            else:
+                # not finished yet
+                pass
+
+            t[T.tr[T.td["seg%d" % s_ev["segment_number"]],
+                   T.td[srt(s_ev["start_time"])],
+                   T.td[srt(s_ev["active_time"])],
+                   T.td[srt(s_ev["finish_time"])],
+                   T.td[range_s],
+                   T.td[decode_time],
+                   T.td[segtime_s], T.td[speed]]]
+
         l[T.h2["Segment Events:"], t]
         l[T.br(clear="all")]
-
         t = T.table(align="left",class_="status-download-events")
         t[T.tr[T.th["serverid"], T.th["shnum"], T.th["range"],
-               T.th["txtime"], T.th["rxtime"], T.th["received"], T.th["RTT"]]]
-        reqtime = (None, None)
-        request_events = []
-        for serverid,requests in self.download_status.requests.iteritems():
-            for req in requests:
-                request_events.append( (serverid,) + req )
-        request_events.sort(key=lambda req: (req[4],req[1]))
-        for r_ev in request_events:
-            (peerid, shnum, start, length, sent, receivedlen, received) = r_ev
+               T.th["txtime"], T.th["rxtime"],
+               T.th["received"], T.th["RTT"]]]
+        for r_ev in self.download_status.block_requests:
             rtt = None
-            if received is not None:
-                rtt = received - sent
-            peerid_s = idlib.shortnodeid_b2a(peerid)
-            t[T.tr(style="background: %s" % self.color(peerid))[
-                T.td[peerid_s], T.td[shnum],
-                T.td["[%d:+%d]" % (start, length)],
-                T.td[srt(sent)], T.td[srt(received)], T.td[receivedlen],
+            if r_ev["finish_time"] is not None:
+                rtt = r_ev["finish_time"] - r_ev["start_time"]
+            serverid_s = idlib.shortnodeid_b2a(r_ev["serverid"])
+            t[T.tr(style="background: %s" % self.color(r_ev["serverid"]))[
+                T.td[serverid_s], T.td[r_ev["shnum"]],
+                T.td["[%d:+%d]" % (r_ev["start"], r_ev["length"])],
+                T.td[srt(r_ev["start_time"])], T.td[srt(r_ev["finish_time"])],
+                T.td[r_ev["response_length"] or ""],
                 T.td[self.render_time(None, rtt)],
                 ]]
-                
+
         l[T.h2["Requests:"], t]
         l[T.br(clear="all")]