Does the Java client driver support replicated pair failover?

31 views
Skip to first unread message

bilbo0s

unread,
Mar 16, 2009, 9:40:51 PM3/16/09
to mongodb-user
Hello again,

I've been working away integrating Mongo and Lift, and
while doing failover testing the java client driver for some
reason will not go to the new master.

It was mentioned to me previously that the Java driver
does support failover. So I am a little confused as to
why it is not working for me. I have gotten to the point
in testing where that is a real buzz kill. The Mongo side
performs great, and should be easily scaled. I finished
that testing today. In testing the Lift side, I find that
this failover issue is just killing me. Mostly because
I have to setup the test again, and again, and again.

OK. I give up. What is the trick to get it to work? Or
do I need to write some logic into my connection code?

What follows is the Scala I am using to connect to the
Mongo:

package pixipoli.helloworld.snippet

import scala.collection.mutable.HashMap

import scala.xml.NodeSeq
import net.liftweb.http.S._
import net.liftweb.http.SHtml._
import net.liftweb.http.RequestVar
import net.liftweb.http.SessionVar
import net.liftweb.util.Helpers._
import net.liftweb.util.Full
import org.mongodb.driver.ts.Mongo
//import java.util.HashMap;

class HelloForm3 {
object who extends RequestVar(Full("world"))
//*** I CREATE THE MONGO HERE ***
object mongo extends SessionVar[Mongo](new Mongo("localhost",
20000))

def show(xhtml: NodeSeq): NodeSeq = {

//*** I HAVE A TEST METHOD CALLED 'test' THAT
// WILL TAKE A Mongo INSTANCE AND ADD A
// SIMPLE DOCUMENT TO IT ***
//*** I CALL IT FROM THE 'bind' CALL BELOW ***
def test(tmongo: Mongo): Unit = {
//*** I GET THE DB HERE ***
val db = tmongo.getDB("scala");

//*** I GET THE COLLECTION HERE ***
val coll = db.getCollection("test");
coll.clear();

val doc = new java.util.HashMap[java.lang.String, java.lang.String]
();
val t = who.openOr("")
doc.put("name", t)
coll.insert(doc);
println("FUNCTION value:" + who.openOr("") + " :: " + param
("whoField"))
}

bind("hello", xhtml,
"whoField" -> text(who.openOr(""), v => {who(Full(v))}) %
("size" -> "10") % ("id" -> "whoField"),
"submit" -> submit(?("Send"), () => {test(mongo)}), //*** I
CALL 'test' HERE ***
"who" -> who.openOr("")
)
}
}

Geir Magnusson

unread,
Mar 16, 2009, 9:48:38 PM3/16/09
to mongod...@googlegroups.com
that's the non-supported driver that I originally wrote before we had
the Babble driver separated out. I was and am very interested in
getting it tailored for ease of working in non-Java langs on the JVM.

So. To be clear - that isn't the officially supported 10gen driver.
It's a different one, supported by me (and anyone who wants to help).

I never put in the pair support - I was just waiting for someone to
care. I'll be happy to do that ASAP if you want.

I'm also interested in what else we should do to it to make it easy
for Scala use - let me know what you think.

geir

Eliot Horowitz

unread,
Mar 16, 2009, 10:29:07 PM3/16/09
to mongod...@googlegroups.com
also - you should be able to use the supported driver pretty easily.
you might want to give that a shot - and look at the docs for creating
a paired connection.

Geir Magnusson

unread,
Mar 16, 2009, 10:44:04 PM3/16/09
to mongod...@googlegroups.com
btw - is it troubling to have to go into java.util for the map?

I tried to make the driver just use natural collections, so that the
'hash' in the various non-java languages would just work, and came
pretty close to supporting ruby, python and clojure, but scala was
problematic since the scala Map isn't a j.u.Map or collection, IIRC.

Is this annoying?

geir

On Mar 16, 2009, at 9:40 PM, bilbo0s wrote:

>

bilbo0s

unread,
Mar 17, 2009, 1:14:39 PM3/17/09
to mongodb-user
Geir,

With respect to the Hashtable, I found it to be something that
confused me for less than two minutes. I don't think it is a
problem. But that is the opinion of someone who is expert
at Java and borders on expert at Scala. I understand what
you are getting at, as more people start to use Scala and
Mongo, maybe you will want to change that in the manner
you indicated. That is, to use Scala's HashMap. It is not
a top priority I think right now, but it would be a nice to
have.

bilbo0s

unread,
Mar 17, 2009, 1:17:22 PM3/17/09
to mongodb-user
OOPS!!!

Accidentally posted this as a Discussion Forum
Post, instead of a reply. Sorry:


