コード例 #1
0
def _main(host, port, unix=None, days=1, username=None, password=None,
         realm=None, blob_dir=None, storage='1', shared_blob_dir=True):
    if unix is not None:
        addr = unix
    else:
        if host is None:
            host = socket.gethostname()
        addr = host, int(port)

    if blob_dir:
        blob_dir = os.path.abspath(blob_dir)

    # We do not want to wait until a zeoserver is up and running; it
    # should already be running.
    wait = False
    cs = None
    try:
        cs = ClientStorage(
            addr, storage=storage, wait=wait, read_only=True,
            username=username, password=password, realm=realm,
            blob_dir=blob_dir, shared_blob_dir=shared_blob_dir,
        )
        try:
            cs.pack(wait=wait, days=int(days))
        except ClientDisconnected:
            logger = logging.getLogger(__name__)
            logger.error("Could not connect to zeoserver. Please make sure it "
                         "is running.")
            sys.exit(1)
    finally:
        if cs is not None:
            cs.close()
コード例 #2
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
 def __init__(self, name, blob_dir, shared=False, extrafsoptions=""):
     if shared:
         server_blob_dir = blob_dir
     else:
         server_blob_dir = "server-" + blob_dir
     self.globs = {}
     port = forker.get_port2(self)
     addr, admin, pid, config = forker.start_zeo_server(
         """
         <blobstorage>
             blob-dir %s
             <filestorage>
                path %s
                %s
             </filestorage>
         </blobstorage>
         """
         % (server_blob_dir, name + ".fs", extrafsoptions),
         port=port,
     )
     os.remove(config)
     zope.testing.setupstack.register(self, os.waitpid, pid, 0)
     zope.testing.setupstack.register(self, forker.shutdown_zeo_server, admin)
     if shared:
         ClientStorage.__init__(self, addr, blob_dir=blob_dir, shared_blob_dir=True)
     else:
         ClientStorage.__init__(self, addr, blob_dir=blob_dir)
コード例 #3
0
class remoteZODB(object):

    def __init__(self, server, port):
        server_and_port = (server, port)
        self.storage = ClientStorage(server_and_port, storage='data',
                                     read_only=True, wait=False, )
        self.db = DB(self.storage)
        self.connection = self.db.open()
        self.dbroot = self.connection.root()

    def close(self):
        self.connection.close()
        self.db.close()
        self.storage.close()
コード例 #4
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
    def _new_storage(self):
        port = get_port(self)
        zconf = forker.ZEOConfig(("", port))
        zport, adminaddr, pid, path = forker.start_zeo_server(self.getConfig(), zconf, port)
        self._pids.append(pid)
        self._servers.append(adminaddr)

        blob_cache_dir = tempfile.mkdtemp(dir=".")

        storage = ClientStorage(
            zport, "1", cache_size=20000000, min_disconnect_poll=0.5, wait=1, wait_timeout=60, blob_dir=blob_cache_dir
        )
        storage.registerDB(DummyDB())
        return storage
コード例 #5
0
ファイル: testZEO.py プロジェクト: wpjunior/proled
 def _makeBaseStorage(self):
     logger.info("setUp() %s", self.id())
     port = get_port()
     zconf = forker.ZEOConfig(('', port))
     zport, adminaddr, pid, path = forker.start_zeo_server(self.getConfig(),
                                                           zconf, port)
     self._pids = [pid]
     self._servers = [adminaddr]
     self._conf_path = path
     _base = ClientStorage(zport, '1', cache_size=20000000,
                                   min_disconnect_poll=0.5, wait=1,
                                   wait_timeout=60)
     _base.registerDB(DummyDB(), None)
     return _base
コード例 #6
0
 def __init__(self, server, port):
     server_and_port = (server, port)
     self.storage = ClientStorage(server_and_port, storage='data',
                                  read_only=True, wait=False, )
     self.db = DB(self.storage)
     self.connection = self.db.open()
     self.dbroot = self.connection.root()
コード例 #7
0
ファイル: DB.py プロジェクト: HVF/Python-OAuth-2.0-Provider
    class DB(BaseDB):
        def __init__(self, server=SERVER, port=PORT):
            self.storage = ClientStorage((server, port,))
            self.db = ZDB(self.storage)
            self.connection = self.db.open()
            self.dbroot = self.connection.root()


        # KISS policy
        # let the lookup raise its own exception on a key lookup problem, etc
        def get(self, key):
            return self.dbroot[key]


        def put(self, key, data):
            self.dbroot[key] = data

        def update(self, key, data):
            if isinstance(data, Persistent):
                data._p_changed = True
            else:
                self.dbroot[key] = data

        def delete(self, key):
            if key in self.dbroot:
                del self.dbroot[key]
            

        def commit(self):
            transaction.commit()


        def abort(self):
            transaction.abort()

        def contains(self, key):
            return key in self.dbroot



        def close(self):
            self.connection.close()
            self.db.close()
            self.storage.close()
コード例 #8
0
ファイル: zeopack.py プロジェクト: OS2World/APP-SERVER-Zope
def pack(addr, storage, days, wait):
    cs = ClientStorage(addr, storage=storage, wait_for_server_on_startup=wait)
    if wait:
        # _startup() is an artifact of the way ZEO 1.0 works.  The
        # ClientStorage doesn't get fully initialized until registerDB()
        # is called.  The only thing we care about, though, is that
        # registerDB() calls _startup().
        cs._startup()
    else:
        connect(cs)
    cs.invalidator = None
    cs.pack(wait=1, days=days)
    cs.close()
コード例 #9
0
ファイル: db.py プロジェクト: davidmorrison/indico
    def __init__( self, hostname=None, port=None ):
        import Configuration # Please leave this import here, db.py is imported during installation process
        cfg = Configuration.Config.getInstance()

        if not hostname:
            hostname = cfg.getDBConnectionParams()[0]
        if not port:
            port = cfg.getDBConnectionParams()[1]

        self._storage=ClientStorage((hostname, port), username=cfg.getDBUserName(), password=cfg.getDBPassword(), realm=cfg.getDBRealm())
        self._db=MaKaCDB(self._storage)
        self._conn={}
コード例 #10
0
ファイル: testZEO.py プロジェクト: wpjunior/proled
 def checkZEOInvalidation(self):
     addr = self._storage._addr
     storage2 = ClientStorage(addr, wait=1, min_disconnect_poll=0.1)
     try:
         oid = self._storage.new_oid()
         ob = MinPO('first')
         revid1 = self._dostore(oid, data=ob)
         data, serial = storage2.load(oid, '')
         self.assertEqual(zodb_unpickle(data), MinPO('first'))
         self.assertEqual(serial, revid1)
         revid2 = self._dostore(oid, data=MinPO('second'), revid=revid1)
         for n in range(3):
             # Let the server and client talk for a moment.
             # Is there a better way to do this?
             asyncore.poll(0.1)
         data, serial = storage2.load(oid, '')
         self.assertEqual(zodb_unpickle(data), MinPO('second'),
                          'Invalidation message was not sent!')
         self.assertEqual(serial, revid2)
     finally:
         storage2.close()
