Swaps the legacy OpcManager read path for the gateway's SubscriptionEngine over the shared OpcConnectionPool. The OPC client certificate now always comes from MinIO + Mongo (CertProvider, Basic256Sha256) — no more certs/ shipped in the Gitea repository, so a new version branch no longer breaks the reads. - io/slots.py: parse_slot accepts slot v2 (names[]) and converts the legacy v1 (tag_name + topics; cert_path => use_security, path dropped), so gateway and Orchestrator can be deployed in any order. - managers/server_reader.py: new per-server reader with the OpcManager interface the IngestorManager already drives (connect, create_subscription, subscribe, unsubscribe, check_cycles, check_opc_listenning, shutdown). - IngestorManager owns the read path: lazy-MinIO CertProvider, the pool and a read DataSink, built without touching the network. Slots from Redis are normalized on lease and refresh; a malformed slot is notified and the last good config is kept. - GatewayRuntime borrows the pool (writes/gRPC) instead of building it, and no longer closes it on stop: a Postgres/NATS failure can't stop the reads. - pool: pin/unpin per active subscription so release_idle never evicts a subscribed connection; public evict() for lost servers / changed config. - SubscriptionEngine pins on subscribe, unpins on unsubscribe, and drops a subscription locally even when the server-side delete fails. - DataSink: model_id only written when the name has one, keeping v1 documents identical to the legacy ones. - config: CERT_BUCKET default is the legacy streamlit-connectors bucket (D7). - Removes OpcManager, its tests and scripts/generate-and-push-opc-cert.sh; README/CLAUDE.md document the MinIO certificate and the slot v2 format. Tests: 218 unit tests pass; ruff check/format clean. Local end-to-end against an asyncua server: v1 slot with two topics -> one doc per collection, legacy doc shape, idle sweep keeps the subscription, lost-server shutdown and re-initialization work. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
251 lines
8.3 KiB
Python
251 lines
8.3 KiB
Python
import asyncio
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
import ingestor.metrics as metrics
|
|
from ingestor.connection.pool import OpcConnectionPool
|
|
|
|
|
|
def make_pool(cert_provider=None):
|
|
pool = OpcConnectionPool(
|
|
cert_provider=cert_provider or AsyncMock(),
|
|
logger=MagicMock(),
|
|
notification_handler=MagicMock(),
|
|
metrics_controller=AsyncMock(),
|
|
)
|
|
pool.emit_metric = AsyncMock()
|
|
return pool
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_reuses_connection_per_server():
|
|
pool = make_pool()
|
|
fake_client = AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client):
|
|
c1 = await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
c2 = await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
assert c1 is c2
|
|
assert fake_client.connect.await_count == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_failure_records_metric_and_raises():
|
|
pool = make_pool()
|
|
fake_client = AsyncMock()
|
|
fake_client.connect.side_effect = RuntimeError('boom')
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client):
|
|
with pytest.raises(RuntimeError):
|
|
await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
pool.emit_metric.assert_any_await(
|
|
metric_object=metrics.OPC_CONNECTIONS_FAILED,
|
|
tags={'pod_id': pool.pod_id, 'server_name': 'srv'},
|
|
)
|
|
assert 'srv' not in pool._connections
|
|
|
|
# a later successful get must reconnect (failed attempt was not cached)
|
|
fake_client2 = AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client2):
|
|
client = await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
assert client is fake_client2
|
|
assert fake_client2.connect.await_count == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_applies_security_when_requested():
|
|
cert_provider = AsyncMock()
|
|
cert_provider.fetch.return_value = ('cert.pem', 'key.pem')
|
|
pool = make_pool(cert_provider=cert_provider)
|
|
fake_client = AsyncMock()
|
|
with (
|
|
patch('ingestor.connection.pool.Client', return_value=fake_client),
|
|
patch('ingestor.connection.pool.apply_security', new=AsyncMock()) as mock_apply,
|
|
):
|
|
await pool.get(
|
|
'srv',
|
|
{'url': 'opc.tcp://h:4840', 'server_uri': 'urn:test', 'use_security': True},
|
|
)
|
|
|
|
cert_provider.fetch.assert_awaited_once()
|
|
mock_apply.assert_awaited_once_with(fake_client, 'urn:test', 'cert.pem', 'key.pem', None)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_release_idle_evicts_and_disconnects():
|
|
pool = make_pool()
|
|
fake_client = AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client):
|
|
await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
client, _ = pool._connections['srv']
|
|
pool._connections['srv'] = (client, -1000.0) # force idle
|
|
|
|
await pool.release_idle(max_idle_s=0)
|
|
|
|
fake_client.disconnect.assert_awaited_once()
|
|
assert 'srv' not in pool._connections
|
|
pool.emit_metric.assert_any_await(
|
|
metric_object=metrics.OPC_DISCONNECTIONS_TOTAL,
|
|
tags={'pod_id': pool.pod_id, 'server_name': 'srv'},
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_release_idle_keeps_recently_used():
|
|
pool = make_pool()
|
|
fake_client = AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client):
|
|
await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
await pool.release_idle(max_idle_s=3600)
|
|
|
|
fake_client.disconnect.assert_not_awaited()
|
|
assert 'srv' in pool._connections
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_close_all_disconnects_everything():
|
|
pool = make_pool()
|
|
fake_a, fake_b = AsyncMock(), AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', side_effect=[fake_a, fake_b]):
|
|
await pool.get('a', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
await pool.get('b', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
await pool.close_all()
|
|
|
|
fake_a.disconnect.assert_awaited_once()
|
|
fake_b.disconnect.assert_awaited_once()
|
|
assert pool._connections == {}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_release_idle_disconnect_retries_then_gives_up():
|
|
pool = make_pool()
|
|
fake_client = AsyncMock()
|
|
fake_client.disconnect.side_effect = RuntimeError('nope')
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client):
|
|
await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
client, _ = pool._connections['srv']
|
|
pool._connections['srv'] = (client, -1000.0)
|
|
|
|
await pool.release_idle(max_idle_s=0)
|
|
|
|
assert fake_client.disconnect.await_count == 5
|
|
assert 'srv' not in pool._connections
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_success_path_emits_connection_metrics():
|
|
pool = make_pool()
|
|
fake_client = AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client):
|
|
await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
pool.emit_metric.assert_any_await(
|
|
metric_object=metrics.OPC_CONNECTIONS_TOTAL,
|
|
tags={'pod_id': pool.pod_id, 'server_name': 'srv'},
|
|
)
|
|
pool.emit_metric.assert_any_await(
|
|
metric_object=metrics.OPC_CONNECTION_STATUS,
|
|
method='set',
|
|
value=1,
|
|
tags={'pod_id': pool.pod_id, 'server_name': 'srv', 'server_url': 'opc.tcp://h:4840'},
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_concurrent_get_same_server_connects_once():
|
|
pool = make_pool()
|
|
started = asyncio.Event()
|
|
proceed = asyncio.Event()
|
|
|
|
clients = [AsyncMock(), AsyncMock()]
|
|
|
|
async def slow_connect():
|
|
started.set()
|
|
await proceed.wait()
|
|
|
|
clients[0].connect.side_effect = slow_connect
|
|
|
|
cfg = {'url': 'opc.tcp://h:4840', 'use_security': False}
|
|
with patch('ingestor.connection.pool.Client', side_effect=clients) as ctor:
|
|
t1 = asyncio.create_task(pool.get('srv', cfg))
|
|
await started.wait() # t1 is mid-connect, holding the per-server lock
|
|
t2 = asyncio.create_task(pool.get('srv', cfg))
|
|
await asyncio.sleep(0) # let t2 block on the lock (not build a 2nd Client)
|
|
proceed.set()
|
|
c1, c2 = await asyncio.gather(t1, t2)
|
|
|
|
assert c1 is c2 is clients[0]
|
|
assert ctor.call_count == 1
|
|
assert clients[0].connect.await_count == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_release_idle_keeps_pinned_connection():
|
|
"""A subscribed connection only gets callbacks (never get()), so it looks idle:
|
|
the pin keeps release_idle from dropping it (SIENTIAPDE-1974 G4)."""
|
|
pool = make_pool()
|
|
fake_client = AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client):
|
|
await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
pool.pin('srv')
|
|
await pool.release_idle(max_idle_s=0)
|
|
|
|
assert 'srv' in pool._connections
|
|
fake_client.disconnect.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_release_idle_evicts_after_last_unpin():
|
|
pool = make_pool()
|
|
fake_client = AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', return_value=fake_client):
|
|
await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
pool.pin('srv')
|
|
pool.pin('srv') # two slots subscribed on the same server
|
|
pool.unpin('srv')
|
|
await pool.release_idle(max_idle_s=0)
|
|
assert 'srv' in pool._connections # one subscription still active
|
|
|
|
pool.unpin('srv')
|
|
await pool.release_idle(max_idle_s=0)
|
|
assert 'srv' not in pool._connections
|
|
fake_client.disconnect.assert_awaited_once()
|
|
|
|
|
|
def test_unpin_never_goes_negative():
|
|
pool = make_pool()
|
|
|
|
pool.unpin('srv')
|
|
pool.pin('srv')
|
|
|
|
assert pool._pins == {'srv': 1}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_evict_disconnects_now_and_next_get_reconnects():
|
|
pool = make_pool()
|
|
first, second = AsyncMock(), AsyncMock()
|
|
with patch('ingestor.connection.pool.Client', side_effect=[first, second]):
|
|
await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
await pool.evict('srv')
|
|
client = await pool.get('srv', {'url': 'opc.tcp://h:4840', 'use_security': False})
|
|
|
|
first.disconnect.assert_awaited_once()
|
|
assert client is second
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_evict_unknown_server_is_noop():
|
|
pool = make_pool()
|
|
|
|
await pool.evict('nope')
|
|
|
|
pool.emit_metric.assert_not_awaited()
|