Skip to content
48 changes: 48 additions & 0 deletions doc/cephadm/concepts.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Cephadm Scheduler
=================

Cephadm uses a declarative state to define the layout of the cluster. This
state consists of a list of service specificatins containing placement
specifications (See :ref:`orchestrator-cli-service-spec` ).

Cephadm constantly compares list of actually running daemons in the cluster
with the desired service specifications and will either add or remove new
daemons.

First, cephadm will select a list of candidate hosts. It first looks for
explicit host names and will select those. In case there are no explicit hosts
defined, cephadm looks for a label specification. If there is no label defined
in the specification, cephadm will select hosts based on a host pattern. If
there is no pattern defined, cepham will finally select all known hosts as
candidates.

Then, cephadm will consider existing daemons of this servics and will try to
avoid moving any daemons.

Cephadm supports the deployment of a specific amount of services. Let's
consider a service specification like so:

.. code-block:: yaml

service_type: mds
service_name: myfs
placement:
count: 3
label: myfs

This instructs cephadm to deploy three daemons on hosts labeld with
``myfs`` across the cluster.

Then, in case there are less than three daemons deployed on the candidate
hosts, cephadm will then then randomly choose hosts for deploying new daemons.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/will then then randomly/will then randomly/


In case there are more than three daemons deployed, cephadm will remove
existing daemons.

Finally, cephadm will remove daemons on hosts that are outside of the list of
candidate hosts.

However, there is a special cases that cephadm needs to consider.

In case the are fewer hosts selected by the placement specification than
demanded by ``count``, cephadm will only deploy on selected hosts.
1 change: 1 addition & 0 deletions doc/cephadm/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ versions of Ceph.
Client Setup <client-setup>
DriveGroups <drivegroups>
troubleshooting
concepts
13 changes: 12 additions & 1 deletion src/pybind/mgr/cephadm/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,15 @@ def add_daemon(self, host, dd):
def rm_daemon(self, host, name):
if host in self.daemons:
if name in self.daemons[host]:
del self.daemons[host][name]
del self.daemons[host][name]

def daemon_cache_filled(self):
"""
i.e. we have checked the daemons for each hosts at least once.
excluding offline hosts.

We're not checking for `host_needs_daemon_refresh`, as this might never be
False for all hosts.
"""
return all((h in self.last_daemon_update or h in self.mgr.offline_hosts)
for h in self.get_hosts())
128 changes: 128 additions & 0 deletions src/pybind/mgr/cephadm/migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import logging
from typing import TYPE_CHECKING, Iterator

from ceph.deployment.service_spec import PlacementSpec, ServiceSpec, HostPlacementSpec
from cephadm.schedule import HostAssignment

from orchestrator import OrchestratorError

if TYPE_CHECKING:
from .module import CephadmOrchestrator

LAST_MIGRATION = 1
Comment thread
sebastian-philipp marked this conversation as resolved.
Resolved

logger = logging.getLogger(__name__)


class Migrations:
def __init__(self, mgr: "CephadmOrchestrator"):
self.mgr = mgr

# Why having a global counter, instead of spec versions?
#
# for the first migration:
# The specs don't change in (this) migration. but the scheduler here.
# Adding the version to the specs at this time just felt wrong to me.
#
# And the specs are only another part of cephadm which needs potential upgrades.
# We have the cache, the inventory, the config store, the upgrade (imagine changing the
# upgrade code, while an old upgrade is still in progress), naming of daemons,
# fs-layout of the daemons, etc.
if self.mgr.migration_current is None:
self.set(0)

# for some migrations, we don't need to do anything except for
# setting migration_current = 1.
# let's try to shortcut things here.
self.migrate()
Comment thread
sebastian-philipp marked this conversation as resolved.
Resolved

def set(self, val):
self.mgr.set_module_option('migration_current', val)
self.mgr.migration_current = val

def is_migration_ongoing(self):
return self.mgr.migration_current != LAST_MIGRATION

def verify_no_migration(self):
if self.is_migration_ongoing():
# this is raised in module.serve()
raise OrchestratorError("cephadm migration still ongoing. Please wait, until the migration is complete.")
Comment thread
sebastian-philipp marked this conversation as resolved.
Resolved

def migrate(self):
if self.mgr.migration_current == 0:
if self.migrate_0_1():
self.set(1)

def migrate_0_1(self) -> bool:
"""
Migration 0 -> 1
New scheduler that takes PlacementSpec as the bound and not as recommendation.
Comment thread
sebastian-philipp marked this conversation as resolved.
Resolved
I.e. the new scheduler won't suggest any new placements outside of the hosts
specified by label etc.

Which means, we have to make sure, we're not removing any daemons directly after
upgrading to the new scheduler.

There is a potential race here:
1. user updates his spec to remove daemons
2. mgr gets upgraded to new scheduler, before the old scheduler removed the daemon
3. now, we're converting the spec to explicit placement, thus reverting (1.)
I think this is ok.
"""

