Testing connectionTimeout threshold using Mongo Java driver 3.0.0 - mongodb

Mongo m = new MongoClient(new ServerAddress(server , port), creds, MongoClientOptions.builder().connectTimeout(2000).socketTimeout(2000).serverSelectionTimeout(10000).socketKeepAlive(true).build());
I want to test connectionTimeOut to a standalone server. Every time I define the wrong port for a client or stop the server, it fails on the select server step and returns a serverSelectionTimeOut. How can I test the connectionTimeOut threshold in a development environment?

You need to make sure that you are connecting to an existing server (hostname and port), because the connection is setup after the server-selection process. If server selection fails it won't go to the connection-setup step.
If there is no Mongo server that is always available for testing, try deploying an embedded Mongo server on the fly on your local machine instead. You could write a unit test that automatically launches the embedded Mongo at the start of the test (as is demonstrated here).
Alternatively you could use MongoUnit for testing, but I have no experience with that myself.
Once you have a proper server you can force a connectionTimeout, by setting the timeout value to 1 millisecond. You can do that in code
MongoClientOptions.builder().connectTimeout(1)
or by adding a setting to your database-url
mongodb://localhost:27017/your_database?connectTimeoutMS=1

Related

MongoDB Rust Client Connection Errors

I am a Rust newbie and am trying to create a REST API using actix and mongodb.
Before starting up the I am trying to connect to a local MongoDB instance Using the official rust client from here. Here is the code I am trying
let client = Client::with_uri_str("mongodb://localhost:27017").expect("Error getting client");
let database = client.database("mydb");
let collection = database.collection("books");
Rustc version is 1.44 and MongoDB driver version 1.0.0
While running this code, I do not get an error if the local mongodb server is not running.
How do we figure out if the connection to the DB has been successful or not ? No point continuing if the DB connection itself is not established.
The drivers are required to establish connections in the background. Instantiating a client is supposed to always succeed.
The purpose of this is to, for example, allow the application and the database to be started at the same time - when the database becomes available, the application will be able to use it.
To find out whether your database is operational, execute a command such as ping. Most applications will simply carry on with their normal queries/updates.

Intermittent connection failures with heroku postgres while using play-slick

I have a play app on heroku connecting to a postgres instance with play-slick. Around 30% of the time when I deploy a new application I get this in my logs:
java.sql.SQLTransientConnectionException: db - Connection is not available, request timed out after 1007ms.
When I restart the application it will usually start again, though sometimes it takes a few tries.
Any advice for what I can do to debug this?
Most likely, there is a period of time where both the old app and the new app are trying to get connections to the database, which means you have double you max allowed connections active.
There are two solutions:
Upgrade your database plan to allow for more connection
Reduce your max db connections by half
play-slick uses HikariCP to pool connections, so you can probably configure your max connections with maximumPoolSize.
I believe I've figured out what the issue was. I used the default heroku play Procfile which contains -Ddb.default.url=${DATABASE_URL} and additionally had the slick db url specified in my conf. Removing the former solved the problem.

Not possible to use server after db = server.use?

I was doing a very basic test using node.js, latest orientjs and latest OrientDB:
open server (using port 2424)
assign db (server.use)
server.list()
If I do this, the promise at server.list() hangs and the server shows the error "Impossible to read a chunk of length...". But if I just don't assign the db, it works fine. Also, if I call db functions (like db.record.get) instead of server.list(), that works fine too.
Is it the case that you can't call a server function after calling server.use?
On a related point, is there a way to set a network timeout for orientjs? If I try to do any of this and the target server is not reachable, it just hangs on opening the server and/or assigning the database.

Query on Ratchet Socket