コード例 #11
0
def main(host, port, unix=None, days=1, username=None, password=None,
         realm=None, blob_dir=None, storage='1'):
    if unix is not None:
        addr = unix
    else:
        if host is None:
            host = socket.gethostname()
        addr = host, int(port)

    wait = True
    cs = None
    try:
        if HAS_BLOB:
            cs = ClientStorage(
                addr, storage=storage, wait=wait, read_only=True,
                username=username, password=password, realm=realm,
                blob_dir=blob_dir
            )
        else:
            cs = ClientStorage(
                addr, storage=storage, wait=wait, read_only=True,
                username=username, password=password, realm=realm
            )
        cs.pack(wait=wait, days=int(days))
    finally:
        if cs is not None:
            cs.close()
コード例 #12
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
    def checkConnectionInvalidationOnReconnect(self):

        storage = ClientStorage(self.addr, wait=1, min_disconnect_poll=0.1)
        self._storage = storage

        # and we'll wait for the storage to be reconnected:
        for i in range(100):
            if storage.is_connected():
                break
            time.sleep(0.1)
        else:
            raise AssertionError("Couldn't connect to server")

        class DummyDB:
            _invalidatedCache = 0

            def invalidateCache(self):
                self._invalidatedCache += 1

            def invalidate(*a, **k):
                pass

        db = DummyDB()
        storage.registerDB(db)

        base = db._invalidatedCache

        # Now we'll force a disconnection and reconnection
        storage._connection.close()

        # and we'll wait for the storage to be reconnected:
        for i in range(100):
            if storage.is_connected():
                break
            time.sleep(0.1)
        else:
            raise AssertionError("Couldn't connect to server")

        # Now, the root object in the connection should have been invalidated:
        self.assertEqual(db._invalidatedCache, base + 1)
コード例 #13
0
def main():
    z1 = ClientStorage(('localhost', 2001), wait=1)
    z2 = ClientStorage(('localhost', 2002), wait=2)
    db1 = ZODB.DB(z1)
    db2 = ZODB.DB(z2)
    c1 = db1.open()
    c2 = db2.open()
    r1 = c1.root()
    r2 = c2.root()

    while 1:
        try:
            try:
                update(r1, r2)
            except ConflictError, msg:
                print msg
                transaction.abort()
                c1.sync()
                c2.sync()
        except (ClientDisconnected, DisconnectedError), err:
            print "disconnected", err
            time.sleep(2)
コード例 #14
0
ファイル: cache_logging.py プロジェクト: mrmaple/open_qon
    def __init__(self, addr, storage='1', cache_size=20 * MB,
                 name='', client=None, debug=0, var=None,
                 min_disconnect_poll=5, max_disconnect_poll=300,
                 wait_for_server_on_startup=None, # deprecated alias for wait
                 wait=None, wait_timeout=None,
                 read_only=0, read_only_fallback=0,
                 username='', password='', realm=None):


        # swap out the ClientCache with QonClientCache if desired.
        self.ClientCacheClass = ClientCache
        if qon.local.CACHE_INSTRUMENTATION:
            self.ClientCacheClass = QonClientCache
            
        ClientStorage.__init__(self, addr, storage, cache_size, \
                               name, client, debug, var, \
                               min_disconnect_poll, max_disconnect_poll, \
                               wait_for_server_on_startup, \
                               wait, wait_timeout, \
                               read_only, read_only_fallback, \
                               username, password, realm)

        # fix the cache_size bug that we manually patched previously.
        # once ZEO's code is fixed, we can remove everything after
        #  this line of code in this routine.
        if self._cache:
            self._cache.close()
            del self._cache

        # Decide whether to use non-temporary files
        if client is not None:
            dir = var or os.getcwd()
            cache_path = os.path.join(dir, "%s-%s.zec" % (client, storage))
        else:
            cache_path = None

        # create the cache            
        self._cache = self.ClientCacheClass(cache_path, size=cache_size) # this here is that actual cache_size fix
        self._cache.open()
コード例 #15
0
ファイル: timeout.py プロジェクト: bendavis78/zope
def main():
    if len(sys.argv) not in (3, 4):
        sys.stderr.write("Usage: timeout.py address delay [storage-name]\n" %
                         sys.argv[0])
        sys.exit(2)

    hostport = sys.argv[1]
    delay = float(sys.argv[2])
    if sys.argv[3:]:
        name = sys.argv[3]
    else:
        name = "1"

    if "/" in hostport:
        address = hostport
    else:
        if ":" in hostport:
            i = hostport.index(":")
            host, port = hostport[:i], hostport[i + 1:]
        else:
            host, port = "", hostport
        port = int(port)
        address = (host, port)

    print "Connecting to %s..." % repr(address)
    storage = ClientStorage(address, name)
    print "Connected.  Now starting a transaction..."

    oid = storage.new_oid()
    version = ""
    revid = ZERO
    data = MinPO("timeout.py")
    pickled_data = zodb_pickle(data)
    t = Transaction()
    t.user = "******"
    storage.tpc_begin(t)
    storage.store(oid, revid, pickled_data, version, t)
    print "Stored.  Now voting..."
    storage.tpc_vote(t)

    print "Voted; now sleeping %s..." % delay
    time.sleep(delay)
    print "Done."
コード例 #16
0
ファイル: storage.py プロジェクト: dbrandt/spambayes-lite
 def create_storage(self):
     from ZEO.ClientStorage import ClientStorage
     if self.port:
         addr = self.host, self.port
     else:
         addr = self.host
     if options["globals", "verbose"]:
         print("Connecting to ZEO server", addr, \
               self.username, self.password, file=sys.stderr)
     # Use persistent caches, with the cache in the temp directory.
     # If the temp directory is cleared out, we lose the cache, but
     # that doesn't really matter, and we should always be able to
     # write to it.
     try:
         self.storage = ClientStorage(addr, name=self.db_name,
                                      read_only=self.mode=='r',
                                      username=self.username,
                                      client=self.db_name,
                                      wait=self.wait,
                                      wait_timeout=self.wait_timeout,
                                      storage=self.storage_name,
                                      var=tempfile.gettempdir(),
                                      password=self.password)
     except ValueError:
         # Probably bad cache; remove it and try without the cache.
         try:
             os.remove(os.path.join(tempfile.gettempdir(),
                                    self.db_name + \
                                    self.storage_name + ".zec"))
         except OSError:
             pass
         self.storage = ClientStorage(addr, name=self.db_name,
                                      read_only=self.mode=='r',
                                      username=self.username,
                                      wait=self.wait,
                                      wait_timeout=self.wait_timeout,
                                      storage=self.storage_name,
                                      password=self.password)
コード例 #17
0
    def create_storage(self):
        from ZEO.ClientStorage import ClientStorage

        if self.port:
            addr = self.host, self.port
        else:
            addr = self.host
        if options["globals", "verbose"]:
            print("Connecting to ZEO server", addr, self.username, self.password, file=sys.stderr)
        try:
            self.storage = ClientStorage(
                addr,
                name=self.db_name,
                read_only=self.mode == "r",
                username=self.username,
                client=self.db_name,
                wait=self.wait,
                wait_timeout=self.wait_timeout,
                storage=self.storage_name,
                var=tempfile.gettempdir(),
                password=self.password,
            )
        except ValueError:
            try:
                os.remove(os.path.join(tempfile.gettempdir(), self.db_name + self.storage_name + ".zec"))
            except OSError:
                pass
            self.storage = ClientStorage(
                addr,
                name=self.db_name,
                read_only=self.mode == "r",
                username=self.username,
                wait=self.wait,
                wait_timeout=self.wait_timeout,
                storage=self.storage_name,
                password=self.password,
            )