def interesting_specs() -> Iterator[ServiceSpec]:
for s in self.mgr.spec_store.specs.values():
if s.unmanaged:
continue
p = s.placement
if p is None:
continue
if p.count is None:
continue
if not p.hosts and not p.host_pattern and not p.label:
continue
yield s

def convert_to_explicit(spec: ServiceSpec) -> None:
placements = HostAssignment(
spec=spec,
get_hosts_func=self.mgr._get_hosts,
get_daemons_func=self.mgr.cache.get_daemons_by_service
).place()

existing_daemons = self.mgr.cache.get_daemons_by_service(spec.service_name())

# We have to migrate, only if the new scheduler would remove daemons
Comment thread
jschmid1 marked this conversation as resolved.
Resolved
if len(placements) >= len(existing_daemons):
return

old_hosts = {h.hostname: h for h in spec.placement.hosts}
new_hosts = [
old_hosts[d.hostname] if d.hostname in old_hosts else HostPlacementSpec(hostname=d.hostname, network='', name='')
for d in existing_daemons
]

new_placement = PlacementSpec(
hosts=new_hosts,
count=spec.placement.count
)

new_spec = ServiceSpec.from_json(spec.to_json())
new_spec.placement = new_placement

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can also see an issue where a user would apply a cluster.yaml only when changing a single service. Since we converted the placement_glob(or pattern) to an explicit placement spec, they probably have to export and overwrite their existing cluster.yaml.

I think this should be recommended somehow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that's a big problem but I didn't have a better answer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having previews for all services would be a first step I think.. If placements would be changed completely we could atleast raise a warning before applying it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both schedulers behave very similar when it comes to assigning hosts. It's just the old assigned more hosts than the new one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. but what's the alternative? what if the user wrongly created a placement like

service_type: mon
placement:
    label: mymons
    count=5

but only a single host was labled as mymons? should we simply remove all the mons? I'm also against being more elaborate here as this increases the likelihood of bugs. I'd be really in favor of keeping this as simple as possible to stay safe.


logger.info(f"Migrating {spec.one_line_str()} to explicit placement")

self.mgr.spec_store.save(new_spec)

specs = list(interesting_specs())
if not specs:
return True # nothing to do. shortcut

if not self.mgr.cache.daemon_cache_filled():
logger.info("Unable to migrate yet. Daemon Cache still incomplete.")
return False

for spec in specs:
convert_to_explicit(spec)

return True
121 changes: 71 additions & 50 deletions src/pybind/mgr/cephadm/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

from . import remotes
from . import utils
from .migrations import Migrations
from .services.cephadmservice import MonService, MgrService, MdsService, RgwService, \
RbdMirrorService, CrashService, CephadmService
from .services.iscsi import IscsiService
Expand Down Expand Up @@ -258,6 +259,13 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule):
'default': '/etc/prometheus/ceph/ceph_default_alerts.yml',
'desc': 'location of alerts to include in prometheus deployments',
},
{
'name': 'migration_current',
'type': 'int',
'default': None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noneis not an integer, thus it will break code that relies on the configured type. It would be better to choose a value of int, e.g. -1 or 0 here.
The regression introduced by this PR is https://tracker.ceph.com/issues/45963.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm.

When looking at the code:

def get_module_option(self, key, default=None):
"""
Retrieve the value of a persistent configuration setting
:param str key:
:param default: the default value of the config if it is not found
:return: str
"""
r = self._ceph_get_module_option(key)
if r is None:
return self.MODULE_OPTION_DEFAULTS.get(key, default)
else:
return r

having a default that is arbitrary does seems to work find. Having the dashboard treat the values differently than the mgr_module.py seems odd to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'desc': 'internal - do not modify',
# used to track track spec and other data migrations.
},
]

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -290,6 +298,7 @@ def __init__(self, *args, **kwargs):
self.warn_on_failed_host_check = True
self.allow_ptrace = False
self.prometheus_alerts_path = ''
self.migration_current = None

self._cons = {} # type: Dict[str, Tuple[remoto.backends.BaseConnection,remoto.backends.LegacyModuleExecute]]

Expand Down Expand Up @@ -335,6 +344,8 @@ def __init__(self, *args, **kwargs):
# in-memory only.
self.offline_hosts: Set[str] = set()

self.migration = Migrations(self)

