-
-
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 1 commit
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
110 changes: 110 additions & 0 deletions
110
...in/java/io/debezium/platform/environment/host/discovery/HostReconciliationRepository.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,110 @@ | ||
| /* | ||
| * 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.time.Instant; | ||
| import java.util.List; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.persistence.EntityManager; | ||
| import jakarta.transaction.Transactional; | ||
|
|
||
| import org.jboss.logging.Logger; | ||
|
|
||
| import io.debezium.platform.data.model.HostStatusEntity; | ||
| import io.debezium.platform.data.model.ProvisioningStatus; | ||
|
|
||
| /** | ||
| * Encapsulates all database operations for host reconciliation. | ||
| * | ||
| * <p>This class exists as a separate {@code @ApplicationScoped} bean because | ||
| * {@code @Transactional} annotations are ignored on self-invocations within | ||
| * the same CDI bean. The {@link SshConfigWatcherService} delegates all DB | ||
| * writes to this repository so that each call goes through the CDI proxy | ||
| * and the transaction is properly managed. | ||
| * | ||
| * <p>Uses {@link EntityManager} directly, matching the existing project pattern | ||
| * (see {@code AbstractService}, {@code PipelineService}, etc.). | ||
| */ | ||
| @ApplicationScoped | ||
| public class HostReconciliationRepository { | ||
|
|
||
| private static final int DEFAULT_AGENT_PORT = 8090; | ||
|
|
||
| private final EntityManager entityManager; | ||
| private final Logger logger; | ||
|
|
||
| public HostReconciliationRepository(EntityManager entityManager, Logger logger) { | ||
| this.entityManager = entityManager; | ||
| this.logger = logger; | ||
| } | ||
|
|
||
| /** | ||
| * Returns all host entities whose status is not {@link ProvisioningStatus#REMOVED}. | ||
| */ | ||
| @Transactional | ||
| public List<HostStatusEntity> findAllActiveHosts() { | ||
| return entityManager | ||
| .createQuery( | ||
| "SELECT h FROM host_status h WHERE h.provisioningStatus <> :removedStatus", | ||
| HostStatusEntity.class) | ||
| .setParameter("removedStatus", ProvisioningStatus.REMOVED) | ||
| .getResultList(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link HostStatusEntity} with {@link ProvisioningStatus#PENDING}. | ||
| */ | ||
| @Transactional | ||
| public void createPendingHost(String alias, String hostname) { | ||
| HostStatusEntity entity = new HostStatusEntity(); | ||
| entity.setSshAlias(alias); | ||
| entity.setHostname(hostname); | ||
| entity.setProvisioningStatus(ProvisioningStatus.PENDING); | ||
| entity.setAgentPort(DEFAULT_AGENT_PORT); | ||
| entity.setLastCheckedAt(Instant.now()); | ||
| entityManager.persist(entity); | ||
| logger.infov("Created pending host: {0}", alias); | ||
| } | ||
|
|
||
| /** | ||
| * Marks the host with the given alias as {@link ProvisioningStatus#REMOVED}. | ||
| */ | ||
| @Transactional | ||
| public void markHostRemoved(String alias) { | ||
| int updated = entityManager | ||
| .createQuery( | ||
| "UPDATE host_status h SET h.provisioningStatus = :status WHERE h.sshAlias = :alias") | ||
| .setParameter("status", ProvisioningStatus.REMOVED) | ||
| .setParameter("alias", alias) | ||
| .executeUpdate(); | ||
| if (updated > 0) { | ||
| logger.infov("Marked host as REMOVED: {0}", alias); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Updates the cached hostname for an existing host and resets its status | ||
| * to {@link ProvisioningStatus#PENDING} so it gets re-provisioned. | ||
| */ | ||
| @Transactional | ||
| public void updateHostDetails(String alias, String newHostname) { | ||
| int updated = entityManager | ||
| .createQuery( | ||
| "UPDATE host_status h SET h.hostname = :hostname, " | ||
| + "h.provisioningStatus = :status, " | ||
| + "h.lastCheckedAt = :now " | ||
| + "WHERE h.sshAlias = :alias") | ||
| .setParameter("hostname", newHostname) | ||
| .setParameter("status", ProvisioningStatus.PENDING) | ||
| .setParameter("now", Instant.now()) | ||
| .setParameter("alias", alias) | ||
| .executeUpdate(); | ||
| if (updated > 0) { | ||
| logger.infov("Updated host details and reset to PENDING: {0}", alias); | ||
| } | ||
| } | ||
| } | ||
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 SshConfigWatcherService#buildReconciliationPlan(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.
Though I like the repository pattern, for historic reason, we use the
BlazeBitpersistence layer.Could you please so align with it? This means your service will extend the
AbstractService, so same basic method likelist, create, update, delete, findByare already there.