Skip to content
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

does not support mongo replica sets #6

Open
jkndrkn opened this issue Apr 14, 2011 · 6 comments
Open

does not support mongo replica sets #6

jkndrkn opened this issue Apr 14, 2011 · 6 comments

Comments

@jkndrkn
Copy link

jkndrkn commented Apr 14, 2011

mongo-java-driver 2.1 does not support mongo replica sets.

Upgrade to a newer version of mongo-java-driver is necessary.

@jkndrkn
Copy link
Author

jkndrkn commented Apr 15, 2011

This might work:

(defn connect
  "Returns a single server connection. Defaults to host 127.0.0.1:27017
   Supports replica sets if passed a vector of host-port vectors."
  ([] (connect "127.0.0.1"))
  ([host]
     (cond
       (string? host) (connect host 27017)
       (vector? host) (Mongo. (map #(ServerAddress. (first %) (second %)) host))))
  ([#^String host port]
     (Mongo. host (int port)))

The below assumes that mongod is running on localhost and that there are three different mongod servers running as a replica set at mongo1.foo.com, mongo2.foo.com, and mongo3.foo.com.

karras.core> (connect)
#<Mongo com.mongodb.Mongo@404c7788>
karras.core> (connect "mongo1.foo.com" 27017)
#<Mongo com.mongodb.Mongo@3d5bf0c5>
karras.core> (connect [["mongo1.foo.com" 27017] ["mongo2.foo" 27017] ["mongo3.foo.com" 27017]])
#<Mongo com.mongodb.Mongo@75896415>

@jkndrkn
Copy link
Author

jkndrkn commented Apr 15, 2011

ServerAddress is in the com.mongodb namespace.

@jkndrkn
Copy link
Author

jkndrkn commented Apr 15, 2011

You'll want to use a newer version of mongo-java-driver. Version 2.3 works and doesn't break any of your tests.

@wilkes
Copy link
Owner

wilkes commented Apr 19, 2011

I'll get this in this week. Thanks!

@jkndrkn
Copy link
Author

jkndrkn commented Apr 19, 2011

Welcome. Thanks for your great work! ^_^

@jkndrkn
Copy link
Author

jkndrkn commented Apr 21, 2011

This bit:

  ([host]
     (cond
       (string? host) (connect host 27017)
       (vector? host) (Mongo. (map #(ServerAddress. (first %) (second %)) host))))

Should be the following instead:

  ([host]
     (cond
       (string? host) (connect host 27017)
       (coll? host) (Mongo. (map #(ServerAddress. (first %) (second %)) host))))

That particular constructor for the Mongo class can accept anything that implements the List interface, so it doesn't make sense to require only a vector (we have yaml-reading code that returns a list when it parses a yaml array).

Also, we've run tests, and it looks like mongo-java-driver 2.3 has pretty solid replica set support that tolerates the failure of a primary node well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants