Esempio n. 1
0
 def __init__(self, cluster):
     """
     Initializes the client
     """
     contents = Configuration.get(PyrakoonStore.CONFIG_KEY.format(cluster), raw=True)
     parser = RawConfigParser()
     parser.readfp(StringIO(contents))
     nodes = {}
     for node in parser.get("global", "cluster").split(","):
         node = node.strip()
         nodes[node] = ([parser.get(node, "ip")], parser.get(node, "client_port"))
     self._client = PyrakoonClient(cluster, nodes)
Esempio n. 2
0
    def build_client(config):
        """
        Build the ArakoonClient object with all configured nodes in the cluster
        :param config: Configuration on which to base the client
        :type config: ArakoonClientConfig
        :return: The newly generated PyrakoonClient
        :rtype: PyrakoonClient
        """
        if os.environ.get('RUNNING_UNITTESTS') == 'True':
            from ovs.extensions.db.arakoon.tests.client import MockPyrakoonClient
            return MockPyrakoonClient(config.cluster_id, None)

        from ovs.extensions.db.arakoon.pyrakoon.client import PyrakoonClient
        nodes = {}
        for node in config.nodes:
            nodes[node.name] = ([node.ip], node.client_port)
        return PyrakoonClient(config.cluster_id, nodes)
Esempio n. 3
0
class PyrakoonStore(object):
    """
    Arakoon client wrapper:
    * Uses json serialisation
    * Raises generic exception
    """

    _logger = LogHandler.get("extensions", name="arakoon_store")
    CONFIG_KEY = "/ovs/arakoon/{0}/config"

    def __init__(self, cluster):
        """
        Initializes the client
        """
        contents = Configuration.get(PyrakoonStore.CONFIG_KEY.format(cluster), raw=True)
        parser = RawConfigParser()
        parser.readfp(StringIO(contents))
        nodes = {}
        for node in parser.get("global", "cluster").split(","):
            node = node.strip()
            nodes[node] = ([parser.get(node, "ip")], parser.get(node, "client_port"))
        self._client = PyrakoonClient(cluster, nodes)

    def get(self, key):
        """
        Retrieves a certain value for a given key
        """
        try:
            return ujson.loads(self._client.get(key))
        except ValueError:
            raise KeyNotFoundException("Could not parse JSON stored for {0}".format(key))
        except ArakoonNotFound as field:
            raise KeyNotFoundException(field.message)

    def get_multi(self, keys):
        """
        Get multiple keys at once
        """
        try:
            for item in self._client.get_multi(keys):
                yield ujson.loads(item)
        except ValueError:
            raise KeyNotFoundException("Could not parse JSON stored")
        except ArakoonNotFound as field:
            raise KeyNotFoundException(field.message)

    def set(self, key, value, transaction=None):
        """
        Sets the value for a key to a given value
        """
        return self._client.set(key, ujson.dumps(value, sort_keys=True), transaction)

    def prefix(self, prefix):
        """
        Lists all keys starting with the given prefix
        """
        return self._client.prefix(prefix)

    def prefix_entries(self, prefix):
        """
        Lists all keys starting with the given prefix
        """
        for item in self._client.prefix_entries(prefix):
            yield [item[0], ujson.loads(item[1])]

    def delete(self, key, must_exist=True, transaction=None):
        """
        Deletes a given key from the store
        """
        try:
            return self._client.delete(key, must_exist, transaction)
        except ArakoonNotFound as field:
            raise KeyNotFoundException(field.message)

    def nop(self):
        """
        Executes a nop command
        """
        return self._client.nop()

    def exists(self, key):
        """
        Check if key exists
        """
        return self._client.exists(key)

    def assert_value(self, key, value, transaction=None):
        """
        Asserts a key-value pair
        """
        try:
            return self._client.assert_value(
                key, None if value is None else ujson.dumps(value, sort_keys=True), transaction
            )
        except ArakoonAssertionFailed as assertion:
            raise AssertException(assertion)

    def assert_exists(self, key, transaction=None):
        """
        Asserts that a given key exists
        """
        try:
            return self._client.assert_exists(key, transaction)
        except ArakoonAssertionFailed as assertion:
            raise AssertException(assertion)

    def begin_transaction(self):
        """
        Creates a transaction (wrapper around Arakoon sequences)
        """
        return self._client.begin_transaction()

    def apply_transaction(self, transaction):
        """
        Applies a transaction
        """
        try:
            return self._client.apply_transaction(transaction)
        except ArakoonAssertionFailed as assertion:
            raise AssertException(assertion)
        except ArakoonNotFound as field:
            raise KeyNotFoundException(field.message)