コード例 #18
0
ファイル: zeoup.py プロジェクト: wpjunior/proled
def check_server(addr, storage, write):
    t0 = time.time()
    if ZEO_VERSION == 2:
        # TODO:  should do retries w/ exponential backoff.
        cs = ClientStorage(addr, storage=storage, wait=0,
                           read_only=(not write))
    else:
        cs = ClientStorage(addr, storage=storage, debug=1,
                           wait_for_server_on_startup=1)
    # _startup() is an artifact of the way ZEO 1.0 works.  The
    # ClientStorage doesn't get fully initialized until registerDB()
    # is called.  The only thing we care about, though, is that
    # registerDB() calls _startup().

    if write:
        db = ZODB.DB(cs)
        cn = db.open()
        root = cn.root()
        try:
            # We store the data in a special `monitor' dict under the root,
            # where other tools may also store such heartbeat and bookkeeping
            # type data.
            monitor = root.get('monitor')
            if monitor is None:
                monitor = root['monitor'] = PersistentMapping()
            obj = monitor['zeoup'] = monitor.get('zeoup', MinPO(0))
            obj.value += 1
            transaction.commit()
        except ConflictError:
            pass
        cn.close()
        db.close()
    else:
        data, serial = cs.load("\0\0\0\0\0\0\0\0", "")
        cs.close()
    t1 = time.time()
    print "Elapsed time: %.2f" % (t1 - t0)
コード例 #19
0
ファイル: pack.py プロジェクト: isotoma/isotoma.recipe.zeo
def _main(host, port, unix=None, days=1, username=None, password=None,
         realm=None, blob_dir=None, storage='1', shared_blob_dir=True):
    if unix is not None:
        addr = unix
    else:
        if host is None:
            host = socket.gethostname()
        addr = host, int(port)

    if blob_dir:
        blob_dir = os.path.abspath(blob_dir)

    wait = True
    cs = None
    try:
        cs = ClientStorage(
            addr, storage=storage, wait=wait, read_only=True,
            username=username, password=password, realm=realm,
            blob_dir=blob_dir, shared_blob_dir=shared_blob_dir,
        )
        cs.pack(wait=wait, days=int(days))
    finally:
        if cs is not None:
            cs.close()
コード例 #20
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
    def checkZEOInvalidation(self):
        addr = self._storage._addr
        storage2 = ClientStorage(addr, wait=1, min_disconnect_poll=0.1)
        try:
            oid = self._storage.new_oid()
            ob = MinPO("first")
            revid1 = self._dostore(oid, data=ob)
            data, serial = storage2.load(oid, "")
            self.assertEqual(zodb_unpickle(data), MinPO("first"))
            self.assertEqual(serial, revid1)
            revid2 = self._dostore(oid, data=MinPO("second"), revid=revid1)

            # Now, storage 2 should eventually get the new data. It
            # will take some time, although hopefully not much.
            # We'll poll till we get it and whine if we time out:
            for n in range(30):
                time.sleep(0.1)
                data, serial = storage2.load(oid, "")
                if serial == revid2 and zodb_unpickle(data) == MinPO("second"):
                    break
            else:
                raise AssertionError("Invalidation message was not sent!")
        finally:
            storage2.close()
コード例 #21
0
ファイル: manager.py プロジェクト: Ictp/indico
    def __init__(self, hostname=None, port=None, max_disconnect_poll=30):
        # Please leave this import here, db.py is imported during installation process
        from indico.core import config as Configuration
        cfg = Configuration.Config.getInstance()

        if not hostname:
            hostname = cfg.getDBConnectionParams()[0]
        if not port:
            port = cfg.getDBConnectionParams()[1]

        self._storage = ClientStorage((hostname, port), username=cfg.getDBUserName(),
                                      password=cfg.getDBPassword(), realm=cfg.getDBRealm(),
                                      max_disconnect_poll=max_disconnect_poll)
        self._db = MigratedDB(self._storage)
        self._conn = threading.local()
        self._conn.conn = None
コード例 #22
0
ファイル: config.py プロジェクト: bendavis78/zope
 def open(self):
     from ZEO.ClientStorage import ClientStorage
     # config.server is a multikey of socket-connection-address values
     # where the value is a socket family, address tuple.
     L = [server.address for server in self.config.server]
     return ClientStorage(
         L,
         storage=self.config.storage,
         cache_size=self.config.cache_size,
         name=self.config.name,
         client=self.config.client,
         var=self.config.var,
         min_disconnect_poll=self.config.min_disconnect_poll,
         max_disconnect_poll=self.config.max_disconnect_poll,
         wait=self.config.wait,
         read_only=self.config.read_only,
         read_only_fallback=self.config.read_only_fallback)
コード例 #23
0
ファイル: timeout.py プロジェクト: wpjunior/proled
def main():
    if len(sys.argv) not in (3, 4):
        sys.stderr.write("Usage: timeout.py address delay [storage-name]\n" %
                         sys.argv[0])
        sys.exit(2)

    hostport = sys.argv[1]
    delay = float(sys.argv[2])
    if sys.argv[3:]:
        name = sys.argv[3]
    else:
        name = "1"

    if "/" in hostport:
        address = hostport
    else:
        if ":" in hostport:
            i = hostport.index(":")
            host, port = hostport[:i], hostport[i+1:]
        else:
            host, port = "", hostport
        port = int(port)
        address = (host, port)

    print "Connecting to %s..." % repr(address)
    storage = ClientStorage(address, name)
    print "Connected.  Now starting a transaction..."

    oid = storage.new_oid()
    version = ""
    revid = ZERO
    data = MinPO("timeout.py")
    pickled_data = zodb_pickle(data)
    t = Transaction()
    t.user = "******"
    storage.tpc_begin(t)
    storage.store(oid, revid, pickled_data, version, t)
    print "Stored.  Now voting..."
    storage.tpc_vote(t)

    print "Voted; now sleeping %s..." % delay
    time.sleep(delay)
    print "Done."
コード例 #24
0
def connect(server=None, zodb=None):
    global conn
    close()  # close any existing connection
    if zodb:
        conn = DB(FileStorage(os.path.expanduser(zodb))).open()
    else:
        if not server:
            server = cluster.ZEOSERVER
        s = server
        p = 12345
        if ':' in server:
            s, p = server.split(':')
            p = int(p)
        MB = 1024**2
        storage = ClientStorage((s, p), cache_size=16 * MB)
        db = DB(storage)
        conn = db.open()
    root = conn.root()
    return root
コード例 #25
0
    def get_storage(self):
        kwargs = {}

        kwargs['storage'] = self.storage
        kwargs['wait'] = False
        kwargs['read_only'] = True
        if self.username:
            kwargs['username'] = self.username
        if self.password:
            kwargs['password'] = self.password
        if self.realm:
            kwargs['realm'] = self.realm
        try:
            from ZODB.interfaces import IBlobStorage
            if self.blob_dir:
                kwargs['blob_dir'] = os.path.abspath(self.blob_dir)
                kwargs['shared_blob_dir'] = self.shared_blob_dir
        except ImportError:
            pass
        return ClientStorage(self.addr, **kwargs)
コード例 #26
0
ファイル: util.py プロジェクト: pmart123/indico
def get_storage(zodb_uri):
    uri_parts = urlparse(str(zodb_uri))

    print colored("Trying to open {}...".format(zodb_uri), 'green')

    if uri_parts.scheme == 'zeo':
        if uri_parts.port is None:
            print colored("No ZEO port specified. Assuming 9675", 'yellow')

        storage = ClientStorage((uri_parts.hostname, uri_parts.port or 9675),
                                username=uri_parts.username,
                                password=uri_parts.password,
                                realm=uri_parts.path[1:])

    elif uri_parts.scheme in ('file', None):
        storage = FileStorage.FileStorage(uri_parts.path)
    else:
        raise Exception("URI scheme not known: {}".format(uri_parts.scheme))
    print colored("Done!", 'green')
    return storage
