MongoDB: java.lang.IllegalStateException: state should be: open

3,823 views
Skip to first unread message

EG

unread,
Nov 22, 2016, 5:38:32 AM11/22/16
to mongodb-user
I have an application that pulls data from one source and then upserts it to MongoDB. Sometimes the pulling process takes a lot of time and when app tries to push a row to MongoDB the following exception is raised probably to the timeout issue:

    com.mongodb.MongoSocketReadException: Prematurely reached end of stream
    at com.mongodb.connection.SocketStream.read(SocketStream.java:88)
    at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:492)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:222)
    at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:105)
    at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:438)
    at com.mongodb.connection.WriteCommandProtocol.receiveMessage(WriteCommandProtocol.java:262)
    at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:104)
    at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64)
    at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37)
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
    at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:143)
    at com.mongodb.operation.MixedBulkWriteOperation$Run$3.executeWriteCommandProtocol(MixedBulkWriteOperation.java:481)
    at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:647)
    at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:400)
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:180)
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:169)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:232)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:223)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:169)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:75)
    at com.mongodb.Mongo.execute(Mongo.java:827)
    at com.mongodb.Mongo$2.execute(Mongo.java:810)
    at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515)
    at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:508)
    at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:355)
    at com.test.db.mongoDB.RetryingMongoCollection.updateOne(RetryingMongoCollection.java:909)

There is a wrapper for MongoCollection class that for each MongoException and now for java.lang.IllegalStateException thrown

    @Override
    public UpdateResult updateOne(Bson filter, Bson update,
    UpdateOptions updateOptions) {
    UpdateResult retVal = null;
    final ExceptionHelper ex = new ExceptionHelper(CAConstants.RETRIES, ErrorType.ERROR, true);
    boolean isDone = false;
    while (!isDone) {
    try {
    retVal = proxied.updateOne(filter, update, updateOptions);
    isDone = true;
    } catch (final MongoException | IllegalStateException e) {
    ex.logIfZero("Failed to launch MongoDB operation", e);
    if (ex.lastTry()) {
    isDone = true;
    Log.error("Failed to launch MongoDB operation.", e);
    } else {
    Log.error("[MDB] Going to update mongo instance due to exception", e);
    db = MongoDBFactory.getUpdatedMongoDBInstance();
    setCollection();
    }
    }
    }
    return retVal;
    }

 tries to reload MongoClient:

    if ( mongoClient != null ) {
    try {
    mongoClient.close();
    } catch (Exception e) {
    Log.error("Mongo client throws exception while closing connection", e);
    }
    ...
    mongoClient = new MongoClient(servers, Arrays.asList(credential), 
    new MongoClientOptions.Builder()
    .requiredReplicaSetName(REPLICASET_NAME)
    .connectTimeout(0)
    .connectionsPerHost(CONNECTION_PER_HOST)
    .cursorFinalizerEnabled(false)
    .build());
   
    db = mongoClient.getDatabase(database);


However, when the mongoClient is updated after "com.mongodb.MongoSocketReadException: Prematurely reached end of stream" it fails with:

    java.lang.IllegalStateException: state should be: open
    at com.mongodb.assertions.Assertions.isTrue(Assertions.java:70)
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:82)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
    at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:169)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:75)
    at com.mongodb.Mongo.execute(Mongo.java:827)
    at com.mongodb.Mongo$2.execute(Mongo.java:810)
    at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515)
    at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:508)
    at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:355)
    at com.test.db.mongoDB.RetryingMongoCollection.updateOne(RetryingMongoCollection.java:909)

and continues failing all the time...

I use mongoDB ver 3.0.6
Mongo java client 3.3.0

Please support.

Ross Lawley

unread,
Nov 22, 2016, 6:35:21 AM11/22/16
to mongod...@googlegroups.com
Hi EG,

Can I ask you to file a ticket in Jira: https://jira.mongodb.org/browse/JAVA  and we'll follow up the discussion there.

Many thanks,

Ross

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9d591c09-7fd7-4177-a3b9-91f16966163b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


{ name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0", "@MongoDB"],
  facebook :"MongoDB"}

Jeff Yemin

unread,
Dec 14, 2016, 12:24:12 PM12/14/16
to mongodb-user
I wasn't able to find a related report in the JAVA project in Jira.  Were you able to find a solution to this?

If not, please post the source code of com.test.db.mongoDB.RetryingMongoCollection and someone can take a look to make sure there are logic errors.  In particular, I'd like to see what the setCollection() method does.  Another possibility is that this class is being used in a multi-threaded environment and it's not multi-thread safe.


Regards,
Jeff

EG

unread,
Dec 15, 2016, 4:00:31 PM12/15/16
to mongodb-user
Hi Jeff & Ross,

I have created a JIRA ticket: https://jira.mongodb.org/browse/JAVA-2411 and provided the requested info.

Furthermore, today I guess I figured out the reason for the initial issue - "Prematurely reached end of stream". I figured out that this issue occured exactly at the same time when MongoDB replica set members interchanged in their roles: PRIMARY & SECONDARY.
While it is not resolves the issue I reported it brings me to another question: whether java driver has to figure out the situation when replica set member it has open a socket to turns to be a SECONDARY instead of PRIMARY and the new socket to the PRIMARY member has to be created?

Thank you a lot,
EG

Regards,
Jeff

To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages