-
Notifications
You must be signed in to change notification settings - Fork 11
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
Debezium streaming integration #170
Open
smallgod
wants to merge
7
commits into
openmrs:main
Choose a base branch
from
smallgod:debezium-streaming-integration
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
83f9287
testing issue with dependencies conflict
smallgod d118ae2
Resolve Debezium-mysql conflict issue
smallgod 78e2015
Setup Debezium with offsets
smallgod bb981dc
Setup Debezium with offsets
smallgod 237c710
Adds externally tested files
smallgod ec33ef0
more cleaning up
smallgod 38d84fd
Remove old implementation of debezium
smallgod 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 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package org.openmrs.module.mambacore.api.dao.impl; | ||
|
||
import org.openmrs.module.dbevent.DbEventSource; | ||
import org.openmrs.module.dbevent.DbEventSourceConfig; | ||
import org.openmrs.module.dbevent.EventContext; | ||
import org.openmrs.module.mambacore.api.dao.FlattenDatabaseDao; | ||
import org.openmrs.module.mambacore.db.ConnectionPoolManager; | ||
import org.openmrs.module.mambacore.db.debezium.MyEventConsumer; | ||
import org.openmrs.module.mambacore.util.MambaETLProperties; | ||
import org.openmrs.module.mambacore.util.StringReplacerUtil; | ||
import org.slf4j.Logger; | ||
|
@@ -17,6 +21,7 @@ | |
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
@@ -29,13 +34,33 @@ public class JdbcFlattenDatabaseDao implements FlattenDatabaseDao { | |
private static final String MYSQL_COMMENT_REGEX = "--.*(?=\\n)"; | ||
private static final String DELIMITER = "~-~-"; | ||
|
||
//private DebeziumListener debeziumListener = new DebeziumListener(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete commented code |
||
|
||
/** | ||
* Deploy MambaETL stored procedures | ||
*/ | ||
@Override | ||
public void deployMambaEtl() { | ||
|
||
log.info("Deploying MambaETL..."); | ||
MambaETLProperties props = MambaETLProperties.getInstance(); | ||
log.info("Deploying MambaETL, scheduled @interval: " + props.getInterval() + " seconds..."); | ||
executeSqlScript(props); | ||
log.info("Done deploying MambaETL..."); | ||
log.info("MambaETL deployed (with interval: " + props.getInterval() + "s )..."); | ||
} | ||
|
||
/** | ||
* Stream in database changes using Debezium | ||
*/ | ||
@Override | ||
public void streamInDatabaseChanges() { | ||
//debeziumListener.startListening(); | ||
EventContext ctx = new EventContext(); | ||
DbEventSourceConfig config = new DbEventSourceConfig(100002, "mamba-debezium", ctx); | ||
config.configureTablesToInclude(Arrays.asList("obs", "patient", "encounter", "encounter_type", "location")); | ||
DbEventSource eventSource = new DbEventSource(config); | ||
MyEventConsumer consumer = new MyEventConsumer(); | ||
eventSource.setEventConsumer(consumer); | ||
eventSource.start(); | ||
} | ||
|
||
private void executeSqlScript(MambaETLProperties props) { | ||
|
This file contains 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 |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
import org.slf4j.LoggerFactory; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import javax.annotation.PreDestroy; | ||
//import javax.annotation.PreDestroy; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete commented code |
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
@@ -38,6 +38,7 @@ public void setupEtl() { | |
executorService.submit(() -> { | ||
try { | ||
dao.deployMambaEtl(); | ||
dao.streamInDatabaseChanges(); | ||
} catch (Exception e) { | ||
log.error("Error deploying Mamba ETL", e); | ||
} | ||
|
@@ -46,7 +47,7 @@ public void setupEtl() { | |
|
||
|
||
@Override | ||
@PreDestroy | ||
//@PreDestroy | ||
public void shutdownEtlThread() { | ||
executorService.shutdown(); | ||
try { | ||
|
67 changes: 67 additions & 0 deletions
67
api/src/main/java/org/openmrs/module/mambacore/debezium/DbChangeConsumer.java
This file contains 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,67 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* <p> | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.mambacore.debezium; | ||
|
||
import io.debezium.engine.ChangeEvent; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Implementation of a Debezium ChangeEvent consumer, which abstracts the Debezium API behind a DbCrudEvent | ||
* and ensures that the registered DbCrudEvent EventConsumer is successfully processed before moving onto the next | ||
* record, with a configurable retryInterval upon failure. | ||
**/ | ||
public class DbChangeConsumer implements Consumer<ChangeEvent<SourceRecord, SourceRecord>> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DbChangeConsumer.class); | ||
|
||
private final EventConsumer eventConsumer; | ||
private final DbChangeToEventMapper eventMapper; | ||
private boolean disabled = false; | ||
|
||
public DbChangeConsumer(EventConsumer eventConsumer, DbChangeToEventMapper eventMapper) { | ||
this.eventConsumer = eventConsumer; | ||
this.eventMapper = eventMapper; | ||
} | ||
|
||
/** | ||
* This the primary handler for all Debezium-generated change events. Per the | ||
* <a href="https://debezium.io/documentation/reference/stable/development/engine.html">Debezium Documentation</a> | ||
* this function should not throw any exceptions, as these will simply get logged and Debezium will continue onto | ||
* the next source record. So if any exception is caught, this logs the Exception, and retries again after | ||
* a configurable retryInterval, until it passes. This effectively blocks any subsequent processing. | ||
* | ||
* @param changeEvent the Debeziumn generated event to process | ||
*/ | ||
@Override | ||
public final void accept(ChangeEvent<SourceRecord, SourceRecord> changeEvent) { | ||
|
||
try { | ||
if (disabled) { | ||
logger.error("The Debezium consumer has been stopped prior to processing: " + changeEvent); | ||
return; | ||
} | ||
|
||
DbEvent dbEvent = eventMapper.apply(changeEvent); | ||
logger.debug("Notifying listener of the database event: " + dbEvent); | ||
eventConsumer.accept(dbEvent); | ||
} catch (Throwable t) { | ||
logger.error("An error occurred processing change event: " + changeEvent, t); | ||
disabled = true; | ||
} | ||
} | ||
|
||
public void cancel() { | ||
this.disabled = true; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
api/src/main/java/org/openmrs/module/mambacore/debezium/DbChangeService.java
This file contains 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,19 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* <p> | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.mambacore.debezium; | ||
|
||
public interface DbChangeService { | ||
|
||
void start(); | ||
|
||
void stop(); | ||
|
||
void reset(); | ||
} |
100 changes: 100 additions & 0 deletions
100
api/src/main/java/org/openmrs/module/mambacore/debezium/DbChangeServiceImpl.java
This file contains 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,100 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* <p> | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.mambacore.debezium; | ||
|
||
import io.debezium.config.Configuration; | ||
import io.debezium.embedded.Connect; | ||
import io.debezium.engine.ChangeEvent; | ||
import io.debezium.engine.DebeziumEngine; | ||
import io.debezium.engine.spi.OffsetCommitPolicy; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class DbChangeServiceImpl implements DbChangeService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DbChangeServiceImpl.class); | ||
|
||
private DbChangeConsumer consumer; | ||
|
||
private Configuration debeziumConfig; | ||
|
||
private RocksDBOffsetBackingStore offsetBackingStore; | ||
|
||
private DebeziumEngine<ChangeEvent<SourceRecord, SourceRecord>> engine; | ||
|
||
private ExecutorService executor; | ||
|
||
@Override | ||
public void start() { | ||
|
||
if (engine != null) { | ||
logger.warn("Debezium engine is already running."); | ||
return; | ||
} | ||
|
||
engine = DebeziumEngine.create(Connect.class) | ||
.using(debeziumConfig.asProperties()) | ||
.notifying(consumer) | ||
.using(OffsetCommitPolicy.always()) | ||
.build(); | ||
|
||
executor = Executors.newSingleThreadExecutor(); | ||
executor.execute(engine); | ||
Runtime.getRuntime().addShutdownHook(new Thread(this::stop)); | ||
|
||
logger.info("Debezium engine started."); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
|
||
if (engine == null) { | ||
logger.warn("Debezium engine is not running."); | ||
return; | ||
} | ||
|
||
try { | ||
engine.close(); | ||
executor.shutdown(); | ||
consumer.cancel(); | ||
if (offsetBackingStore != null) { | ||
offsetBackingStore.stop(); | ||
} | ||
|
||
try { | ||
while (!this.executor.awaitTermination(5L, TimeUnit.SECONDS)) { | ||
logger.info("Waiting 5 seconds for the Debezium engine to shut down..."); | ||
} | ||
} catch (InterruptedException ie) { | ||
logger.error("Interrupted while waiting for termination", ie); | ||
Thread.currentThread().interrupt(); | ||
} | ||
|
||
} catch (Exception e) { | ||
logger.error("Error stopping Debezium engine", e); | ||
executor.shutdownNow(); | ||
} finally { | ||
engine = null; | ||
executor = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
stop(); | ||
start(); | ||
logger.info("Debezium engine restarted."); | ||
} | ||
} |
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.
Please delete the comments