source file: /home/buildslave/tahoe/edgy/build/src/allmydata/offloaded.py
file stats: 460 lines, 442 executed: 96.1% covered
   1. 
   2. import os, stat, time, weakref
   3. from zope.interface import implements
   4. from twisted.application import service
   5. from twisted.internet import defer
   6. from foolscap import Referenceable
   7. from foolscap.eventual import eventually
   8. from allmydata import interfaces, storage, uri
   9. from allmydata.immutable import upload
  10. from allmydata.util import idlib, log, observer, fileutil, hashutil
  11. 
  12. 
  13. class NotEnoughWritersError(Exception):
  14.     pass
  15. 
  16. 
  17. class CHKCheckerAndUEBFetcher:
  18.     """I check to see if a file is already present in the grid. I also fetch
  19.     the URI Extension Block, which is useful for an uploading client who
  20.     wants to avoid the work of encryption and encoding.
  21. 
  22.     I return False if the file is not completely healthy: i.e. if there are
  23.     less than 'N' shares present.
  24. 
  25.     If the file is completely healthy, I return a tuple of (sharemap,
  26.     UEB_data, UEB_hash).
  27.     """
  28. 
  29.     def __init__(self, peer_getter, storage_index, logparent=None):
  30.         self._peer_getter = peer_getter
  31.         self._found_shares = set()
  32.         self._storage_index = storage_index
  33.         self._sharemap = {}
  34.         self._readers = set()
  35.         self._ueb_hash = None
  36.         self._ueb_data = None
  37.         self._logparent = logparent
  38. 
  39.     def log(self, *args, **kwargs):
  40.         if 'facility' not in kwargs:
  41.             kwargs['facility'] = "tahoe.helper.chk.checkandUEBfetch"
  42.         if 'parent' not in kwargs:
  43.             kwargs['parent'] = self._logparent
  44.         return log.msg(*args, **kwargs)
  45. 
  46.     def check(self):
  47.         d = self._get_all_shareholders(self._storage_index)
  48.         d.addCallback(self._get_uri_extension)
  49.         d.addCallback(self._done)
  50.         return d
  51. 
  52.     def _get_all_shareholders(self, storage_index):
  53.         dl = []
  54.         for (peerid, ss) in self._peer_getter("storage", storage_index):
  55.             d = ss.callRemote("get_buckets", storage_index)
  56.             d.addCallbacks(self._got_response, self._got_error,
  57.                            callbackArgs=(peerid,))
  58.             dl.append(d)
  59.         return defer.DeferredList(dl)
  60. 
  61.     def _got_response(self, buckets, peerid):
  62.         # buckets is a dict: maps shum to an rref of the server who holds it
  63.         shnums_s = ",".join([str(shnum) for shnum in buckets])
  64.         self.log("got_response: [%s] has %d shares (%s)" %
  65.                  (idlib.shortnodeid_b2a(peerid), len(buckets), shnums_s),
  66.                  level=log.NOISY)
  67.         self._found_shares.update(buckets.keys())
  68.         for k in buckets:
  69.             if k not in self._sharemap:
  70.                 self._sharemap[k] = []
  71.             self._sharemap[k].append(peerid)
  72.         self._readers.update( [ (bucket, peerid)
  73.                                 for bucket in buckets.values() ] )
  74. 
  75.     def _got_error(self, f):
  76.         if f.check(KeyError):
  77.             pass
  78.         log.err(f, parent=self._logparent)
  79.         pass
  80. 
  81.     def _get_uri_extension(self, res):
  82.         # assume that we can pull the UEB from any share. If we get an error,
  83.         # declare the whole file unavailable.
  84.         if not self._readers:
  85.             self.log("no readers, so no UEB", level=log.NOISY)
  86.             return
  87.         b,peerid = self._readers.pop()
  88.         rbp = storage.ReadBucketProxy(b, peerid,
  89.                                       storage.si_b2a(self._storage_index))
  90.         d = rbp.startIfNecessary()
  91.         d.addCallback(lambda res: rbp.get_uri_extension())
  92.         d.addCallback(self._got_uri_extension)
  93.         d.addErrback(self._ueb_error)
  94.         return d
  95. 
  96.     def _got_uri_extension(self, ueb):
  97.         self.log("_got_uri_extension", level=log.NOISY)
  98.         self._ueb_hash = hashutil.uri_extension_hash(ueb)
  99.         self._ueb_data = uri.unpack_extension(ueb)
 100. 
 101.     def _ueb_error(self, f):
 102.         # an error means the file is unavailable, but the overall check
 103.         # shouldn't fail.
 104.         self.log("UEB fetch failed", failure=f, level=log.WEIRD, umid="sJLKVg")
 105.         return None
 106. 
 107.     def _done(self, res):
 108.         if self._ueb_data:
 109.             found = len(self._found_shares)
 110.             total = self._ueb_data['total_shares']
 111.             self.log(format="got %(found)d shares of %(total)d",
 112.                      found=found, total=total, level=log.NOISY)
 113.             if found < total:
 114.                 # not all shares are present in the grid
 115.                 self.log("not enough to qualify, file not found in grid",
 116.                          level=log.NOISY)
 117.                 return False
 118.             # all shares are present
 119.             self.log("all shares present, file is found in grid",
 120.                      level=log.NOISY)
 121.             return (self._sharemap, self._ueb_data, self._ueb_hash)
 122.         # no shares are present
 123.         self.log("unable to find UEB data, file not found in grid",
 124.                  level=log.NOISY)
 125.         return False
 126. 
 127. 
 128. class CHKUploadHelper(Referenceable, upload.CHKUploader):
 129.     """I am the helper-server -side counterpart to AssistedUploader. I handle
 130.     peer selection, encoding, and share pushing. I read ciphertext from the
 131.     remote AssistedUploader.
 132.     """
 133.     implements(interfaces.RICHKUploadHelper)
 134. 
 135.     def __init__(self, storage_index, helper,
 136.                  incoming_file, encoding_file,
 137.                  results, log_number):
 138.         self._storage_index = storage_index
 139.         self._helper = helper
 140.         self._incoming_file = incoming_file
 141.         self._encoding_file = encoding_file
 142.         self._upload_id = storage.si_b2a(storage_index)[:5]
 143.         self._log_number = log_number
 144.         self._results = results
 145.         self._upload_status = upload.UploadStatus()
 146.         self._upload_status.set_helper(False)
 147.         self._upload_status.set_storage_index(storage_index)
 148.         self._upload_status.set_status("fetching ciphertext")
 149.         self._upload_status.set_progress(0, 1.0)
 150.         self._helper.log("CHKUploadHelper starting for SI %s" % self._upload_id,
 151.                          parent=log_number)
 152. 
 153.         self._client = helper.parent
 154.         self._fetcher = CHKCiphertextFetcher(self, incoming_file, encoding_file,
 155.                                              self._log_number)
 156.         self._reader = LocalCiphertextReader(self, storage_index, encoding_file)
 157.         self._finished_observers = observer.OneShotObserverList()
 158. 
 159.         d = self._fetcher.when_done()
 160.         d.addCallback(lambda res: self._reader.start())
 161.         d.addCallback(lambda res: self.start_encrypted(self._reader))
 162.         d.addCallback(self._finished)
 163.         d.addErrback(self._failed)
 164. 
 165.     def log(self, *args, **kwargs):
 166.         if 'facility' not in kwargs:
 167.             kwargs['facility'] = "tahoe.helper.chk"
 168.         return upload.CHKUploader.log(self, *args, **kwargs)
 169. 
 170.     def start(self):
 171.         self._started = time.time()
 172.         # determine if we need to upload the file. If so, return ({},self) .
 173.         # If not, return (UploadResults,None) .
 174.         self.log("deciding whether to upload the file or not", level=log.NOISY)
 175.         if os.path.exists(self._encoding_file):
 176.             # we have the whole file, and we might be encoding it (or the
 177.             # encode/upload might have failed, and we need to restart it).
 178.             self.log("ciphertext already in place", level=log.UNUSUAL)
 179.             return (self._results, self)
 180.         if os.path.exists(self._incoming_file):
 181.             # we have some of the file, but not all of it (otherwise we'd be
 182.             # encoding). The caller might be useful.
 183.             self.log("partial ciphertext already present", level=log.UNUSUAL)
 184.             return (self._results, self)
 185.         # we don't remember uploading this file
 186.         self.log("no ciphertext yet", level=log.NOISY)
 187.         return (self._results, self)
 188. 
 189.     def remote_upload(self, reader):
 190.         # reader is an RIEncryptedUploadable. I am specified to return an
 191.         # UploadResults dictionary.
 192. 
 193.         # let our fetcher pull ciphertext from the reader.
 194.         self._fetcher.add_reader(reader)
 195.         # and also hashes
 196.         self._reader.add_reader(reader)
 197. 
 198.         # and inform the client when the upload has finished
 199.         return self._finished_observers.when_fired()
 200. 
 201.     def _finished(self, res):
 202.         (uri_extension_hash, needed_shares, total_shares, size) = res
 203.         r = self._results
 204.         r.uri_extension_hash = uri_extension_hash
 205.         f_times = self._fetcher.get_times()
 206.         r.timings["cumulative_fetch"] = f_times["cumulative_fetch"]
 207.         r.ciphertext_fetched = self._fetcher.get_ciphertext_fetched()
 208.         r.timings["total_fetch"] = f_times["total"]
 209.         self._reader.close()
 210.         os.unlink(self._encoding_file)
 211.         self._finished_observers.fire(r)
 212.         self._helper.upload_finished(self._storage_index, size)
 213.         del self._reader
 214. 
 215.     def _failed(self, f):
 216.         self.log(format="CHKUploadHelper(%(si)s) failed",
 217.                  si=storage.si_b2a(self._storage_index)[:5],
 218.                  failure=f,
 219.                  level=log.UNUSUAL)
 220.         self._finished_observers.fire(f)
 221.         self._helper.upload_finished(self._storage_index, 0)
 222.         del self._reader
 223. 
 224. class AskUntilSuccessMixin:
 225.     # create me with a _reader array
 226.     _last_failure = None
 227. 
 228.     def add_reader(self, reader):
 229.         self._readers.append(reader)
 230. 
 231.     def call(self, *args, **kwargs):
 232.         if not self._readers:
 233.             raise NotEnoughWritersError("ran out of assisted uploaders, last failure was %s" % self._last_failure)
 234.         rr = self._readers[0]
 235.         d = rr.callRemote(*args, **kwargs)
 236.         def _err(f):
 237.             self._last_failure = f
 238.             if rr in self._readers:
 239.                 self._readers.remove(rr)
 240.             self._upload_helper.log("call to assisted uploader %s failed" % rr,
 241.                                     failure=f, level=log.UNUSUAL)
 242.             # we can try again with someone else who's left
 243.             return self.call(*args, **kwargs)
 244.         d.addErrback(_err)
 245.         return d
 246. 
 247. class CHKCiphertextFetcher(AskUntilSuccessMixin):
 248.     """I use one or more remote RIEncryptedUploadable instances to gather
 249.     ciphertext on disk. When I'm done, the file I create can be used by a
 250.     LocalCiphertextReader to satisfy the ciphertext needs of a CHK upload
 251.     process.
 252. 
 253.     I begin pulling ciphertext as soon as a reader is added. I remove readers
 254.     when they have any sort of error. If the last reader is removed, I fire
 255.     my when_done() Deferred with a failure.
 256. 
 257.     I fire my when_done() Deferred (with None) immediately after I have moved
 258.     the ciphertext to 'encoded_file'.
 259.     """
 260. 
 261.     def __init__(self, helper, incoming_file, encoded_file, logparent):
 262.         self._upload_helper = helper
 263.         self._incoming_file = incoming_file
 264.         self._encoding_file = encoded_file
 265.         self._upload_id = helper._upload_id
 266.         self._log_parent = logparent
 267.         self._done_observers = observer.OneShotObserverList()
 268.         self._readers = []
 269.         self._started = False
 270.         self._f = None
 271.         self._times = {
 272.             "cumulative_fetch": 0.0,
 273.             "total": 0.0,
 274.             }
 275.         self._ciphertext_fetched = 0
 276. 
 277.     def log(self, *args, **kwargs):
 278.         if "facility" not in kwargs:
 279.             kwargs["facility"] = "tahoe.helper.chkupload.fetch"
 280.         if "parent" not in kwargs:
 281.             kwargs["parent"] = self._log_parent
 282.         return log.msg(*args, **kwargs)
 283. 
 284.     def add_reader(self, reader):
 285.         AskUntilSuccessMixin.add_reader(self, reader)
 286.         eventually(self._start)
 287. 
 288.     def _start(self):
 289.         if self._started:
 290.             return
 291.         self._started = True
 292.         started = time.time()
 293. 
 294.         if os.path.exists(self._encoding_file):
 295.             self.log("ciphertext already present, bypassing fetch",
 296.                      level=log.UNUSUAL)
 297.             # we'll still need the plaintext hashes (when
 298.             # LocalCiphertextReader.get_plaintext_hashtree_leaves() is
 299.             # called), and currently the easiest way to get them is to ask
 300.             # the sender for the last byte of ciphertext. That will provoke
 301.             # them into reading and hashing (but not sending) everything
 302.             # else.
 303.             have = os.stat(self._encoding_file)[stat.ST_SIZE]
 304.             d = self.call("read_encrypted", have-1, 1)
 305.             d.addCallback(self._done2, started)
 306.             return
 307. 
 308.         # first, find out how large the file is going to be
 309.         d = self.call("get_size")
 310.         d.addCallback(self._got_size)
 311.         d.addCallback(self._start_reading)
 312.         d.addCallback(self._done)
 313.         d.addCallback(self._done2, started)
 314.         d.addErrback(self._failed)
 315. 
 316.     def _got_size(self, size):
 317.         self.log("total size is %d bytes" % size, level=log.NOISY)
 318.         self._upload_helper._upload_status.set_size(size)
 319.         self._expected_size = size
 320. 
 321.     def _start_reading(self, res):
 322.         # then find out how much crypttext we have on disk
 323.         if os.path.exists(self._incoming_file):
 324.             self._have = os.stat(self._incoming_file)[stat.ST_SIZE]
 325.             self._upload_helper._helper.count("chk_upload_helper.resumes")
 326.             self.log("we already have %d bytes" % self._have, level=log.NOISY)
 327.         else:
 328.             self._have = 0
 329.             self.log("we do not have any ciphertext yet", level=log.NOISY)
 330.         self.log("starting ciphertext fetch", level=log.NOISY)
 331.         self._f = open(self._incoming_file, "ab")
 332. 
 333.         # now loop to pull the data from the readers
 334.         d = defer.Deferred()
 335.         self._loop(d)
 336.         # this Deferred will be fired once the last byte has been written to
 337.         # self._f
 338.         return d
 339. 
 340.     # read data in 50kB chunks. We should choose a more considered number
 341.     # here, possibly letting the client specify it. The goal should be to
 342.     # keep the RTT*bandwidth to be less than 10% of the chunk size, to reduce
 343.     # the upload bandwidth lost because this protocol is non-windowing. Too
 344.     # large, however, means more memory consumption for both ends. Something
 345.     # that can be transferred in, say, 10 seconds sounds about right. On my
 346.     # home DSL line (50kBps upstream), that suggests 500kB. Most lines are
 347.     # slower, maybe 10kBps, which suggests 100kB, and that's a bit more
 348.     # memory than I want to hang on to, so I'm going to go with 50kB and see
 349.     # how that works.
 350.     CHUNK_SIZE = 50*1024
 351. 
 352.     def _loop(self, fire_when_done):
 353.         # this slightly weird structure is needed because Deferreds don't do
 354.         # tail-recursion, so it is important to let each one retire promptly.
 355.         # Simply chaining them will cause a stack overflow at the end of a
 356.         # transfer that involves more than a few hundred chunks.
 357.         # 'fire_when_done' lives a long time, but the Deferreds returned by
 358.         # the inner _fetch() call do not.
 359.         start = time.time()
 360.         d = defer.maybeDeferred(self._fetch)
 361.         def _done(finished):
 362.             elapsed = time.time() - start
 363.             self._times["cumulative_fetch"] += elapsed
 364.             if finished:
 365.                 self.log("finished reading ciphertext", level=log.NOISY)
 366.                 fire_when_done.callback(None)
 367.             else:
 368.                 self._loop(fire_when_done)
 369.         def _err(f):
 370.             self.log(format="[%(si)s] ciphertext read failed",
 371.                      si=self._upload_id, failure=f, level=log.UNUSUAL)
 372.             fire_when_done.errback(f)
 373.         d.addCallbacks(_done, _err)
 374.         return None
 375. 
 376.     def _fetch(self):
 377.         needed = self._expected_size - self._have
 378.         fetch_size = min(needed, self.CHUNK_SIZE)
 379.         if fetch_size == 0:
 380.             self._upload_helper._upload_status.set_progress(1, 1.0)
 381.             return True # all done
 382.         percent = 0.0
 383.         if self._expected_size:
 384.             percent = 1.0 * (self._have+fetch_size) / self._expected_size
 385.         self.log(format="fetching [%(si)s] %(start)d-%(end)d of %(total)d (%(percent)d%%)",
 386.                  si=self._upload_id,
 387.                  start=self._have,
 388.                  end=self._have+fetch_size,
 389.                  total=self._expected_size,
 390.                  percent=int(100.0*percent),
 391.                  level=log.NOISY)
 392.         d = self.call("read_encrypted", self._have, fetch_size)
 393.         def _got_data(ciphertext_v):
 394.             for data in ciphertext_v:
 395.                 self._f.write(data)
 396.                 self._have += len(data)
 397.                 self._ciphertext_fetched += len(data)
 398.                 self._upload_helper._helper.count("chk_upload_helper.fetched_bytes", len(data))
 399.                 self._upload_helper._upload_status.set_progress(1, percent)
 400.             return False # not done
 401.         d.addCallback(_got_data)
 402.         return d
 403. 
 404.     def _done(self, res):
 405.         self._f.close()
 406.         self._f = None
 407.         self.log(format="done fetching ciphertext, size=%(size)d",
 408.                  size=os.stat(self._incoming_file)[stat.ST_SIZE],
 409.                  level=log.NOISY)
 410.         os.rename(self._incoming_file, self._encoding_file)
 411. 
 412.     def _done2(self, _ignored, started):
 413.         self.log("done2", level=log.NOISY)
 414.         elapsed = time.time() - started
 415.         self._times["total"] = elapsed
 416.         self._readers = []
 417.         self._done_observers.fire(None)
 418. 
 419.     def _failed(self, f):
 420.         if self._f:
 421.             self._f.close()
 422.         self._readers = []
 423.         self._done_observers.fire(f)
 424. 
 425.     def when_done(self):
 426.         return self._done_observers.when_fired()
 427. 
 428.     def get_times(self):
 429.         return self._times
 430. 
 431.     def get_ciphertext_fetched(self):
 432.         return self._ciphertext_fetched
 433. 
 434. 
 435. class LocalCiphertextReader(AskUntilSuccessMixin):
 436.     implements(interfaces.IEncryptedUploadable)
 437. 
 438.     def __init__(self, upload_helper, storage_index, encoding_file):
 439.         self._readers = []
 440.         self._upload_helper = upload_helper
 441.         self._storage_index = storage_index
 442.         self._encoding_file = encoding_file
 443.         self._status = None
 444. 
 445.     def start(self):
 446.         self._upload_helper._upload_status.set_status("pushing")
 447.         self._size = os.stat(self._encoding_file)[stat.ST_SIZE]
 448.         self.f = open(self._encoding_file, "rb")
 449. 
 450.     def get_size(self):
 451.         return defer.succeed(self._size)
 452. 
 453.     def get_all_encoding_parameters(self):
 454.         return self.call("get_all_encoding_parameters")
 455. 
 456.     def get_storage_index(self):
 457.         return defer.succeed(self._storage_index)
 458. 
 459.     def read_encrypted(self, length, hash_only):
 460.         assert hash_only is False
 461.         d = defer.maybeDeferred(self.f.read, length)
 462.         d.addCallback(lambda data: [data])
 463.         return d
 464.     def get_plaintext_hashtree_leaves(self, first, last, num_segments):
 465.         return self.call("get_plaintext_hashtree_leaves", first, last,
 466.                          num_segments)
 467.     def get_plaintext_hash(self):
 468.         return self.call("get_plaintext_hash")
 469.     def close(self):
 470.         self.f.close()
 471.         # ??. I'm not sure if it makes sense to forward the close message.
 472.         return self.call("close")
 473. 
 474. 
 475. 
 476. class Helper(Referenceable, service.MultiService):
 477.     implements(interfaces.RIHelper, interfaces.IStatsProducer)
 478.     # this is the non-distributed version. When we need to have multiple
 479.     # helpers, this object will become the HelperCoordinator, and will query
 480.     # the farm of Helpers to see if anyone has the storage_index of interest,
 481.     # and send the request off to them. If nobody has it, we'll choose a
 482.     # helper at random.
 483. 
 484.     name = "helper"
 485.     chk_upload_helper_class = CHKUploadHelper
 486.     MAX_UPLOAD_STATUSES = 10
 487. 
 488.     def __init__(self, basedir, stats_provider=None):
 489.         self._basedir = basedir
 490.         self._chk_incoming = os.path.join(basedir, "CHK_incoming")
 491.         self._chk_encoding = os.path.join(basedir, "CHK_encoding")
 492.         fileutil.make_dirs(self._chk_incoming)
 493.         fileutil.make_dirs(self._chk_encoding)
 494.         self._active_uploads = {}
 495.         self._all_uploads = weakref.WeakKeyDictionary() # for debugging
 496.         self._all_upload_statuses = weakref.WeakKeyDictionary()
 497.         self._recent_upload_statuses = []
 498.         self.stats_provider = stats_provider
 499.         if stats_provider:
 500.             stats_provider.register_producer(self)
 501.         self._counters = {"chk_upload_helper.upload_requests": 0,
 502.                           "chk_upload_helper.upload_already_present": 0,
 503.                           "chk_upload_helper.upload_need_upload": 0,
 504.                           "chk_upload_helper.resumes": 0,
 505.                           "chk_upload_helper.fetched_bytes": 0,
 506.                           "chk_upload_helper.encoded_bytes": 0,
 507.                           }
 508.         service.MultiService.__init__(self)
 509. 
 510.     def setServiceParent(self, parent):
 511.         service.MultiService.setServiceParent(self, parent)
 512. 
 513.     def log(self, *args, **kwargs):
 514.         if 'facility' not in kwargs:
 515.             kwargs['facility'] = "tahoe.helper"
 516.         return self.parent.log(*args, **kwargs)
 517. 
 518.     def count(self, key, value=1):
 519.         if self.stats_provider:
 520.             self.stats_provider.count(key, value)
 521.         self._counters[key] += value
 522. 
 523.     def get_stats(self):
 524.         OLD = 86400*2 # 48hours
 525.         now = time.time()
 526.         inc_count = inc_size = inc_size_old = 0
 527.         enc_count = enc_size = enc_size_old = 0
 528.         inc = os.listdir(self._chk_incoming)
 529.         enc = os.listdir(self._chk_encoding)
 530.         for f in inc:
 531.             s = os.stat(os.path.join(self._chk_incoming, f))
 532.             size = s[stat.ST_SIZE]
 533.             mtime = s[stat.ST_MTIME]
 534.             inc_count += 1
 535.             inc_size += size
 536.             if now - mtime > OLD:
 537.                 inc_size_old += size
 538.         for f in enc:
 539.             s = os.stat(os.path.join(self._chk_encoding, f))
 540.             size = s[stat.ST_SIZE]
 541.             mtime = s[stat.ST_MTIME]
 542.             enc_count += 1
 543.             enc_size += size
 544.             if now - mtime > OLD:
 545.                 enc_size_old += size
 546.         stats = { 'chk_upload_helper.active_uploads': len(self._active_uploads),
 547.                   'chk_upload_helper.incoming_count': inc_count,
 548.                   'chk_upload_helper.incoming_size': inc_size,
 549.                   'chk_upload_helper.incoming_size_old': inc_size_old,
 550.                   'chk_upload_helper.encoding_count': enc_count,
 551.                   'chk_upload_helper.encoding_size': enc_size,
 552.                   'chk_upload_helper.encoding_size_old': enc_size_old,
 553.                   }
 554.         stats.update(self._counters)
 555.         return stats
 556. 
 557.     def remote_upload_chk(self, storage_index):
 558.         self.count("chk_upload_helper.upload_requests")
 559.         r = upload.UploadResults()
 560.         started = time.time()
 561.         si_s = storage.si_b2a(storage_index)
 562.         lp = self.log(format="helper: upload_chk query for SI %(si)s", si=si_s)
 563.         incoming_file = os.path.join(self._chk_incoming, si_s)
 564.         encoding_file = os.path.join(self._chk_encoding, si_s)
 565.         if storage_index in self._active_uploads:
 566.             self.log("upload is currently active", parent=lp)
 567.             uh = self._active_uploads[storage_index]
 568.             return uh.start()
 569. 
 570.         d = self._check_for_chk_already_in_grid(storage_index, r, lp)
 571.         def _checked(already_present):
 572.             elapsed = time.time() - started
 573.             r.timings['existence_check'] = elapsed
 574.             if already_present:
 575.                 # the necessary results are placed in the UploadResults
 576.                 self.count("chk_upload_helper.upload_already_present")
 577.                 self.log("file already found in grid", parent=lp)
 578.                 return (r, None)
 579. 
 580.             self.count("chk_upload_helper.upload_need_upload")
 581.             # the file is not present in the grid, by which we mean there are
 582.             # less than 'N' shares available.
 583.             self.log("unable to find file in the grid", parent=lp,
 584.                      level=log.NOISY)
 585.             # We need an upload helper. Check our active uploads again in
 586.             # case there was a race.
 587.             if storage_index in self._active_uploads:
 588.                 self.log("upload is currently active", parent=lp)
 589.                 uh = self._active_uploads[storage_index]
 590.             else:
 591.                 self.log("creating new upload helper", parent=lp)
 592.                 uh = self.chk_upload_helper_class(storage_index, self,
 593.                                                   incoming_file, encoding_file,
 594.                                                   r, lp)
 595.                 self._active_uploads[storage_index] = uh
 596.                 self._add_upload(uh)
 597.             return uh.start()
 598.         d.addCallback(_checked)
 599.         def _err(f):
 600.             self.log("error while checking for chk-already-in-grid",
 601.                      failure=f, level=log.WEIRD, parent=lp, umid="jDtxZg")
 602.             return f
 603.         d.addErrback(_err)
 604.         return d
 605. 
 606.     def _check_for_chk_already_in_grid(self, storage_index, results, lp):
 607.         # see if this file is already in the grid
 608.         lp2 = self.log("doing a quick check+UEBfetch",
 609.                        parent=lp, level=log.NOISY)
 610.         c = CHKCheckerAndUEBFetcher(self.parent.get_permuted_peers,
 611.                                     storage_index, lp2)
 612.         d = c.check()
 613.         def _checked(res):
 614.             if res:
 615.                 (sharemap, ueb_data, ueb_hash) = res
 616.                 self.log("found file in grid", level=log.NOISY, parent=lp)
 617.                 results.uri_extension_hash = ueb_hash
 618.                 results.sharemap = {}
 619.                 for shnum, peerids in sharemap.items():
 620.                     peers_s = ",".join(["[%s]" % idlib.shortnodeid_b2a(peerid)
 621.                                         for peerid in peerids])
 622.                     results.sharemap[shnum] = "Found on " + peers_s
 623.                 results.uri_extension_data = ueb_data
 624.                 results.preexisting_shares = len(sharemap)
 625.                 results.pushed_shares = 0
 626.                 return True
 627.             return False
 628.         d.addCallback(_checked)
 629.         return d
 630. 
 631.     def _add_upload(self, uh):
 632.         self._all_uploads[uh] = None
 633.         s = uh.get_upload_status()
 634.         self._all_upload_statuses[s] = None
 635.         self._recent_upload_statuses.append(s)
 636.         while len(self._recent_upload_statuses) > self.MAX_UPLOAD_STATUSES:
 637.             self._recent_upload_statuses.pop(0)
 638. 
 639.     def upload_finished(self, storage_index, size):
 640.         # this is called with size=0 if the upload failed
 641.         self.count("chk_upload_helper.encoded_bytes", size)
 642.         uh = self._active_uploads[storage_index]
 643.         del self._active_uploads[storage_index]
 644.         s = uh.get_upload_status()
 645.         s.set_active(False)
 646. 
 647.     def get_all_upload_statuses(self):
 648.         return self._all_upload_statuses