Tuesday, May 16, 2017

Converting JSON to LCOV (infofile)

Wrote a small NPM module that converts JSON to LCOV data

https://github.com/srikanthjeeva/hitmap_json_to_lcov



Tuesday, May 9, 2017

Install Cpanm module error : No such file or directory opening compressed index

system$ HOME=/tmp /home/perl/5.10/bin/cpanm REST::Client
! Finding REST::Client on cpanmetadb failed.
! cannot open file '/tmp/.cpanm/sources/http%www.cpan.org/02packages.details.txt.gz': No such file or directory opening compressed index
! Couldn't find module or a distribution REST::Client


--

Solution:

The problem is because of "LWP::Protocol::https" module.

Removing the directory worked for me :

/home/perl/5.10/lib/site_perl/5.10.1$ rm –rf LWP*

or try with option "--no-lwp"

Example: cpanm REST::Client --no-lwp


Error while installing Perl 5.10.1


 Error while installing Perl 5.10.1

a -Wdeclaration-after-statement -Wendif-labels -Wc++-compat 
cc -fstack-protector -L/usr/local/lib -o miniperl \
      gv.o toke.o perly.o pad.o regcomp.o dump.o util.o mg.o reentr.o mro.o hv.o av.o run.o pp_hot.o sv.o pp.o scope.o pp_ctl.o pp_sys.o doop.o doio.o regexec.o utf8.o taint.o deb.o universal.o xsutils.o globals.o perlio.o perlapi.o numeric.o mathoms.o locale.o pp_pack.o pp_sort.o  \
    miniperlmain.o opmini.o perlmini.o 
