-
-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running as a single node replica set #74
Comments
It's a good feature request. 👍 You need to write a new module
Sorry, I have no bandwidth for this feature realisation. |
But for the fast start, you need to improve |
👍 great request. |
I have created a pull request that makes it possible to run the How I use this in my project:
Please let me know your thoughts on this implementation. |
Related pull request by @jloveridge #79 It's really cool implementation 👍 |
For anyone watching this issue it is now possible, since 2.4.0, to spin up a replica set by using the new |
Thanks everyone! This is really awesome. I just gave it a try and I've run into a small issue. Sometimes when starting the script I get I was using the sample code from the docs for initialization: import { MongoMemoryReplSet } from 'mongodb-memory-server';
const replSet = new MongoMemoryReplSet();
await replSet.waitUntilRunning();
const uri = await mongod.getConnectionString();
const port = await mongod.getPort();
const dbPath = await mongod.getDbPath();
const dbName = await mongod.getDbName();
// connected and launched some queries here I tried adding a small delay before launching queries and it started working correctly, so it looks like replica isn't fully initialized when |
@sebastian-nowak Yes, I have noticed that as well in my own testing. I also had to introduce an additional 2s, 1s wasn't enough, delay before the tests would reliably pass. I plan to make some changes to the I am not sure if you read through the implementation at all but the promise returned by I will not be able to make the changes until later this week as I am out of town for a few days. If someone else wants to tackle the issue feel free. Some of the ways I have considered resolving the issue are as follows:
I am not 100% sure that I particularly like any of these options but all seem like viable ways of preventing the issue. |
@jloveridge, you got any updates on this? |
@flpStrri will be amazing! Just need to improve this method with additional awaits while all RS members became PRIMARY/SECONDARY states: Better to do it via reading its log files. |
@nodkz I tried to do it but it was taking too much time to understand the code, I won't be able to do it for now. |
@flpStrri no problem. Let keep this issue opened. |
I will try to get around to fixing these outstanding issues in the near future. For my own tests I just added an additional timeout to wait for the replica set to actually be ready since it wasn't ready on the initial transition. Sorry for the long delay in getting an actual fix out. |
Hello guys, I've been trying to use a replica set because I'm planning to use change streams in my application. I'm trying to spin up a replica in my test suite but it hangs for a very long time saying Here's my test: // @flow
import { MongoClient } from "mongodb"
import { MongoMemoryReplSet } from "mongodb-memory-server"
jest.setTimeout(30000) // 30 seconds
describe("Change streams", () => {
test("test", async done => {
const dbName = "mytest"
const replicaSetName = "testset"
// $FlowFixMe
const replSet = new MongoMemoryReplSet({
autoStart: true,
binary: {
version: "4.0.3",
},
instanceOpts: [
{ storageEngine: "wiredTiger" },
{ storageEngine: "wiredTiger" },
],
replSet: {
dbName,
name: replicaSetName,
},
debug: true,
})
await replSet.waitUntilRunning()
const uri = await replSet.getConnectionString()
console.log("URI", uri) // prints mongodb://127.0.0.1:34587,127.0.0.1:43473/mytest
const client = await MongoClient.connect(
uri + `?replicaSet=${replicaSetName}`,
{ useNewUrlParser: true },
)
const db = client.db(dbName)
const product1 = {
id: "p01",
name: "Product 01",
}
const product2 = {
id: "p01",
name: "Product 01",
}
const collection = db.collection("products")
await collection.insert(product1)
await collection.insert(product2)
const results = await (await collection.find()).toArray()
console.log(JSON.stringify(results, null, 4))
done()
})
}) After a while I see: console.log node_modules/mongodb-memory-server/lib/MongoMemoryReplSet.js:58
No PRIMARY yet. Waiting...
Mongo[34587] 2019-03-13T11:44:53.056+0100 I REPL [replexec-0] Starting an election, since we've seen no PRIMARY in the past 10000ms Then after some more time: Mongo[43473] 2019-03-13T11:44:53.614+0100 I REPL [replexec-2] Member 127.0.0.1:34587 is now in state PRIMARY
Mongo[43473] +513ms So it seems like one member manages to go PRIMARY but then the script ends with this
What am I missing? 🤔 Also, is it possible to make one node PRIMARY right away without having to wait those 10 seconds? The test timeout is 30s but it fails with that |
OK I think I got it working by adding a const uri = await replSet.getConnectionString()
console.log("URI", uri)
await sleep(2000)
const client = await MongoClient.connect(
uri + `?replicaSet=${replicaSetName}`,
{ useNewUrlParser: true },
) Is there anything you can think of that I could use to speed up the bootstrap time? 13s + 2s of sleep is a very long time for a test. |
I managed to get it working in under 6 seconds but I had to make some changes, here's the Pull Request: #155 @nodkz @jloveridge |
The above PR doesn't solve the original issue. No matter which value is used with the new configuration option, the tests will still result in a |
#203 is my attempt to make this faster&reliable. Getting times 2100ms for single node and 4100ms for 3nodes. Hopefully I did not miss something. Please check it out. Thanks! |
In 5.2.11 was fixed "Maximum call stack size exceeded" 470f094 |
closing thanks to the support of MongoDB-ReplSet's and |
Hello,
Is it possible to run
mongodb-memory-server
as a standalone replica set, so that it could be used to test transactions?I think it should be easy to add: https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/
Regards,
Sebastian
The text was updated successfully, but these errors were encountered: