背景:
您好,我正在 Azure 上运行 MongoDB 副本集,并已从 Android 应用程序中远程连接到它。我已经从所有实例中获得了很好的读取效果(更新:因为允许它们在主节点和辅助节点上读取)。但是,写入数据库仍然会出现间歇性错误,并出现以下错误,因为写入必须仅在主节点上完成。
此外,如果您可以提供任何更具体的资源来处理此问题,那么这也会非常有帮助。我已经浏览了大部分文档并针对此错误进行了大量搜索。
Question:
如何防止此错误并允许 100% 的时间写入?
E/AndroidRuntime(): com.mongodb.WriteConcernException: {
"serverUsed" : "/<my-remote-ip>:27017" , "err" : "not master" ,
"code" : 10058 , "n" : 0 , "lastOp" : { "$ts" : 0 , "$inc" : 0} ,
"connectionId" : 1918 , "ok" : 1.0}
堆栈跟踪:
E/AndroidRuntime(13731): FATAL EXCEPTION: Thread-7629
E/AndroidRuntime(13731): Process: com.myapplication.examplemongodb, PID: 13731
E/AndroidRuntime(13731): com.mongodb.WriteConcernException: { "serverUsed" : "/<my-remote-ip>:27017" , "err" : "not master" , "code" : 10058 , "n" : 0 , "lastOp" : { "$ts" : 0 , "$inc" : 0} , "connectionId" : 1918 , "ok" : 1.0}
E/AndroidRuntime(13731): at com.mongodb.CommandResult.getException(CommandResult.java:77)
E/AndroidRuntime(13731): at com.mongodb.CommandResult.throwOnError(CommandResult.java:110)
E/AndroidRuntime(13731): at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:102)
E/AndroidRuntime(13731): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:142)
E/AndroidRuntime(13731): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:115)
E/AndroidRuntime(13731): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:248)
E/AndroidRuntime(13731): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:204)
E/AndroidRuntime(13731): at com.mongodb.DBCollection.insert(DBCollection.java:76)
E/AndroidRuntime(13731): at com.mongodb.DBCollection.insert(DBCollection.java:60)
E/AndroidRuntime(13731): at com.mongodb.DBCollection.insert(DBCollection.java:105)
E/AndroidRuntime(13731): at com.myapplication.examplemongodb.ActivityMain$1.run(ActivityMain.java:83)
E/AndroidRuntime(13731): at java.lang.Thread.run(Thread.java:841)
Notes:
- 我在用mongo-java-驱动程序 v2.11.3.
- I used the mongo-azure library to help create the MongoDB replica set with two worker roles.
- (如果您有更多这方面的资源,那么我很乐意阅读它。我已经阅读了 GitHub 自述文件,this, this以及一些与 MongoDB/Azure 无关的其他内容。但是,这些资源没有更新,也没有详细说明。)
可能的解决方案:
- 我认为与拥有副本集有关。
- 我不确定是否会发生这种情况,因为我只有一个两个实例副本集(一个主副本集和一个辅助副本集),并且它们正在为谁想成为主副本而争论(阅读:投票)。也许,需要仲裁者?但是,我目前不知道该怎么做。
Update:
- Thanks to the help of @David Makogon, I'm pretty sure the issue lies with how I have set up the connection to Azure, and how I access the worker roles. So, here's my updated notes on how the system is configured:
- 两个辅助角色 (MongoDB.WindowsAzure.MongoDBRole),我通过以下方式直接连接到
TCP Input Endpoint
来自 Android 应用程序的端口 27017。正如@David 所说,我目前无法控制连接到哪个实例。
- 一个 Web 角色 (MongoDB.WindowsAzure.Manager),我没有对其执行任何操作,但有一个
HTTP Input Endpoint
在端口 80 上。这只是我上面提到的 mongo-azure 库默认创建的。我不确定我是否应该对此做些什么。