Example #1
0
 def __init__(self):
     self.host_query_manager = AvailableHostQueryManager()
     self.cache = rdb_cache_manager.RDBHostCacheManager()
     self.response_map = {}
     self.unsatisfied_requests = 0
     self.leased_hosts_count = 0
     self.request_accountant = None
Example #2
0
    def testCacheAPI(self):
        """Test the cache managers api."""
        cache = rdb_cache_manager.RDBHostCacheManager()
        key = cache.get_key(deps=test_utils.DEFAULT_DEPS,
                            acls=test_utils.DEFAULT_ACLS)

        # Cannot set None, it's reserved for cache expiration.
        self.assertRaises(rdb_utils.RDBException, cache.set_line, *(key, None))

        # Setting an empty list indicates a query with no results.
        cache.set_line(key, [])
        self.assertTrue(cache.get_line(key) == [])

        # Getting a value will delete the key, leading to a miss on subsequent
        # gets before a set.
        self.assertRaises(rdb_utils.CacheMiss, cache.get_line, *(key, ))

        # Caching a leased host is just a waste of cache space.
        host = test_utils.FakeHost('h1',
                                   1,
                                   labels=test_utils.DEFAULT_DEPS,
                                   acls=test_utils.DEFAULT_ACLS,
                                   leased=1)
        cache.set_line(key, [host])
        self.assertRaises(rdb_utils.CacheMiss, cache.get_line, *(key, ))

        # Retrieving an unleased cached host shouldn't mutate it, even if the
        # key is reconstructed.
        host.leased = 0
        cache.set_line(cache.get_key(host.labels, host.acls), [host])
        self.assertTrue(
            cache.get_line(cache.get_key(host.labels, host.acls)) == [host])

        # Caching different hosts under the same key isn't allowed.
        different_host = test_utils.FakeHost(
            'h2',
            2,
            labels=[test_utils.DEFAULT_DEPS[0]],
            acls=test_utils.DEFAULT_ACLS,
            leased=0)
        cache.set_line(key, [host, different_host])
        self.assertRaises(rdb_utils.CacheMiss, cache.get_line, *(key, ))

        # Caching hosts with the same deps but different acls under the
        # same key is allowed, as long as the acls match the key.
        different_host = test_utils.FakeHost('h2',
                                             2,
                                             labels=test_utils.DEFAULT_DEPS,
                                             acls=[test_utils.DEFAULT_ACLS[1]],
                                             leased=0)
        cache.set_line(key, [host, different_host])
        self.assertTrue(
            set(cache.get_line(key)) == set([host, different_host]))

        # Make sure we don't divide by zero while calculating hit ratio
        cache.misses = 0
        cache.hits = 0
        self.assertTrue(cache.hit_ratio() == 0)
        cache.hits = 1
        hit_ratio = cache.hit_ratio()
        self.assertTrue(type(hit_ratio) == float and hit_ratio == 100)