未分类MongoDbException:命令失败,错误10107(NotMaster):服务器上'not master'



在执行MongoDB事务功能的集成测试用例时,我得到以下异常。

org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 10107 (NotMaster): 'not master' on server localhost:60876. The full response is {"operationTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "ok": 0.0, "errmsg": "not master", "code": 10107, "codeName": "NotMaster", "$gleStats": {"lastOpTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "electionId": {"$oid": "7fffffff0000000000000001"}}, "lastCommittedOpTime": {"$timestamp": {"t": 0, "i": 0}}, "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1615104939, "i": 2}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}; nested exception is com.mongodb.MongoNotPrimaryException: Command failed with error 10107 (NotMaster): 'not master' on server localhost:60876. The full response is {"operationTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "ok": 0.0, "errmsg": "not master", "code": 10107, "codeName": "NotMaster", "$gleStats": {"lastOpTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "electionId": {"$oid": "7fffffff0000000000000001"}}, "lastCommittedOpTime": {"$timestamp": {"t": 0, "i": 0}}, "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1615104939, "i": 2}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:133)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2874)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:568)
at org.springframework.data.mongodb.core.MongoTemplate.saveDocument(MongoTemplate.java:1485)
at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1421)
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1363)
....................
Caused by: com.mongodb.MongoNotPrimaryException: Command failed with error 10107 (NotMaster): 'not master' on server localhost:60876. The full response is {"operationTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "ok": 0.0, "errmsg": "not master", "code": 10107, "codeName": "NotMaster", "$gleStats": {"lastOpTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "electionId": {"$oid": "7fffffff0000000000000001"}}, "lastCommittedOpTime": {"$timestamp": {"t": 0, "i": 0}}, "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1615104939, "i": 2}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}
at com.mongodb.internal.connection.ProtocolHelper.createSpecialException(ProtocolHelper.java:244)
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:171)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:302)

我的测试类有以下方法:

@Test
@Order(1)
void testUpdateValidationsById() {
MyEntity entity = new MyEntity();
entity.setId("6030a6e1b32d1f70b4ce8039");
entity.setAttributeType("IP Address, FQDN");
entity.setCategory("NTP");
entity.setCategoryLabel("NTP");
entity.setTrueMsgDetails("It is true");
entity.setFalseMsgDetails("It is false");
entity.setCorevalidation("Core Validation");
ResponseEntity<?> re = controller.updateValidationsById(entity);
boolean flag = re.getStatusCode().is2xxSuccessful();
assertEquals(true, flag);
}

我的测试配置类是这样的。

@Profile("test")
@ActiveProfiles("test")
@TestConfiguration
public class TestMongoDBConfig implements InitializingBean, DisposableBean {
private MongodExecutable executable;
private String replicaSetName = "rs0";
private int mongoDBPort = 27021;
@Override
public void afterPropertiesSet() throws Exception {
mongoDBPort = Network.getFreeServerPort();
IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder().useNoPrealloc(false).useSmallFiles(false)
.master(false).verbose(false).useNoJournal(false).syncDelay(0).build();
//      IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.V4_0)
//              .net(new Net(27023, Network.localhostIsIPv6()))
.net(new Net(mongoDBPort, Network.localhostIsIPv6()))
.replication(new Storage(null, replicaSetName, 5000)).configServer(true).cmdOptions(cmdOptions)
.build();
MongodStarter starter = MongodStarter.getDefaultInstance();
executable = starter.prepare(mongodConfig);
executable.start();
}

@Bean(name = "test1")
public MongoClient mongoClient() {
ConnectionString cs = new ConnectionString("mongodb://localhost:" + mongoDBPort + "/");
MongoClient mongoClient = MongoClients.create(cs);
System.out.println("--------------------------------------");
System.out.println("Mongo Selected ort : " + mongoDBPort);
System.out.println("MongoClient : " + mongoClient);
System.out.println("--------------------------------------");
mongoClient.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
return mongoClient;
}
/**
* Destroy.
*
* @throws Exception the exception
*/
@Override
public void destroy() throws Exception {
executable.stop();
}
}

由于没有解决方案,我将发布我的解决方案,请记住这种情况发生在对于嵌入式mongo db(flapdoodle版本3.0.0),副本集只有下面是Java &Spring mongo drivers:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.11.3</version>
</dependency>

我们使用的解决方案是如下忙等待:

private void waitUntilReplicaReady() throws InterruptedException {
Document document = getRaplicaStatus();
while (isReplicaInitializing(document)) {
document = getRaplicaStatus();
TimeUnit.MILLISECONDS.sleep(100);
}
}
private boolean isReplicaInitializing(Document document) {
return document.get("lastStableCheckpointTimestamp", BsonTimestamp.class).getTime() == 0;
}
private Document getRaplicaStatus() {
return getDatabase("admin").runCommand(new BasicDBObject("replSetGetStatus", "1"));
}

lastStableCheckpointTimestamp时间没有设置时,我们继续池化,当lastStableCheckpointTimestamp设置时,我们允许程序前进(它需要2秒来填充)

在连接字符串中添加读优先级解决了上述问题

mongodb://userName:Password@mongodb0.example.com:27017?ReadPreference=primary

在更改此设置之前,检查mongodb的ip和端口是否有效。

相关内容

  • 没有找到相关文章

最新更新