Commit b72b662d authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_cloud&jio: Add API for calculate status (news) for compute partition

  Similar to getComputeNodeInformation, use caching system to not
  recalculate status for the related software instances, only acquire
  from cache if it is updated.
parent 4dd4d038
...@@ -329,3 +329,61 @@ class SlapOSComputeNodeMixin(object): ...@@ -329,3 +329,61 @@ class SlapOSComputeNodeMixin(object):
self.getReference(), ', '.join([q.getRelativeUrl() for q \ self.getReference(), ', '.join([q.getRelativeUrl() for q \
in software_installation_list]) in software_installation_list])
)) ))
def getComputePartitionNewsDict(self):
key = '%s_partition_news' % self.getReference()
cache_plugin = self._getCachePlugin()
refresh_etag = self._calculateRefreshEtag()
try:
entry = cache_plugin.get(key, DEFAULT_CACHE_SCOPE)
except KeyError:
entry = None
if entry is not None and isinstance(entry.getValue(), dict):
cached_dict = entry.getValue()
cached_etag = cached_dict.get('refresh_etag', None)
if (refresh_etag != cached_etag):
return self._getCachedComputePartitionNewsDict(key, refresh_etag)
else:
return cached_dict.get('data')
return self._getCachedComputePartitionNewsDict(key, refresh_etag)
def _getCachedComputePartitionNewsDict(self, key, refresh_etag):
unrestrictedSearchResults = self.getPortalObject().portal_catalog.unrestrictedSearchResults
compute_partition_uid_list = [x.uid for x in unrestrictedSearchResults(
parent_uid=self.getUid(),
validation_state="validated",
portal_type="Compute Partition")]
software_instance_list = unrestrictedSearchResults(
portal_type="Software Instance",
default_aggregate_uid=compute_partition_uid_list,
validation_state="validated",
group_by_list=['default_aggregate_uid'],
select_list=['default_aggregate_uid', 'default_aggregate_title']
)
compute_partition_dict = { }
for software_instance in software_instance_list:
compute_partition_dict[software_instance.default_aggregate_title] = software_instance.getAccessStatus()
try:
self._getCachePlugin().set(key, DEFAULT_CACHE_SCOPE,
dict (
time=time.time(),
refresh_etag=refresh_etag,
data=compute_partition_dict
),
cache_duration=self.getPortalObject().portal_caches\
.getRamCacheRoot().get('compute_node_information_cache_factory'\
).cache_duration
)
except (Unauthorized, IndexError):
# XXX: Unauthorized hack. Race condition of not ready setup delivery which provides
# security information shall not make this method fail, as it will be
# called later anyway
# Note: IndexError ignored, as it happend in case if full reindex is
# called on site
pass
return compute_partition_dict
\ No newline at end of file
...@@ -2,13 +2,9 @@ from zExceptions import Unauthorized ...@@ -2,13 +2,9 @@ from zExceptions import Unauthorized
if REQUEST is not None: if REQUEST is not None:
raise Unauthorized raise Unauthorized
def get_compute_partition_dict(reference): def get_compute_partition_dict():
compute_node_dict = context.getAccessStatus() compute_node_dict = context.getAccessStatus()
compute_partition_dict = { } compute_partition_dict = context.getComputePartitionNewsDict()
for compute_partition in context.objectValues(portal_type="Compute Partition"):
software_instance = compute_partition.getAggregateRelatedValue(portal_type="Software Instance")
if software_instance is not None:
compute_partition_dict[compute_partition.getTitle()] = software_instance.getAccessStatus()
return {"compute_node": compute_node_dict, return {"compute_node": compute_node_dict,
"partition": compute_partition_dict, "partition": compute_partition_dict,
...@@ -16,5 +12,4 @@ def get_compute_partition_dict(reference): ...@@ -16,5 +12,4 @@ def get_compute_partition_dict(reference):
"reference": compute_node_dict['reference'], "reference": compute_node_dict['reference'],
"monitor_url": context.Base_getStatusMonitorUrl()} "monitor_url": context.Base_getStatusMonitorUrl()}
# Use Cache here, at least transactional one. return get_compute_partition_dict()
return get_compute_partition_dict(context.getReference())
...@@ -488,6 +488,11 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -488,6 +488,11 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin):
} }
self.assertEqual(_decode_with_json(news_dict), self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict)) _decode_with_json(expected_news_dict))
self.tic()
# Retest so cache is evaludated
news_dict = compute_node.ComputeNode_getNewsDict()
self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict))
def test_stopped(self): def test_stopped(self):
compute_node = self._makeComputeNode() compute_node = self._makeComputeNode()
...@@ -514,6 +519,11 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -514,6 +519,11 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin):
} }
self.assertEqual(_decode_with_json(news_dict), self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict)) _decode_with_json(expected_news_dict))
self.tic()
# Retest so cache is evaludated
news_dict = compute_node.ComputeNode_getNewsDict()
self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict))
def test_destroyed(self): def test_destroyed(self):
compute_node = self._makeComputeNode() compute_node = self._makeComputeNode()
...@@ -540,6 +550,11 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -540,6 +550,11 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin):
} }
self.assertEqual(_decode_with_json(news_dict), self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict)) _decode_with_json(expected_news_dict))
self.tic()
# Retest so cache is evaludated
news_dict = compute_node.ComputeNode_getNewsDict()
self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict))
def test_no_data(self): def test_no_data(self):
compute_node = self._makeComputeNode() compute_node = self._makeComputeNode()
...@@ -561,6 +576,11 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -561,6 +576,11 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin):
} }
self.assertEqual(_decode_with_json(news_dict), self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict)) _decode_with_json(expected_news_dict))
self.tic()
# Retest so cache is evaludated
news_dict = compute_node.ComputeNode_getNewsDict()
self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict))
def test_with_instance(self): def test_with_instance(self):
compute_node = self._makeComputeNode() compute_node = self._makeComputeNode()
...@@ -596,6 +616,12 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -596,6 +616,12 @@ class TestComputeNode_getNewsDict(TestSlapOSHalJsonStyleMixin):
self.assertEqual(_decode_with_json(news_dict), self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict)) _decode_with_json(expected_news_dict))
self.tic()
# Retest so cache is evaludated
news_dict = compute_node.ComputeNode_getNewsDict()
self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict))
class TestComputerNetwork_getNewsDict(TestSlapOSHalJsonStyleMixin): class TestComputerNetwork_getNewsDict(TestSlapOSHalJsonStyleMixin):
def test(self): def test(self):
......
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