-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare injection of Versioning compatible StreamReaderFactory (#17487)
* Use Version instead of AirbyteVersion * Update Deserializer interface to fit better in existing flow * Add versioned migrator * Refactor DefaultAirbyteStreamFactory to enable Versioning * Use explicit constructor * Add logging on failed message upgrade
- Loading branch information
Showing
11 changed files
with
170 additions
and
64 deletions.
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
35 changes: 35 additions & 0 deletions
35
...s-protocol/src/main/java/io/airbyte/commons/protocol/AirbyteMessageVersionedMigrator.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,35 @@ | ||
/* | ||
* Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.commons.protocol; | ||
|
||
import io.airbyte.commons.version.Version; | ||
import io.airbyte.protocol.models.v0.AirbyteMessage; | ||
|
||
/** | ||
* Wraps message migration from a fixed version to the most recent version | ||
*/ | ||
public class AirbyteMessageVersionedMigrator<OriginalMessageType> { | ||
|
||
private final AirbyteMessageMigrator migrator; | ||
private final Version version; | ||
|
||
public AirbyteMessageVersionedMigrator(final AirbyteMessageMigrator migrator, final Version version) { | ||
this.migrator = migrator; | ||
this.version = version; | ||
} | ||
|
||
public OriginalMessageType downgrade(final AirbyteMessage message) { | ||
return migrator.downgrade(message, version); | ||
} | ||
|
||
public AirbyteMessage upgrade(final OriginalMessageType message) { | ||
return migrator.upgrade(message, version); | ||
} | ||
|
||
public Version getVersion() { | ||
return version; | ||
} | ||
|
||
} |
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
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
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
61 changes: 61 additions & 0 deletions
61
airbyte-workers/src/main/java/io/airbyte/workers/internal/VersionedAirbyteStreamFactory.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,61 @@ | ||
/* | ||
* Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.workers.internal; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.airbyte.commons.json.Jsons; | ||
import io.airbyte.commons.logging.MdcScope; | ||
import io.airbyte.commons.protocol.AirbyteMessageVersionedMigrator; | ||
import io.airbyte.commons.protocol.serde.AirbyteMessageDeserializer; | ||
import io.airbyte.protocol.models.AirbyteMessage; | ||
import java.util.stream.Stream; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Extends DefaultAirbyteStreamFactory to handle version specific conversions. | ||
* | ||
* A VersionedAirbyteStreamFactory handles parsing and validation from a specific version of the | ||
* Airbyte Protocol as well as upgrading messages to the current version. | ||
*/ | ||
public class VersionedAirbyteStreamFactory<T> extends DefaultAirbyteStreamFactory { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(VersionedAirbyteStreamFactory.class); | ||
|
||
private final AirbyteMessageDeserializer<T> deserializer; | ||
private final AirbyteMessageVersionedMigrator<T> migrator; | ||
|
||
public VersionedAirbyteStreamFactory(final AirbyteMessageDeserializer<T> deserializer, | ||
final AirbyteMessageVersionedMigrator<T> migrator) { | ||
this(deserializer, migrator, MdcScope.DEFAULT_BUILDER); | ||
} | ||
|
||
public VersionedAirbyteStreamFactory(final AirbyteMessageDeserializer<T> deserializer, | ||
final AirbyteMessageVersionedMigrator<T> migrator, | ||
final MdcScope.Builder containerLogMdcBuilder) { | ||
// TODO AirbyteProtocolPredicate needs to be updated to be protocol version aware | ||
super(new AirbyteProtocolPredicate(), LOGGER, containerLogMdcBuilder); | ||
this.deserializer = deserializer; | ||
this.migrator = migrator; | ||
} | ||
|
||
@Override | ||
protected Stream<AirbyteMessage> toAirbyteMessage(final JsonNode json) { | ||
try { | ||
final io.airbyte.protocol.models.v0.AirbyteMessage message = migrator.upgrade(deserializer.deserialize(json)); | ||
return Stream.of(convert(message)); | ||
} catch (RuntimeException e) { | ||
LOGGER.warn("Failed to upgrade a message from version {}: {}", migrator.getVersion(), Jsons.serialize(json)); | ||
return Stream.empty(); | ||
} | ||
} | ||
|
||
// TODO remove this conversion once we migrated default AirbyteMessage to be from a versioned | ||
// namespace | ||
private AirbyteMessage convert(final io.airbyte.protocol.models.v0.AirbyteMessage message) { | ||
return Jsons.object(Jsons.jsonNode(message), AirbyteMessage.class); | ||
} | ||
|
||
} |