Sunday, July 2, 2017

Do you ssh too often and to many different machines?

It's been a long time, since I last wrote a post. And this is going to be a short one.
At my recent job, we have all these test environments (about 3500 of them) that have the same username and password.
I kind of got tired typing the username and password every time I had to login to a machine to test out something.

Well a keyless access would definitely have made things easier, but you can understand why you couldn't have keyless access to 3500 environments, each running the risk of being phoenixed anytime.

I created a small expect script to get around the problem. Expect is a bash utility, that helps provide inputs in an automated way for scripts that expect user inputs. You may need to install it.

On a OsX machine, you can simply do brew install expect
#!/usr/bin/expect
set user ""
set ip [lindex $argv 0]
set password ""
spawn ssh $user@$ip
expect "password"
send "$password\r"
interact

The interact on last line opens an interactive ssh shell.

Now, you can save this with a .exp extension, give an executable permission to it. Setup an alias, so that your newssh command lands to this, and you are all set.
This has made me save a couple of seconds on every login. And now I am wondering why did I wait for so many months before doing this :)

Monday, December 5, 2016

10 Intellij Idea Must Know Shortcuts on Ubuntu

  1. Optimizing imports - Ctrl + Alt+ O
  2. Auto Imports - Alt+enter
  3. System.out.println - type sysout, then press tab
  4. Open a file - Ctrl+N
  5. GoTo Function in class - Ctrl + F12
  6. Auto Indent - Ctrl + Alt + I
  7. Basic Code Completion - Ctrl + Space
  8. Comment a line of code - Ctrl + /
    Uncomment a line of code - Ctrl + Shift + /
  9. Duplicate the current line - Ctrl + D
  10. Finally, Find any action by name - Ctrl + A

Monday, October 24, 2016

Mount a Remote system as a drive on OSx using SSH

Recently I ran into a situation where I needed to mount a remote system folder on my OSx for ease of sharing and convenience. You can totally do without this by just using the terminal. However, writing this hoping to help those who need to do this for one or the other reason.

1. Install Fuse and SSHFS

Download FUSE and SSHFS from OSx Fuse site. Follow the instructions to install Fuse, followed by SSHFS.

2. Create a folder on your mac. This will be the mount point of the remote folder.

anjanas:~ $ ls
Applications Library  Public  id_rsa
Desktop  Movies  PycharmProjects test.txt
Documents Music  dev-vm-mount workspace
Downloads Pictures dwhelper
anjanas:~ $ mkdir mount
anjanasblrmbp:~ $ ls
Applications Library  Public  id_rsa
Desktop  Movies  PycharmProjects mount
Documents Music  dev-vm-mount test.txt
Downloads Pictures dwhelper workspace

3. Create a keyless access to your server

Refer this link for the same.

4. Mount the remote system as a folder on OSx

sshfs -o IdentityFile=~/.ssh/id_rsa anjana@remote.ip:/home/anjana/workspace ~/mount

Now you can treat the remote folder as any other folder on your machine.

Unmounting this system can be achieved as follows:
sudo diskutil umount force ~/mount

That's it for this post.
References: Mount a remote system as a drive on Mac OSx

Thursday, March 31, 2016

Accessing Solr Cloud on AWS from SolrJ

We have a Solr cloud installation on AWS EC2 instances. We use the SolrJ Client from our Java application. Till date we used to have a Solr Cloud installation on our local machine in order to test the code against. As the team started growing, we realized that we should have a way to access the AWS Solr Cloud from our local boxes, so that the Solr Cloud setup on local is not a blocker for feature development.

When I started looking around the web for a solution to this issue, I had to mix a few things from a few different documentation pages and this stackoverflow page. Decided to write this blog post to help out others who are running into the same issue. I hope it helps.

So first let's try to understand the reason behind why you get the java.net.UnknownHostException from the SolrJ client.
When the solr cloud is run, each instance registers itself with the zookeeper that is running. This is done with the private IP of the solr machine. So when SolrJ connects to zookeeper, it gets the private IP. And then SolrJ tries to hit the Solr instance with the private IP, leading to an unknown host exception.

In order to get around this you can follow these steps:
Step 1: Run the solr instance with a host name. Zookeeper will use this hostname.
./bin/solr start -cloud -h hostname -p 8985 -z localhost:2181

Step 2: Add the host entry to the /etc/hosts file on your local machine corresponding to the public ip of the solr machine.

And you are done. You can run the java application that uses SolrJ client on your local machine and access the Solr Cloud running on your AWS instance :)

Friday, December 18, 2015

Managing Multiple Github Accounts from a single machine

Recently, I ran into a situation where I needed to manage multiple Github accounts from a single computer. I have two github account, one related to work and the other for my personal repositories.
It is a very simple combination of ssh and git config. Before I start explaining the steps for the same, let me clarify that the following steps are meant for LINUX/UNIX users.

1. Set up SSH keys


You will need to set up two different ssh keys for the two accounts.
$ ssh-keygen -t rsa -b 4096 -C "work.emailid"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/****/.ssh/id_rsa):       
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/anjana/.ssh/id_rsa.
Your public key has been saved in /home/anjana/.ssh/id_rsa.pub.

