You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "jw-itq (via GitHub)" <gi...@apache.org> on 2025/06/18 06:12:43 UTC
[PR] Fix the issue where mongo isExactlyOnce defaults to true, causing room to malfunction [seatunnel]
jw-itq opened a new pull request, #9454:
URL: https://github.com/apache/seatunnel/pull/9454
<!--
Thank you for contributing to SeaTunnel! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
## Contribution Checklist
- Make sure that the pull request corresponds to a [GITHUB issue](https://github.com/apache/seatunnel/issues).
- Name the pull request in the form "[Feature] [component] Title of the pull request", where *Feature* can be replaced by `Hotfix`, `Bug`, etc.
- Minor fixes should be named following this pattern: `[hotfix] [docs] Fix typo in README.md doc`.
-->
### Purpose of this pull request
<!-- Describe the purpose of this pull request. For example: This pull request adds checkstyle plugin.-->

When I was synchronizing a 500 million table of MongoDB, the snapshot phase had not yet been completed, and the cluster restarted. The task needed to be restored, which caused a memory room and CPU utilization to climb. After testing, it was confirmed that the default value of the isExactlyOnce method was true, which caused the issue.
@hailin0. thank you for your guidance!
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released SeaTunnel versions or within the unreleased branches such as dev.
If no, write 'No'.
If you are adding/modifying connector documents, please follow our new specifications: https://github.com/apache/seatunnel/issues/4544.
-->
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If you are adding E2E test cases, maybe refer to https://github.com/apache/seatunnel/blob/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-mysql-e2e/src/test/resources/mysqlcdc_to_mysql.conf, here is a good example.
-->
### Check list
* [x] If any new Jar binary package adding in your PR, please add License Notice according
[New License Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/contribution/new-license.md)
* [x] If necessary, please update the documentation to describe the new feature. https://github.com/apache/seatunnel/tree/dev/docs
* [x] If you are contributing the connector code, please check that the following files are updated:
1. Update [plugin-mapping.properties](https://github.com/apache/seatunnel/blob/dev/plugin-mapping.properties) and add new connector information in it
2. Update the pom file of [seatunnel-dist](https://github.com/apache/seatunnel/blob/dev/seatunnel-dist/pom.xml)
3. Add ci label in [label-scope-conf](https://github.com/apache/seatunnel/blob/dev/.github/workflows/labeler/label-scope-conf.yml)
4. Add e2e testcase in [seatunnel-e2e](https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/)
5. Update connector [plugin_config](https://github.com/apache/seatunnel/blob/dev/config/plugin_config)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix the issue where mongo isExactlyOnce defaults to true, causing room to malfunction [seatunnel]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #9454:
URL: https://github.com/apache/seatunnel/pull/9454#discussion_r2153735868
##########
seatunnel-connectors-v2/connector-cdc/connector-cdc-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/cdc/mongodb/utils/MongodbUtils.java:
##########
@@ -404,4 +413,25 @@ public static String encodeValue(String value) {
throw new MongodbConnectorException(ILLEGAL_ARGUMENT, e.getMessage());
}
}
+
+ // Checks if given exception is caused by change stream cursor issues, including
+ // network connection failures, sharded cluster changes, or invalidate events.
+ // See: https://www.mongodb.com/docs/manual/changeStreams/ for more details.
+ public static boolean checkIfChangeStreamCursorExpires(final MongoCommandException e) {
+ return INVALID_CHANGE_STREAM_ERRORS.contains(e.getCode());
+ }
+
+ // This check is stricter than checkIfChangeStreamCursorExpires, which specifically
+ // checks if given exception is caused by an expired resume token.
+ public static boolean checkIfResumeTokenExpires(final MongoCommandException e) {
+ if (e.getCode() != CHANGE_STREAM_FATAL_ERROR) {
+ return false;
+ }
+ String errorMessage = e.getErrorMessage().toLowerCase(Locale.ROOT);
Review Comment:
Guard against a null `e.getErrorMessage()` before calling `toLowerCase(...)` to avoid potential NPEs.
```suggestion
String errorMessage = e.getErrorMessage();
if (errorMessage == null) {
return false;
}
errorMessage = errorMessage.toLowerCase(Locale.ROOT);
```
##########
seatunnel-connectors-v2/connector-cdc/connector-cdc-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/cdc/mongodb/source/fetch/MongodbStreamFetchTask.java:
##########
@@ -117,7 +118,23 @@ public void execute(Context context) {
this.taskRunning = true;
try {
while (taskRunning) {
- Optional<BsonDocument> next = Optional.ofNullable(changeStreamCursor.tryNext());
+ Optional<BsonDocument> next;
+ try {
+ next = Optional.ofNullable(changeStreamCursor.tryNext());
+ } catch (MongoCommandException e) {
+ if (MongodbUtils.checkIfChangeStreamCursorExpires(e)) {
+ log.warn("Change stream cursor has expired, trying to recreate cursor");
+ boolean resumeTokenExpires = MongodbUtils.checkIfResumeTokenExpires(e);
+ if (resumeTokenExpires) {
+ log.warn(
+ "Resume token has expired, fallback to timestamp restart mode");
+ }
+ changeStreamCursor = openChangeStreamCursor(descriptor, resumeTokenExpires);
+ next = Optional.ofNullable(changeStreamCursor.tryNext());
Review Comment:
The second `tryNext()` call after cursor recreation isn’t wrapped in a try/catch block, so any exception it throws will go unhandled; consider enclosing it similarly to the first call.
```suggestion
try {
next = Optional.ofNullable(changeStreamCursor.tryNext());
} catch (MongoCommandException ex) {
log.error("Exception during second tryNext() call after cursor recreation", ex);
throw ex;
}
```
##########
seatunnel-connectors-v2/connector-cdc/connector-cdc-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/cdc/mongodb/config/MongodbSourceOptions.java:
##########
@@ -76,6 +80,25 @@ public class MongodbSourceOptions extends SourceOptions {
public static final int ILLEGAL_OPERATION_ERROR = 20;
+ public static final int INVALIDATED_RESUME_TOKEN_ERROR = 260;
+ public static final int CHANGE_STREAM_FATAL_ERROR = 280;
+ public static final int CHANGE_STREAM_HISTORY_LOST = 286;
+ public static final int BSON_OBJECT_TOO_LARGE = 10334;
+
+ public static final Set<Integer> INVALID_CHANGE_STREAM_ERRORS =
+ new HashSet<>(
+ asList(
+ INVALIDATED_RESUME_TOKEN_ERROR,
+ CHANGE_STREAM_FATAL_ERROR,
+ CHANGE_STREAM_HISTORY_LOST,
+ BSON_OBJECT_TOO_LARGE));
Review Comment:
[nitpick] Consider using `Set.of(...)` to create an immutable set literal, which is more concise and signals immutability.
```suggestion
Set.of(
INVALIDATED_RESUME_TOKEN_ERROR,
CHANGE_STREAM_FATAL_ERROR,
CHANGE_STREAM_HISTORY_LOST,
BSON_OBJECT_TOO_LARGE);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Fix][Mongo-CDC] Fix the issue where mongo isExactlyOnce defaults to true, causing room to malfunction [seatunnel]
Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #9454:
URL: https://github.com/apache/seatunnel/pull/9454#discussion_r2166740131
##########
docs/en/connector-v2/source/MongoDB-CDC.md:
##########
@@ -122,6 +122,7 @@ For specific types in MongoDB, we use Extended JSON format to map them to Seatun
| poll.await.time.ms | Long | No | 1000 | The amount of time to wait before checking for new results on the change stream. |
| heartbeat.interval.ms | String | No | 0 | The length of time in milliseconds between sending heartbeat messages. Use 0 to disable. |
| incremental.snapshot.chunk.size.mb | Long | No | 64 | The chunk size mb of incremental snapshot. |
+| exactly_once | Boolean| No | false | Enable exactly once semantic. |
Review Comment:
Can you tell users the disadvantages of enabling exactly-once?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Fix][Mongo-CDC] Fix the issue where mongo isExactlyOnce defaults to true, causing room to malfunction [seatunnel]
Posted by "jw-itq (via GitHub)" <gi...@apache.org>.
jw-itq commented on PR #9454:
URL: https://github.com/apache/seatunnel/pull/9454#issuecomment-3002225372
> Thanks @jw-itq for update! Please update the docs.
ok!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Fix][Mongo-CDC] Fix the issue where mongo isExactlyOnce defaults to true, causing room to malfunction [seatunnel]
Posted by "jw-itq (via GitHub)" <gi...@apache.org>.
jw-itq commented on PR #9454:
URL: https://github.com/apache/seatunnel/pull/9454#issuecomment-2986413343
> Could you add new option named `exactly_once` to control the behavior? Just like other cdc connector.
ok! thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Fix][Mongo-CDC] Fix the issue where mongo isExactlyOnce defaults to true, causing room to malfunction [seatunnel]
Posted by "jw-itq (via GitHub)" <gi...@apache.org>.
jw-itq commented on code in PR #9454:
URL: https://github.com/apache/seatunnel/pull/9454#discussion_r2166745637
##########
docs/en/connector-v2/source/MongoDB-CDC.md:
##########
@@ -122,6 +122,7 @@ For specific types in MongoDB, we use Extended JSON format to map them to Seatun
| poll.await.time.ms | Long | No | 1000 | The amount of time to wait before checking for new results on the change stream. |
| heartbeat.interval.ms | String | No | 0 | The length of time in milliseconds between sending heartbeat messages. Use 0 to disable. |
| incremental.snapshot.chunk.size.mb | Long | No | 64 | The chunk size mb of incremental snapshot. |
+| exactly_once | Boolean| No | false | Enable exactly once semantic. |
Review Comment:
> Can you tell users the disadvantages of enabling exactly-once?
ok!I thought so, too.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org