Commit 4990aeaf authored by Łukasz Nowak's avatar Łukasz Nowak

proxy: Update timestamp partition on slave changes

Each time slave partition changes (it is added or updated) the master
partition timestamp has to be recalculated.

/reviewed-on nexedi/slapos.core!176
parent fea3bd23
...@@ -756,23 +756,35 @@ def requestSlave(software_release, software_type, partition_reference, partition ...@@ -756,23 +756,35 @@ def requestSlave(software_release, software_type, partition_reference, partition
new_slave[key] = partition_parameter_kw[key] new_slave[key] = partition_parameter_kw[key]
# Add slave to partition slave_list if not present else replace information # Add slave to partition slave_list if not present else replace information
slave_updated_or_added = False
slave_instance_list = partition['slave_instance_list'] slave_instance_list = partition['slave_instance_list']
if slave_instance_list: if slave_instance_list:
slave_instance_list = loads(slave_instance_list.encode('utf-8')) slave_instance_list = loads(slave_instance_list.encode('utf-8'))
for i, x in enumerate(slave_instance_list): for i, x in enumerate(slave_instance_list):
if x['slave_reference'] == slave_reference: if x['slave_reference'] == slave_reference:
slave_instance_list[i] = new_slave if slave_instance_list[i] != new_slave:
slave_instance_list[i] = new_slave
slave_updated_or_added = True
break break
else: else:
slave_instance_list.append(new_slave) slave_instance_list.append(new_slave)
slave_updated_or_added = True
else: else:
slave_instance_list = [new_slave] slave_instance_list = [new_slave]
slave_updated_or_added = True
q += ' WHERE reference=? AND computer_reference=?'
a(partition['reference'])
a(partition['computer_reference'])
# Update slave_instance_list in database # Update slave_instance_list in database
args = [] args = []
a = args.append a = args.append
q = 'UPDATE %s SET slave_instance_list=?' q = 'UPDATE %s SET slave_instance_list=?'
a(bytes2str(dumps(slave_instance_list))) a(bytes2str(dumps(slave_instance_list)))
if slave_updated_or_added:
timestamp = time.time()
q += ', timestamp=?'
a(timestamp)
q += ' WHERE reference=? and computer_reference=?' q += ' WHERE reference=? and computer_reference=?'
a(partition['reference']) a(partition['reference'])
a(requested_computer_id) a(requested_computer_id)
......
...@@ -870,6 +870,53 @@ class TestSlaveRequest(MasterMixin): ...@@ -870,6 +870,53 @@ class TestSlaveRequest(MasterMixin):
master_partition = self.getPartitionInformation(master_partition_id) master_partition = self.getPartitionInformation(master_partition_id)
self.assertEqual(len(master_partition._parameter_dict['slave_instance_list']), 0) self.assertEqual(len(master_partition._parameter_dict['slave_instance_list']), 0)
def test_slave_request_updates_timestamp(self):
self.format_for_number_of_partitions(1)
# Provide partition
master_partition = self.request('http://sr//', None, 'MyMasterInstance', 'slappart4')
def getPartition():
return self.getFullComputerInformation()._computer_partition_list[0]
def getTimestamp(partition):
return float(partition._parameter_dict['timestamp'])
before = getPartition()
before_timestamp = getTimestamp(before)
self.assertEqual(len(before._parameter_dict['slave_instance_list']), 0)
time.sleep(0.1)
self.request('http://sr//', None, 'MyFirstSlave', shared=True)
after = getPartition()
after_timestamp = getTimestamp(after)
self.assertEqual(len(after._parameter_dict['slave_instance_list']), 1)
self.assertLess(before_timestamp, after_timestamp, 'Adding first slave shall change the timestamp')
before_timestamp = after_timestamp
time.sleep(0.1)
self.request('http://sr//', None, 'MySecondSlave', shared=True)
after = getPartition()
after_timestamp = getTimestamp(after)
self.assertEqual(len(after._parameter_dict['slave_instance_list']), 2)
self.assertLess(before_timestamp, after_timestamp, 'Adding second slave shall change the timestamp')
before_timestamp = after_timestamp
time.sleep(0.1)
self.request('http://sr//', None, 'MySecondSlave', shared=True, partition_parameter_kw={'a':'b'})
after = getPartition()
after_timestamp = getTimestamp(after)
self.assertEqual(len(after._parameter_dict['slave_instance_list']), 2)
self.assertLess(before_timestamp, after_timestamp, 'Updating any slave shall change the timestamp')
before_timestamp = after_timestamp
time.sleep(0.1)
self.request('http://sr//', None, 'MySecondSlave', shared=True, partition_parameter_kw={'a':'b'})
after = getPartition()
after_timestamp = getTimestamp(after)
self.assertEqual(len(after._parameter_dict['slave_instance_list']), 2)
self.assertEqual(before_timestamp, after_timestamp, 'No-op change of the slave shall not change the timestamp')
def test_slave_request_set_parameters_are_updated(self): def test_slave_request_set_parameters_are_updated(self):
""" """
Parameters sent in slave request must be put in slave master Parameters sent in slave request must be put in slave master
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment