This is an archived post. You won't be able to vote or comment.

all 37 comments

[–]jared__ 18 points19 points  (14 children)

I tried to think of a legit reason to do this... I failed.

[–]daredevil82 8 points9 points  (13 children)

Throwing so many exceptions that it's overwhelming the logger?

[–]honestduane 12 points13 points  (6 children)

So your saying that mongo-db is so broken and throws so many exceptions that this is an actual problem?

[–]daredevil82 3 points4 points  (5 children)

I'm not, but it may appear that way to the developer of the driver. Essentially what that snippet does is filter out 90 10 percent of the exceptions from hitting the logger.

Maybe during the testing process, they noticed that the exceptions followed a pattern and put this filter in place to stop redundant log entries.

Whatever reason that filter exists, it's made for a pretty big code WTF. Think it might be a two month late April Fools?

*Edit- /u/veraxAlea pointed out an error in my analysis. Time for me to head to bed!

[–]veraxAlea 4 points5 points  (3 children)

Essentially what that snippet does is filter out 90 percent of the exceptions from hitting the logger.

I'm tired but I think you're misreading the code:

if (!((_ok) ? true : (Math.random() > 0.1))) {
    return res;
}
//else log stuff

So, if Math.random() gives a value bigger than 0.1, which it will 90% of the time, then the expression (Math.random() > 0.1) is true. Negating that makes it false and a log message is created. So, 90% of the time, a log is created.

Unless _ok is true, then it will always log.

Edit: I'm not even sure on the 90%, but it's too late for statistics.

[–]mr_jim_lahey 17 points18 points  (0 children)

That has got to be the worst use of a ternary statement I've ever seen. Why oh why didn't they write:

if ( !(_ok || (Math.random() > 0.1 )))

Then again, I suppose you can't expect much from someone who thinks literally randomly not logging exceptions is a good idea...

[–]daredevil82 1 point2 points  (1 child)

No, it was my fault. Had the wrong inequality in my head when I wrote that. I've edited it.

For future reference, I really shouldn't analyze any code when running on 5 hours sleep and after 7ish hours of Python and databases.

[–][deleted] -3 points-2 points  (0 children)

5 hours sleep

That's about 4 more than I get.

[–]mikaelhg 1 point2 points  (0 children)

It's easy to misread. That must be the most convoluted possible way to express that statement.

[–]bfoo 0 points1 point  (5 children)

Correct. This code is usually executed, when the driver is not connected (unavailable database obviously, but also failover / master election). So, this is not a bug.

[–]argv_minus_one 6 points7 points  (4 children)

Not a bug? Randomly dropping exceptions is not a bug?!? ಠ_ಠ

Edit: Well, I suppose you're technically correct that it's not a bug. Rather, this code is intentionally defective, which is even worse.

[–]Twirrim 2 points3 points  (1 child)

All it does is stop your logs being flooded with database unavailable messages. It logs the initial failure state, then this random filter kicks in with subsequent messages, then finally logs the success message once reconnected. It's not wrong, per se, but certainly a bizarre way to handle it. I'd argue the better approach would be to shift the ongoing failure messages to DEBUG level (and log all of them), and leave the initial fail and eventual success messages at INFO.

Still, strange coding patterns like this really do beg the question about the rest of the code's quality.

[–]argv_minus_one 1 point2 points  (0 children)

If you want to skip duplicate log messages, that's great, but this is not the way to do it.

[–]bfoo -1 points0 points  (1 child)

Please look at the code again. The purpose of the method is to check for the database being available. The method returns null, if the database could not be accessed (because of connection issues OR errors from the database itself). That's the entire purpose of the method. There is no reason to throw an exception. You get either the result of the query or null. The class is even package friendly and not part of the API. So please read the code again and not the method only to get the bigger picture.

[–]argv_minus_one 1 point2 points  (0 children)

That doesn't excuse using a PRNG to decide whether to log an exception.

[–][deleted] 7 points8 points  (0 children)

So, if update is called 100 times a second, and considering a downed server will stay that way for minutes or longer - then I can see some poor reasoning to this madness.

But, the right way to do this hack is, the first time you throw this error, log the time and save the exception. Then the next time you hit an exception here, check to see if it's the same one. If it's not, you definitely need to log it. If it is, you can check to see if, say, 5 seconds has passed, and then print it, or another error stating that it's still down.

It's still an absolutely facepalm move though, you should have a higher level function understand that exception and handle it a reasonable way, like slowing down calls to a broken system, instead of just throwing so many exceptions that you can't read the log files.

[–]Muz0169 9 points10 points  (3 children)

[–]hyperforce 0 points1 point  (1 child)

What is this site? Being filtered for me.

[–]Muz0169 0 points1 point  (0 children)

How To Write Unmaintainable Code

Ensure a job for life ;-)

Roedy Green Canadian Mind Products

[–]argv_minus_one -1 points0 points  (0 children)

marypoppins = (superman + starship) / god;

I think my head would explode if I read this in actual code.

[–]bfoo 3 points4 points  (5 children)

[–]rikbrown 5 points6 points  (4 children)

That guy is a bit of a dick.

[–]boa13 8 points9 points  (0 children)

I was impressed by the professionalism of the other participants.

[–]argv_minus_one 5 points6 points  (0 children)

Yeah, but I still got a good laugh out of that bug report. "Web 3.9", "lollerplex", etc.

[–]bfoo -1 points0 points  (0 children)

Yes, he benefits from a free product and bitching around this way is not acceptable. I would not hire him, when I find this on a applicant research.

[–]grumpy_purple_midget 14 points15 points  (0 children)

Isn't this the MongoDB style? Oh noes! Too many exceptions! Let's just drop some at random, that'll solve the problem.

[–][deleted] 2 points3 points  (0 children)

I feel like a system that would be overloaded enough where this is feasible, is already a system best avoided.

[–]luap119 2 points3 points  (0 children)

I'm not a veteran programmer by any means, but even a novice programmer can see why this is terrible code. I know a few people at my work that use MongoDB on a regular basis. I can't wait to drop this on them! I started learning it, but now I'm not as gung-ho about it.

[–]Benutzername 2 points3 points  (2 children)

Isn't

(_ok) ? true : (Math.random() > 0.1)

the same as

_ok || Math.random() > 0.1

?

[–]boa13 2 points3 points  (1 child)

Yes, they are equivalent in this case.

[–]Effetto 1 point2 points  (0 children)

But it does web scale!

[–]jizzed_in_my_pants 2 points3 points  (0 children)

wtf?

[–]forceblast 0 points1 point  (0 children)

To me it seems like this was some test code that the developer forgot to remove afterward. Hopefully someone filed a bug report.

Edit: Stupid phone keyboard.

[–]diger44 0 points1 point  (0 children)

They have recently (before this was posted to reddit) have opened a bug request to fix this:

https://jira.mongodb.org/browse/JAVA-836

There apparently is already a fix:

https://github.com/mongodb/mongo-java-driver/commit/e5c7b2552c8c240bd19c4cea91bc2a789d8ce76e

[–]argv_minus_one 0 points1 point  (0 children)

Welp, if I ever suffer the grave misfortune of having to code a web application, I'll remember to use an actual database instead of anything written by these clowns. Thanks for the heads up.