# services:
self.osd_service = OSDService(self)
self.nfs_service = NFSService(self)
Expand Down Expand Up @@ -481,56 +492,8 @@ def serve(self):
while self.run:

# refresh daemons
self.log.debug('refreshing hosts')
bad_hosts = []
failures = []
for host in self.cache.get_hosts():
if self.cache.host_needs_check(host):
r = self._check_host(host)
if r is not None:
bad_hosts.append(r)
if self.cache.host_needs_daemon_refresh(host):
self.log.debug('refreshing %s daemons' % host)
r = self._refresh_host_daemons(host)
if r:
failures.append(r)
if self.cache.host_needs_device_refresh(host):
self.log.debug('refreshing %s devices' % host)
r = self._refresh_host_devices(host)
if r:
failures.append(r)

if self.cache.host_needs_osdspec_preview_refresh(host):
self.log.debug(f"refreshing OSDSpec previews for {host}")
r = self._refresh_host_osdspec_previews(host)
if r:
failures.append(r)

health_changed = False
if 'CEPHADM_HOST_CHECK_FAILED' in self.health_checks:
del self.health_checks['CEPHADM_HOST_CHECK_FAILED']
health_changed = True
if bad_hosts:
self.health_checks['CEPHADM_HOST_CHECK_FAILED'] = {
'severity': 'warning',
'summary': '%d hosts fail cephadm check' % len(bad_hosts),
'count': len(bad_hosts),
'detail': bad_hosts,
}
health_changed = True
if failures:
self.health_checks['CEPHADM_REFRESH_FAILED'] = {
'severity': 'warning',
'summary': 'failed to probe daemons or devices',
'count': len(failures),
'detail': failures,
}
health_changed = True
elif 'CEPHADM_REFRESH_FAILED' in self.health_checks:
del self.health_checks['CEPHADM_REFRESH_FAILED']
health_changed = True
if health_changed:
self.set_health_checks(self.health_checks)
self.log.debug('refreshing hosts and daemons')
self._refresh_hosts_and_daemons()

self._check_for_strays()

Expand All @@ -549,6 +512,10 @@ def serve(self):

self.rm_util._remove_osds_bg()

self.migration.migrate()
if self.migration.is_migration_ongoing():
continue

if self._apply_all_services():
continue # did something, refresh

Expand Down Expand Up @@ -1158,6 +1125,58 @@ def _refresh_host_osdspec_previews(self, host) -> bool:
self.log.debug(f'Refreshed OSDSpec previews for host <{host}>')
return True

def _refresh_hosts_and_daemons(self):
bad_hosts = []
failures = []
for host in self.cache.get_hosts():
if self.cache.host_needs_check(host):
r = self._check_host(host)
if r is not None:
bad_hosts.append(r)
if self.cache.host_needs_daemon_refresh(host):
self.log.debug('refreshing %s daemons' % host)
r = self._refresh_host_daemons(host)
if r:
failures.append(r)
if self.cache.host_needs_device_refresh(host):
self.log.debug('refreshing %s devices' % host)
r = self._refresh_host_devices(host)
if r:
failures.append(r)

if self.cache.host_needs_osdspec_preview_refresh(host):
self.log.debug(f"refreshing OSDSpec previews for {host}")
r = self._refresh_host_osdspec_previews(host)
if r:
failures.append(r)


health_changed = False
if 'CEPHADM_HOST_CHECK_FAILED' in self.health_checks:
del self.health_checks['CEPHADM_HOST_CHECK_FAILED']
health_changed = True
if bad_hosts:
self.health_checks['CEPHADM_HOST_CHECK_FAILED'] = {
'severity': 'warning',
'summary': '%d hosts fail cephadm check' % len(bad_hosts),
'count': len(bad_hosts),
'detail': bad_hosts,
}
health_changed = True
if failures:
self.health_checks['CEPHADM_REFRESH_FAILED'] = {
'severity': 'warning',
'summary': 'failed to probe daemons or devices',
'count': len(failures),
'detail': failures,
}
health_changed = True
elif 'CEPHADM_REFRESH_FAILED' in self.health_checks:
del self.health_checks['CEPHADM_REFRESH_FAILED']
health_changed = True
if health_changed:
self.set_health_checks(self.health_checks)

def _refresh_host_daemons(self, host):
try:
out, err, code = self._run_cephadm(
Expand Down Expand Up @@ -1995,6 +2014,8 @@ def add_mgr(self, spec):
return self._add_daemon('mgr', spec, self.mgr_service.create)

def _apply(self, spec: GenericSpec) -> str:
self.migration.verify_no_migration()

if spec.service_type == 'host':
return self._add_host(cast(HostSpec, spec))

Expand Down
Loading