コード例 #27
0
 def initConnection(self, clear=True):
     if not (self.conn and self.conn.opened):
         if self.url.endswith('.fs'):
             from ZODB.FileStorage import FileStorage
             if clear and os.path.exists(self.path):
                 os.unlink('/tmp/zodb_local2.fs')
                 os.unlink('/tmp/zodb_local2.fs.index')
                 os.unlink('/tmp/zodb_local2.fs.tmp')
                 os.unlink('/tmp/zodb_local2.fs.lock')
             openstr = os.path.abspath(os.path.expanduser(self.url[7:]))
             fs = FileStorage(openstr)
         else:
             from ZEO.ClientStorage import ClientStorage
             schema, opts = _parse_rfc1738_args(self.url)
             fs = ClientStorage((opts['host'], int(opts['port'])))
         self.zdb = ZODB.DB(fs)
         self.conn = self.zdb.open()
     root = self.conn.root()
     if 'rdflib' not in root:
         root['rdflib'] = ConjunctiveGraph(self.store_name)
     self.graph = self.g = root['rdflib']
     transaction.commit()
コード例 #28
0
ファイル: zlibstorage.py プロジェクト: NextThought/nti.zodb
        def zlibfactory():
            # Wraps uri in :class:`zc.slibstorage.ZlibStorage` and returns a
            # :class:`ZODB.DB`

            # Delay setting the client name until the very end so whatever is going to
            # set environment variables will have done so.
            if 'client' not in storage_kw:
                name = os.environ.get("DATASERVER_ZEO_CLIENT_NAME")
                if name:  # pragma: no cover This isn't documented.
                    # storage name is automatically part of it
                    storage_kw['client'] = name
            # ClientCache docs say 200MB is good
            storage_kw.setdefault('cache_size', 200 * 1024 * 1024)

            # Client storage is very picky: a Unix path must be bytes, not
            # unicode
            client = ClientStorage(*args, **storage_kw)
            if 'demostorage' in orig_kw:  # pragma: no cover
                client = DemoStorage(base=client)

            zlib = ZlibStorage(client)
            return DB(zlib, **dbkw)
コード例 #29
0
ファイル: zeoup.py プロジェクト: lookerb/course-combine
def check_server(addr, storage, write):
    t0 = time.time()
    if ZEO_VERSION == 2:
        # TODO:  should do retries w/ exponential backoff.
        cs = ClientStorage(addr,
                           storage=storage,
                           wait=0,
                           read_only=(not write))
    else:
        cs = ClientStorage(addr,
                           storage=storage,
                           debug=1,
                           wait_for_server_on_startup=1)
    # _startup() is an artifact of the way ZEO 1.0 works.  The
    # ClientStorage doesn't get fully initialized until registerDB()
    # is called.  The only thing we care about, though, is that
    # registerDB() calls _startup().

    if write:
        db = ZODB.DB(cs)
        cn = db.open()
        root = cn.root()
        try:
            # We store the data in a special `monitor' dict under the root,
            # where other tools may also store such heartbeat and bookkeeping
            # type data.
            monitor = root.get('monitor')
            if monitor is None:
                monitor = root['monitor'] = PersistentMapping()
            obj = monitor['zeoup'] = monitor.get('zeoup', MinPO(0))
            obj.value += 1
            transaction.commit()
        except ConflictError:
            pass
        cn.close()
        db.close()
    else:
        data, serial = cs.load("\0\0\0\0\0\0\0\0", "")
        cs.close()
    t1 = time.time()
    print("Elapsed time: %.2f" % (t1 - t0))
コード例 #30
0
ファイル: config.py プロジェクト: bendavis78/zope
 def open(self):
     from ZEO.ClientStorage import ClientStorage
     # config.server is a multikey of socket-connection-address values
     # where the value is a socket family, address tuple.
     L = [server.address for server in self.config.server]
     return ClientStorage(
         L,
         blob_dir=self.config.blob_dir,
         shared_blob_dir=self.config.shared_blob_dir,
         storage=self.config.storage,
         cache_size=self.config.cache_size,
         name=self.config.name,
         client=self.config.client,
         var=self.config.var,
         min_disconnect_poll=self.config.min_disconnect_poll,
         max_disconnect_poll=self.config.max_disconnect_poll,
         wait=self.config.wait,
         read_only=self.config.read_only,
         read_only_fallback=self.config.read_only_fallback,
         drop_cache_rather_verify=self.config.drop_cache_rather_verify,
         username=self.config.username,
         password=self.config.password,
         realm=self.config.realm)
コード例 #31
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
 def setUp(self):
     StorageTestBase.StorageTestBase.setUp(self)
     logger.info("setUp() %s", self.id())
     port = get_port(self)
     zconf = forker.ZEOConfig(("", port))
     zport, adminaddr, pid, path = forker.start_zeo_server(self.getConfig(), zconf, port)
     self._pids = [pid]
     self._servers = [adminaddr]
     self._conf_path = path
     if not self.blob_cache_dir:
         # This is the blob cache for ClientStorage
         self.blob_cache_dir = tempfile.mkdtemp("blob_cache", dir=os.path.abspath(os.getcwd()))
     self._storage = ClientStorage(
         zport,
         "1",
         cache_size=20000000,
         min_disconnect_poll=0.5,
         wait=1,
         wait_timeout=60,
         blob_dir=self.blob_cache_dir,
         shared_blob_dir=self.shared_blob_dir,
     )
     self._storage.registerDB(DummyDB())
コード例 #32
0
def _main(host, port, unix=None, days=1, username=None, password=None,
          realm=None, blob_dir=None, storage='1', shared_blob_dir=True):
    if unix is not None:
        addr = unix
    else:
        if host is None:
            host = socket.gethostname()
        addr = host, int(port)

    if blob_dir:
        blob_dir = os.path.abspath(blob_dir)

    cs = None
    logger = logging.getLogger(__name__)
    try:
        # We do not want to wait until a zeoserver is up and running; it
        # should already be running, so wait=False
        cs = ClientStorage(
            addr, storage=storage, wait=False, read_only=True,
            username=username, password=password, realm=realm,
            blob_dir=blob_dir, shared_blob_dir=shared_blob_dir,
        )
        if not cs.is_connected():
            logger.error("Could not connect to zeoserver. Please make sure it "
                         "is running.")
            sys.exit(1)
        try:
            # The script should not exit until the packing is done.
            # => wait=True
            cs.pack(wait=True, days=int(days))
        except ClientDisconnected:
            logger.error("Disconnected from zeoserver. Please make sure it "
                         "is still running.")
            sys.exit(1)
    finally:
        if cs is not None:
            cs.close()
