2008-04-11 14:31:16 -07:00
|
|
|
|
2009-01-14 17:36:20 -07:00
|
|
|
import random
|
2008-04-11 14:31:16 -07:00
|
|
|
|
|
|
|
from zope.interface import implements
|
2008-04-17 19:12:42 -07:00
|
|
|
from twisted.internet import defer, reactor
|
2009-05-21 17:38:23 -07:00
|
|
|
from foolscap.api import eventually
|
2009-11-11 14:45:42 -08:00
|
|
|
from allmydata.interfaces import IMutableFileNode, \
|
2009-01-06 13:37:03 -07:00
|
|
|
ICheckable, ICheckResults, NotEnoughSharesError
|
2008-09-20 10:35:45 -07:00
|
|
|
from allmydata.util import hashutil, log
|
2008-04-17 19:12:42 -07:00
|
|
|
from allmydata.util.assertutil import precondition
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
from allmydata.uri import WriteableSSKFileURI, ReadonlySSKFileURI
|
2008-10-22 01:38:18 -07:00
|
|
|
from allmydata.monitor import Monitor
|
2008-04-11 14:31:16 -07:00
|
|
|
from pycryptopp.cipher.aes import AES
|
|
|
|
|
2010-02-26 01:14:33 -07:00
|
|
|
from allmydata.mutable.publish import Publish
|
|
|
|
from allmydata.mutable.common import MODE_READ, MODE_WRITE, UnrecoverableFileError, \
|
2008-04-17 19:12:42 -07:00
|
|
|
ResponseCache, UncoordinatedWriteError
|
2010-02-26 01:14:33 -07:00
|
|
|
from allmydata.mutable.servermap import ServerMap, ServermapUpdater
|
|
|
|
from allmydata.mutable.retrieve import Retrieve
|
|
|
|
from allmydata.mutable.checker import MutableChecker, MutableCheckAndRepairer
|
|
|
|
from allmydata.mutable.repairer import Repairer
|
2008-04-11 14:31:16 -07:00
|
|
|
|
|
|
|
|
2008-04-17 19:12:42 -07:00
|
|
|
class BackoffAgent:
|
|
|
|
# these parameters are copied from foolscap.reconnector, which gets them
|
|
|
|
# from twisted.internet.protocol.ReconnectingClientFactory
|
|
|
|
initialDelay = 1.0
|
|
|
|
factor = 2.7182818284590451 # (math.e)
|
|
|
|
jitter = 0.11962656492 # molar Planck constant times c, Joule meter/mole
|
|
|
|
maxRetries = 4
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._delay = self.initialDelay
|
|
|
|
self._count = 0
|
|
|
|
def delay(self, node, f):
|
|
|
|
self._count += 1
|
|
|
|
if self._count == 4:
|
|
|
|
return f
|
|
|
|
self._delay = self._delay * self.factor
|
|
|
|
self._delay = random.normalvariate(self._delay,
|
|
|
|
self._delay * self.jitter)
|
|
|
|
d = defer.Deferred()
|
|
|
|
reactor.callLater(self._delay, d.callback, None)
|
|
|
|
return d
|
|
|
|
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
# use nodemaker.create_mutable_file() to make one of these
|
2008-04-11 14:31:16 -07:00
|
|
|
|
|
|
|
class MutableFileNode:
|
2008-07-15 17:23:25 -07:00
|
|
|
implements(IMutableFileNode, ICheckable)
|
2008-04-11 14:31:16 -07:00
|
|
|
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
def __init__(self, storage_broker, secret_holder,
|
|
|
|
default_encoding_parameters, history):
|
|
|
|
self._storage_broker = storage_broker
|
|
|
|
self._secret_holder = secret_holder
|
|
|
|
self._default_encoding_parameters = default_encoding_parameters
|
|
|
|
self._history = history
|
2008-04-11 14:31:16 -07:00
|
|
|
self._pubkey = None # filled in upon first read
|
|
|
|
self._privkey = None # filled in if we're mutable
|
|
|
|
# we keep track of the last encoding parameters that we use. These
|
|
|
|
# are updated upon retrieve, and used by publish. If we publish
|
|
|
|
# without ever reading (i.e. overwrite()), then we use these values.
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
self._required_shares = default_encoding_parameters["k"]
|
|
|
|
self._total_shares = default_encoding_parameters["n"]
|
2008-04-11 14:31:16 -07:00
|
|
|
self._sharemap = {} # known shares, shnum-to-[nodeids]
|
|
|
|
self._cache = ResponseCache()
|
2009-11-18 11:16:24 -08:00
|
|
|
self._most_recent_size = None
|
2008-04-11 14:31:16 -07:00
|
|
|
|
2008-04-17 17:51:38 -07:00
|
|
|
# all users of this MutableFileNode go through the serializer. This
|
|
|
|
# takes advantage of the fact that Deferreds discard the callbacks
|
|
|
|
# that they're done with, so we can keep using the same Deferred
|
|
|
|
# forever without consuming more and more memory.
|
|
|
|
self._serializer = defer.succeed(None)
|
|
|
|
|
2008-04-11 14:31:16 -07:00
|
|
|
def __repr__(self):
|
2008-04-17 21:32:38 -07:00
|
|
|
if hasattr(self, '_uri'):
|
|
|
|
return "<%s %x %s %s>" % (self.__class__.__name__, id(self), self.is_readonly() and 'RO' or 'RW', self._uri.abbrev())
|
|
|
|
else:
|
|
|
|
return "<%s %x %s %s>" % (self.__class__.__name__, id(self), None, None)
|
2008-04-11 14:31:16 -07:00
|
|
|
|
2009-11-11 14:25:42 -08:00
|
|
|
def init_from_cap(self, filecap):
|
2008-04-11 14:31:16 -07:00
|
|
|
# we have the URI, but we have not yet retrieved the public
|
|
|
|
# verification key, nor things like 'k' or 'N'. If and when someone
|
|
|
|
# wants to get our contents, we'll pull from shares and fill those
|
|
|
|
# in.
|
2009-11-11 14:25:42 -08:00
|
|
|
assert isinstance(filecap, (ReadonlySSKFileURI, WriteableSSKFileURI))
|
|
|
|
self._uri = filecap
|
|
|
|
self._writekey = None
|
|
|
|
if isinstance(filecap, WriteableSSKFileURI):
|
2008-04-11 14:31:16 -07:00
|
|
|
self._writekey = self._uri.writekey
|
|
|
|
self._readkey = self._uri.readkey
|
|
|
|
self._storage_index = self._uri.storage_index
|
|
|
|
self._fingerprint = self._uri.fingerprint
|
|
|
|
# the following values are learned during Retrieval
|
|
|
|
# self._pubkey
|
|
|
|
# self._required_shares
|
|
|
|
# self._total_shares
|
|
|
|
# and these are needed for Publish. They are filled in by Retrieval
|
|
|
|
# if possible, otherwise by the first peer that Publish talks to.
|
|
|
|
self._privkey = None
|
|
|
|
self._encprivkey = None
|
|
|
|
return self
|
|
|
|
|
2009-10-12 20:12:32 -07:00
|
|
|
def create_with_keys(self, (pubkey, privkey), contents):
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
"""Call this to create a brand-new mutable file. It will create the
|
2009-10-12 20:12:32 -07:00
|
|
|
shares, find homes for them, and upload the initial contents (created
|
|
|
|
with the same rules as IClient.create_mutable_file() ). Returns a
|
|
|
|
Deferred that fires (with the MutableFileNode instance you should
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
use) when it completes.
|
2008-04-11 14:31:16 -07:00
|
|
|
"""
|
2008-04-22 11:49:53 -07:00
|
|
|
self._pubkey, self._privkey = pubkey, privkey
|
|
|
|
pubkey_s = self._pubkey.serialize()
|
|
|
|
privkey_s = self._privkey.serialize()
|
|
|
|
self._writekey = hashutil.ssk_writekey_hash(privkey_s)
|
|
|
|
self._encprivkey = self._encrypt_privkey(self._writekey, privkey_s)
|
|
|
|
self._fingerprint = hashutil.ssk_pubkey_fingerprint_hash(pubkey_s)
|
|
|
|
self._uri = WriteableSSKFileURI(self._writekey, self._fingerprint)
|
|
|
|
self._readkey = self._uri.readkey
|
|
|
|
self._storage_index = self._uri.storage_index
|
2009-10-12 20:12:32 -07:00
|
|
|
initial_contents = self._get_initial_contents(contents)
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
return self._upload(initial_contents, None)
|
2008-04-11 14:31:16 -07:00
|
|
|
|
2009-10-12 20:12:32 -07:00
|
|
|
def _get_initial_contents(self, contents):
|
|
|
|
if isinstance(contents, str):
|
|
|
|
return contents
|
|
|
|
if contents is None:
|
|
|
|
return ""
|
|
|
|
assert callable(contents), "%s should be callable, not %s" % \
|
|
|
|
(contents, type(contents))
|
|
|
|
return contents(self)
|
|
|
|
|
2008-04-11 14:31:16 -07:00
|
|
|
def _encrypt_privkey(self, writekey, privkey):
|
|
|
|
enc = AES(writekey)
|
|
|
|
crypttext = enc.process(privkey)
|
|
|
|
return crypttext
|
|
|
|
|
|
|
|
def _decrypt_privkey(self, enc_privkey):
|
|
|
|
enc = AES(self._writekey)
|
|
|
|
privkey = enc.process(enc_privkey)
|
|
|
|
return privkey
|
|
|
|
|
|
|
|
def _populate_pubkey(self, pubkey):
|
|
|
|
self._pubkey = pubkey
|
|
|
|
def _populate_required_shares(self, required_shares):
|
|
|
|
self._required_shares = required_shares
|
|
|
|
def _populate_total_shares(self, total_shares):
|
|
|
|
self._total_shares = total_shares
|
|
|
|
|
|
|
|
def _populate_privkey(self, privkey):
|
|
|
|
self._privkey = privkey
|
|
|
|
def _populate_encprivkey(self, encprivkey):
|
|
|
|
self._encprivkey = encprivkey
|
2010-10-26 21:33:02 -07:00
|
|
|
def _add_to_cache(self, verinfo, shnum, offset, data):
|
|
|
|
self._cache.add(verinfo, shnum, offset, data)
|
2009-12-08 09:29:21 -08:00
|
|
|
def _read_from_cache(self, verinfo, shnum, offset, length):
|
|
|
|
return self._cache.read(verinfo, shnum, offset, length)
|
2008-04-11 14:31:16 -07:00
|
|
|
|
|
|
|
def get_write_enabler(self, peerid):
|
|
|
|
assert len(peerid) == 20
|
|
|
|
return hashutil.ssk_write_enabler_hash(self._writekey, peerid)
|
|
|
|
def get_renewal_secret(self, peerid):
|
|
|
|
assert len(peerid) == 20
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
crs = self._secret_holder.get_renewal_secret()
|
2008-04-11 14:31:16 -07:00
|
|
|
frs = hashutil.file_renewal_secret_hash(crs, self._storage_index)
|
|
|
|
return hashutil.bucket_renewal_secret_hash(frs, peerid)
|
|
|
|
def get_cancel_secret(self, peerid):
|
|
|
|
assert len(peerid) == 20
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
ccs = self._secret_holder.get_cancel_secret()
|
2008-04-11 14:31:16 -07:00
|
|
|
fcs = hashutil.file_cancel_secret_hash(ccs, self._storage_index)
|
|
|
|
return hashutil.bucket_cancel_secret_hash(fcs, peerid)
|
|
|
|
|
|
|
|
def get_writekey(self):
|
|
|
|
return self._writekey
|
|
|
|
def get_readkey(self):
|
|
|
|
return self._readkey
|
|
|
|
def get_storage_index(self):
|
|
|
|
return self._storage_index
|
2009-12-08 09:26:08 -08:00
|
|
|
def get_fingerprint(self):
|
|
|
|
return self._fingerprint
|
2008-04-11 14:31:16 -07:00
|
|
|
def get_privkey(self):
|
|
|
|
return self._privkey
|
|
|
|
def get_encprivkey(self):
|
|
|
|
return self._encprivkey
|
|
|
|
def get_pubkey(self):
|
|
|
|
return self._pubkey
|
|
|
|
|
|
|
|
def get_required_shares(self):
|
|
|
|
return self._required_shares
|
|
|
|
def get_total_shares(self):
|
|
|
|
return self._total_shares
|
|
|
|
|
2008-07-17 21:09:23 -07:00
|
|
|
####################################
|
|
|
|
# IFilesystemNode
|
2008-04-11 14:31:16 -07:00
|
|
|
|
|
|
|
def get_size(self):
|
2009-11-18 11:16:24 -08:00
|
|
|
return self._most_recent_size
|
|
|
|
def get_current_size(self):
|
|
|
|
d = self.get_size_of_best_version()
|
|
|
|
d.addCallback(self._stash_size)
|
|
|
|
return d
|
|
|
|
def _stash_size(self, size):
|
|
|
|
self._most_recent_size = size
|
|
|
|
return size
|
2009-11-11 14:25:42 -08:00
|
|
|
|
|
|
|
def get_cap(self):
|
|
|
|
return self._uri
|
|
|
|
def get_readcap(self):
|
|
|
|
return self._uri.get_readonly()
|
|
|
|
def get_verify_cap(self):
|
2009-11-11 14:45:42 -08:00
|
|
|
return self._uri.get_verify_cap()
|
2009-11-11 14:25:42 -08:00
|
|
|
def get_repair_cap(self):
|
|
|
|
if self._uri.is_readonly():
|
|
|
|
return None
|
|
|
|
return self._uri
|
|
|
|
|
|
|
|
def get_uri(self):
|
|
|
|
return self._uri.to_string()
|
2010-01-26 22:44:30 -08:00
|
|
|
|
|
|
|
def get_write_uri(self):
|
|
|
|
if self.is_readonly():
|
|
|
|
return None
|
|
|
|
return self._uri.to_string()
|
|
|
|
|
2009-11-11 14:25:42 -08:00
|
|
|
def get_readonly_uri(self):
|
|
|
|
return self._uri.get_readonly().to_string()
|
|
|
|
|
2008-04-11 14:31:16 -07:00
|
|
|
def get_readonly(self):
|
|
|
|
if self.is_readonly():
|
|
|
|
return self
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
ro = MutableFileNode(self._storage_broker, self._secret_holder,
|
|
|
|
self._default_encoding_parameters, self._history)
|
2009-11-11 14:25:42 -08:00
|
|
|
ro.init_from_cap(self._uri.get_readonly())
|
2008-04-11 14:31:16 -07:00
|
|
|
return ro
|
|
|
|
|
|
|
|
def is_mutable(self):
|
|
|
|
return self._uri.is_mutable()
|
2010-01-26 22:44:30 -08:00
|
|
|
|
2008-04-11 14:31:16 -07:00
|
|
|
def is_readonly(self):
|
|
|
|
return self._uri.is_readonly()
|
|
|
|
|
2010-01-26 22:44:30 -08:00
|
|
|
def is_unknown(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_allowed_in_immutable_directory(self):
|
|
|
|
return not self._uri.is_mutable()
|
|
|
|
|
|
|
|
def raise_error(self):
|
|
|
|
pass
|
|
|
|
|
2008-04-11 14:31:16 -07:00
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.__class__, self._uri))
|
|
|
|
def __cmp__(self, them):
|
|
|
|
if cmp(type(self), type(them)):
|
|
|
|
return cmp(type(self), type(them))
|
|
|
|
if cmp(self.__class__, them.__class__):
|
|
|
|
return cmp(self.__class__, them.__class__)
|
|
|
|
return cmp(self._uri, them._uri)
|
|
|
|
|
2008-04-17 17:51:38 -07:00
|
|
|
def _do_serialized(self, cb, *args, **kwargs):
|
|
|
|
# note: to avoid deadlock, this callable is *not* allowed to invoke
|
|
|
|
# other serialized methods within this (or any other)
|
|
|
|
# MutableFileNode. The callable should be a bound method of this same
|
|
|
|
# MFN instance.
|
2008-04-11 14:31:16 -07:00
|
|
|
d = defer.Deferred()
|
2008-04-17 17:51:38 -07:00
|
|
|
self._serializer.addCallback(lambda ignore: cb(*args, **kwargs))
|
2008-04-18 00:43:29 -07:00
|
|
|
# we need to put off d.callback until this Deferred is finished being
|
|
|
|
# processed. Otherwise the caller's subsequent activities (like,
|
|
|
|
# doing other things with this node) can cause reentrancy problems in
|
|
|
|
# the Deferred code itself
|
|
|
|
self._serializer.addBoth(lambda res: eventually(d.callback, res))
|
|
|
|
# add a log.err just in case something really weird happens, because
|
|
|
|
# self._serializer stays around forever, therefore we won't see the
|
|
|
|
# usual Unhandled Error in Deferred that would give us a hint.
|
|
|
|
self._serializer.addErrback(log.err)
|
2008-04-11 14:31:16 -07:00
|
|
|
return d
|
|
|
|
|
2008-04-17 17:51:38 -07:00
|
|
|
#################################
|
2008-07-17 21:09:23 -07:00
|
|
|
# ICheckable
|
2008-04-11 14:31:16 -07:00
|
|
|
|
2009-02-17 19:32:43 -07:00
|
|
|
def check(self, monitor, verify=False, add_lease=False):
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
checker = MutableChecker(self, self._storage_broker,
|
|
|
|
self._history, monitor)
|
2009-02-17 19:32:43 -07:00
|
|
|
return checker.check(verify, add_lease)
|
2008-04-11 14:31:16 -07:00
|
|
|
|
2009-02-17 19:32:43 -07:00
|
|
|
def check_and_repair(self, monitor, verify=False, add_lease=False):
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
checker = MutableCheckAndRepairer(self, self._storage_broker,
|
|
|
|
self._history, monitor)
|
2009-02-17 19:32:43 -07:00
|
|
|
return checker.check(verify, add_lease)
|
2008-09-07 12:44:56 -07:00
|
|
|
|
2008-07-17 21:09:23 -07:00
|
|
|
#################################
|
|
|
|
# IRepairable
|
|
|
|
|
2009-01-06 13:37:03 -07:00
|
|
|
def repair(self, check_results, force=False):
|
|
|
|
assert ICheckResults(check_results)
|
|
|
|
r = Repairer(self, check_results)
|
2008-08-06 12:06:07 -07:00
|
|
|
d = r.start(force)
|
2008-07-17 21:09:23 -07:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
#################################
|
|
|
|
# IMutableFileNode
|
|
|
|
|
2008-04-17 17:51:38 -07:00
|
|
|
def download_best_version(self):
|
|
|
|
return self._do_serialized(self._download_best_version)
|
|
|
|
def _download_best_version(self):
|
|
|
|
servermap = ServerMap()
|
|
|
|
d = self._try_once_to_download_best_version(servermap, MODE_READ)
|
|
|
|
def _maybe_retry(f):
|
|
|
|
f.trap(NotEnoughSharesError)
|
|
|
|
# the download is worth retrying once. Make sure to use the
|
|
|
|
# old servermap, since it is what remembers the bad shares,
|
|
|
|
# but use MODE_WRITE to make it look for even more shares.
|
|
|
|
# TODO: consider allowing this to retry multiple times.. this
|
|
|
|
# approach will let us tolerate about 8 bad shares, I think.
|
|
|
|
return self._try_once_to_download_best_version(servermap,
|
|
|
|
MODE_WRITE)
|
|
|
|
d.addErrback(_maybe_retry)
|
|
|
|
return d
|
|
|
|
def _try_once_to_download_best_version(self, servermap, mode):
|
|
|
|
d = self._update_servermap(servermap, mode)
|
2008-04-17 19:12:42 -07:00
|
|
|
d.addCallback(self._once_updated_download_best_version, servermap)
|
2008-04-11 14:31:16 -07:00
|
|
|
return d
|
2008-04-17 19:12:42 -07:00
|
|
|
def _once_updated_download_best_version(self, ignored, servermap):
|
|
|
|
goal = servermap.best_recoverable_version()
|
|
|
|
if not goal:
|
|
|
|
raise UnrecoverableFileError("no recoverable versions")
|
|
|
|
return self._try_once_to_download_version(servermap, goal)
|
2008-04-17 17:51:38 -07:00
|
|
|
|
2008-08-12 19:02:52 -07:00
|
|
|
def get_size_of_best_version(self):
|
|
|
|
d = self.get_servermap(MODE_READ)
|
|
|
|
def _got_servermap(smap):
|
|
|
|
ver = smap.best_recoverable_version()
|
|
|
|
if not ver:
|
|
|
|
raise UnrecoverableFileError("no recoverable version")
|
|
|
|
return smap.size_of_version(ver)
|
|
|
|
d.addCallback(_got_servermap)
|
|
|
|
return d
|
|
|
|
|
2008-04-17 17:51:38 -07:00
|
|
|
def overwrite(self, new_contents):
|
|
|
|
return self._do_serialized(self._overwrite, new_contents)
|
|
|
|
def _overwrite(self, new_contents):
|
|
|
|
servermap = ServerMap()
|
|
|
|
d = self._update_servermap(servermap, mode=MODE_WRITE)
|
|
|
|
d.addCallback(lambda ignored: self._upload(new_contents, servermap))
|
2008-04-11 14:31:16 -07:00
|
|
|
return d
|
|
|
|
|
2008-04-17 17:51:38 -07:00
|
|
|
|
2008-04-17 19:12:42 -07:00
|
|
|
def modify(self, modifier, backoffer=None):
|
2008-04-11 14:31:16 -07:00
|
|
|
"""I use a modifier callback to apply a change to the mutable file.
|
|
|
|
I implement the following pseudocode::
|
|
|
|
|
|
|
|
obtain_mutable_filenode_lock()
|
2008-12-05 22:07:10 -07:00
|
|
|
first_time = True
|
2008-04-11 14:31:16 -07:00
|
|
|
while True:
|
|
|
|
update_servermap(MODE_WRITE)
|
|
|
|
old = retrieve_best_version()
|
2008-12-05 22:07:10 -07:00
|
|
|
new = modifier(old, servermap, first_time)
|
|
|
|
first_time = False
|
2008-04-11 14:31:16 -07:00
|
|
|
if new == old: break
|
|
|
|
try:
|
|
|
|
publish(new)
|
2008-04-17 19:12:42 -07:00
|
|
|
except UncoordinatedWriteError, e:
|
|
|
|
backoffer(e)
|
2008-04-11 14:31:16 -07:00
|
|
|
continue
|
|
|
|
break
|
|
|
|
release_mutable_filenode_lock()
|
|
|
|
|
|
|
|
The idea is that your modifier function can apply a delta of some
|
|
|
|
sort, and it will be re-run as necessary until it succeeds. The
|
|
|
|
modifier must inspect the old version to see whether its delta has
|
|
|
|
already been applied: if so it should return the contents unmodified.
|
2008-04-17 17:51:38 -07:00
|
|
|
|
|
|
|
Note that the modifier is required to run synchronously, and must not
|
|
|
|
invoke any methods on this MutableFileNode instance.
|
2008-04-17 19:12:42 -07:00
|
|
|
|
|
|
|
The backoff-er is a callable that is responsible for inserting a
|
|
|
|
random delay between subsequent attempts, to help competing updates
|
|
|
|
from colliding forever. It is also allowed to give up after a while.
|
|
|
|
The backoffer is given two arguments: this MutableFileNode, and the
|
|
|
|
Failure object that contains the UncoordinatedWriteError. It should
|
|
|
|
return a Deferred that will fire when the next attempt should be
|
|
|
|
made, or return the Failure if the loop should give up. If
|
|
|
|
backoffer=None, a default one is provided which will perform
|
|
|
|
exponential backoff, and give up after 4 tries. Note that the
|
|
|
|
backoffer should not invoke any methods on this MutableFileNode
|
|
|
|
instance, and it needs to be highly conscious of deadlock issues.
|
2008-04-11 14:31:16 -07:00
|
|
|
"""
|
2008-04-17 19:12:42 -07:00
|
|
|
return self._do_serialized(self._modify, modifier, backoffer)
|
|
|
|
def _modify(self, modifier, backoffer):
|
|
|
|
servermap = ServerMap()
|
|
|
|
if backoffer is None:
|
|
|
|
backoffer = BackoffAgent().delay
|
2008-12-05 22:07:10 -07:00
|
|
|
return self._modify_and_retry(servermap, modifier, backoffer, True)
|
|
|
|
def _modify_and_retry(self, servermap, modifier, backoffer, first_time):
|
|
|
|
d = self._modify_once(servermap, modifier, first_time)
|
2008-04-17 19:12:42 -07:00
|
|
|
def _retry(f):
|
|
|
|
f.trap(UncoordinatedWriteError)
|
|
|
|
d2 = defer.maybeDeferred(backoffer, self, f)
|
|
|
|
d2.addCallback(lambda ignored:
|
|
|
|
self._modify_and_retry(servermap, modifier,
|
2008-12-05 22:07:10 -07:00
|
|
|
backoffer, False))
|
2008-04-17 19:12:42 -07:00
|
|
|
return d2
|
|
|
|
d.addErrback(_retry)
|
|
|
|
return d
|
2008-12-05 22:07:10 -07:00
|
|
|
def _modify_once(self, servermap, modifier, first_time):
|
2008-04-17 19:12:42 -07:00
|
|
|
d = self._update_servermap(servermap, MODE_WRITE)
|
|
|
|
d.addCallback(self._once_updated_download_best_version, servermap)
|
|
|
|
def _apply(old_contents):
|
2008-12-05 22:07:10 -07:00
|
|
|
new_contents = modifier(old_contents, servermap, first_time)
|
2008-04-17 19:12:42 -07:00
|
|
|
if new_contents is None or new_contents == old_contents:
|
|
|
|
# no changes need to be made
|
2008-12-05 22:49:23 -07:00
|
|
|
if first_time:
|
|
|
|
return
|
|
|
|
# However, since Publish is not automatically doing a
|
|
|
|
# recovery when it observes UCWE, we need to do a second
|
|
|
|
# publish. See #551 for details. We'll basically loop until
|
|
|
|
# we managed an uncontested publish.
|
|
|
|
new_contents = old_contents
|
2008-04-17 19:12:42 -07:00
|
|
|
precondition(isinstance(new_contents, str),
|
|
|
|
"Modifier function must return a string or None")
|
|
|
|
return self._upload(new_contents, servermap)
|
|
|
|
d.addCallback(_apply)
|
|
|
|
return d
|
2008-04-11 14:31:16 -07:00
|
|
|
|
2008-04-17 17:51:38 -07:00
|
|
|
def get_servermap(self, mode):
|
|
|
|
return self._do_serialized(self._get_servermap, mode)
|
|
|
|
def _get_servermap(self, mode):
|
|
|
|
servermap = ServerMap()
|
|
|
|
return self._update_servermap(servermap, mode)
|
|
|
|
def _update_servermap(self, servermap, mode):
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
u = ServermapUpdater(self, self._storage_broker, Monitor(), servermap,
|
|
|
|
mode)
|
|
|
|
if self._history:
|
|
|
|
self._history.notify_mapupdate(u.get_status())
|
2008-04-16 19:05:41 -07:00
|
|
|
return u.update()
|
2008-04-16 17:49:06 -07:00
|
|
|
|
2008-08-26 16:34:54 -07:00
|
|
|
def download_version(self, servermap, version, fetch_privkey=False):
|
2008-04-17 17:51:38 -07:00
|
|
|
return self._do_serialized(self._try_once_to_download_version,
|
2008-08-26 16:34:54 -07:00
|
|
|
servermap, version, fetch_privkey)
|
|
|
|
def _try_once_to_download_version(self, servermap, version,
|
|
|
|
fetch_privkey=False):
|
|
|
|
r = Retrieve(self, servermap, version, fetch_privkey)
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
if self._history:
|
|
|
|
self._history.notify_retrieve(r.get_status())
|
2009-11-18 11:16:24 -08:00
|
|
|
d = r.download()
|
|
|
|
d.addCallback(self._downloaded_version)
|
|
|
|
return d
|
|
|
|
def _downloaded_version(self, data):
|
|
|
|
self._most_recent_size = len(data)
|
|
|
|
return data
|
2008-04-11 14:31:16 -07:00
|
|
|
|
2008-04-17 17:51:38 -07:00
|
|
|
def upload(self, new_contents, servermap):
|
|
|
|
return self._do_serialized(self._upload, new_contents, servermap)
|
|
|
|
def _upload(self, new_contents, servermap):
|
2008-04-16 17:49:06 -07:00
|
|
|
assert self._pubkey, "update_servermap must be called before publish"
|
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally.
* stop using IURI as an adapter
* pass cap strings around instead of URI instances
* move filenode/dirnode creation duties from Client to new NodeMaker class
* move other Client duties to KeyGenerator, SecretHolder, History classes
* stop passing Client reference to dirnode/filenode constructors
- pass less-powerful references instead, like StorageBroker or Uploader
* always create DirectoryNodes by wrapping a filenode (mutable for now)
* remove some specialized mock classes from unit tests
Detailed list of changes (done one at a time, then merged together)
always pass a string to create_node_from_uri(), not an IURI instance
always pass a string to IFilesystemNode constructors, not an IURI instance
stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri()
client.py: move SecretHolder code out to a separate class
test_web.py: hush pyflakes
client.py: move NodeMaker functionality out into a separate object
LiteralFileNode: stop storing a Client reference
immutable Checker: remove Client reference, it only needs a SecretHolder
immutable Upload: remove Client reference, leave SecretHolder and StorageBroker
immutable Repairer: replace Client reference with StorageBroker and SecretHolder
immutable FileNode: remove Client reference
mutable.Publish: stop passing Client
mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference
MutableChecker: reference StorageBroker and History directly, not through Client
mutable.FileNode: removed unused indirection to checker classes
mutable.FileNode: remove Client reference
client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker
move create_mutable_file() into NodeMaker
test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests.
test_mutable.py: clean up basedir names
client.py: move create_empty_dirnode() into NodeMaker
dirnode.py: get rid of DirectoryNode.create
remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match
stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker
remove Client from NodeMaker
move helper status into History, pass History to web.Status instead of Client
test_mutable.py: fix minor typo
2009-08-15 04:02:56 -07:00
|
|
|
p = Publish(self, self._storage_broker, servermap)
|
|
|
|
if self._history:
|
|
|
|
self._history.notify_publish(p.get_status(), len(new_contents))
|
2009-11-18 11:16:24 -08:00
|
|
|
d = p.publish(new_contents)
|
|
|
|
d.addCallback(self._did_upload, len(new_contents))
|
|
|
|
return d
|
|
|
|
def _did_upload(self, res, size):
|
|
|
|
self._most_recent_size = size
|
|
|
|
return res
|