pp.o: In function `Perl_pp_pow':
pp.c:(.text+0x2d79): undefined reference to `pow'
pp.o: In function `Perl_pp_modulo':
pp.c:(.text+0x3b22): undefined reference to `floor'
pp.c:(.text+0x3b58): undefined reference to `floor'
pp.c:(.text+0x3b90): undefined reference to `fmod'
pp.o: In function `Perl_pp_atan2':
pp.c:(.text+0x8985): undefined reference to `atan2'
pp.o: In function `Perl_pp_sin':
pp.c:(.text+0x8b22): undefined reference to `sin'
pp.o: In function `Perl_pp_int':
pp.c:(.text+0x8fc1): undefined reference to `floor'
pp.c:(.text+0x9031): undefined reference to `ceil'
pp.o:(.rodata+0x120): undefined reference to `cos'
pp.o:(.rodata+0x128): undefined reference to `sin'
pp.o:(.rodata+0x130): undefined reference to `sin'
pp.o:(.rodata+0x138): undefined reference to `exp'
pp.o:(.rodata+0x140): undefined reference to `log'
pp.o:(.rodata+0x148): undefined reference to `sqrt'
pp_pack.o: In function `S_pack_rec':
pp_pack.c:(.text+0x740b): undefined reference to `floor'
pp_pack.c:(.text+0x742e): undefined reference to `floor'
pp_pack.c:(.text+0x7453): undefined reference to `floor'
collect2: error: ld returned 1 exit status
makefile:347: recipe for target 'miniperl' failed

make: *** [miniperl] Error 1


This link helped me to find what i was missing: https://rt.perl.org/Public/Bug/Display.html?id=55980

and finally this worked for me

Step 1: find your lib path which has all libm* files
In most systems it will be under /usr/lib64. Search for the files in your system. 
system:/usr/lib64 $ ls libm*
libm.a         libmcheck.a     libmenuw.so.5    libmpc.so.3.0.0    libmpfr.so.4  libmpx.so.0.0.0      libm.so         libmvec.a
libmagic.so.1      libmenu.so.5    libmenuw.so.5.9  libmpdec.so.2      libmpfr.so.4.1.4  libmpxwrappers.so.0      libmspack.so.0      libmvec_nonshared.a
libmagic.so.1.0.0  libmenu.so.5.9  libmpc.so.3      libmpdec.so.2.4.2  libmpx.so.0   libmpxwrappers.so.0.0.0  libmspack.so.0.1.0  libmvec.so
Step 2: Use the path with glibpth option while doing configure
./Configure -des -Dglibpth='/lib/lib64 /usr/lib64'
or I see some forums suggested plibpth
./Configure -des -Dplibpth='/lib/lib64 /usr/lib64' -Dglibpth='/lib/lib64 /usr/lib64' -Dlibpth='/lib/lib64 /usr/lib64'

I have answered the same here :  http://stackoverflow.com/a/43878868/453486




Friday, April 28, 2017

How to place Perl modules in non-standard locations and use it

If this is the project structure,

    /home/directory/lib
    -- Libary2.pm   

    /home/directory/project   
    -- file1.pl  
    -- file2.pl  
    -- Libary1.pm    

In `file1.pl`, we can include both libraries by,

    use lib '/home/directory/project';
    use lib '/home/directory/lib';

    or 

    #include both directory at once, 

    use lib qw(
        /home/directory/project
        /home/directory/lib
    );
    use Libary1;
    use Libary2;

`file2.pl`, will have the same path,

    use lib qw(
        /home/directory/project
        /home/directory/lib
    );
    use Libary1;
    use Libary2;

The above code is good for 1 or 2 files. But if there are 10+ perl files, we have to include the same lib path in all files. If the library path changes, we had to change all files with the correct path name.
So what I did was to have 1 module `config/LibPaths.pm` that will have the paths, and all files will include the module.

    lib
    -- Libary2.pm 

    project
    -- file1.pl
    -- file2.pl
    -- Libary1.pm  

    config 
    -- LibPaths.pm 

In `LibPaths.pm`

    package LibPaths;
    use lib qw(
        /home/directory/project
        /home/directory/lib
    );

and I include the `LibPaths.pm` module In `file1.pl` as follows

    use File::Basename;
    use Cwd qw(abs_path);
    use lib dirname (abs_path(__FILE__)) . "/config";
    use LibPaths;

-   `use Cwd qw(abs_path)` is needed to get the current file.
-   `use lib dirname (abs_path(__FILE__))` will get the directory name of the current file
-   `use lib dirname (abs_path(__FILE__)) . "/config";` will include the config folder. I liked to put the `LibPaths.pm` under a config directory and my project has 50+ files. But If the module is in same directory, including config is not needed.

**So now when the library paths changes, I can update in 1 place which is `LibPaths.pm` **

There are other Perl modules like “FindBin” which will serve this purpose, But I didnt want to introduce a new dependency in my project. So I followed the above hack. I might be an ugly hack, but it works :D

Thanks
Srikanth

Monday, April 24, 2017

How to private chat using node.js and socket.io


Create a room with conversation_id and make users to subscribe to that room, 
so that you can emit a private message to that room it by,

client
------
                 
var socket = io.connect('http://ip:port');

socket.emit('subscribe', conversation_id);

socket.emit('send message', {
    room: conversation_id,
    message: "Some message"
});

socket.on('conversation private post', function(data) {
    //display data.message
});

Server
-------
socket.on('subscribe', function(room) {
    console.log('joining room', room);
    socket.join(room);
});

socket.on('send message', function(data) {
    console.log('sending room post', data.room);
    socket.broadcast.to(data.room).emit('conversation private post', {
        message: data.message
    });
});

Here is my Stackoverflow answer : http://stackoverflow.com/a/23623724/453486  

Tuesday, May 31, 2016

How to setup Elasticsearch Custer in Centos

I have followed these steps in order to setup Elastic search in production.

# OS Requirements: Centos 6+ & Java 1.8+    

Step1:
------
------

Installing Java
---------------

Download JDK from : http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 
tar xzvf jdk.tar.gz
sudo mkdir /usr/local/java
sudo mv jdk1.8.0_45 /usr/local/java/
sudo ln -s /usr/local/java/jdk1.8.0_45 /usr/local/java/jdk
export PATH="$PATH:/usr/local/java/jdk/bin"
export JAVA_HOME=/usr/local/java/jdk1.8.0_91/jre
sudo sh -c "echo export JAVA_HOME=/usr/local/java/jdk1.8.0_91/jre >> /etc/environment"


Step2:
-----
-----

Installing Elasticsearch
------------------------

wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.3.3/elasticsearch-2.3.3.rpm
sudo rpm -ivh elasticsearch-2.3.3.rpm


Step 3:
-------
-------

Configure Elasticsearch
-----------------------

sudo vi /etc/elasticsearch/elasticsearch.yml

cluster.name: cluster_name # [cluster name. common name given to all elasticsearch servers to join the same cluster]
node.name: hostname-31 #[servername]
node.master: false #[set true only for master node]
node.data: true #[set true for all nodes, except for dedicated master node]

#[This is the path where all data is stored. Give one or multiple paths. Directories must have permsission to user elasticsearch] 
# chown elasticsearch:elasticsearch /home/es/
path.data: ["/home/es/data/es"] 

#[This is the path where all log files are stored. Directories must have permsission to user elasticsearch] 
#chown elasticsearch:elasticsearch /var/logs/es
path.logs: /var/logs/es 

path.work: /home/es/work


bootstrap.mlockall: true #[This setting is must for production]

network.host: "hostname.com"
#http.port: 9200 [9200 is default port. if needed modify this]

#Enter all master nodes
discovery.zen.ping.unicast.hosts: ["hostname.com"]

# calculate by formula (total number of nodes / 2 + 1)
discovery.zen.minimum_master_nodes: 3

# required for production
node.max_local_storage_nodes: 1

# -------------------------------- Threads ------------------
threadpool.index.type: fixed
threadpool.index.size: 40
threadpool.index.queue_size: 10000

threadpool.search.type: fixed
threadpool.search.size: 40
threadpool.search.queue_size: 10000

threadpool.bulk.type: fixed
threadpool.bulk.size: 40
threadpool.bulk.queue_size: 30000


#------------------ allocate memory for write --------------
  indices.memory.index_buffer_size: 30%

#------------------ shard --------------
cluster.routing.allocation.same_shard.host: true

Step 4:
-------
-------

Configure Elasticsearch System Level Setting
--------------------------------------------
# (Important setting)

sudo vi /etc/sysconfig/elasticsearch

#setheap size to maximum 32GB or Half of the RAM size
ES_HEAP_SIZE=32g 
MAX_OPEN_FILES=65535
MAX_LOCKED_MEMORY=unlimited
MAX_MAP_COUNT=262144


Step 4:
-------
-------

Change number of open files
--------------------------------------------
Change ulimit Setting: [http://stackoverflow.com/a/36142698/453486]
-------------------------------------------------------------------

sudo vi /etc/security/limits.conf

#add line: 
elasticsearch - nofile 65535


Step 5:
-------
-------

disable swap
------------


sudo vi /etc/sysctl.conf
#add lines: 
vm.swappiness=0
vm.max_map_count=262144

Step 6:
-------
-------

Installing plugins for monitoring cluster
-----------------------------------------

sudo /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
sudo /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf

# reboot system for the system level settings to work. /sbin/reboot

Step 7:
-------
-------

To Start/Stop/Restart Elasticsearch
------------------------------------

sudo /etc/init.d/elasticsearch start
sudo /etc/init.d/elasticsearch stop
sudo /etc/init.d/elasticsearch restart