huridocs / uwazi

Uwazi is a web-based, open-source solution for building and sharing document collections
http://www.uwazi.io
MIT License
240 stars 80 forks source link

docker compose for development mongodb starts replica set with wrong primary #5396

Closed daneryl closed 1 year ago

daneryl commented 1 year ago

Even with manually executing rs.initiate its not working.

LaszloKecskes commented 1 year ago

I know of two issues that can come up.

First, docker keeps volumes when you are deleting the containers, and your new mongo container will try to use the old volume which has some configuration from the old replica set, therefore any new instance you make will be a secondary. Maybe there is some way to make it forget the old primary (and I even think it should, since it is not available, so whatever they have for choosing the new primary should kick in), but deleting the mongo volume also works.

Second, mongoose has some problem picking up the replica set host through the docker. If you run rs.conf(), you can see that the host of the containerized mongo contains the network string from the docker network, and mongoose (so, Uwazi) cannot resolve that. If I remember correctly, we have debugged this with you @daneryl and @fnocetti, and maybe @elreplicante. The solution we came up with was to reconfigure the host of the primary according to: https://www.mongodb.com/docs/v4.4/reference/method/rs.reconfig/#mongodb-method-rs.reconfig i.e. in the mongo shell: cfg = rs.conf(); cfg.members[0].host = 'localhost:27017'; rs.reconfig(cfg);

After these steps, I can run Uwazi. I think the problem here is not that we do not have a solution, but that due to the replica set there are too many manual steps now to make this work. We should somehow try to make this easier. As a first idea, for example with a script you could call through yarn, or make this part one of the setup scripts (like yarn migrate, for example).

Also, note that these only affect the docker compose. For local mongo 'the old way', rs.initiate should still be enough, and if there's a problem with that, that is separate from these two issues.

daneryl commented 1 year ago

cfg = rs.conf(); cfg.members[0].host = 'localhost:27017'; rs.reconfig(cfg);

Thank you @LaszloKecskes after some testing this worked for me, based on this info and this answer on stack overflow i have prepared a PR #5419, that will start the replicaset automatically on docker compose up, can you test this and give me your feedback ?

to test this probably you need to remove current docker volumes/images for mongo