コード例 #33
0
ファイル: testZEO.py プロジェクト: bendavis78/zope
 def checkZEOInvalidation(self):
     addr = self._storage._addr
     storage2 = ClientStorage(addr, wait=1, min_disconnect_poll=0.1)
     try:
         oid = self._storage.new_oid()
         ob = MinPO('first')
         revid1 = self._dostore(oid, data=ob)
         data, serial = storage2.load(oid, '')
         self.assertEqual(zodb_unpickle(data), MinPO('first'))
         self.assertEqual(serial, revid1)
         revid2 = self._dostore(oid, data=MinPO('second'), revid=revid1)
         for n in range(3):
             # Let the server and client talk for a moment.
             # Is there a better way to do this?
             asyncore.poll(0.1)
         data, serial = storage2.load(oid, '')
         self.assertEqual(zodb_unpickle(data), MinPO('second'),
                          'Invalidation message was not sent!')
         self.assertEqual(serial, revid2)
     finally:
         storage2.close()
コード例 #34
0
ファイル: testZEO.py プロジェクト: bendavis78/zope
    def checkConnectionInvalidationOnReconnect(self):

        storage = ClientStorage(self.addr, wait=1, min_disconnect_poll=0.1)
        self._storage = storage

        # and we'll wait for the storage to be reconnected:
        for i in range(100):
            if storage.is_connected():
                break
            time.sleep(0.1)
        else:
            raise AssertionError("Couldn't connect to server")

        class DummyDB:
            _invalidatedCache = 0

            def invalidateCache(self):
                self._invalidatedCache += 1

            def invalidate(*a, **k):
                pass

        db = DummyDB()
        storage.registerDB(db)

        base = db._invalidatedCache

        # Now we'll force a disconnection and reconnection
        storage._connection.close()

        # and we'll wait for the storage to be reconnected:
        for i in range(100):
            if storage.is_connected():
                break
            time.sleep(0.1)
        else:
            raise AssertionError("Couldn't connect to server")

        # Now, the root object in the connection should have been invalidated:
        self.assertEqual(db._invalidatedCache, base + 1)
コード例 #35
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
class GenericTests(
    # Base class for all ZODB tests
    StorageTestBase.StorageTestBase,
    # ZODB test mixin classes (in the same order as imported)
    BasicStorage.BasicStorage,
    PackableStorage.PackableStorage,
    Synchronization.SynchronizedStorage,
    MTStorage.MTStorage,
    ReadOnlyStorage.ReadOnlyStorage,
    # ZEO test mixin classes (in the same order as imported)
    CommitLockTests.CommitLockVoteTests,
    ThreadTests.ThreadTests,
    # Locally defined (see above)
    MiscZEOTests,
):

    """Combine tests from various origins in one class."""

    shared_blob_dir = False
    blob_cache_dir = None

    def setUp(self):
        StorageTestBase.StorageTestBase.setUp(self)
        logger.info("setUp() %s", self.id())
        port = get_port(self)
        zconf = forker.ZEOConfig(("", port))
        zport, adminaddr, pid, path = forker.start_zeo_server(self.getConfig(), zconf, port)
        self._pids = [pid]
        self._servers = [adminaddr]
        self._conf_path = path
        if not self.blob_cache_dir:
            # This is the blob cache for ClientStorage
            self.blob_cache_dir = tempfile.mkdtemp("blob_cache", dir=os.path.abspath(os.getcwd()))
        self._storage = ClientStorage(
            zport,
            "1",
            cache_size=20000000,
            min_disconnect_poll=0.5,
            wait=1,
            wait_timeout=60,
            blob_dir=self.blob_cache_dir,
            shared_blob_dir=self.shared_blob_dir,
        )
        self._storage.registerDB(DummyDB())

    def tearDown(self):
        self._storage.close()
        for server in self._servers:
            forker.shutdown_zeo_server(server)
        if hasattr(os, "waitpid"):
            # Not in Windows Python until 2.3
            for pid in self._pids:
                os.waitpid(pid, 0)
        StorageTestBase.StorageTestBase.tearDown(self)

    def runTest(self):
        try:
            super(GenericTests, self).runTest()
        except:
            self._failed = True
            raise
        else:
            self._failed = False

    def open(self, read_only=0):
        # Needed to support ReadOnlyStorage tests.  Ought to be a
        # cleaner way.
        addr = self._storage._addr
        self._storage.close()
        self._storage = ClientStorage(addr, read_only=read_only, wait=1)

    def checkWriteMethods(self):
        # ReadOnlyStorage defines checkWriteMethods.  The decision
        # about where to raise the read-only error was changed after
        # Zope 2.5 was released.  So this test needs to detect Zope
        # of the 2.5 vintage and skip the test.

        # The __version__ attribute was not present in Zope 2.5.
        if hasattr(ZODB, "__version__"):
            ReadOnlyStorage.ReadOnlyStorage.checkWriteMethods(self)

    def checkSortKey(self):
        key = "%s:%s" % (self._storage._storage, self._storage._server_addr)
        self.assertEqual(self._storage.sortKey(), key)
コード例 #36
0
        else:
            cluster = False

        if save:
            Storage = FileStorage(data_fs_path)
        elif load:
            Storage = FileStorage(data_fs_path, read_only=True)
            Storage = DemoStorage(base=Storage)
        else:
            Storage = DemoStorage()
        break
    else:
        forkNodes()
        from ZEO.ClientStorage import ClientStorage
        try:
            Storage = ClientStorage(zeo_client, server_sync=True)
        except TypeError:  # BBB: ZEO<5
            Storage = ClientStorage(zeo_client)

# launch WCFS server if wendelin.core usage is requested and we are running
# with wendelin.core 2.
wcfs_server = None
if with_wendelin_core and not in_forked_process:
    try:
        from wendelin import wcfs
    except ImportError:
        pass  # wendelin.core 1
    else:
        from wendelin.lib.zodb import zstor_2zurl
        zurl = zstor_2zurl(Storage)
        wcfs_server = wcfs.start(zurl)
コード例 #37
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
    def checkVolatileCacheWithImmediateLastTransaction(self):
        # Earlier, a ClientStorage would not have the last transaction id
        # available right after successful connection, this is required now.
        addr = self._storage._addr
        storage2 = ClientStorage(addr)
        self.assert_(storage2.is_connected())
        self.assertEquals(None, storage2.lastTransaction())
        storage2.close()

        self._dostore()
        storage3 = ClientStorage(addr)
        self.assert_(storage3.is_connected())
        self.assertEquals(8, len(storage3.lastTransaction()))
        self.assertNotEquals(ZODB.utils.z64, storage3.lastTransaction())
        storage3.close()
コード例 #38
0
ファイル: resolvers.py プロジェクト: pyzh/zodburi
 def factory():
     return DemoStorage(base=ClientStorage(*args, **kw))
コード例 #39
0
ファイル: custom_zodb.py プロジェクト: jgpjuniorj/j
      if zeo_server_pid:
        save_mysql = None
        os.close(zeo_client)
        zeo_client = eval(os.fdopen(r).read())
        continue
      else:
        node_pid_list = activity_node = None
        os.close(r)
        signal.signal(signal.SIGINT, signal.SIG_IGN)
    elif activity_node is not None:
      # run ZEO server but no need to fork
      zeo_server_pid = 0
    else:
      cluster = False

    if save:
      Storage = FileStorage(data_fs_path)
    elif load:
      Storage = FileStorage(data_fs_path, read_only=True)
      Storage = DemoStorage(base=Storage)
    else:
      Storage = DemoStorage()
    break
  else:
    forkNodes()
    from ZEO.ClientStorage import ClientStorage
    Storage = ClientStorage(zeo_client)

if node_pid_list is not None:
  _print("Instance at %r loaded ... " % instance_home)
