|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Airbyte |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package io.airbyte.integrations.debezium; |
| 26 | + |
| 27 | +import com.fasterxml.jackson.databind.JsonNode; |
| 28 | +import io.airbyte.commons.util.AutoCloseableIterator; |
| 29 | +import io.airbyte.commons.util.AutoCloseableIterators; |
| 30 | +import io.airbyte.commons.util.CompositeIterator; |
| 31 | +import io.airbyte.commons.util.MoreIterators; |
| 32 | +import io.airbyte.integrations.debezium.internals.AirbyteFileOffsetBackingStore; |
| 33 | +import io.airbyte.integrations.debezium.internals.AirbyteSchemaHistoryStorage; |
| 34 | +import io.airbyte.integrations.debezium.internals.DebeziumEventUtils; |
| 35 | +import io.airbyte.integrations.debezium.internals.DebeziumRecordIterator; |
| 36 | +import io.airbyte.integrations.debezium.internals.DebeziumRecordPublisher; |
| 37 | +import io.airbyte.integrations.debezium.internals.FilteredFileDatabaseHistory; |
| 38 | +import io.airbyte.protocol.models.AirbyteMessage; |
| 39 | +import io.airbyte.protocol.models.ConfiguredAirbyteCatalog; |
| 40 | +import io.debezium.engine.ChangeEvent; |
| 41 | +import java.time.Instant; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.Iterator; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.Optional; |
| 47 | +import java.util.Properties; |
| 48 | +import java.util.concurrent.LinkedBlockingQueue; |
| 49 | +import java.util.function.Supplier; |
| 50 | +import org.slf4j.Logger; |
| 51 | +import org.slf4j.LoggerFactory; |
| 52 | + |
| 53 | +/** |
| 54 | + * This class acts as the bridge between Airbyte DB connectors and debezium. If a DB connector wants |
| 55 | + * to use debezium for CDC, it should use this class |
| 56 | + */ |
| 57 | +public class AirbyteDebeziumHandler { |
| 58 | + |
| 59 | + private static final Logger LOGGER = LoggerFactory.getLogger(AirbyteDebeziumHandler.class); |
| 60 | + /** |
| 61 | + * We use 10000 as capacity cause the default queue size and batch size of debezium is : |
| 62 | + * {@link io.debezium.config.CommonConnectorConfig#DEFAULT_MAX_BATCH_SIZE}is 2048 |
| 63 | + * {@link io.debezium.config.CommonConnectorConfig#DEFAULT_MAX_QUEUE_SIZE} is 8192 |
| 64 | + */ |
| 65 | + private static final int QUEUE_CAPACITY = 10000; |
| 66 | + |
| 67 | + private final Properties connectorProperties; |
| 68 | + private final JsonNode config; |
| 69 | + private final CdcTargetPosition targetPosition; |
| 70 | + private final ConfiguredAirbyteCatalog catalog; |
| 71 | + private final boolean trackSchemaHistory; |
| 72 | + |
| 73 | + private final LinkedBlockingQueue<ChangeEvent<String, String>> queue; |
| 74 | + |
| 75 | + public AirbyteDebeziumHandler(JsonNode config, |
| 76 | + CdcTargetPosition targetPosition, |
| 77 | + Properties connectorProperties, |
| 78 | + ConfiguredAirbyteCatalog catalog, |
| 79 | + boolean trackSchemaHistory) { |
| 80 | + this.config = config; |
| 81 | + this.targetPosition = targetPosition; |
| 82 | + this.connectorProperties = connectorProperties; |
| 83 | + this.catalog = catalog; |
| 84 | + this.trackSchemaHistory = trackSchemaHistory; |
| 85 | + this.queue = new LinkedBlockingQueue<>(QUEUE_CAPACITY); |
| 86 | + } |
| 87 | + |
| 88 | + public List<AutoCloseableIterator<AirbyteMessage>> getIncrementalIterators(CdcSavedInfoFetcher cdcSavedInfoFetcher, |
| 89 | + CdcStateHandler cdcStateHandler, |
| 90 | + CdcMetadataInjector cdcMetadataInjector, |
| 91 | + Instant emittedAt) { |
| 92 | + LOGGER.info("using CDC: {}", true); |
| 93 | + final AirbyteFileOffsetBackingStore offsetManager = AirbyteFileOffsetBackingStore.initializeState(cdcSavedInfoFetcher.getSavedOffset()); |
| 94 | + final Optional<AirbyteSchemaHistoryStorage> schemaHistoryManager = schemaHistoryManager(cdcSavedInfoFetcher); |
| 95 | + final DebeziumRecordPublisher publisher = new DebeziumRecordPublisher(connectorProperties, config, catalog, offsetManager, |
| 96 | + schemaHistoryManager); |
| 97 | + publisher.start(queue); |
| 98 | + |
| 99 | + // handle state machine around pub/sub logic. |
| 100 | + final AutoCloseableIterator<ChangeEvent<String, String>> eventIterator = new DebeziumRecordIterator( |
| 101 | + queue, |
| 102 | + targetPosition, |
| 103 | + publisher::hasClosed, |
| 104 | + publisher::close); |
| 105 | + |
| 106 | + // convert to airbyte message. |
| 107 | + final AutoCloseableIterator<AirbyteMessage> messageIterator = AutoCloseableIterators |
| 108 | + .transform( |
| 109 | + eventIterator, |
| 110 | + (event) -> DebeziumEventUtils.toAirbyteMessage(event, cdcMetadataInjector, emittedAt)); |
| 111 | + |
| 112 | + // our goal is to get the state at the time this supplier is called (i.e. after all message records |
| 113 | + // have been produced) |
| 114 | + final Supplier<AirbyteMessage> stateMessageSupplier = () -> { |
| 115 | + Map<String, String> offset = offsetManager.read(); |
| 116 | + String dbHistory = trackSchemaHistory ? schemaHistoryManager |
| 117 | + .orElseThrow(() -> new RuntimeException("Schema History Tracking is true but manager is not initialised")).read() : null; |
| 118 | + |
| 119 | + return cdcStateHandler.saveState(offset, dbHistory); |
| 120 | + }; |
| 121 | + |
| 122 | + // wrap the supplier in an iterator so that we can concat it to the message iterator. |
| 123 | + final Iterator<AirbyteMessage> stateMessageIterator = MoreIterators.singletonIteratorFromSupplier(stateMessageSupplier); |
| 124 | + |
| 125 | + // this structure guarantees that the debezium engine will be closed, before we attempt to emit the |
| 126 | + // state file. we want this so that we have a guarantee that the debezium offset file (which we use |
| 127 | + // to produce the state file) is up-to-date. |
| 128 | + final CompositeIterator<AirbyteMessage> messageIteratorWithStateDecorator = |
| 129 | + AutoCloseableIterators.concatWithEagerClose(messageIterator, AutoCloseableIterators.fromIterator(stateMessageIterator)); |
| 130 | + |
| 131 | + return Collections.singletonList(messageIteratorWithStateDecorator); |
| 132 | + } |
| 133 | + |
| 134 | + private Optional<AirbyteSchemaHistoryStorage> schemaHistoryManager(CdcSavedInfoFetcher cdcSavedInfoFetcher) { |
| 135 | + if (trackSchemaHistory) { |
| 136 | + FilteredFileDatabaseHistory.setDatabaseName(config.get("database").asText()); |
| 137 | + return Optional.of(AirbyteSchemaHistoryStorage.initializeDBHistory(cdcSavedInfoFetcher.getSavedSchemaHistory())); |
| 138 | + } |
| 139 | + |
| 140 | + return Optional.empty(); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments