Sunday, 21 April 2019

Coroutines or Java Fibers in Project Loom

Project Loom introduces lightweight concurrency construct in Java. The main purpose or goal of Project Loom is to support a high-throughput, lightweight concurrency model in Java.
Problems with the current Java Concurrency Model:
We already knew that the current java concurrency model uses operating system kernel level threads. let’s see some problems with the current model.

Problem 1: Waiting of threads or Blocking of threads.


We already knew that if the thread blocks the till that time thread is blocked due to I/O operations, we are not using CPU or CPU cycles are getting wastes so we are not using CPU efficiently.
Let’s say we start creating more no of threads to solve the above problems, for example, if some threads are blocked we can spawn similar no threads so that CPU not idle and we can use CPU cycle efficiently, But it creates another problem.

Problem 2: Creation of more threads can cause JVM Crash (Out of memory):


The creation of a thread takes time and takes approximately 1MB of memory. so we start creating more threads it can cause JVM Crash depends upon the memory given to JVM.

How can we solve the above two problems:

There are two we can solve the above problems:
  1. Reactive Programming:
Reactive programming works under the concepts of the event loop which never get blocked or not supposed to do any blocking operation of on the event loop. It uses event queues so any I/O operation happens it gets pushed to queue(callbacks get pushed to queue) so that main thread not get blocked and whenever thread gets the response and gets executed by main thread or event loop.



2. Using Coroutines: 
It introduces user level or mode threads which rely on Java runtime implementation of continuations and schedulers instead of the OS implementation.
Currently, java implementation relies on kernel level schedulers, continuation, and threads. So let’s say if the thread is blocked due to some I/O operation in order to suspend a continuation, it’s required to store the entire call-stack. And similarly, retrieve the call-stack on resumption. 
Java doesn’t have controls over threads CPU treats all the request in the same manner, and hence we cannot control so it is not certain that same request goes to the same CPU this is very less probable. 
To get control over threads scheduling we required Application level or user level threads, continuation (aka task) and scheduler.
Fibers Schedulers:
  • To achieve this Schedulers is required which can control over fibers and schedules task in OS level threads 
  • Takes care of mount and unmount of task (due to I/O) operation.
  • During I/O operation continue with other tasks
  • Continue with the same task where it left during I/O operation.




Advantages over Threads:

  1. Very LightWeight 
  2. Takes only a few Kb of the stack as compare to Thread (takes around 1MB)
  3. Can run millions of fibers in an application.
  4. Servers can handle more no of concurrent connections.
  5. Efficient utilization of CPU

Happy Learning.
Thanks 






Sunday, 7 April 2019

How to increase the port range in Linux


Lots of time we required a lot of opening connections from Linux servers like for Load or performance testing or benchmarking of servers. To achieve this we might need to utilize the utmost capacity of the servers. To utilize utmost from the server we might need to modify some of the kernel parameters of the server.
The default range is less, we might need to increase the range. Using sysctlutility or command we can modify kernel parameters.

Check the current port range:

Below commands, we can use to check the current port range.
cat  /proc/sys/net/ipv4/ip_local_port_range
                   or 
 sysctl net.ipv4.ip_local_port_range
OUTPUT:
net.ipv4.ip_local_port_range = 2028   32768

Set the new port range:

As we know that default ports range that is reserved for the kernel is 0–1023, and total no of ports in the kernel is 2¹⁶ ==65536 so we can use maximum from 1024 to 65535. Let’s see how can we set.
echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range
               ORsudo sysctl -w net.ipv4.ip_local_port_range="1024 65535"
Note: To set this range or modify kernel parameters you should have root privileges.
To make a permanent change in the server update or add net.ipv4.ip_local_port_range = 1024 65535 this to /etc/sysctl.conf.
We can use ss or netstat command to validate or check all active connections.
That’s it.
Thanks and Happy Learning.

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...