コード例 #40
0
ファイル: settings.py プロジェクト: marticongost/woost
    "global": {
        "server.socket_host": "_WEBSERVER_HOST_",
        "server.socket_port": _WEBSERVER_PORT_,
        "tools.encode.on": True,
        "tools.encode.encoding": "utf-8",
        "tools.decode.on": True,
        "tools.decode.encoding": 'utf-8'
    }
})

# Object store provider
from cocktail.persistence import datastore
from ZEO.ClientStorage import ClientStorage
db_host = "_DATABASE_HOST_"
db_port = _DATABASE_PORT_
datastore.storage = lambda: ClientStorage((db_host, db_port))

# Use file based sessions
from cocktail.controllers import session
session.config["session.type"] = "file"

# Uncomment the code below to enable the interactive debugger
# WARNING: *THE CODE BELOW MUST BE COMMENTED ON A PRODUCTION ENVIRONMENT*
#from paste import evalexception
#from _PROJECT_MODULE_.controllers import _PROJECT_NAME_CMS
#
#cherrypy.config.update({
#    "global": {
#        "request.throw_errors": True,
#    }
#})
コード例 #41
0
class DBMgr:
    """This class provides the access point to the Shelf (every client will
        use this class in order to obtain a shelf) and some mechanism to
        ensure there is only one connection opened to the DB during a single
        request.
        This class must not be instantiated, an instance can be obtained
        though the "getInstance" method (implements the singleton pattern
        to ensure unicity)
        Needs to be checked if the class (static) attribute _instance is
        thread-safe: as it is shared by all the objects of this class,
        it could provoke concurrency troubles having 2 threads using the db
        connection at the same time. However, the model under which we are
        programming is not multi-threaded (mod_python seems to run different
        interpreters for each apache subprocess, see mod_python doc section
        4.1) so this thechnique can be used. This has to be taken into account
        when migrating the system to a multi-threading environment.
    """
    _instances = {}

    def __init__(self, hostname=None, port=None, max_disconnect_poll=30):
        # Please leave this import here, db.py is imported during installation process
        from indico.core.config import Config
        cfg = Config.getInstance()

        if not hostname:
            hostname = cfg.getDBConnectionParams()[0]
        if not port:
            port = cfg.getDBConnectionParams()[1]

        self._storage = ClientStorage((hostname, port),
                                      username=cfg.getDBUserName(),
                                      password=cfg.getDBPassword(),
                                      realm=cfg.getDBRealm(),
                                      max_disconnect_poll=max_disconnect_poll)
        self._db = MigratedDB(self._storage)
        self._conn = threading.local()
        self._conn.conn = None

    @classmethod
    def getInstance(cls, *args, **kwargs):
        pid = os.getpid()
        if os.getpid() not in cls._instances:
            cls._instances[pid] = DBMgr(*args, **kwargs)
        return cls._instances[pid]

    @classmethod
    def setInstance(cls, dbInstance):
        if dbInstance is None:
            cls._instances.pop(os.getpid(), None)
        else:
            cls._instances[os.getpid()] = dbInstance

    def _getConnObject(self):
        return self._conn.conn

    def _setConnObject(self, obj):
        self._conn.conn = obj

    def _delConnObject(self):
        self._conn.conn = None

    def startRequest(self):
        """Initialise the DB and starts a new transaction.
        """
        self._conn.conn = self._db.open()

    def endRequest(self, commit=True):
        """Closes the DB and commits changes.
        """
        if commit:
            self.commit()
        else:
            self.abort()

        self._getConnObject().close()
        self._delConnObject()

    def getDBConnection(self):
        return self._getConnObject()

    def isConnected(self):
        return hasattr(self._conn, 'conn') and self._conn.conn is not None

    def getDBConnCache(self):
        conn = self._getConnObject()
        return conn._cache

    def getDBClassFactory(self):
        return self._db.classFactory

    def commit(self, sub=False):
        import StringIO
        import transaction._transaction
        transaction._transaction.StringIO = StringIO.StringIO
        if (sub):
            transaction.savepoint()
        else:
            transaction.commit()

    def abort(self):
        transaction.abort()

    def sync(self):
        self._getConnObject().sync()

    def pack(self, days=1):
        self._storage.pack(days=days)

    def undoInfo(self, stepNumber=0):
        # One step is made of 1000 transactions. First step is 0 and returns
        # transactions 0 to 999.
        return self._db.undoInfo(stepNumber * 1000, (stepNumber + 1) * 1000)

    def undo(self, trans_id):
        self._db.undo(trans_id)

    def getDBSize(self):
        """Return an approximate size of the database, in bytes."""
        return self._storage.getSize()

    def loadObject(self, oid, version):
        return self._storage.load(oid, version)

    def storeObject(self, oid, serial, data, version, trans):
        return self._storage.store(oid, serial, data, version, trans)

    def tpcBegin(self, trans):
        self._storage.tpc_begin(trans)

    def tpcVote(self, trans):
        self._storage.tpc_vote(trans)

    def tpcFinish(self, trans):
        self._storage.tpc_finish(trans)

    @contextmanager
    def transaction(self, sync=False):
        """
        context manager (`with`)
        """
        if sync:
            self.sync()
        yield self.getDBConnection()
        self.commit()

    @contextmanager
    def global_connection(self, commit=False):
        """Helper if you NEED a connection and don't know if one is available or not.

        Useful e.g. in flask code that runs outside a request"""
        if self.isConnected():
            yield
        else:
            self.startRequest()
            try:
                yield
            finally:
                self.endRequest(commit)
コード例 #42
0
 def notify_connected(self, conn, info):
     ClientStorage.notify_connected(self, conn, info)
     self.connection_count_for_tests += 1
     self.verify_result = conn.verify_result
コード例 #43
0
def pack2(addr, storage, days):
    cs = ClientStorage(addr, storage=storage, wait=1, read_only=1)
    cs.pack(wait=1, days=days)
    cs.close()
コード例 #44
0
ファイル: DB.py プロジェクト: HVF/Python-OAuth-2.0-Provider
 def __init__(self, server=SERVER, port=PORT):
     self.storage = ClientStorage((server, port,))
     self.db = ZDB(self.storage)
     self.connection = self.db.open()
     self.dbroot = self.connection.root()
コード例 #45
0
ファイル: zeopack.py プロジェクト: VishrutMehta/indico
def pack2(addr, storage, days):
    cs = ClientStorage(addr, storage=storage, wait=1, read_only=1)
    cs.pack(wait=1, days=days)
    cs.close()
コード例 #46
0
ファイル: resolvers.py プロジェクト: py361/repoze.zodbconn
 def factory():
     from ZEO.ClientStorage import ClientStorage
     from ZODB.DB import DB
     from ZODB.DemoStorage import DemoStorage
     demostorage = DemoStorage(base=ClientStorage(*args, **kw))
     return DB(demostorage, **dbkw) #pragma NO COVERAGE
コード例 #47
0
 def notify_connected(self, conn, info):
     ClientStorage.notify_connected(self, conn, info)
     self.connection_count_for_tests += 1
     self.verify_result = conn.verify_result
コード例 #48
0
 def testConnection(self, conn):
     try:
         return ClientStorage.testConnection(self, conn)
     finally:
         self.test_connection = True
