Changeset c6fc8266 in trunk


Ignore:
Timestamp:
2022-11-29T14:33:05Z (2 years ago)
Author:
Jean-Paul Calderone <exarkun@…>
Branches:
master
Children:
c4c9d138
Parents:
537ab5c
Message:

Pull _make_storage_system into a free function for easier testing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/storage_client.py

    r537ab5c rc6fc8266  
    3434
    3535from six import ensure_text
    36 from typing import Union
     36from typing import Union, Callable, Optional
    3737import re, time, hashlib
    3838from os import urandom
     
    5858    log_call,
    5959)
     60from foolscap.ipb import IRemoteReference
    6061from foolscap.api import eventually, RemoteException
    6162from foolscap.reconnector import (
     
    8182    ReadVector, TestWriteVectors, WriteVector, TestVector, ClientException
    8283)
     84from .node import _Config
     85
     86_log = Logger()
    8387
    8488ANONYMOUS_STORAGE_NURLS = "anonymous-storage-NURLs"
     
    733737
    734738
     739def _make_storage_system(
     740        node_config: _Config,
     741        config: StorageClientConfig,
     742        ann: dict,
     743        server_id: bytes,
     744        get_rref: Callable[[], Optional[IRemoteReference]],
     745) -> IFoolscapStorageServer:
     746    """
     747    Create an object for interacting with the storage server described by
     748    the given announcement.
     749
     750    :param node_config: The node configuration to pass to any configured
     751        storage plugins.
     752
     753    :param config: Configuration specifying desired storage client behavior.
     754
     755    :param ann: The storage announcement from the storage server we are meant
     756        to communicate with.
     757
     758    :param server_id: The unique identifier for the server.
     759
     760    :param get_rref: A function which returns a remote reference to the
     761        server-side object which implements this storage system, if one is
     762        available (otherwise None).
     763
     764    :return: An object enabling communication via Foolscap with the server
     765        which generated the announcement.
     766    """
     767    # Try to match the announcement against a plugin.
     768    try:
     769        furl, storage_server = _storage_from_foolscap_plugin(
     770            node_config,
     771            config,
     772            ann,
     773            # Pass in an accessor for our _rref attribute.  The value of
     774            # the attribute may change over time as connections are lost
     775            # and re-established.  The _StorageServer should always be
     776            # able to get the most up-to-date value.
     777            get_rref,
     778        )
     779    except AnnouncementNotMatched as e:
     780        _log.error(
     781            'No plugin for storage-server "{nickname}" from plugins: {plugins}',
     782            nickname=ann.get("nickname", "<unknown>"),
     783            plugins=e.args[0],
     784        )
     785    except MissingPlugin as e:
     786        _log.failure("Missing plugin")
     787        ns = _NullStorage()
     788        ns.longname = '<missing plugin "{}">'.format(e.args[0])
     789        return ns
     790    else:
     791        return _FoolscapStorage.from_announcement(
     792            server_id,
     793            furl,
     794            ann,
     795            storage_server,
     796        )
     797
     798    # Try to match the announcement against the anonymous access scheme.
     799    try:
     800        furl = ann[u"anonymous-storage-FURL"]
     801    except KeyError:
     802        # Nope
     803        pass
     804    else:
     805        # See comment above for the _storage_from_foolscap_plugin case
     806        # about passing in get_rref.
     807        storage_server = _StorageServer(get_rref=get_rref)
     808        return _FoolscapStorage.from_announcement(
     809            server_id,
     810            furl,
     811            ann,
     812            storage_server,
     813        )
     814
     815    # Nothing matched so we can't talk to this server.
     816    return _null_storage
     817
    735818@implementer(IServer)
    736819class NativeStorageServer(service.MultiService):
     
    759842        "application-version": "unknown: no get_version()",
    760843        })
    761     log = Logger()
    762844
    763845    def __init__(self, server_id, ann, tub_maker, handler_overrides, node_config, config=StorageClientConfig()):
     
    769851        self._handler_overrides = handler_overrides
    770852
    771         self._storage = self._make_storage_system(node_config, config, ann)
     853        self._storage = _make_storage_system(node_config, config, ann, self._server_id, self.get_rref)
    772854
    773855        self.last_connect_time = None
     
    778860        self._trigger_cb = None
    779861        self._on_status_changed = ObserverList()
    780 
    781     def _make_storage_system(self, node_config, config, ann):
    782         """
    783         :param allmydata.node._Config node_config: The node configuration to pass
    784             to any configured storage plugins.
    785 
    786         :param StorageClientConfig config: Configuration specifying desired
    787             storage client behavior.
    788 
    789         :param dict ann: The storage announcement from the storage server we
    790             are meant to communicate with.
    791 
    792         :return IFoolscapStorageServer: An object enabling communication via
    793             Foolscap with the server which generated the announcement.
    794         """
    795         # Try to match the announcement against a plugin.
    796         try:
    797             furl, storage_server = _storage_from_foolscap_plugin(
    798                 node_config,
    799                 config,
    800                 ann,
    801                 # Pass in an accessor for our _rref attribute.  The value of
    802                 # the attribute may change over time as connections are lost
    803                 # and re-established.  The _StorageServer should always be
    804                 # able to get the most up-to-date value.
    805                 self.get_rref,
    806             )
    807         except AnnouncementNotMatched as e:
    808             self.log.error(
    809                 'No plugin for storage-server "{nickname}" from plugins: {plugins}',
    810                 nickname=ann.get("nickname", "<unknown>"),
    811                 plugins=e.args[0],
    812             )
    813         except MissingPlugin as e:
    814             self.log.failure("Missing plugin")
    815             ns = _NullStorage()
    816             ns.longname = '<missing plugin "{}">'.format(e.args[0])
    817             return ns
    818         else:
    819             return _FoolscapStorage.from_announcement(
    820                 self._server_id,
    821                 furl,
    822                 ann,
    823                 storage_server,
    824             )
    825 
    826         # Try to match the announcement against the anonymous access scheme.
    827         try:
    828             furl = ann[u"anonymous-storage-FURL"]
    829         except KeyError:
    830             # Nope
    831             pass
    832         else:
    833             # See comment above for the _storage_from_foolscap_plugin case
    834             # about passing in get_rref.
    835             storage_server = _StorageServer(get_rref=self.get_rref)
    836             return _FoolscapStorage.from_announcement(
    837                 self._server_id,
    838                 furl,
    839                 ann,
    840                 storage_server,
    841             )
    842 
    843         # Nothing matched so we can't talk to this server.
    844         return _null_storage
    845862
    846863    def get_permutation_seed(self):
Note: See TracChangeset for help on using the changeset viewer.