0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-27 15:06:34 +01:00
mongodb/jstests/sharding/shard_insert_getlasterror_w2.js
Moustafa Maher 2fd5f78d5a SERVER-95421 make initiateWithHighElectionTimeout the default in ReplSetTest (#28174)
GitOrigin-RevId: df168ee363c3f0e86526270437d3688ac4bb326d
2024-10-22 02:53:25 +00:00

109 lines
3.8 KiB
JavaScript

// replica set as solo shard
// TODO: Add assertion code that catches hang
import {ReplSetTest} from "jstests/libs/replsettest.js";
import {ShardingTest} from "jstests/libs/shardingtest.js";
// The UUID and index check must be able to contact the shard primaries, but this test manually
// stops 2/3 nodes of a replica set.
TestData.skipCheckingUUIDsConsistentAcrossCluster = true;
TestData.skipCheckingIndexesConsistentAcrossCluster = true;
TestData.skipCheckOrphans = true;
TestData.skipCheckShardFilteringMetadata = true;
// The routing table consistency check runs with 'snapshot' level readConcern. This readConcern
// level cannot be satisfied without a replica set primary, which we won't have because this test
// removes the replica set primary from a shard.
TestData.skipCheckRoutingTableConsistency = true;
var numDocs = 2000;
var baseName = "shard_insert_getlasterror_w2";
var testDBName = baseName;
var testCollName = 'coll';
var replNodes = 3;
// ~1KB string
var textString = '';
for (var i = 0; i < 40; i++) {
textString += 'abcdefghijklmnopqrstuvwxyz';
}
// Spin up a sharded cluster, but do not add the shards
var shardingTestConfig = {
name: baseName,
mongos: 1,
shards: 1,
rs: {nodes: replNodes},
other: {manualAddShard: true},
// By default, our test infrastructure sets the election timeout to a very high value (24
// hours). For this test, we need a shorter election timeout because it relies on nodes running
// an election when they do not detect an active primary. Therefore, we are setting the
// electionTimeoutMillis to its default value.
initiateWithDefaultElectionTimeout: true
};
var shardingTest = new ShardingTest(shardingTestConfig);
// Get connection to the individual shard
var replSet1 = shardingTest.rs0;
// Add data to it
var testDBReplSet1 = replSet1.getPrimary().getDB(testDBName);
var bulk = testDBReplSet1.foo.initializeUnorderedBulkOp();
for (let i = 0; i < numDocs; i++) {
bulk.insert({x: i, text: textString});
}
assert.commandWorked(bulk.execute());
// Get connection to mongos for the cluster
var mongosConn = shardingTest.s;
var testDB = mongosConn.getDB(testDBName);
// Add replSet1 as only shard
if (!TestData.configShard) {
assert.commandWorked(mongosConn.adminCommand({addshard: replSet1.getURL()}));
} else {
assert.commandWorked(mongosConn.adminCommand({transitionFromDedicatedConfigServer: 1}));
}
// Enable sharding on test db and its collection foo
assert.commandWorked(mongosConn.getDB('admin').runCommand({enablesharding: testDBName}));
testDB[testCollName].createIndex({x: 1});
assert.commandWorked(mongosConn.getDB('admin').runCommand(
{shardcollection: testDBName + '.' + testCollName, key: {x: 1}}));
// Test case where GLE should return an error
assert.commandWorked(testDB.foo.insert({_id: 'a', x: 1}));
assert.writeError(testDB.foo.insert({_id: 'a', x: 1}, {writeConcern: {w: 2, wtimeout: 30000}}));
// Add more data
bulk = testDB.foo.initializeUnorderedBulkOp();
for (let i = numDocs; i < 2 * numDocs; i++) {
bulk.insert({x: i, text: textString});
}
assert.commandWorked(bulk.execute({w: replNodes, wtimeout: 30000}));
// Take down two nodes and make sure secondaryOk reads still work
var primary = replSet1.getPrimary();
var [secondary1, secondary2] = replSet1.getSecondaries();
replSet1.stop(secondary1);
replSet1.stop(secondary2);
replSet1.waitForState(primary, ReplSetTest.State.SECONDARY);
testDB.getMongo().adminCommand({setParameter: 1, logLevel: 1});
testDB.getMongo().setSecondaryOk();
print("trying some queries");
assert.soon(function() {
try {
testDB.foo.find().next();
} catch (e) {
print(e);
return false;
}
return true;
}, "Queries took too long to complete correctly.", 2 * 60 * 1000);
// Shutdown cluster
shardingTest.stop();
print('shard_insert_getlasterror_w2.js SUCCESS');