コード例 #49
0
class DBMgr:
    """This class provides the access point to the Shelf (every client will
        use this class in order to obtain a shelf) and some mechanism to
        ensure there is only one connection opened to the DB during a single
        request.
        This class must not be instantiated, an instance can be obtained
        though the "getInstance" method (implements the singleton pattern
        to ensure unicity)
        Needs to be checked if the class (static) attribute _instance is
        thread-safe: as it is shared by all the objects of this class,
        it could provoke concurrency troubles having 2 threads using the db
        connection at the same time. However, the model under which we are
        programming is not multi-threaded (mod_python seems to run different
        interpreters for each apache subprocess, see mod_python doc section
        4.1) so this thechnique can be used. This has to be taken into account
        when migrating the system to a multi-threading environment.
    """
    _instance = None

    def __init__(self, hostname=None, port=None, max_disconnect_poll=30):
        import Configuration  # Please leave this import here, db.py is imported during installation process
        cfg = Configuration.Config.getInstance()

        if not hostname:
            hostname = cfg.getDBConnectionParams()[0]
        if not port:
            port = cfg.getDBConnectionParams()[1]

        self._storage = ClientStorage((hostname, port),
                                      username=cfg.getDBUserName(),
                                      password=cfg.getDBPassword(),
                                      realm=cfg.getDBRealm(),
                                      max_disconnect_poll=max_disconnect_poll)
        self._db = MaKaCDB(self._storage)
        self._conn = threading.local()
        self._conn.conn = None

    @classmethod
    def getInstance(cls, *args, **kwargs):
        if cls._instance == None:
            Logger.get('dbmgr').debug('cls._instance is None')
            cls._instance = DBMgr(*args, **kwargs)
        return cls._instance

    @classmethod
    def setInstance(cls, dbInstance):
        cls._instance = dbInstance

    def _getConnObject(self):
        return self._conn.conn

    def _setConnObject(self, obj):
        self._conn.conn = obj

    def _delConnObject(self):
        self._conn.conn = None

    def startRequest(self):
        """Initialise the DB and starts a new transaction.
        """
        self._conn.conn = self._db.open()

    def endRequest(self, commit=True):
        """Closes the DB and commits changes.
        """
        if commit:
            self.commit()
        else:
            self.abort()

        self._getConnObject().close()
        self._delConnObject()

    def getDBConnection(self):
        return self._getConnObject()

    def isConnected(self):
        return hasattr(self._conn, 'conn') and self._conn.conn != None

    def getDBConnCache(self):
        conn = self._getConnObject()
        return conn._cache

    def getDBClassFactory(self):
        return self._db.classFactory

    def commit(self, sub=False):
        if (sub):
            transaction.savepoint()
        else:
            transaction.commit()

    def commitZODBOld(self, sub=False):
        transaction.commit(sub)

    def abort(self):
        transaction.abort()

    def sync(self):
        self._getConnObject().sync()

    def pack(self, days=1):
        self._storage.pack(days=days)

    def undoInfo(self, stepNumber=0):
        # One step is made of 1000 transactions. First step is 0 and returns
        # transactions 0 to 999.
        return self._db.undoInfo(stepNumber * 1000, (stepNumber + 1) * 1000)

    def undo(self, trans_id):
        self._db.undo(trans_id)

    def getDBSize(self):
        """Return an approximate size of the database, in bytes."""
        return self._storage.getSize()

    def loadObject(self, oid, version):
        return self._storage.load(oid, version)

    def storeObject(self, oid, serial, data, version, trans):
        return self._storage.store(oid, serial, data, version, trans)

    def tpcBegin(self, trans):
        self._storage.tpc_begin(trans)

    def tpcVote(self, trans):
        self._storage.tpc_vote(trans)

    def tpcFinish(self, trans):
        self._storage.tpc_finish(trans)

    @contextmanager
    def transaction(self, sync=False):
        """
        context manager (`with`)
        """
        if sync:
            self.sync()
        yield self.getDBConnection()
        self.commit()

    # ZODB version check
    try:
        zodbPkg = pkg_resources.require('ZODB3')[0]
        zodbVersion = zodbPkg.parsed_version
        zodbVersion = (int(zodbVersion[0]), int(zodbVersion[1]))
    except pkg_resources.DistributionNotFound:
        # Very old versions, in which ZODB didn't register
        # with pkg_resources
        import ZODB
        zodbVersion = ZODB.__version__.split('.')

    if int(zodbVersion[0]) < 3:
        raise Exception("ZODB 3 required! %s found" % zodbPkg.version)
    elif int(zodbVersion[1]) < 7:
        commit = commitZODBOld
コード例 #50
0
ファイル: ConnectionTests.py プロジェクト: nucypher/ZEO
 def testConnection(self, conn):
     try:
         return ClientStorage.testConnection(self, conn)
     finally:
         self.test_connection = True
コード例 #51
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
 def open(self, read_only=0):
     # Needed to support ReadOnlyStorage tests.  Ought to be a
     # cleaner way.
     addr = self._storage._addr
     self._storage.close()
     self._storage = ClientStorage(addr, read_only=read_only, wait=1)
コード例 #52
0
ファイル: ConnectionTests.py プロジェクト: nucypher/ZEO
 def endVerify(self):
     ClientStorage.endVerify(self)
     self.end_verify.set()
コード例 #53
0
ファイル: resolvers.py プロジェクト: pyzh/zodburi
 def factory():
     return ClientStorage(*args, **kw)
コード例 #54
0
ファイル: ConnectionTests.py プロジェクト: nucypher/ZEO
 def verify_cache(self, stub):
     self.end_verify = threading.Event()
     self.verify_result = ClientStorage.verify_cache(self, stub)