$ssh-keygen -t rsa -b 4096 -C "personal.emailid"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/****/.ssh/id_rsa): /home/****/.ssh/id_rsa_personal       
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/anjana/.ssh/id_rsa_personal.
Your public key has been saved in /home/anjana/.ssh/id_rsa_personal.pub.

Make sure that when prompted for the filename when generating the ssh keys for the second account, you give the appropriate filename.

2. Add the ssh key to your github account


> On your github account, Go to your Account Settings
> Click "SSH Keys" then "Add SSH key"
> Paste your key into the "Key" field and add a relevant title
> Click "Add key" then enter your Github password to confirm
> Repeat this step for your other account with the appropriate keys.

3. Create a ssh configuration file to manage the two separate keys

$ touch ~/.ssh/config
Add the following in your ~/.ssh/config file.
# Default GitHub
Host personal-github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal

# Work GitHub
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

Once you save this file, you need to configure your git repos on local accordingly.

4. Setup your repo


Replace "git@github.com" part in the remote origin of all your personal repositories to "git@personal-github.com"
And that's it. From now on, the appropriate user account will be used for pushing to these repositories.

Monday, October 26, 2015

Interfaces in Java8

Interfaces are a key feature of object oriented programming. An interface provides a set of methods that an implementing class must provide. One can assign instances of the class to variables of the interface type. As of Java8, an interface can contain default methods that an implementing class can inherit or override. Default methods (also known as Defender Methods) enable us to add new functionalities to interfaces without breaking the classes that implements the interface.


1. Declaring an interface

Interfaces are declared by specifying a keyword "interface"


2. Implementing an interface

The class implementing the interface has to provide the implementation of all the methods in the interface


The @Override annotation tells the compiler that this method is inherited from the interface.

Output



3. Default methods in Interface

Java 8 onwards, you can provide default implementation of methods in interfaces. If the class implementing the interface does not provide an implementation of the method, then the default implementation is used.


Let's change the main() method to call the newMethod. Observe that the interface implementation has not changed.



4. Using Default Methods or Abstract Classes

After the introduction of default methods, it may seem that there are no differences between abstract classes, and interfaces. However, it is not so. Abstract classes can define constructor. They can have a state associated with them. In contrast, default methods can only be implemented in terms of invoking other interface methods, with no reference to a particular implementation's state.

5. Default methods and Multiple inheritance ambiguity

A Java class can implement multiple interfaces. Each interface can define default method with same method signature, therefore, the methods can conflict with each other.

Let's consider an example:
Declare two interfaces with default methods implemented with the same method signature.




The above code will give a compilation error:
java: class InterfaceImpl inherits unrelated defaults for method() from types InterfaceA and InterfaceB

We need to provide an implementation of the default method in order to resolve this conflict.



Summary

To summarise, default methods enable addition of new functionality without breaking existing implementations.
When we extend an interface containing the default method, we can do one of the following:
1. Not override the default method and the implementing class will inherit the default method.
2. Override the default method similar to other methods we override in subclass.
3. Redeclare default method as abstract, which forces the subclass to override it.

Reference: Java Documentation and D Zone

Thursday, September 3, 2015

Database Indexing Basics

Database Indexing allows us to cut down the number of rows/records that need to be examined when a select query with a where clause is executed.
Let's try to understand what happens when a select query is executed on a database table without an index.
Suppose we have an Employee table, with fields 'Employee_Id' and 'Employee_Name'. We want to execute the following select query on this table:


When this query is executed, what goes on behind the scenes? Every single row is checked to see if the employee_name matches with 'abc'. Which effectively means that the entire table will have to be scanned. This is known as full table scan.

An index helps us avoid a full table scan. It is a data structure, that stores the values for a certain specific column of a table. Let's look at the common data structures that are used for database indexing.

Data Structures for Indexing


1. B-trees
B-trees are the most commonly used data structures for indexes. As they are time-efficient for lookups, deletions and insertions. All these operations can be done in logarithmic time. Also, the data being stored inside B-tree can be sorted.

2. Hash Tables
The indexes that use hash tables are generally referred to as hash index. Hash tables are extremely efficient for looking up values. So queries that look for an exact match can be executed very fast. The way a hash index works is that the key is the column value, and the value in the hash table is a pointer to the row data in the table. However, hash tables are not sorted data structures. And hence, for other type of queries, they may not be efficient.

3. R-tree
This is commonly used with spatial databases. It helps with queries like "find all the coffee shops within 2 kilometers of my location".

4. Bitmap Index
Works for columns that have many instances of those values, that is columns with low selectivity. For example, a column with boolean values.

So, what is the cost of having a database index?
The first thing is the index takes additional space, and the larger your table, the larger is your index. Every time you perform an add, delete or update operation, the same operation needs to be performed on the index as well.

Creating an index
The following snippet shows how to create an index on a single column and multiple columns.


As a general rule, an index should only be created on a table if the data in the indexed column will be queried frequently.

References:
http://www.programmerinterview.com/index.php/database-sql/what-is-an-index/