apache/seatunnel · critical · MongodbConnectorException

WRITER_OPERATION_FAILED

WRITER_OPERATION_FAILED

Error message

Bulk Write to MongoDB failed

What it means

The MongoDB sink writer wraps a MongoException thrown during a bulk write into a MongodbConnectorException after exhausting its retry loop. doBulkWrite retries the write up to maxRetries times with linear backoff (retryIntervalMs * attempt); if the underlying Mongo client keeps failing (network, auth, replica unavailability, oversized documents), the last MongoException is attached as the cause.

Solutions

  1. Inspect the attached cause MongoException in the stack trace to find the underlying server/network problem.
  2. Verify MongoDB connectivity and credentials from the SeaTunnel worker nodes (firewall, mongod/replica set health).
  3. Increase sink retry settings (max-retries, retry-interval-ms) if failures are transient.
  4. Reduce batch size so no single bulk write exceeds MongoDB limits (e.g. 16MB max document/message size).

Example fix

// before
url = "mongodb://localhost:27017"
// after (point to reachable replica set with auth)
url = "mongodb://user:pass@rs1.example.com:27017,rs2.example.com:27017/?replicaSet=rs0&authSource=admin"
Defensive patterns

Strategy: retry

Validate before calling

// Before submitting the job, verify connectivity from the worker host:
// mongosh "mongodb://host:27017" --eval "db.runCommand({ping:1})"

Try / catch

try {
    sinkWriter.write(row);
} catch (MongodbConnectorException e) {
    if (e.getErrorCode() == WRITER_OPERATION_FAILED && e.getCause() instanceof MongoException) {
        // inspect MongoException code; decide retry vs fail job
    }
    throw e;
}

Prevention

When it happens

Trigger: write(), prepareCommit(), timerFlush() or close() invokes doBulkWrite, the driver throws MongoException, and the retry limit (maxRetries) is reached on a retry attempt.

Common situations: MongoDB server unreachable or restarting; replica-set elections; bad credentials; network partition between SeaTunnel worker and MongoDB; write rejected by server (document too large, duplicate key with certain configs).

Related errors


AI-assisted analysis of apache/seatunnel@cf67b549a7 (2026-09-10). Data as JSON: /api/errors/a417b94592523a2c. Report an issue: GitHub.

Appendix: source

Thrown at seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/sink/MongodbWriter.java:194

                IntStream.rangeClosed(0, maxRetries)
                        .anyMatch(
                                i -> {
                                    try {
                                        lastSendTime = System.currentTimeMillis();
                                        collectionProvider
                                                .getDefaultCollection()
                                                .bulkWrite(
                                                        bulkRequests,
                                                        new BulkWriteOptions().ordered(true));
                                        bulkRequests.clear();
                                        return true;
                                    } catch (MongoException e) {
                                        log.debug(
                                                "Bulk Write to MongoDB failed, retry times = {}",
                                                i,
                                                e);
                                        if (i >= maxRetries) {
                                            throw new MongodbConnectorException(
                                                    WRITER_OPERATION_FAILED,
                                                    "Bulk Write to MongoDB failed",
                                                    e);
                                        }
                                        try {
                                            TimeUnit.MILLISECONDS.sleep(retryIntervalMs * (i + 1));
                                        } catch (InterruptedException ex) {
                                            Thread.currentThread().interrupt();
                                            throw new MongodbConnectorException(
                                                    WRITER_OPERATION_FAILED,
                                                    "Unable to flush; interrupted while doing another attempt",
                                                    e);
                                        }
                                        return false;
                                    }
                                });

        if (!success) {

View on GitHub (pinned to cf67b549a7)