コード例 #55
0
ファイル: manager.py プロジェクト: Ictp/indico
class DBMgr:
    """This class provides the access point to the Shelf (every client will
        use this class in order to obtain a shelf) and some mechanism to
        ensure there is only one connection opened to the DB during a single
        request.
        This class must not be instantiated, an instance can be obtained
        though the "getInstance" method (implements the singleton pattern
        to ensure unicity)
        Needs to be checked if the class (static) attribute _instance is
        thread-safe: as it is shared by all the objects of this class,
        it could provoke concurrency troubles having 2 threads using the db
        connection at the same time. However, the model under which we are
        programming is not multi-threaded (mod_python seems to run different
        interpreters for each apache subprocess, see mod_python doc section
        4.1) so this thechnique can be used. This has to be taken into account
        when migrating the system to a multi-threading environment.
    """
    _instances = {}

    def __init__(self, hostname=None, port=None, max_disconnect_poll=30):
        # Please leave this import here, db.py is imported during installation process
        from indico.core import config as Configuration
        cfg = Configuration.Config.getInstance()

        if not hostname:
            hostname = cfg.getDBConnectionParams()[0]
        if not port:
            port = cfg.getDBConnectionParams()[1]

        self._storage = ClientStorage((hostname, port), username=cfg.getDBUserName(),
                                      password=cfg.getDBPassword(), realm=cfg.getDBRealm(),
                                      max_disconnect_poll=max_disconnect_poll)
        self._db = MigratedDB(self._storage)
        self._conn = threading.local()
        self._conn.conn = None

    @classmethod
    def getInstance(cls, *args, **kwargs):
        pid = os.getpid()
        if os.getpid() not in cls._instances:
            from MaKaC.common.logger import Logger
            Logger.get('dbmgr').debug('cls._instance is None')
            cls._instances[pid] = DBMgr(*args, **kwargs)
        return cls._instances[pid]

    @classmethod
    def setInstance(cls, dbInstance):
        if dbInstance is None:
            cls._instances.pop(os.getpid(), None)
        else:
            cls._instances[os.getpid()] = dbInstance

    def _getConnObject(self):
        return self._conn.conn

    def _setConnObject(self, obj):
        self._conn.conn = obj

    def _delConnObject(self):
        self._conn.conn = None

    def startRequest(self):
        """Initialise the DB and starts a new transaction.
        """
        self._conn.conn = self._db.open()

    def endRequest(self, commit=True):
        """Closes the DB and commits changes.
        """
        if commit:
            self.commit()
        else:
            self.abort()

        self._getConnObject().close()
        self._delConnObject()

    def getDBConnection(self):
        return self._getConnObject()

    def isConnected(self):
        return hasattr(self._conn, 'conn') and self._conn.conn is not None

    def getDBConnCache(self):
        conn = self._getConnObject()
        return conn._cache

    def getDBClassFactory(self):
        return self._db.classFactory

    def commit(self, sub=False):
        import StringIO
        import transaction._transaction
        transaction._transaction.StringIO = StringIO.StringIO
        if (sub):
            transaction.savepoint()
        else:
            transaction.commit()

    def commitZODBOld(self, sub=False):
        transaction.commit(sub)

    def abort(self):
        transaction.abort()

    def sync(self):
        self._getConnObject().sync()

    def pack(self, days=1):
        self._storage.pack(days=days)

    def undoInfo(self, stepNumber=0):
        # One step is made of 1000 transactions. First step is 0 and returns
        # transactions 0 to 999.
        return self._db.undoInfo(stepNumber*1000, (stepNumber+1)*1000)

    def undo(self, trans_id):
        self._db.undo(trans_id)

    def getDBSize(self):
        """Return an approximate size of the database, in bytes."""
        return self._storage.getSize()

    def loadObject(self, oid, version):
        return self._storage.load(oid, version)

    def storeObject(self, oid, serial, data, version, trans):
        return self._storage.store(oid, serial, data, version, trans)

    def tpcBegin(self, trans):
        self._storage.tpc_begin(trans)

    def tpcVote(self, trans):
        self._storage.tpc_vote(trans)

    def tpcFinish(self, trans):
        self._storage.tpc_finish(trans)

    @contextmanager
    def transaction(self, sync=False):
        """
        context manager (`with`)
        """
        if sync:
            self.sync()
        yield self.getDBConnection()
        self.commit()

    @contextmanager
    def global_connection(self, commit=False):
        """Helper if you NEED a connection and don't know if one is available or not.

        Useful e.g. in flask code that runs outside a request"""
        if self.isConnected():
            yield
        else:
            self.startRequest()
            try:
                yield
            finally:
                self.endRequest(commit)

    # ZODB version check
    try:
        zodbPkg = pkg_resources.require('ZODB3')[0]
        zodbVersion = zodbPkg.parsed_version
        zodbVersion = (int(zodbVersion[0]), int(zodbVersion[1]))
    except pkg_resources.DistributionNotFound:
        # Very old versions, in which ZODB didn't register
        # with pkg_resources
        import ZODB
        zodbVersion = ZODB.__version__.split('.')

    if int(zodbVersion[0]) < 3:
        raise Exception("ZODB 3 required! %s found" % zodbPkg.version)
    elif int(zodbVersion[1]) < 7:
        commit = commitZODBOld
コード例 #56
0
ファイル: ConnectionTests.py プロジェクト: nucypher/ZEO
 def notifyConnected(self, conn):
     ClientStorage.notifyConnected(self, conn)
     self.connection_count_for_tests += 1
コード例 #57
0
host = 'IP OR HOST'
port = PORT NUMBER
addr = host, int(port)
wait = True
days = 7

# Log initialize
logger = logging.getLogger('Instance Pack')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(logpath)
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)


cs = ClientStorage(addr,storage='1',wait=wait,read_only=True,username=None,password=None,realm=None)
logger.info('Pack initialize \n ---------------------------------')

cs.pack(wait=wait, days=int(days))

noSecurityManager()

transaction.savepoint(1)
transaction.commit()
app._p_jar.sync()

logger.info('Pack finally \n ---------------------------------')
コード例 #58
0
ファイル: testZEO.py プロジェクト: grodniewicz/oship
 def close(self):
     ClientStorage.close(self)
     zope.testing.setupstack.tearDown(self)
コード例 #59
0
ファイル: __init__.py プロジェクト: sblack-usu/RDFAlchemy
def create_engine(url='', identifier="", create=False):
    """
    :returns: returns an opened rdflib ConjunctiveGraph

    :param url: a string of the url
    :param identifier: URIRef of the default context for writing e.g.:

      - create_engine('sleepycat://~/working/rdf_db')
      - create_engine('kyotocabinet://~/working/rdf_db')
      - create_engine('zodb:///var/rdflib/Data.fs')
      - create_engine('zodb://*****:*****@localhost/rdflibdb')
      - create_engine('sqlalchemy+postgresql://myname@localhost/rdflibdb')

    etc.

    """
    if url == '' or url.startswith('IOMemory'):
        from rdflib import ConjunctiveGraph
        db = ConjunctiveGraph('IOMemory')

    elif url.lower().startswith('sleepycat://'):
        from rdflib import ConjunctiveGraph
        db = ConjunctiveGraph('Sleepycat', identifier=identifier)
        openstr = os.path.abspath(os.path.expanduser(url[12:]))
        db.open(openstr, create=create)

    elif url.lower().startswith('kyotocabinet://'):
        from rdflib import ConjunctiveGraph
        db = ConjunctiveGraph('Kyotocabinet', identifier=identifier)
        openstr = os.path.abspath(os.path.expanduser(url[15:]))
        db.open(openstr, create=create)

    elif url.lower().startswith('sqlalchemy+'):
        from rdflib import ConjunctiveGraph
        db = ConjunctiveGraph('SQLAlchemy', identifier=identifier)
        db.open(url[11:], create=create)

    elif url.lower().startswith('zodb://'):
        import ZODB
        # import transaction
        from rdflib import ConjunctiveGraph
        db = ConjunctiveGraph('ZODB')
        if url.endswith('.fs'):
            from ZODB.FileStorage import FileStorage
            openstr = os.path.abspath(os.path.expanduser(url[7:]))
            if not os.path.exists(openstr) and not create:
                raise "File not found: %s"
            fs = FileStorage(openstr)
        else:
            from ZEO.ClientStorage import ClientStorage
            schema, opts = _parse_rfc1738_args(url)
            fs = ClientStorage((opts['host'], int(opts['port'])))
        # get the Zope Database
        zdb = ZODB.DB(fs)
        # open it
        conn = zdb.open()
        #get the root
        root = conn.root()
        # get the Conjunctive Graph
        if 'rdflib' not in root and create:
            root['rdflib'] = ConjunctiveGraph('ZODB')
        db = root['rdflib']

    elif url.lower().startswith('sesame://'):
        from rdfalchemy.sparql.sesame2 import SesameGraph
        db = SesameGraph("http://" + url[9:])

    elif url.lower().startswith('sparql://'):
        from rdfalchemy.sparql import SPARQLGraph
        db = SPARQLGraph("http://" + url[9:])

    else:
        raise "Could not parse  string '%s'" % url
    return db
コード例 #60
0
ファイル: resolvers.py プロジェクト: py361/repoze.zodbconn
 def factory():
     from ZEO.ClientStorage import ClientStorage
     from ZODB.DB import DB
     clientstorage = ClientStorage(*args, **kw)
     return DB(clientstorage, **dbkw) #pragma NO COVERAGE