Thursday, 16 May 2019

/etc/skel directory in Linux

  • skel is derived from the skeleton because it contains basic structure of home directory
  • The /etc/skel directory contains files and directories that are automatically copied over to a new user’s when it is created from useradd command.
  • This will ensure that all the users gets same intial settings and environment.
ls -la /etc/skel/
total 24
drwxr-xr-x.  2 root root   62 Apr 11  2018 .
drwxr-xr-x. 77 root root 2880 Mar 28 03:38 ..
-rw-r--r--.  1 root root   18 May 30 17:07 .bash_logout
-rw-r--r--.  1 root root  193 May 30 17:07 .bash_profile
-rw-r--r--.  1 root root  231 May 30 17:07 .bashrc
  • The location of /etc/skel can be changed by editing the line that begins with SKEL= in the configuration file /etc/default/useradd. By default this line says SKEL=/etc/skel.
cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
  • Default Permission of the /etc/skel directory is drwxr-xr-x.
  • It is not recommended to change the permission of skel directory or its contents. skel directory there are some profiles that needs the permission of read and trying to give it permission of execute will cause some programs/profiles to stop work or not works as expected.

Change TimeZone in linux

Standard and precise timezone is crucial for the evaluation and execution of many tasks and processes running on a Linux instance. we come across certain circumstances where we need of changing and setting up the different timezone on the Linux system.
Let’s see how can we do it.

Check Current TimeZone :

We can do using date command
date
Thu May 16 10:35:11 IST 2019


or
using timedatectl command
timedatectl 
 Local time: Thu 2019–05–16 23:05:55 IST
 Universal time: Thu 2019–05–16 17:35:55 UTC
 RTC time: Thu 2019–05–16 17:35:55
 Time zone: Asia/Kolkata (IST, +0530)
 System clock synchronized: yes
 systemd-timesyncd.service active: yes
 RTC in local TZ: no


How to change:

  • All the time zones are located under /usr/share/zoneinfo directory


  • Now create a link the timezone file from the above directory to the /etc/localtime directory
ln -s /usr/share/zoneinfo/US/CET /etc/localtime
  • In some of the distributions, the timezone is controlled by /etc/timezonefile.
cat /etc/timezone 
Asia/Kolkata
  • To change this to Australia time (Brisbane), modify the /etc/timezone file as shown below.
# vim /etc/timezone
America/Brisbane
That’s it.
Happy Learning.

Saturday, 11 May 2019

Ahead Of Time VS Just In Time in Java

How Java works:

  • Java compiles the code and converts into byte code(.class)
  • JVM interprets the byte codes and converts into machine level codes so that it can run on any machine

Byte Code To Machine Code:

  • Byte code is get converted to machine level code using a dictionary of instructions (byte to m/c), because of different types of machines(like Ubuntu, mac, windows, etc) have different types of the instruction set.
  • The interpreter is very quick to start and load the app.
  • One problem is interpreter does not perform any optimization, because of that same byte code get converted to machine code.
  • To solve this problem C1 Compiler used. which uses code cache.

C1 Compiler:

  • So when interpreter uses counter how many times the same byte code gets converted to machine code which is saved in the code cache.
  • When the counter reaches the threshold then C1 compiler compiles the codes and save in code cache so that when same byte codes get executed it will get from code cache.
  • Code cache is a memory area separate from the JVM heap that contains all the JVM bytecode for a method compiled down to native code, each called a nmethod1. This is where the JIT compiled methods are kept
  • This is also called JIT(just in time) compilation.
  • Default Code cache size is 240MB in java 8 but we can set a different value, using the flag -XReservedCodeCacheSize.
  • The default compilation threshold is 1500 for C1 Compiler

C2 Compiler:

  • After sometime when JVM runs for some time, its start collecting statistics in the background how code is being executed called code profiling. Its creates control flow graphs(code paths). It tries to find out hottest code paths once it has enough statistics then JVM asked for C2 compiler to perform optimizations on the hottest code paths.
  • It also stores optimized code in Code Cache.

Optimizations Perform by C2:

  • Dead Code
  • Escape Analysis
  • Loops
  • Methods Inlining
  • Null check Elimination
etc are used by C2 compiler for to optimize the hottest code

More about C1 and C2:

  • Two compilers, C1, and C2 run in parallel and keep on optimizing the code
  • . C1 is preferred for the client application and C2 is preferred for long running server applications.
  • Tiered compilation combines the best features of both compilers. Client-side compilation yields quick startup time and speedy optimization, while server-side compilation delivers more advanced optimizations later in the execution cycle.
  • When the Code Cache is constrained (its usage approaches or reaches the ReservedCodeCacheSize), to compile more methods, the JIT must first throw out some already compiled methods. Discarding compiled methods are known as Code Cache flushing.
  • In JAVA 7 we have the option to select to both the compiler.
  • In JAVA 8 both are available by default.

Ahead of Time Compilation:

While doing profiling manually or manually check thread or memory dump from JVM we might need to perform compilation ahead of time. this feature is enabled after JAVA9.
After Java9 we have the option to convert some of the classes or libraries to compiled code before the start of the application.

Compile class before:

Before we can use the AOT compiler, we need to compile the class with the Java compiler:
javac <classname>.java

Pass the class to the AOT compiler:

We then pass the resulting <classname>.java to the AOT compiler, which is located in the same directory as the standard Java compiler.
jaotc --output <classname>.so <classname>.class

Running the Program

