Skip to content

Commit

Permalink
Merge PR #32686 into master
Browse files Browse the repository at this point in the history
* refs/pull/32686/head:
	mgr/cephadm: speed up when not refresh is needed
	mgr/cephadm: track age of service metadata
	mgr/orchestrator: include age of 'service ls' metadata
	mgr/mgr_util: add to_pretty_timedelta

Reviewed-by: Sebastian Wagner <swagner@suse.com>
  • Loading branch information
liewegas committed Jan 24, 2020
2 parents fe52da7 + c0060e2 commit 5eef6e6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/pybind/mgr/cephadm/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
pass # just for type checking


import datetime
import six
import os
import random
Expand All @@ -22,6 +23,7 @@

from ceph.deployment import inventory
from mgr_module import MgrModule
import mgr_util
import orchestrator
from orchestrator import OrchestratorError, HostSpec, OrchestratorValidationError

Expand All @@ -46,6 +48,8 @@
'StrictHostKeyChecking no\n'
'UserKnownHostsFile /dev/null\n')

DATEFMT = '%Y-%m-%dT%H:%M:%S.%f'

# for py2 compat
try:
from tempfile import TemporaryDirectory # py3
Expand Down Expand Up @@ -1075,6 +1079,8 @@ def _refresh_host_services(self, host):
out, err, code = self._run_cephadm(
host, 'mon', 'ls', [], no_fsid=True)
data = json.loads(''.join(out))
for d in data:
d['last_refresh'] = datetime.datetime.utcnow().strftime(DATEFMT)
self.log.debug('Refreshed host %s services: %s' % (host, data))
self.service_cache[host] = orchestrator.OutdatableData(data)
return host, data
Expand Down Expand Up @@ -1116,6 +1122,8 @@ def _get_services_result(results):
continue
self.log.debug('including %s %s' % (host, d))
sd = orchestrator.ServiceDescription()
sd.last_refresh = datetime.datetime.strptime(
d.get('last_refresh'), DATEFMT)
sd.service_type = d['name'].split('.')[0]
if service_type and service_type != sd.service_type:
continue
Expand All @@ -1142,8 +1150,11 @@ def _get_services_result(results):
result.append(sd)
return result

return self._refresh_host_services(wait_for_args).then(
_get_services_result)
if wait_for_args:
return self._refresh_host_services(wait_for_args).then(
_get_services_result)
else:
return trivial_result(_get_services_result({}))

def describe_service(self, service_type=None, service_id=None,
node_name=None, refresh=False):
Expand Down
1 change: 1 addition & 0 deletions src/pybind/mgr/mgr_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
except ImportError:
# just for type checking
pass
import datetime
import logging
import errno
import json
Expand Down
16 changes: 16 additions & 0 deletions src/pybind/mgr/mgr_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import datetime
import os
import socket
import logging
Expand Down Expand Up @@ -305,3 +306,18 @@ def _pairwise(iterable):
for b in it:
yield (a, b)
a = b

def to_pretty_timedelta(n):
if n < datetime.timedelta(seconds=120):
return str(n.seconds) + 's'
if n < datetime.timedelta(minutes=120):
return str(n.seconds // 60) + 'm'
if n < datetime.timedelta(hours=48):
return str(n.seconds // 360) + 'h'
if n < datetime.timedelta(days=14):
return str(n.days) + 'd'
if n < datetime.timedelta(days=7*12):
return str(n.days // 7) + 'w'
if n < datetime.timedelta(days=365*2):
return str(n.days // 30) + 'M'
return str(n.days // 365) + 'y'
3 changes: 3 additions & 0 deletions src/pybind/mgr/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,9 @@ def __init__(self, nodename=None,
# Service status description when status == -1.
self.status_desc = status_desc

# datetime when this info was last refreshed
self.last_refresh = None # type: Optional[datetime.datetime]

def name(self):
if self.service_instance:
return '%s.%s' % (self.service_type, self.service_instance)
Expand Down
11 changes: 9 additions & 2 deletions src/pybind/mgr/orchestrator_cli/module.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import datetime
import errno
import json
from functools import wraps

from ceph.deployment.inventory import Device
from prettytable import PrettyTable

from mgr_util import format_bytes
from mgr_util import format_bytes, to_pretty_timedelta

try:
from typing import List, Set, Optional
Expand Down Expand Up @@ -299,8 +300,9 @@ def ukn(s):
data = [s.to_json() for s in services]
return HandleCommandResult(stdout=json.dumps(data))
else:
now = datetime.datetime.utcnow()
table = PrettyTable(
['NAME', 'HOST', 'STATUS',
['NAME', 'HOST', 'STATUS', 'REFRESHED',
'VERSION', 'IMAGE NAME', 'IMAGE ID', 'CONTAINER ID'],
border=False)
table.align = 'l'
Expand All @@ -314,10 +316,15 @@ def ukn(s):
None: '<unknown>'
}[s.status]

if s.last_refresh:
age = to_pretty_timedelta(now - s.last_refresh) + ' ago'
else:
age = '-'
table.add_row((
s.name(),
ukn(s.nodename),
status,
age,
ukn(s.version),
ukn(s.container_image_name),
ukn(s.container_image_id)[0:12],
Expand Down

0 comments on commit 5eef6e6

Please sign in to comment.