-
-
Notifications
You must be signed in to change notification settings - Fork 31
debezium/dbz#2090 Add SSH config file watcher with host reconciliation #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
d1vyanshu-kumar
wants to merge
2
commits into
debezium:main
Choose a base branch
from
d1vyanshu-kumar:debezium/dbz#2090
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
debezium-platform-conductor/src/main/java/io/debezium/platform/domain/HostStatusService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Copyright Debezium Authors. | ||
| * | ||
| * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package io.debezium.platform.domain; | ||
|
|
||
| import static jakarta.transaction.Transactional.TxType.SUPPORTS; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.persistence.EntityManager; | ||
| import jakarta.transaction.Transactional; | ||
|
|
||
| import org.jboss.logging.Logger; | ||
|
|
||
| import com.blazebit.persistence.CriteriaBuilderFactory; | ||
| import com.blazebit.persistence.view.EntityViewManager; | ||
| import com.blazebit.persistence.view.EntityViewSetting; | ||
|
|
||
| import io.debezium.platform.data.model.HostStatusEntity; | ||
| import io.debezium.platform.data.model.ProvisioningStatus; | ||
| import io.debezium.platform.domain.views.HostStatus; | ||
| import io.debezium.platform.domain.views.refs.HostStatusReference; | ||
|
|
||
| /** | ||
| * Service for managing {@link HostStatusEntity} lifecycle via the | ||
| * Blaze-Persistence entity view layer. | ||
| * | ||
| * <p>Extends {@link AbstractService} to inherit standard CRUD operations | ||
| * ({@code list}, {@code create}, {@code update}, {@code delete}, {@code findById}) | ||
| * and adds host-specific queries needed by the SSH config watcher's | ||
| * reconciliation logic. | ||
| */ | ||
| @ApplicationScoped | ||
| public class HostStatusService extends AbstractService<HostStatusEntity, HostStatus, HostStatusReference> { | ||
|
|
||
| private static final int DEFAULT_AGENT_PORT = 8090; | ||
|
|
||
| private static final Logger logger = Logger.getLogger(HostStatusService.class); | ||
|
|
||
| public HostStatusService(EntityManager em, CriteriaBuilderFactory cbf, EntityViewManager evm) { | ||
| super(HostStatusEntity.class, HostStatus.class, HostStatusReference.class, em, cbf, evm); | ||
| } | ||
|
|
||
| /** | ||
| * Returns all host views whose provisioning status is not | ||
| * {@link ProvisioningStatus#REMOVED}. | ||
| */ | ||
| @Transactional(SUPPORTS) | ||
| public List<HostStatus> findAllActiveHosts() { | ||
| return evm.applySetting( | ||
| EntityViewSetting.create(HostStatus.class), | ||
| cb().where("provisioningStatus").notEq(ProvisioningStatus.REMOVED)).getResultList(); | ||
| } | ||
|
|
||
| /** | ||
| * Finds a host by its SSH config alias. | ||
| * | ||
| * @param alias the {@code Host} alias from {@code ~/.ssh/config} | ||
| * @return the matching view, or empty if no host with that alias exists | ||
| */ | ||
| @Transactional(SUPPORTS) | ||
| public Optional<HostStatus> findBySshAlias(String alias) { | ||
| List<HostStatus> results = evm.applySetting( | ||
| EntityViewSetting.create(HostStatus.class), | ||
| cb().where("sshAlias").eq(alias)).getResultList(); | ||
| return results.stream().findFirst(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new host with {@link ProvisioningStatus#PENDING}. | ||
| * | ||
| * @param alias the SSH config alias (natural key) | ||
| * @param hostname the resolved hostname or IP address | ||
| */ | ||
| public void createPendingHost(String alias, String hostname) { | ||
| HostStatus view = createEmpty(); | ||
| view.setSshAlias(alias); | ||
| view.setHostname(hostname); | ||
| view.setProvisioningStatus(ProvisioningStatus.PENDING); | ||
| view.setAgentPort(DEFAULT_AGENT_PORT); | ||
| view.setLastCheckedAt(Instant.now()); | ||
| create(view); | ||
| logger.infov("Created pending host: {0}", alias); | ||
| } | ||
|
|
||
| /** | ||
| * Marks the host with the given alias as {@link ProvisioningStatus#REMOVED}. | ||
| * | ||
| * @param alias the SSH config alias | ||
| */ | ||
| public void markHostRemoved(String alias) { | ||
| findBySshAlias(alias).ifPresent(host -> { | ||
| host.setProvisioningStatus(ProvisioningStatus.REMOVED); | ||
| update(host); | ||
| logger.infov("Marked host as REMOVED: {0}", alias); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Updates the cached hostname and resets status to | ||
| * {@link ProvisioningStatus#PENDING} so the host gets re-provisioned. | ||
| * | ||
| * @param alias the SSH config alias | ||
| * @param newHostname the new hostname or IP address | ||
| */ | ||
| public void updateHostDetails(String alias, String newHostname) { | ||
| findBySshAlias(alias).ifPresent(host -> { | ||
| host.setHostname(newHostname); | ||
| host.setProvisioningStatus(ProvisioningStatus.PENDING); | ||
| host.setLastCheckedAt(Instant.now()); | ||
| update(host); | ||
| logger.infov("Updated host details and reset to PENDING: {0}", alias); | ||
| }); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
debezium-platform-conductor/src/main/java/io/debezium/platform/domain/views/HostStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright Debezium Authors. | ||
| * | ||
| * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package io.debezium.platform.domain.views; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| import com.blazebit.persistence.view.CreatableEntityView; | ||
| import com.blazebit.persistence.view.EntityView; | ||
| import com.blazebit.persistence.view.UpdatableEntityView; | ||
|
|
||
| import io.debezium.platform.data.model.HostStatusEntity; | ||
| import io.debezium.platform.data.model.ProvisioningStatus; | ||
| import io.debezium.platform.domain.views.refs.HostStatusReference; | ||
|
|
||
| @EntityView(HostStatusEntity.class) | ||
| @CreatableEntityView | ||
| @UpdatableEntityView | ||
| public interface HostStatus extends HostStatusReference { | ||
|
|
||
| String getHostname(); | ||
|
|
||
| void setHostname(String hostname); | ||
|
|
||
| void setSshAlias(String sshAlias); | ||
|
|
||
| ProvisioningStatus getProvisioningStatus(); | ||
|
|
||
| void setProvisioningStatus(ProvisioningStatus status); | ||
|
|
||
| int getAgentPort(); | ||
|
|
||
| void setAgentPort(int port); | ||
|
|
||
| String getAgentToken(); | ||
|
|
||
| void setAgentToken(String token); | ||
|
|
||
| Instant getLastCheckedAt(); | ||
|
|
||
| void setLastCheckedAt(Instant lastCheckedAt); | ||
| } |
16 changes: 16 additions & 0 deletions
16
...m-conductor/src/main/java/io/debezium/platform/domain/views/refs/HostStatusReference.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * Copyright Debezium Authors. | ||
| * | ||
| * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package io.debezium.platform.domain.views.refs; | ||
|
|
||
| import com.blazebit.persistence.view.EntityView; | ||
|
|
||
| import io.debezium.platform.data.model.HostStatusEntity; | ||
| import io.debezium.platform.domain.views.base.IdView; | ||
|
|
||
| @EntityView(HostStatusEntity.class) | ||
| public interface HostStatusReference extends IdView { | ||
| String getSshAlias(); | ||
| } |
73 changes: 73 additions & 0 deletions
73
...n/java/io/debezium/platform/environment/host/discovery/HostReconciliationPlanBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright Debezium Authors. | ||
| * | ||
| * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package io.debezium.platform.environment.host.discovery; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import io.debezium.platform.domain.views.HostStatus; | ||
|
|
||
| /** | ||
| * Pure-function builder that compares SSH config file state against database | ||
| * state and produces a {@link ReconciliationPlan}. | ||
| * | ||
| * <p>This class is intentionally <strong>not</strong> a CDI bean — it has no | ||
| * injected dependencies, no mutable state, and no side effects. Every method | ||
| * is a deterministic transformation of its inputs. | ||
| */ | ||
| class HostReconciliationPlanBuilder { | ||
|
|
||
| /** | ||
| * Compares SSH config file state against database state and produces | ||
| * a reconciliation plan. | ||
| * | ||
| * @param fileHosts hosts parsed from the SSH config file | ||
| * @param dbHosts active host views from the database (status ≠ REMOVED) | ||
| * @return the reconciliation plan describing what needs to change | ||
| */ | ||
| ReconciliationPlan buildPlan(List<SshHostEntry> fileHosts, | ||
| List<HostStatus> dbHosts) { | ||
| Map<String, HostStatus> dbByAlias = dbHosts.stream() | ||
| .collect(Collectors.toMap(HostStatus::getSshAlias, Function.identity())); | ||
| Map<String, SshHostEntry> fileByAlias = fileHosts.stream() | ||
| .collect(Collectors.toMap(SshHostEntry::alias, Function.identity())); | ||
|
|
||
| List<SshHostEntry> toAdd = fileHosts.stream() | ||
| .filter(fileHost -> !dbByAlias.containsKey(fileHost.alias())) | ||
| .toList(); | ||
|
|
||
| List<SshHostEntry> toUpdate = fileHosts.stream() | ||
| .filter(fileHost -> dbByAlias.containsKey(fileHost.alias())) | ||
| .filter(fileHost -> isModified(fileHost, dbByAlias.get(fileHost.alias()))) | ||
| .toList(); | ||
|
|
||
| List<String> toRemove = dbHosts.stream() | ||
| .map(HostStatus::getSshAlias) | ||
| .filter(alias -> !fileByAlias.containsKey(alias)) | ||
| .toList(); | ||
|
|
||
| return new ReconciliationPlan(toAdd, toRemove, toUpdate); | ||
| } | ||
|
|
||
| /** | ||
| * Per ssh_config(5): if {@code HostName} is absent, the alias is used as hostname. | ||
| */ | ||
| String resolveHostname(SshHostEntry entry) { | ||
| return entry.hostname() != null ? entry.hostname() : entry.alias(); | ||
| } | ||
|
|
||
| /** | ||
| * Hostname is the critical field — if the IP/address changed, re-provisioning | ||
| * is needed. Per ssh_config(5), absent HostName defaults to the alias. | ||
| */ | ||
| private boolean isModified(SshHostEntry fileHost, HostStatus dbHost) { | ||
| String fileHostname = resolveHostname(fileHost); | ||
| String dbHostname = dbHost.getHostname(); | ||
| return !fileHostname.equals(dbHostname); | ||
| } | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
...tor/src/main/java/io/debezium/platform/environment/host/discovery/ReconciliationPlan.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright Debezium Authors. | ||
| * | ||
| * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package io.debezium.platform.environment.host.discovery; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Immutable result of comparing SSH config file state against database state. | ||
| * | ||
| * <p>Produced by | ||
| * {@link HostReconciliationPlanBuilder#buildPlan(List, List)} | ||
| * — a pure function with zero side effects and zero database access. | ||
| * | ||
| * @param toAdd hosts present in the file but not in the database | ||
| * @param toRemove aliases of hosts present in the database but not in the file | ||
| * @param toUpdate hosts present in both but with a changed hostname | ||
| */ | ||
| record ReconciliationPlan( | ||
| List<SshHostEntry> toAdd, | ||
| List<String> toRemove, | ||
| List<SshHostEntry> toUpdate) { | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this is not a bean? I think it should be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mfvitale I kept it as a plain Java object instantiated with new because it's completely stateless and acts as a pure function (no dependencies or side effects).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you make a good point about consistency — I can see that making it a bean keeps the dependency graph explicit and follows the same pattern as the rest of the codebase. I'll convert it to an
@ApplicationScopedbean and inject it into the watcher in the next commit.