我在执行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();
}
}
3条答案
按热度按时间xxb16uws1#
由于没有解决方案,我将发布我的解决方案,请记住,这种情况发生在嵌入式mongo db(flapdoodle版本3.0.0),只有副本集和以下java & spring mongo驱动程序:
我们使用的解决方案是以下忙碌等待:
当没有设置
lastStableCheckpointTimestamp
时间时,我们保持池化,当设置了lastStableCheckpointTimestamp
时,我们允许程序前进(填充需要大约2秒)ojsjcaue2#
在连接字符串中添加读取首选项解决了上述问题
wz8daaqr3#
在更改此之前,检查IP和端口是否对Mongo Db有效可能会有所帮助。