-
Notifications
You must be signed in to change notification settings - Fork 4.5k
MySQL CDC sync fails because starting binlog position not found in DB #6425 #9514
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
Merged
mkhokh-33
merged 12 commits into
master
from
mkhokh/6425-mysql-source-cdc-binlog-not-found
Feb 10, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
870fe6c
Check binlog position on mysql server before run sync job, add error …
mkhokh-33 086b446
fix MySqlStrictEncryptSourceAcceptanceTest
mkhokh-33 2b24999
fix formatting
mkhokh-33 3022590
Merge branch 'master' into mkhokh/6425-mysql-source-cdc-binlog-not-found
mkhokh-33 c40c718
Merge branch 'master' into mkhokh/6425-mysql-source-cdc-binlog-not-found
mkhokh-33 a32eb6a
fix review comments
mkhokh-33 d006a81
Merge branch 'master' into mkhokh/6425-mysql-source-cdc-binlog-not-found
mkhokh-33 6e98620
added java docs and fixed few minor comments
mkhokh-33 abc261a
fix formatting
mkhokh-33 e5e69c4
Merge branch 'master' into mkhokh/6425-mysql-source-cdc-binlog-not-found
mkhokh-33 a92f70a
update versions
mkhokh-33 2f1b665
update source_specs.yaml
mkhokh-33 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
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
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
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
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
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
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
120 changes: 120 additions & 0 deletions
120
...ql/src/main/java/io/airbyte/integrations/source/mysql/helpers/CdcConfigurationHelper.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,120 @@ | ||
/* | ||
* Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.integrations.source.mysql.helpers; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.airbyte.commons.functional.CheckedConsumer; | ||
import io.airbyte.commons.json.Jsons; | ||
import io.airbyte.db.jdbc.JdbcDatabase; | ||
import java.sql.SQLException; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Helper class for MySqlSource used to check cdc configuration in case of: | ||
* <p> | ||
* 1. adding new source and checking operations #getCheckOperations method. | ||
* </p> | ||
* <p> | ||
* 2. checking whether binlog required from saved cdc offset is available on mysql server | ||
* #checkBinlog method | ||
* </p> | ||
*/ | ||
public class CdcConfigurationHelper { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CdcConfigurationHelper.class); | ||
private static final String CDC_OFFSET = "mysql_cdc_offset"; | ||
private static final String LOG_BIN = "log_bin"; | ||
private static final String BINLOG_FORMAT = "binlog_format"; | ||
private static final String BINLOG_ROW_IMAGE = "binlog_row_image"; | ||
|
||
/** | ||
* Method will check whether required binlog is available on mysql server | ||
* | ||
* @param offset - saved cdc offset with required binlog file | ||
* @param database - database | ||
*/ | ||
public static void checkBinlog(JsonNode offset, JdbcDatabase database) { | ||
Optional<String> binlogOptional = getBinlog(offset); | ||
binlogOptional.ifPresent(binlog -> { | ||
if (isBinlogAvailable(binlog, database)) { | ||
LOGGER.info(""" | ||
Binlog %s is available""".formatted(binlog)); | ||
} else { | ||
String error = | ||
""" | ||
Binlog %s is not available. This is a critical error, it means that requested binlog is not present on mysql server. To fix data synchronization you need to reset your data. Please check binlog retention policy configurations.""" | ||
.formatted(binlog); | ||
LOGGER.error(error); | ||
throw new RuntimeException(""" | ||
Binlog %s is not available.""".formatted(binlog)); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Method will get required configurations for cdc sync | ||
* | ||
* @return list of List<CheckedConsumer<JdbcDatabase, Exception>> | ||
*/ | ||
public static List<CheckedConsumer<JdbcDatabase, Exception>> getCheckOperations() { | ||
return List.of(getCheckOperation(LOG_BIN, "ON"), | ||
getCheckOperation(BINLOG_FORMAT, "ROW"), | ||
getCheckOperation(BINLOG_ROW_IMAGE, "FULL")); | ||
|
||
} | ||
|
||
private static boolean isBinlogAvailable(String binlog, JdbcDatabase database) { | ||
try { | ||
List<String> binlogs = database.resultSetQuery(connection -> connection.createStatement().executeQuery("SHOW BINARY LOGS"), | ||
resultSet -> resultSet.getString("Log_name")).collect(Collectors.toList()); | ||
|
||
return !binlog.isEmpty() && binlogs.stream().anyMatch(e -> e.equals(binlog)); | ||
} catch (SQLException e) { | ||
LOGGER.error("Can not get binlog list. Error: ", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static Optional<String> getBinlog(JsonNode offset) { | ||
JsonNode node = offset.get(CDC_OFFSET); | ||
Iterator<Map.Entry<String, JsonNode>> fields = node.fields(); | ||
while (fields.hasNext()) { | ||
Map.Entry<String, JsonNode> jsonField = fields.next(); | ||
return Optional.ofNullable(Jsons.deserialize(jsonField.getValue().asText()).path("file").asText()); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private static CheckedConsumer<JdbcDatabase, Exception> getCheckOperation(String name, String value) { | ||
return database -> { | ||
final List<String> result = database.resultSetQuery(connection -> { | ||
final String sql = """ | ||
show variables where Variable_name = '%s'""".formatted(name); | ||
|
||
return connection.createStatement().executeQuery(sql); | ||
}, resultSet -> resultSet.getString("Value")).collect(toList()); | ||
|
||
if (result.size() != 1) { | ||
throw new RuntimeException(""" | ||
Could not query the variable %s""".formatted(name)); | ||
} | ||
|
||
final String resultValue = result.get(0); | ||
if (!resultValue.equalsIgnoreCase(value)) { | ||
throw new RuntimeException(""" | ||
The variable %s should be set to %s, but it is : %s""".formatted(name, value, resultValue)); | ||
} | ||
}; | ||
} | ||
|
||
} |
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
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
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.
Should this part of the code executed too if
binlogOptional.ifPresent
is not true?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.
binlogOptional.ifPresent check that saved cdc_offset contain information about last sync binlog and returns binlog file name
if (isBinlogAvailable(binlog, database)) - will check that required for sync binlog file is present on mysql-server
if binlog is not on mysql server anymore only then this part of code will be executed.