def StartUpCluster(self): """Initializes and start up Compute Engine cluster. Records user ID and use it later by Taskqueue, Cron job handlers and other handlers which is initiated by Compute Engine (therefore without log in), to retrieve credential from Datastore. It means those tasks work under permission of the user who started the cluster. """ LoadInfo.InitializeTable() self.IncreaseEngine(self.INITIAL_CLUSTER_SIZE)
def testGetIdleInstanceExcludingImmatureInstance(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance1', 55) LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') # test-instance2 hasn't yet reported load information. self.assertEqual('1.2.3.4', LoadInfo.GetIdleInstanceIpAddress())
def testAverageLoadExcludingImmatureInstance(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') # test-instance1 hasn't yet reported load information. LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') LoadInfo.UpdateLoadInfo('test-instance2', 33) self.assertEqual((1, 33), LoadInfo.GetAverageLoad())
def testAddInstance(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') self.assertEqual(['test-instance'], memcache.get(LoadInfo.ALL_INSTANCES)) self.assertEqual({}, LoadInfo.GetAll()) self.assertIsNone(memcache.get('test-instance')) self.assertIsNotNone(SingleInstance.GetByName('test-instance')) self.assertRaises(ValueError, SingleInstance.GetByName('test-instance').ip_address)
def testRemoveInstanceFromOne(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance', 55) LoadInfo.RemoveInstance('test-instance') self.assertEqual({}, LoadInfo.GetAll()) self.assertEqual([], memcache.get(LoadInfo.ALL_INSTANCES)) self.assertIsNone(memcache.get('test-instance')) self.assertIsNone(SingleInstance.GetByName('test-instance'))
def testRegisterInstance(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') self.assertEqual(['test-instance'], memcache.get(LoadInfo.ALL_INSTANCES)) self.assertEqual({'test-instance': { 'ip_address': '1.2.3.4' }}, LoadInfo.GetAll()) self.assertEqual({'ip_address': '1.2.3.4'}, memcache.get('test-instance')) self.assertEqual('1.2.3.4', SingleInstance.GetByName('test-instance').ip_address)
def testAverageLoad(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance1', 55) LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') LoadInfo.UpdateLoadInfo('test-instance2', 11) LoadInfo.AddInstance('test-instance3') LoadInfo.RegisterInstanceIpAddress('test-instance3', '9.10.11.12') LoadInfo.UpdateLoadInfo('test-instance3', 66) self.assertEqual((3, 44), LoadInfo.GetAverageLoad())
def testMemcacheClear(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance', 55) # Simulate loss of all data in Memcache. memcache.flush_all() LoadInfo.UpdateLoadInfo('test-instance', 38) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 38, 'force': False } }, LoadInfo.GetAll())
def testGetIdleInstanceIpAddress(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance1', 55) LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') LoadInfo.UpdateLoadInfo('test-instance2', 11) LoadInfo.AddInstance('test-instance3') LoadInfo.RegisterInstanceIpAddress('test-instance3', '9.10.11.12') LoadInfo.UpdateLoadInfo('test-instance3', 66) LoadInfo.AddInstance('test-instance4') LoadInfo.RegisterInstanceIpAddress('test-instance4', '13.14.15.16') LoadInfo.UpdateLoadInfo('test-instance4', 22) # IP address should be picked up from one of the 3 least loaded instances. self.assertIn(LoadInfo.GetIdleInstanceIpAddress(), ('1.2.3.4', '5.6.7.8', '13.14.15.16'))
def testRemoveInstanceFromTwo(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance1', 55) LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') LoadInfo.UpdateLoadInfo('test-instance2', 22) LoadInfo.RemoveInstance('test-instance1') self.assertEqual( { 'test-instance2': { 'ip_address': '5.6.7.8', 'load': 22, 'force': False } }, LoadInfo.GetAll()) self.assertIsNone(memcache.get('test-instance1')) self.assertIsNone(SingleInstance.GetByName('test-instance1'))
def testUpdateLoadInfo(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance', 55) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 55, 'force': False } }, LoadInfo.GetAll()) LoadInfo.UpdateLoadInfo('test-instance', 73) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 73, 'force': False } }, LoadInfo.GetAll())
def StartUpCluster(self): LoadInfo.InitializeTable() self.IncreaseEngine()
def testUpdateLoadInfoForce(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance', 55) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 55, 'force': False } }, LoadInfo.GetAll()) LoadInfo.UpdateLoadInfo('test-instance', 92, True) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 92, 'force': True } }, LoadInfo.GetAll()) # This update is ignored since force flag is set in data and this is not # force update. LoadInfo.UpdateLoadInfo('test-instance', 15) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 92, 'force': True } }, LoadInfo.GetAll()) # Updated because of force_set flag. LoadInfo.UpdateLoadInfo('test-instance', 8, True) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 8, 'force': True } }, LoadInfo.GetAll()) LoadInfo.UpdateLoadInfo('test-instance', 41, False) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 41, 'force': False } }, LoadInfo.GetAll()) LoadInfo.UpdateLoadInfo('test-instance', 28) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 28, 'force': False } }, LoadInfo.GetAll())
def testInitializeTable(self): LoadInfo.InitializeTable() self.assertEqual([], memcache.get(LoadInfo.ALL_INSTANCES)) self.assertEqual({}, LoadInfo.GetAll())