We can then execute the program while running the program we need to use the flag -XX: AOTLibrary to tell the JVM for AOT compiled class.
java -XX:AOTLibrary=./<classname>.so <classname>
  • We can also see the library was loaded by adding -XX:+PrintAOT as a JVM argument.
That’s it.
Happy Learning.

Thursday, 2 May 2019

Change Default Editor in Ubuntu

In Ubuntu, many of the utilities use the editor to edit the configuration files.
Some of the command like visudo , crontab -e, etc uses the editor to edit there corresponding files. By default, nano is the editor.
In case if you wanted to update or change the default editor you can do it very easily using update-alternatives command.
Lets see how can we do it.
Run Command:
sudo update-alternatives — config editor








Now you can select any editor by entering the sequence number.
For Example, I have selected vim which is option 4
You can validate that editor has changed or not using a command like visudo etc. You should see the editor that you chose, instead of the default.
That’s it.
Happy Learning

Achieve 600k Concurrent WebSocket Connections

Recently we have done a performance test for WebSocket connections using AWS infrastructure how much concurrent connections can be handled by a single server.

Technology Used:

  • 1 Machine of M3.xlarge. It has 4 CPUs and 15Gb of memory for the server.
  • 10 Machine of c5.large It has 2CPUs and 4Gb of memory for client
  • Spring Webflux
  • Netty Server: An Event-driven, Non-blocking Server
  • Tsung: Performance Testing tool

Server Side:

It just contains simple one controller which handles WebSocket connections
and log the message received.
public class ReactiveWebSocketHandler implements WebSocketHandler {
@Override
    public Mono<Void> handle(WebSocketSession webSocketSession) {
        return webSocketSession.send(intervalFlux
                   .map(webSocketSession::textMessage))
                   .and(webSocketSession.receive()
                   .map(WebSocketMessage::getPayloadAsText)
                   .log());
      }
}

Configurations:

EC2 Configurations:

  • Set the Soft and Hard nofile limit to 1000000.
File : /etc/security/limits.d/custom.conf
append in the above file.
root soft nofile 1000000
root hard nofile 1000000
* soft nofile 1000000
* hard nofile 1000000
  • Now set
File: /etc/sysctl.conf
append in the above file.
fs.file-max = 1000000
fs.nr_open = 1000000
net.ipv4.netfilter.ip_conntrack_max = 1048576
net.nf_conntrack_max = 1048576
net.ipv4.ip_local_port_range= 1024 65535
net.ipv4.tcp_mem='10000000 10000000 10000000
net.ipv4.tcp_rmem='1024 4096 16384'
net.ipv4.tcp_wmem='1024 4096 16384'
net.core.rmem_max=16384
net.core.wmem_max=16384
fs.file-max: The maximum file handles that can be allocated.
fs.nr_open: Max amount of file handles that can be opened.
net.ipv4.netfilter.ip_conntrack_max: Specifies how many connections the NAT can keep track of in the tracking table before it starts to drop packets and just break connections.
net.ipv4.ip_local_port_range: Link for more explaination.

Tsung :

Tsung is an open-source multi-protocol distributed load testing tool. It’s written in Erlang. Tsung also supports benchmarking WebSocket protocol. Tsung configuration and scenarios are written in xml.
Install:
sudo apt-get update
sudo apt-get install tsung
tsung -v // to validate tsung installation
Configurations:
We have used 10 Tsung server because maximum we can create 64K connection from a single server.
  • We have also tried Apache Thor for creating connections its taking lot of time to create connections.
  • Tsung has very good UI and graphs so it easy for us to get the stats.
<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/user/share/tsung/tsung-1.0.dtd">
<tsung loglevel="notice" version="1.0">
  <clients>
    <client host="tsung-machine-1" use_controller_vm="false" maxusers="64000" />
    <client host="tsung-machine-2" use_controller_vm="false" maxusers="64000" />
<client host="tsung-machine-3" use_controller_vm="false" maxusers="64000" />
<client host="tsung-machine-4" use_controller_vm="false" maxusers="64000" />
<client host="tsung-machine-5" use_controller_vm="false" maxusers="64000" />
<client host="tsung-machine-6" use_controller_vm="false" maxusers="64000" />
<client host="tsung-machine-7" use_controller_vm="false" maxusers="64000" />
<client host="tsung-machine-8" use_controller_vm="false" maxusers="64000" />
<client host="tsung-machine-9" use_controller_vm="false" maxusers="64000" />
<client host="tsung-machine-10" use_controller_vm="false" maxusers="64000" />  
</clients>

  <servers>
    <server host="${SERVER IP}" port="3000" type="tcp" />
  </servers>

  <load>
    <arrivalphase phase="1" duration="100" unit="second">
      <users maxnumber="100000" arrivalrate="1000" unit="second" />
    </arrivalphase>
  </load>

  <sessions>
    <session name="websocket" probability="100" type="ts_websocket">
        <request>
             <websocket type="connect" path="/"></websocket>
        </request>

        <request subst="true">
            <websocket type="message">{"name":"Websocket"}</websocket>
        </request>

        <for var="i" from="1" to="100" incr="1">
          <thinktime value="10"/>
        </for>
    </session>
  </sessions>
</tsung>
Start:
To start Tsung below is the usage
tsung -f config.xml start
Dashboard:
to see the dashboard in Tsung use URL http://${server_url}:8091/
For example:





Thats it.
Happy Learning
Thanks

Generating Unique Id in Distributed Environment in high Scale:

Recently I was working on a project which requires unique id in a distributed environment which we used as a  primary  key to store in dat...