--------

Hello again all,

I apologize for the long time between posts.
My daughter was sick last week and is on
Spring Break this week. So I am working
on this stuff only when I have the time.

So it is slow going.

At any rate, I gave the new driver a try
and I still don't get failover. Below is
the code I am using now:

package pixipoli.helloworld.snippet

import scala.collection.mutable.HashMap

import scala.xml.NodeSeq
import net.liftweb.http.S._
import net.liftweb.http.SHtml._
import net.liftweb.http.RequestVar
import net.liftweb.http.SessionVar
import net.liftweb.util.Helpers._
import net.liftweb.util.Full
import com.mongodb.Mongo
import com.mongodb.BasicDBObject

class HelloForm3 {
object who extends RequestVar(Full("world"))
object mongo extends SessionVar[Mongo](new Mongo("localhost", 20000,
"scala"))

def show(xhtml: NodeSeq): NodeSeq = {

def test(tmongo: Mongo): Unit = {

val coll = tmongo.getCollection("test");

val doc = new BasicDBObject()
val t = who.openOr("")
doc.put("name", t)
coll.insert(doc);
println("FUNCTION value:" + who.openOr("") + " :: " + param
("whoField"))
}

bind("hello", xhtml,
"whoField" -> text(who.openOr(""), v => {who(Full(v))}) %
("size" -> "10") % ("id" -> "whoField"),
"submit" -> submit(?("Send"), () => {test(mongo)}),
"who" -> who.openOr("")
)
}

}

Any pointers?

Where are these docs for creating a paired connection?

--------

Eliot

unread,
Mar 17, 2009, 1:20:14 PM3/17/09
to mongodb-user
Code pushed to github for nicer Mongo constructor that takes left and
right pair.

bilbo0s

unread,
Mar 17, 2009, 6:54:16 PM3/17/09
to mongodb-user
OK,

I built the new driver you pushed,
works fine with failover.

A couple of questions:

Is it the case that I can use the DBTCP class
to make a connection to more than 2 Mongos?

For instance, if I have 1 Master and then a
Slave that is configured as a Master for a
third Slave, can I give the DBTCP constructor
a list of 3 IP addresses?

Secondly,

What exactly is the difference between the
Mongo Java Driver and XJDM? Since XJDM
is unsupported, I am wondering what the
intent and history of that driver is?

As usual, thank you for the patience you guys
are showing.

Geir Magnusson

unread,
Mar 17, 2009, 7:14:34 PM3/17/09
to mongod...@googlegroups.com

On Mar 17, 2009, at 6:54 PM, bilbo0s wrote:

>
> OK,
>
> I built the new driver you pushed,
> works fine with failover.
>
> A couple of questions:
>
> Is it the case that I can use the DBTCP class
> to make a connection to more than 2 Mongos?
>
> For instance, if I have 1 Master and then a
> Slave that is configured as a Master for a
> third Slave, can I give the DBTCP constructor
> a list of 3 IP addresses?

I didn't think that MongoDB supports that mode yet - I believe that
you can only have a two-level replication structure.

>
> Secondly,
>
> What exactly is the difference between the
> Mongo Java Driver and XJDM? Since XJDM
> is unsupported, I am wondering what the
> intent and history of that driver is?

It's not "unsupported" as it's just not "10gen supported". It's an
independent open source project, and I'll support XJDM as I'm using it
in a few personal projects, and want to continue to explore support
for alternate languages on the JVM.

The history is simple. I wrote XJDM last October for a few reasons :

1) I was documenting BSON and the MongoDB wire protocol, and the best
way I found to be sure you grok something is to write code that does it.

2) There was no independent driver for java for MongoDB, as MongoDB
and Babble were tightly coupled, and the code that became the 10gen
supported driver was embedded in Babble and designed for JS support.
So I thought it would be nice to have a small Java driver that could
be used in any of the languages that run on the JVM, in the most
idiomatic way possible for those languages.

After Jan 5, when we decided to drop work on Babble and just focus on
MongoDB, the code from the appserver was pulled out and turned into
the "10gen supported" Java driver.

So I'll keep going with XJDM - it gives us an opportunity to
experiment with things, and if they work and feel right, bring them
back into the 10gen-supported driver.

>
> As usual, thank you for the patience you guys
> are showing.

I think that you are the one showing the most patience. Thanks for
that :)

geir

bilbo0s

unread,
Mar 17, 2009, 9:05:35 PM3/17/09
to mongodb-user
OK,

That all makes sense.

I have yet another question about these drivers.

There seems to be no obvious way in the 10gen
supported drivers to do server side eval(). Is it
the case that I am just not seeing it?

