Fix Edge registration health routing

This commit is contained in:
Andrei Smaliak 2026-10-23 07:26:56 +02:00
parent 9ca6c24f18
commit f6dc3d94cc
5 changed files with 86 additions and 10 deletions

View File

@ -0,0 +1,16 @@
# stage_mqtt 1.0.6 — authoritative registration routing
Release date: 2026-10-23.
- Treat the registered IT-Screen client's MQTT prefix as authoritative when it
identifies the same broker as the system bridge.
- Replace a stale platform `BRIDGE_BASE_TOPIC` left by an earlier server
transfer instead of continuing to publish health into both environments.
- Preserve the intentional system-owned `v1/edge` namespace alongside the
active platform namespace.
- Clear the retired retained health topic before replacing its bridge route, so
the previous platform stops presenting the migrated Edge as current.
Validation: routing tests execute the generated `iomatex-mqtt-apply` functions
against matching and mismatched brokers, stale EVA-to-DEV platform prefixes,
the preserved system namespace, invalid prefixes and retained-topic cleanup.

View File

@ -8,7 +8,7 @@
"version": "1.0.0"
},
"stage_mqtt": {
"version": "1.0.5"
"version": "1.0.6"
},
"stage_z2m": {
"version": "1.0.1"

View File

@ -421,6 +421,38 @@ if (host == sys.argv[1].lower().rstrip(".")
__PLATFORM_HEALTH_PREFIX__
}
# A legacy client could move the bridge credentials to a new cloud without
# replacing BRIDGE_BASE_TOPIC. The registered client's prefix is authoritative
# once its broker identity matches the bridge. Keep v1/edge as the intentional
# system namespace, but never retain a different platform namespace beside the
# active registration.
RETIRED_PLATFORM_BASE_TOPIC=""
reconcile_platform_base_topic(){
local platform_prefix
platform_prefix="$(platform_health_prefix)"
[[ -n "$platform_prefix" ]] || return 0
[[ "$platform_prefix" != "$BRIDGE_BASE_TOPIC" ]] || return 0
case "$BRIDGE_BASE_TOPIC" in
it-screen/*|iomatex/*)
RETIRED_PLATFORM_BASE_TOPIC="$BRIDGE_BASE_TOPIC"
set_kv BRIDGE_BASE_TOPIC "$platform_prefix"
BRIDGE_BASE_TOPIC="$platform_prefix"
log "Reconciled platform base topic to active registration: ${platform_prefix}"
;;
esac
}
retire_previous_platform_health(){
[[ -n "$RETIRED_PLATFORM_BASE_TOPIC" ]] || return 0
# Clear the retained heartbeat while the previous bridge route is still
# active. Freshness still protects consumers if the broker is unavailable.
if ! mosquitto_pub -h 127.0.0.1 -p 1883 -q 1 -r \
-t "${RETIRED_PLATFORM_BASE_TOPIC}/health/${DEVICE_ID}" -n; then
log "WARNING: could not clear retired health topic ${RETIRED_PLATFORM_BASE_TOPIC}/health/${DEVICE_ID}"
fi
}
write_bridge_conf(){
rm -f "$BRIDGE_CONF" || true
@ -704,6 +736,8 @@ main(){
[[ "$DO_CHECK" == "1" ]] && check_remote
reconcile_platform_base_topic
retire_previous_platform_health
write_bridge_conf
restart_broker_if_changed

View File

@ -15,11 +15,14 @@ class HealthRouteTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmp:
config = Path(tmp) / 'client.conf'
bridge = Path(tmp) / 'bridge.conf'
bridge_env = Path(tmp) / 'bridge.env'
config.write_text(f'mqttHost={config_host or host}\nmqttPrefix={prefix}\nmqttPort={config_port or port}\nmqttTls=0\n')
bridge_env.write_text(f'BRIDGE_BASE_TOPIC={base}\n')
code = FUNCTIONS.replace('/var/lib/itscreen/config/it-screen/it-screen.conf', str(config))
code += '''
chown(){ :; }
chmod(){ :; }
ENV_FILE="$AUDIT_ENV"
REMOTE_HOST="$AUDIT_HOST"
REMOTE_PORT="$AUDIT_PORT"
REMOTE_TLS=0
@ -31,14 +34,16 @@ ENABLE_BRIDGE=1
LEGACY_ITSCREEN_TREE=1
BRIDGE_BASE_TOPIC="$AUDIT_BASE"
BRIDGE_CONF="$AUDIT_BRIDGE"
reconcile_platform_base_topic
write_bridge_conf
'''
env = dict(os.environ, AUDIT_HOST=host, AUDIT_PORT=port, AUDIT_BASE=base, AUDIT_BRIDGE=str(bridge))
env = dict(os.environ, AUDIT_HOST=host, AUDIT_PORT=port, AUDIT_BASE=base,
AUDIT_BRIDGE=str(bridge), AUDIT_ENV=str(bridge_env))
subprocess.run(['bash'], input=code, text=True, env=env, check=True, capture_output=True)
return bridge.read_text()
return bridge.read_text(), bridge_env.read_text()
def test_default_service_namespace_routes_to_dev(self):
text = self.route()
text, _ = self.route()
self.assertIn('topic it-screen/dev/health/g-box-deadbeef out 1 "" ""', text)
self.assertIn('topic v1/edge/command/g-box-deadbeef/# in 0', text)
self.assertIn('topic it-screen/$devices/settings/g-box-deadbeef/sub in 0', text)
@ -46,23 +51,44 @@ write_bridge_conf
self.assertIn('topic it-screen/+/client/pub/component/notify/g-box-deadbeef/# out 0', text)
def test_console_already_selected_platform_has_no_duplicate(self):
text = self.route(base='it-screen/dev')
text, _ = self.route(base='it-screen/dev')
self.assertEqual(1, text.count('/health/g-box-deadbeef out 1'))
self.assertIn('topic it-screen/dev/health/g-box-deadbeef out 1', text)
def test_new_cloud_uses_its_own_prefix(self):
text = self.route(host='uae.iomatex.com', prefix='iomatex/ae')
text, _ = self.route(host='uae.iomatex.com', prefix='iomatex/ae')
self.assertIn('topic iomatex/ae/health/g-box-deadbeef out 1', text)
self.assertNotIn('topic it-screen/dev/health/', text)
def test_mid_switch_does_not_route_old_prefix_to_new_broker(self):
self.assertNotIn('topic it-screen/dev/health/', self.route(host='uae.iomatex.com', config_host='dev.iomatex.com'))
self.assertNotIn('topic it-screen/dev/health/', self.route(port='11883', config_port='1883'))
text, _ = self.route(host='uae.iomatex.com', config_host='dev.iomatex.com')
self.assertNotIn('topic it-screen/dev/health/', text)
text, _ = self.route(port='11883', config_port='1883')
self.assertNotIn('topic it-screen/dev/health/', text)
def test_invalid_and_unregistered_prefix_never_adds_wildcards(self):
for prefix in ['', 'it-screen/+', 'it-screen/#', 'bad prefix']:
with self.subTest(prefix=prefix):
self.assertNotIn('topic it-screen/dev/health/', self.route(prefix=prefix))
text, _ = self.route(prefix=prefix)
self.assertNotIn('topic it-screen/dev/health/', text)
def test_registration_replaces_stale_platform_base_topic(self):
text, bridge_env = self.route(prefix='it-screen/dev', base='it-screen/eva')
self.assertIn('BRIDGE_BASE_TOPIC=it-screen/dev', bridge_env)
self.assertIn('topic it-screen/dev/health/g-box-deadbeef out 1', text)
self.assertNotIn('topic it-screen/eva/health/g-box-deadbeef', text)
def test_system_base_topic_is_preserved(self):
text, bridge_env = self.route(prefix='it-screen/dev', base='v1/edge')
self.assertIn('BRIDGE_BASE_TOPIC=v1/edge', bridge_env)
self.assertIn('topic v1/edge/health/g-box-deadbeef out 1', text)
self.assertIn('topic it-screen/dev/health/g-box-deadbeef out 1', text)
def test_reconciliation_clears_retired_retained_health(self):
self.assertIn(
'-t "${RETIRED_PLATFORM_BASE_TOPIC}/health/${DEVICE_ID}" -n',
FUNCTIONS,
)
def test_upgrade_preserves_registration_and_retained_store(self):
cleanup = SOURCE.split('MQTT_CLEANUP_FILES=(', 1)[1].split('\n)', 1)[0]

View File

@ -1,3 +1,3 @@
{
"version": "1.0.5"
"version": "1.0.6"
}