Skip to content
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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@
<artifactId>commons-dbcp2</artifactId>
</dependency>

<!-- <dependency>-->
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete the comments

<!-- <groupId>io.debezium</groupId>-->
<!-- <artifactId>debezium-embedded</artifactId>-->
<!-- </dependency>-->

<!-- <dependency>-->
<!-- <groupId>io.debezium</groupId>-->
<!-- <artifactId>debezium-api</artifactId>-->
<!-- </dependency>-->

<!-- <dependency>-->
<!-- <groupId>io.debezium</groupId>-->
<!-- <artifactId>debezium-connector-mysql</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>dbevent-api</artifactId>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -125,4 +145,4 @@

</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@

public interface FlattenDatabaseDao {

/**
* Deploy MambaETL stored procedures
*/
void deployMambaEtl();

/**
* Stream in database changes using Debezium
*/
void streamInDatabaseChanges();
}
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;
Expand All @@ -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;
Expand All @@ -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();
Copy link

Choose a reason for hiding this comment

The 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PreDestroy;
//import javax.annotation.PreDestroy;
Copy link

Choose a reason for hiding this comment

The 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;
Expand All @@ -38,6 +38,7 @@ public void setupEtl() {
executorService.submit(() -> {
try {
dao.deployMambaEtl();
dao.streamInDatabaseChanges();
} catch (Exception e) {
log.error("Error deploying Mamba ETL", e);
}
Expand All @@ -46,7 +47,7 @@ public void setupEtl() {


@Override
@PreDestroy
//@PreDestroy
public void shutdownEtlThread() {
executorService.shutdown();
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* 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.db.debezium;

import org.apache.kafka.connect.json.JsonConverter;
import org.apache.kafka.connect.json.JsonConverterConfig;
import org.apache.kafka.connect.storage.FileOffsetBackingStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

/**
* Custom {@link FileOffsetBackingStore} that only saves the offset if no exception was encountered
* while processing a source record read by debezium from the MySQL binlog to ensure no binlog entry
* goes unprocessed.
*/
public class CustomFileOffsetBackingStore extends FileOffsetBackingStore {

protected static final Logger log = LoggerFactory.getLogger(CustomFileOffsetBackingStore.class);
private static final JsonConverter KEY_CONVERTER = new JsonConverter();
private static boolean disabled = false;

private OffsetUtils offsetUtils;

public CustomFileOffsetBackingStore() {
super(KEY_CONVERTER);
KEY_CONVERTER.configure(Collections.singletonMap(JsonConverterConfig.SCHEMAS_ENABLE_CONFIG, "false"), true);
}

/**
* Disables offset storage
*/
public static void disable() {
disabled = true;
log.debug("Disabled saving of offsets");
}

/**
* Re-enables offset storage
*/
public static void reset() {
disabled = false;
}

/**
* @see FileOffsetBackingStore#save()
*/
@Override
protected void save() {

synchronized (CustomFileOffsetBackingStore.class) {

if (disabled) {
log.warn("Skipping saving of offset because an error was encountered while processing a change event");
return;
}
log.debug("Saving binlog offset");
super.save();
}
}

/**
* @see FileOffsetBackingStore#start()
*/
@Override
public synchronized void start() {

doStart();

try {
//The offset file structure changed from that generated by previous versions therefore, we need to
//transform any existing offset file to match the new structure otherwise remote sites will lose any
//events that are recorded between pre-upgrade and post-upgrade application runs of the sender.
offsetUtils.transformOffsetIfNecessary(data);
} catch (Exception e) {
throw new RuntimeException("An error occurred while verifying the existing debezium offset file data", e);
}
}

protected void doStart() {
super.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* 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.db.debezium;

import io.debezium.DebeziumException;
import io.debezium.engine.ChangeEvent;
import org.apache.kafka.connect.data.Struct;
import org.apache.kafka.connect.source.SourceRecord;

import java.util.Optional;
import java.util.function.Function;

/**
* Utility function that converts a Debezium {@link ChangeEvent} to a {@link DbEvent}.
*/
public class DbChangeToEvent
implements Function<ChangeEvent<SourceRecord, SourceRecord>, DbEvent> {

private static final String BEFORE_FIELD = "before";
private static final String AFTER_FIELD = "after";
private static final String SOURCE_FIELD = "source";
private static final String OPERATION_FIELD = "op";
private static final String TIMESTAMP_FIELD = "ts_ms";
private static final String SNAPSHOT_FIELD = "snapshot";
private static final String TABLE_FIELD = "table";
private static final String NAME_FIELD = "name";

@Override
public DbEvent apply(ChangeEvent<SourceRecord, SourceRecord> changeEvent) {

SourceRecord record = Optional.ofNullable(changeEvent)
.map(ChangeEvent::value)
.orElseThrow(() -> new DebeziumException("ChangeEvent value is null"));

Struct keyStruct = getStruct(record.key(), "key");
Struct valueStruct = getStruct(record.value(), "value");
Struct sourceStruct = getStruct(valueStruct, SOURCE_FIELD);

validateKeyStruct(keyStruct);

ObjectMap primaryKey = new ObjectMapImpl(keyStruct);
ObjectMap previousState = new ObjectMapImpl(valueStruct.getStruct(BEFORE_FIELD));
ObjectMap newState = new ObjectMapImpl(valueStruct.getStruct(AFTER_FIELD));
ObjectMap source = new ObjectMapImpl(sourceStruct);

DbOperation operation = DbOperation.convertToEnum(getString(valueStruct, OPERATION_FIELD));
ObjectMap values = (operation == DbOperation.DELETE) ? previousState : newState;

Long timestamp = getLong(valueStruct, TIMESTAMP_FIELD);
String tableName = getString(sourceStruct, TABLE_FIELD);
String sourceName = getString(sourceStruct, NAME_FIELD);
DbSnapshot snapshot = DbSnapshot.convertToEnum(getString(sourceStruct, SNAPSHOT_FIELD));

return new DbEvent(primaryKey,
previousState,
newState,
source,
values,
tableName,
sourceName,
operation,
timestamp,
snapshot);
}

/**
* Helper method to retrieve a {@link Struct} and throw an appropriate exception if it is null.
*/
private Struct getStruct(Object object, String structName) {
return Optional.ofNullable((Struct) object)
.orElseThrow(() -> new DebeziumException(structName + " struct is null"));
}

/**
* Helper method to retrieve a String field value from a {@link Struct}.
*/
private String getString(Struct struct, String fieldName) {
return Optional.ofNullable(struct.getString(fieldName))
.orElseThrow(() -> new DebeziumException(fieldName + " field is missing or null"));
}

/**
* Helper method to retrieve a Long field value from a {@link Struct}.
*/
private Long getLong(Struct struct, String fieldName) {
return Optional.ofNullable(struct.getInt64(fieldName))
.orElseThrow(() -> new DebeziumException(fieldName + " field is missing or null"));
}

/**
* Validates the {@link Struct} for the key, ensuring it contains a single primary key.
*/
private void validateKeyStruct(Struct keyStruct) {

int keyFieldsSize = keyStruct.schema().fields().size();

if (keyFieldsSize == 0) {
throw new DebeziumException("Tables with no primary key column are not supported");
}

if (keyFieldsSize > 1) {
throw new DebeziumException("Tables with composite primary keys are not supported");
}
}
}
Loading