Geir Magnusson Jr.

unread,
Mar 17, 2009, 9:12:41 PM3/17/09
to mongod...@googlegroups.com
Odd - that should be there.

I'll add if not.

geir

Eliot

unread,
Mar 17, 2009, 9:24:49 PM3/17/09
to mongod...@googlegroups.com
oops - sorry about that. Added.

bilbo0s

unread,
Mar 17, 2009, 9:25:41 PM3/17/09
to mongodb-user
Take a look, and let me know what you find.

On a related note.

Is there anything I can do to help you guys
right now? I feel like a real free loader asking
all of these questions. Some of which have
you guys making changes to the code to
accommodate me. Please feel free to tell
me to write it myself if I am getting too
annoying.

On the other hand, maybe just testing and
looking at Mongo from the perspective of
a user is helping you out in other ways.

Let me know!!!

Eliot

unread,
Mar 17, 2009, 9:25:49 PM3/17/09
to mongod...@googlegroups.com

Geir Magnusson Jr.

unread,
Mar 17, 2009, 9:26:27 PM3/17/09
to mongod...@googlegroups.com
funny - I was just doing a push to add...

Geir Magnusson Jr.

unread,
Mar 17, 2009, 9:27:36 PM3/17/09
to mongod...@googlegroups.com

On Mar 17, 2009, at 9:25 PM, bilbo0s wrote:

>
> Take a look, and let me know what you find.
>
> On a related note.
>
> Is there anything I can do to help you guys
> right now? I feel like a real free loader asking
> all of these questions. Some of which have
> you guys making changes to the code to
> accommodate me. Please feel free to tell
> me to write it myself if I am getting too
> annoying.
>
> On the other hand, maybe just testing and
> looking at Mongo from the perspective of
> a user is helping you out in other ways.

That's a huge help. But if you want to submit patches, that's
welcome as well.

geir

Geir Magnusson Jr.

unread,
Mar 17, 2009, 9:31:03 PM3/17/09
to mongod...@googlegroups.com
I wonder if it would be better to not throw an exception on a failed
eval(), but just return the result object and give the caller the
option to examine and react.

bilb0s?

geir

bilbo0s

unread,
Mar 17, 2009, 9:33:33 PM3/17/09
to mongodb-user
I am going to start calling this Eliot guy 'Speedy'!

Thanks Again Man!

On Mar 17, 8:25 pm, Eliot <eliothorow...@gmail.com> wrote:
> oops - sorry about that. Added.http://github.com/mongodb/mongo-java-driver/commit/2b773f2b59376d4765...

Geir Magnusson Jr.

unread,
Mar 17, 2009, 9:35:37 PM3/17/09
to mongod...@googlegroups.com
He certainly is.

Are the driver tests passing for you? I'm getting two failures.
Doing a mongod rebuild now.

geir

Ray Dookie

unread,
Mar 17, 2009, 9:42:44 PM3/17/09
to mongod...@googlegroups.com
just out of curosity...

i've decided to build a project i'm working on in java...which driver should i use?
and whats the main difference bet the 2?

Eliot

unread,
Mar 17, 2009, 9:45:34 PM3/17/09
to mongod...@googlegroups.com
There was a bug in getCount - just fixed it.
It seems like it was because of a database change on 2/24
So not sure why we haven't been alerted of it before.

Geir Magnusson

unread,
Mar 17, 2009, 9:46:49 PM3/17/09
to mongod...@googlegroups.com
I just fixed it as well.

geir

Geir Magnusson

unread,
Mar 17, 2009, 9:47:30 PM3/17/09
to mongod...@googlegroups.com
The 10gen driver is supported by 10gen.

geir

bilbo0s

unread,
Mar 17, 2009, 9:47:44 PM3/17/09
to mongodb-user
You're right.

On both counts.

It would be more intuitive to get
the result object and react.

And there do seem to be some
issues with the build I made of
Eliot's changes.

Just a moment...
I am about to be able to get rid
of my daughter for the night...

Single dads LUV children's
bedtime!!!

FREEDOM!!!
> ...
>
> read more »

Geir Magnusson

unread,
Mar 17, 2009, 9:47:56 PM3/17/09
to mongod...@googlegroups.com
We might not be running those tests in CI. Will fix.

geir

Eliot

unread,
Mar 17, 2009, 10:27:56 PM3/17/09
to mongodb-user
I split out doEval from eval
So doEval will return the raw object and eval will just return the
return value and throw an exception on error.

I think in general you should be using eval. Unless there is a
problem - the only thing you're going to want is the return value, and
especially in java exceptions work pretty well for error handling.
You have to handle network errors and such anyway.
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages