Sunday, November 10, 2013

Give it a try BoneCP


I used c3p0 pooling almost everywhere. After reading few articles and suggestions I want to try BoneCP to see how its workout.
Here is basic sample how to test it.

You must have below jar files in your classpath
bonecp-0.8.0.RELEASE.jar
guava-15.0.jar
slf4j-api-1.7.5.jar
mysql-connector-java-5.0.8-bin
DB driver
My case its Mysql. So I use below link to download.

You can try these on below links.
http://jolbox.com/ - Go to download link

Once the setup is done its quite straightforward. You can try piece of code appear below.

------------------------------------------------------------------
oneCP connectionPool = null;
Connection connection = null;
        try {
            // load the database driver (make sure this is in your classpath!)
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        try {
            // setup the connection pool
            BoneCPConfig config = new BoneCPConfig();
            config.setJdbcUrl("jdbc:mysql://10.82.100.61/kasun"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/db
            config.setUsername("kasun");
            config.setPassword("kasun123");
            config.setMinConnectionsPerPartition(5);
            config.setMaxConnectionsPerPartition(10);
            config.setPartitionCount(1);
            connectionPool = new BoneCP(config); // setup the connection pool

            connection = connectionPool.getConnection(); // fetch a connection

            if (connection != null) {
                System.out.println("Connection successful!");
                Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM t1"); // do something with the connection.
                while (rs.next()) {
                    System.out.println("Column One - " + rs.getString(1));
                    System.out.println("Column Two - " + rs.getString(2));
                }
            }
            Thread.sleep(30000);
            connectionPool.shutdown(); // shutdown connection pool.
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InterruptedException iex) {
            iex.printStackTrace();

        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
------------------------------------------------------------------

Thread sleep is introduce in order to check the connectivity from Mysql.
Table definition as below.

CREATE TABLE `t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `address` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

Table data as below

mysql> select * from t1 \G
*************************** 1. row ***************************
     id: 2
address: bandarawela
1 row in set (0.00 sec)

Compile it and run.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Connection successful!
Column One - 2
Column Two – bandarawela

Below is the connections listed in Mysql.


mysql> show processlist;
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
| Id  | User  | Host                        | db    | Command | Time | State | Info             |
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
| 214 | kasun | MA02ENLKASUN:50805 | kasun | Query   |    0 | init  | show processlist |
| 231 | kasun | MA02ENLKASUN:52283 | kasun | Sleep   |    7 |       | NULL             |
| 232 | kasun | MA02ENLKASUN:52284 | kasun | Sleep   |    7 |       | NULL             |
| 233 | kasun | MA02ENLKASUN:52285 | kasun | Sleep   |    7 |       | NULL             |
| 234 | kasun | MA02ENLKASUN:52286 | kasun | Sleep   |    7 |       | NULL             |
| 235 | kasun | MA02ENLKASUN:52287 | kasun | Sleep   |    7 |       | NULL             |
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
6 rows in set (0.00 sec)

BoneCP simple and easy to user. You can check their website for further and for more examples. Worth a try.


Wednesday, November 6, 2013

LVM snapshot and volume creation


I test this on my CentOS VirtualBox. Went well for me.
Increase the virtual disk size in Windows 7. Make sure you shut down the virtual machine (As it give me errors, may be if I have two disks I can do it on the non-primary)
C:\Program Files\Oracle\VirtualBox>VBoxManage.exe modifyhd "C:\Users\kasun\VirtualBox VMs\centOS1\centOS1.vdi" --resize 20000
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

Login to the CentOS(virtual machine)
fdisk /edv/sda
n
primary partition
3
2089
2300
w
--------------------------
Reboot so it will be written to the partition table
--------------------------
n
primary partition
4
2301
2549
w
reboot
--------------------------

Two physical volumes.
pvcreate /dev/sda3
pvcreate /dev/sda4


pvscan
pvdisplay - display the physical volume

Create the volume group
vgcreate vol_grp_k /dev/sda3 /dev/sda4

vgscan
vgdisplay – will display the volume group

Create the logical volume
lvcreate -v -L 900MB -n vol_k_one vol_grp_k

Create the file system
mkfs.ext4 /dev/vol_grp_k/lv_one_k
Create a dir
mkdir /back
Mount so can be use for,
mount /dev/vol_grp_k/lv_one_k /back

Take a snapshot
Getting the snapshot from previously created partition
lvcreate --snapshot –-size=100M --name=Snap /dev/vol_grp_k

Snap will be mounted to this
mkdir /snap_shot

Mounting
mount -o ro /dev/vol_grp_k/lv_one_k /snap_shot
<Do what ever you want to do>
then remove the snapshot as you dont need it anymore

umount /dev/vol_grp_k/lv_one_k
lvremove /dev/vol_grp_k/lv_one_k


Tuesday, November 5, 2013

Test smtp with telnet



Something very useful testing smtp connections. You can just telnet can see the email delivery. Take a tcpdump for future reference as well.

server> telnet outlook1.kwire.local 25
Trying 10.21.90.1...
Connected to outlook1.kwire.local.
Escape character is '^]'.
220 kwire.local Microsoft ESMTP MAIL Service ready at Wed, 6 Nov 2013 12:03:59 +0800
helo kwire.com
250 EXCAS01.kwire.local Hello [10.21.1.3]
mail from:sender@kwire.com
250 2.1.0 Sender OK
rcpt to:admin@kwire.com
250 2.1.5 Recipient OK
data
354 Start mail input; end with <CRLF>.<CRLF>

test mail from kwire
.
250 2.6.0 <67af9592-163d-4c10-8b65-21d582af9cfa@EXCAS01.kwire.com> [InternalId=3278440] Queued mail for delivery
---------------------------------------------------------------------------------------

Note - each new line is with ENTER. 

Monday, November 4, 2013

MongoDB replica set hidden member and application configuration


Replica set usage

It seems the driver made 3 connection when we specify the three mongod servers in connection URL.
            MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),new ServerAddress("localhost", 37017), new ServerAddress("localhost", 47017)));

Tue Nov 05 12:04:53.690 [conn56] end connection 127.0.0.1:64511 (3 connections now open)
Tue Nov 05 12:04:53.693 [conn57] end connection 127.0.0.1:64516 (2 connections now open)
Tue Nov 05 12:04:53.909 [conn52] end connection 127.0.0.1:64486 (1 connection now open)
Tue Nov 05 12:04:53.911 [initandlisten] connection accepted from 127.0.0.1:64520 #58 (2 connections now open)


            MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),new ServerAddress("localhost", 37017)));

Tue Nov 05 12:05:23.918 [conn58] end connection 127.0.0.1:64520 (1 connection now open)
Tue Nov 05 12:05:23.920 [initandlisten] connection accepted from 127.0.0.1:64555 #60 (2 connections now open)

Tue Nov 05 12:05:27.360 [conn59] end connection 127.0.0.1:64521 (1 connection now open)
Tue Nov 05 12:05:27.361 [initandlisten] connection accepted from 127.0.0.1:64556 #61 (2 connections now open)


When the PRIMARY is down can clearly see it first try with previous PRIMARY but then move to the new PRIMARY. After sometime it will make sure it will route to the correct server on next attempt.

Nov 05, 2013 12:17:08 PM com.mongodb.ConnectionStatus$UpdatableNode update
WARNING: Server seen down: localhost/127.0.0.1:37017 - java.io.IOException - message: Connection refused: connect
Nov 05, 2013 12:17:08 PM com.mongodb.ConnectionStatus$UpdatableNode update
WARNING: Server seen down: localhost/127.0.0.1:37017 - java.io.IOException - message: Connection refused: connect
Nov 05, 2013 12:17:09 PM com.mongodb.ConnectionStatus$UpdatableNode update
WARNING: Server seen down: MA02ENLKASUN/127.0.0.1:37017 - java.io.IOException - message: Connection refused: connect

But on next attempt it is getting without connection exceptions.


Hidden member
You can make hidden member, which will never be a primary rather synch with data and can be used for backup or reporting. But whenever new primary need to elected it will be join in. Priority is 0 and must enable the hidden.

{
"_id" : <num>
  "host" : <hostname:port>,
  "priority" : 0,
  "hidden" : true
}

cfg = rs.conf()
cfg.members[0].priority = 0
cfg.members[0].hidden = true
rs.reconfig(cfg)
After the hidden member add in. Can’t be view from db.isMaster() 

rs0:PRIMARY> db.isMaster()
{
        "setName" : "rs0",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "MA02ENLKASUN:37017",
                "MA02ENLKASUN:57017",
                "MA02ENLKASUN:27017"
        ],
        "arbiters" : [
                "MA02ENLKASUN:47017"
        ],
        "primary" : "MA02ENLKASUN:37017",
        "me" : "MA02ENLKASUN:37017",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2013-11-05T07:25:25.248Z"),
        "ok" : 1
}
rs0:PRIMARY> cfg = rs.conf();
rs0:PRIMARY> cfg.members[3].priority=0;
0
rs0:PRIMARY> cfg.members[3].hidden=true;

rs0:PRIMARY> rs.reconfig(cfg);
Tue Nov 05 15:26:37.998 DBClientCursor::init call() failed
Tue Nov 05 15:26:38.005 trying reconnect to 127.0.0.1:37017
Tue Nov 05 15:26:38.009 reconnect 127.0.0.1:37017 ok
reconnected to server after rs command (which is normal)

rs0:SECONDARY> db.isMaster()
{
        "setName" : "rs0",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "MA02ENLKASUN:37017",
                "MA02ENLKASUN:27017"
        ],
        "arbiters" : [
                "MA02ENLKASUN:47017"
        ],
        "primary" : "MA02ENLKASUN:37017",
        "me" : "MA02ENLKASUN:37017",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2013-11-05T07:26:45.595Z"),
        "ok" : 1
}