I am using Ratchet Socket. I have established a new server connection and I want to stop server from running. In this scenario I have IP(Hostname) and port with me, So how can I stop that?
Is it possible to make a server connection that never ends?
When I make a server connection, first day the data output is perfect from DB, But on second day, the error is generated as "Connection is closed by foreign host". But still I can connect to that port.
Code
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;
use React\EventLoop\Factory;
use React\ZMQ\Context;
require dirname(__DIR__) . '/vendor/autoload.php';
require dirname(__DIR__) . '/src/MyApp/Chat.php';
$server = IoServer::factory(
new Chat(),
6666
);
$server->run();
?>
1.
I am using Ratchet Socket. I have established a new server connection
and I want to stop server from running. In this scenario I have
IP(Hostname) and port with me, So how can I stop that?
I assume that you currently run your Ratchet server by running it as a php script in a terminal window or screen.
eg: php push-server.php
Once you stop running the script your server will stop.
2.
Is it possible to make a server connection that never ends?
Yes, if your php script stops working from the terminal, you have to manually restart it. Its better to use a program like Supervisor (A Process Control System) which is recommended by Ratchet.
Check this link for more info http://supervisord.org/installing.html
The supervisord service will monitor your php script and will automatically restart it if it crashes which is suited for production environments.
3.
When I make a server connection, first day the data output is perfect
from DB, But on second day, the error is generated as "Connection is
closed by foreign host". But still I can connect to that port.
This is quite common and I've noticed it too. It usually happens when the server is heavily loaded or times out. Your JavaScript should check for this message and re-initiate a new connection if you see this message. You can also get it to try again after a random timer as well.
Edits
Also the __construct method for the Ratchet\Server\IoServer requires 3 prams, of which the 3rd one being optional. The first and second need to be objects of MessageComponentInterface and ServerInterface.
public function __construct(MessageComponentInterface $app, ServerInterface $socket, LoopInterface $loop = null) {
The way you instantiate the IoServer seems incorrect.

ReactiveMongo with Play 2 Framework saying "entire node set is unreachable"

I'm trying to get a Play (2.1) app with ReactiveMongo (0.9) working on the app's test server. However, when our application is run on my dev box, is able to store image metadata just fine, even pointing to the mongo 2.2 install on the mongo test server. Even ran it with "play stage", then run directly with java 1.6.0. However, run the same way, also with Java 1.6.0 on the test server, the app continuously logs this error:
r.c.a.MongoDBSystem - The entire node set is unreachable, is there a network problem?
r.c.a.MongoDBSystem - The entire node set is unreachable, is there a network problem?
r.c.a.MongoDBSystem - The entire node set is unreachable, is there a network problem?
And not just during initialization... it repeats indefinitely. I've seen this error mentioned elsewhere, but I don't think those solutions apply to this. From the app's test server, I'm able to telnet to port 27017 on the mongo test server successfully. I see both my local install and the test server install of the app log that it's using the same mongodb url.
So based on what I said, I believe I can eliminate:
Blocked port
Mongo server down
Pointing to wrong mongo server
Mongo version mismatch
Java version mismatch
I'm going through the reactivemongo source but it seems the error is spewed when the MongoChannels are not set as authenticating or ready state (usable). I'm planning to try remote-debugging to see where it's going wrong, but I'm running out of time on this, so I'm hoping for a troubleshooting tip or two if I can get any.
Thanks!
Alright, figured it out. We're running Casbah/Salat on the same app, for now. There's a mongodb.uri in the config file that gets read in by both. However, ReactiveMongo seems to only work if the database name is included, which according to the mongodb "connection string uri" spec:
http://docs.mongodb.org/manual/reference/connection-string/
... you only need to include the database if you have credentials you need to authenticate with. In our case, we don't have credentials, so Casbah wasn't including the database. I added it in anyway... casbah ignored it safely, and reactivemongo worked. I neglected to do the same in the test config file, so even though it was showing the correct host, it wasn't about to work correctly.
I see how the host url + db name in one string replaces the two fields "mongodb.servers" and "mongodb.db", but it can be confusing if not conforming to mongo's similar spec.