def invalidate(self, table_name, row_id, token): logging.debug('Invalidating %s(%d) - %s:', table_name, row_id, token) version, cache_event_token, _ = self.cache.gets(table_name, row_id) if version is None: logging.debug(' no entry in cache, saving event_token') self.cache.add(table_name, row_id, token, None) return if event_token.fresher(cache_event_token, token) >= 0: # For invalidation, a couple things to consider: # 1. If we can't compare the EventTokens, we want to store the # invalidation, so it's safer. # 2. If we have exactly the same EventToken, we do not want to # store the ivalidation. We have either an invalidation or a # value, in both cases we're fine keeping it. logging.debug(' invalidation event is older or equal than cache value,' ' ignoring') return logging.debug(' updating entry in the cache') self.cache.cas(table_name, row_id, version, token, None)
def test_event_token_fresher(self): """event_token.fresher test suite.""" test_cases = [ { 'ev1': None, 'ev2': None, 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=123, ), 'ev2': None, 'expected': -1, }, { 'ev1': None, 'ev2': query_pb2.EventToken( timestamp=123, ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=123, ), 'ev2': query_pb2.EventToken( timestamp=123, ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=200, ), 'ev2': query_pb2.EventToken( timestamp=100, ), 'expected': 100, }, { 'ev1': query_pb2.EventToken( timestamp=100, ), 'ev2': query_pb2.EventToken( timestamp=200, ), 'expected': -100, }, { # Test cases with not enough information to compare. 'ev1': query_pb2.EventToken( timestamp=100, ), 'ev2': query_pb2.EventToken( timestamp=100, ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s2', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='pos1', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='pos2', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='pos1', # invalid on purpose ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='pos2', # invalid on purpose ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-123', # valid but different ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MySQL56/33333333-3333-3333-3333-333333333333:456-789', ), 'expected': -1, }, { # MariaDB test cases. 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-200', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-100', ), 'expected': 100, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-100', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-200', ), 'expected': -100, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-100', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-100', ), 'expected': 0, }, { # MySQL56 test cases, not supported yet. 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MySQL56/33333333-3333-3333-3333-333333333333:1-200', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MySQL56/33333333-3333-3333-3333-333333333333:1-100', ), 'expected': -1, # Should be: 1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MySQL56/33333333-3333-3333-3333-333333333333:1-100', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MySQL56/33333333-3333-3333-3333-333333333333:1-200', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MySQL56/33333333-3333-3333-3333-333333333333:1-100', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MySQL56/33333333-3333-3333-3333-333333333333:1-100', ), 'expected': -1, # Should be: 0, } ] for tcase in test_cases: got = event_token.fresher(tcase['ev1'], tcase['ev2']) self.assertEqual(got, tcase['expected'], 'got %d but expected %d for Fresher(%s, %s)' % (got, tcase['expected'], tcase['ev1'], tcase['ev2']))
def test_event_token_fresher(self): """event_token.fresher test suite.""" test_cases = [ { 'ev1': None, 'ev2': None, 'expected': -1, }, { 'ev1': query_pb2.EventToken(timestamp=123, ), 'ev2': None, 'expected': -1, }, { 'ev1': None, 'ev2': query_pb2.EventToken(timestamp=123, ), 'expected': -1, }, { 'ev1': query_pb2.EventToken(timestamp=123, ), 'ev2': query_pb2.EventToken(timestamp=123, ), 'expected': -1, }, { 'ev1': query_pb2.EventToken(timestamp=200, ), 'ev2': query_pb2.EventToken(timestamp=100, ), 'expected': 100, }, { 'ev1': query_pb2.EventToken(timestamp=100, ), 'ev2': query_pb2.EventToken(timestamp=200, ), 'expected': -100, }, { # Test cases with not enough information to compare. 'ev1': query_pb2.EventToken(timestamp=100, ), 'ev2': query_pb2.EventToken(timestamp=100, ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s2', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='pos1', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='pos2', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='pos1', # invalid on purpose ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='pos2', # invalid on purpose ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-123', # valid but different ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position= 'MySQL56/33333333-3333-3333-3333-333333333333:456-789', ), 'expected': -1, }, { # MariaDB test cases. 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-200', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-100', ), 'expected': 100, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-100', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-200', ), 'expected': -100, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-100', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position='MariaDB/0-1-100', ), 'expected': 0, }, { # MySQL56 test cases, not supported yet. 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position= 'MySQL56/33333333-3333-3333-3333-333333333333:1-200', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position= 'MySQL56/33333333-3333-3333-3333-333333333333:1-100', ), 'expected': -1, # Should be: 1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position= 'MySQL56/33333333-3333-3333-3333-333333333333:1-100', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position= 'MySQL56/33333333-3333-3333-3333-333333333333:1-200', ), 'expected': -1, }, { 'ev1': query_pb2.EventToken( timestamp=100, shard='s1', position= 'MySQL56/33333333-3333-3333-3333-333333333333:1-100', ), 'ev2': query_pb2.EventToken( timestamp=100, shard='s1', position= 'MySQL56/33333333-3333-3333-3333-333333333333:1-100', ), 'expected': -1, # Should be: 0, } ] for tcase in test_cases: got = event_token.fresher(tcase['ev1'], tcase['ev2']) self.assertEqual( got, tcase['expected'], 'got %d but expected %d for Fresher(%s, %s)' % (got, tcase['expected'], tcase['ev1'], tcase['ev2']))