Akshay Deo

Do(nt) Code Seriously

How to Use Packages Specifically for Debug/Release Builds in Android

Preface

Currently I am working on an Android app for one of the most interesting startups in Fintech. I have been really choosy about the packages that are getting shipped with this app, simply because it involves a lot of money related functionalties. During the development, I came across a requirement that debug builds should have instabug integrated for reporting UI issues easily. APK size matters a lot, so I wanted to achieve this without shipping Instabug SDK in production builds.

How to do this?

Gradle file

...
dependencies {
    ...
    compile appDependencies.rateUs
    compile appDependencies.markdownJ
    debugCompile(appDependencies.instabug) {
        exclude group: 'com.mcxiaoke.volley'
    }
}
...    

I changed the way gradle compiles instabug dependency. Now it’s done during debug builds only. Release builds will not consider this dependency.

How to use this conditional dependency in code?

Create debug and release folders inside the src folder of your app. Create the exact same package structure (same as main folder) in both of these folders. Create a class with overriding nature i.e. same name and methods. Now write add instabug initialization inside the debug flavour, while release flavour won’t have this bit. Update your main Application class to include the initialization of this newly created class.

Application class in main.

    @Override
    public void onCreate() {
        super.onCreate();        
        ...
        SimplAppInitializer.init(this);
        ...
    }

And you are done, now the instabug will be only compiled with the debug builds and not with your production builds.

Let me know if there are some issues/suggestions related to this post. Happy coding \m/

Docker + Golang Web Apps Using Godep on AWS With Elastic Beanstalk Using CodeShip

A lot of things in one title right :D. We are coming up with a small tool for developers that is going to help them to distribute mobile application releases easily, and mainly during development phase. Initially we were using the free dyno provided by Heroku, as staging environment. But as the date of release is approaching, it was time to move onto more scalable (heroku is scalable but becomes a bit costly when you start using it for production purpose) and cheaper infrastructure.

Golang web application structure

My web application is based on slightly customised version of Gin + PostgreSQL at the backend, AngularJS + Bootstrap 3 + Custom CSS for front end. I have been using Godep for dependency management on Heroku. And I feel, this is one of the better ways than creating a bash script with all the go get. If any of the dependencies have introduced some changes that breaks the existing apps, then you are kind of fucked. Godep basically works on the commit id of the code that you have checked out while development.

Note 1: Also a lot of people on the forums suggested me to go for precompiled binary to reduce the size of the docker images. I haven’t given a try to it. You have to use a few ready made tools to create binary for your production environment.

Note 2: If you are not sure which ORM you should use,then give a try to Gorm.

First thing first, Install boot2docker

As you will first test everything locally, you will need boot2docker. And just initialise it with following commands

Initializing boot2docker
1
2
boot2docker init
boot2docker start

Creating a Dockerfile

Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Using wheezy from the official golang docker repo
FROM golang:1.4.1-wheezy

# Setting up working directory
WORKDIR /go/src/<project>
Add . /go/src/<project>/

# Get godeps from main repo
RUN go get github.com/tools/godep

# Restore godep dependencies
RUN godep restore

# Install
RUN go install <project>

# Setting up environment variables
ENV ENV dev

# My web app is running on port 8080 so exposed that port for the world
EXPOSE 8080
EXPOSE
ENTRYPOINT ["/go/bin/<project>"]

Most of the things in this Dockerfile are self explanatory (List of commands).

Golang team is kind enough to create the debian images with the latest Golang versions available. So the first line means that I intend to run this in a container with OS Debian(wheezy) with Golang 1.4.1.

What about the DB connection ?

So in my DEV environment I had a postgres instance running locally. I struggled a bit for allowing my docker container to connect the database on the host but it was taking a lot of time. So for a quick fix, I created a new app on heroku, added a free postgres addon.

Building the repository

So we have a Dockerfile, now the next step is to build the repository for the container and running it. Your working directory is the root of your project.

Building Repository
1
docker build -t <project_name> .

You will see all of the commands that we have added in the Docker file will start executing one by one. At the end if everything goes well you shall see Successfully built ID.

If you see this message then we are all set to deploy project on the cloud. For testing you can publish the repository with following command and test it from local browser

Running built repository
1
docker run --publish 3000:8080 --name <tag-name> --rm <project-name>

Check your docker container ip address using following command

Check container IP address
1
boot2docker ip

And now go to your browser and enter address as http://ip_address:3000 and everything should work.

Setting up continous integration

I went through a couple of CI tools supporting Golang web apps and settled down onto Codeship. (I am evaluating Shippable).

For my Go projects, I don’t keep up with the regular naming conventions i.e. github.com/rainingclouds/project_name. So I had to work a bit on the test scripts as Codeship assumes you are following the naming conventions. (The reason I don’t follow it because, the project is not supposed to be reused as it is by anyone else :))

Codeship Script
1
2
3
4
5
mv ../distribute ${HOME}/src/distribute
cd ${HOME}/src/distribute
go get github.com/tools/godep
godep restore
go get

And then you can put your testing commands. I am not using any framework for writing tests. Default testing package is more than enough for me at this stage.

Codeship Test Script
1
go test <module_to_test> -v

Setting up the deployment hook

Codeship comes up with Elastic beanstalk deployment hook. For that you have to configure a few things before you can use it.

Configuring ElasticBeanstalk environment

Create IAM user for codeship

I keep on using different users for these different services, So that we can manage the access independently. I created a user with all access for Elastic Beanstalk. There is a ready template for this in AWS IAM panel.

Create a new application

Create a new application in elastic beanstalk and don’t select a demo application option. You will see a Create Application button on right top of the panel. For this you will have to upload a zip of your source code manually.

Note : I am also using ELB in front of my EC2 instances. For https, I am doing the SSL termination at LB level, and then redirecting it to my Golang application.

Create a new environment

Create a new environment inside your new application.

Configure codeship deployment hook

Once you have configured AWS, go to codeship and put all the information in AWS Beanstalk deployment settings page.

Done

If all goes well then the entire pipeline is ready. For testing make a small change and push it to your configured branch. You will see that the new task in the codeship with the exact status of it :).

Map to Struct in Golang

Recently I was working with a piece of code where I had to convert a JSON into a Golang struct. I faced hell lot of issues, and waster a bit of time in achieving that, so thought of documenting it.

Scenario

I have a struct called User, which has fields. There were no issues with primary data types, but when it came to time.Time, it started fucking me.

User model
1
2
3
4
5
6
7
8
9
10
11
12
  Id      int    `json:"user_id"`
  AuthKey string `json:"-" sql:"not null;unique"`

  Name         string `json:"user_name" sql:"not null;unique"`
  EmailAddress string `json:"email_address" sql:"not null;unique"`
  Password     string `json:"-" sql:"not null"`

  AccountId int `json:"account_id" sql:"not null;unique"`

  CreatedAt time.Time `json:"created_at"`
  UpdatedAt time.Time `json:"updated_at"`
  DeletedAt time.Time `json:"deleted_at"`

A bit of googling landed me onto this repo : http://github.com/ottemo/mapstructure. This had everything I wanted. (Prefer this repo over https://github.com/mitchellh/mapstructure as ottemo/mapstructure has an AdvancedDecodeHook that helps a bit in this situation)

Solution

Suppose that body has the entire JSON. It has some fields along with User struct with user as key.

  • First we will convert the JSON into a map[string]interface{}
JSON to map[string]interface{}
1
2
3
4
5
  var data map[string]interface{}
  err = json.Unmarshal(body, &data)
  if err != nil {
    return nil, err
  }
  • Okay so data contains our JSON, now as we have a custom fields time.Time we will have to write our own decoder, which means create on AdvancedDecodeHook func and pass it to the DecoderConfig
AdvancedDecodeHook
1
2
3
4
5
6
7
8
func myDecoder(val *reflect.Value, data interface{}) (interface{}, error) {
  if val.Type().String() == "time.Time" {
    value, err := time.Parse(time.RFC3339Nano, data.(string))
    val.Set(reflect.ValueOf(value))
    return nil, err
  }
  return data, nil
}

So lets understand the structure of this hook - You get two parameters:

  • val => indicates type of the data
  • data => value of that data

This function returns an interface and an error. Now this AdvancedDecodeHook works in a way that if you return nil in place of interface, it the decoder assumes that our custom decoder has parsed the value for the given data, and it leaves it in that way. In case of error the entire parsing fails. So you have to decide on whether to throw the error or set a default value when parsing fails.

  • And now just write a function to return the decoder with our custom configuration
DecoderFactory
1
2
3
4
5
6
7
func getDecoder(result interface{}) (*mapstructure.Decoder, error) {
  return mapstructure.NewDecoder(&mapstructure.DecoderConfig{
    AdvancedDecodeHook: myDecoder,
    TagName:            "json",
    Result:             result,
    WeaklyTypedInput:   false})
}

The parameter that we pass in is the result type we expect. This has to be the pointer to the struct.

  • And finally call it from your routine
Decode the JSON
1
2
3
4
5
6
7
8
  decoder, err := getDecoder(&user)
  if err != nil {
    return nil, err
  }
  err = decoder.Decode(data["user"])
  if err != nil {
    return nil, err
  }
  • And you are done.

Final code

So the final code looks like this

Converting JSON to Struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
func myDecoder(val *reflect.Value, data interface{}) (interface{}, error) {
  if val.Type().String() == "time.Time" {
    value, err := time.Parse(time.RFC3339Nano, data.(string))
    val.Set(reflect.ValueOf(value))
    return nil, err
  }
  return data, nil
}

func getDecoder(result interface{}) (*mapstructure.Decoder, error) {
  return mapstructure.NewDecoder(&mapstructure.DecoderConfig{
    AdvancedDecodeHook: myDecoder,
    TagName:            "json",
    Result:             result,
    WeaklyTypedInput:   false})
}

func GetUserFromJSON(jsonUser string) (*User,error){
  var data map[string]interface{}
  err = json.Unmarshal(jsonUser, &data)
  if err != nil {
    return nil, err
  }
  decoder, err := getDecoder(&user)
  if err != nil {
    return nil, err
  }
  err = decoder.Decode(data["user"])
  if err != nil {
    return nil, err
  }
  return &user,nil
}

Thank you guys, keep coding :)

Writing Android App With Kotlin

When I read the description about kotlin, I thought of it as a very beautiful port of Java (as its a JVM based language and 100% interoperable with Java). I have been trying to like Scala for a lot time now, but hell lot of options for everything, complex semantics makes the code unreadable and confusing (for me). But kotlin, according to me is a decisive approach towards making Java more functional and more compact for developers.

The first thing I searched when I read about Kotlin was, about Android support. Scaloid being more and more popular and I being completely not in love with Scala, this was going to be an interesting thing for me. Kotlin does have support for Android and they do have amazing documentation about both language as well as Android support, So I went ahead and tried one simple list app with an adapter and a fragment, just to simulate a simple part of general Android application.

Setup

  • Install Kotlin plugin available in Preferences for Android Studio.
  • Add following classpath to the gradle script

    classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:0.9.206”
    

  • And add the dependency in the application gradle script

    compile “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version”
    

The Application

I started with writing a HackerNews listing app. But then restricted it to simulating the API call with a stub so as to focus onto the Kotlin more than the actual functionality of the application.

Code

Let’s dive into some code.

Home

It’s the main activity (launcher) of the application.

Home Activity
1
2
3
4
5
6
7
8
9
10
11
12
open class Home(): ActionBarActivity(){

    val logTag = "###Home###"

    override fun onCreate(savedInstanceState: android.os.Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        val transaction = getSupportFragmentManager().beginTransaction()
        transaction.replace(R.id.posts_container, PostListFragment.Maker.Create())
        transaction.commit()
    }
}

Things I loved

  • Kotlin has val, var keywords to separate out the final and variables (if we compare it with Java). I personally feel that val and var are the best ways to indicate that if a member is final or not.
  • Nullable is always appended with a ‘?’. So this is again a very informative way of indicating the possible exceptions that can happen when you are working with a particular member or parameter.

PostListFragment

This is the fragment that contains list of posts. It’s a DialogFragment, so as to make it available to be used as a dialog as well.

Post List Fragment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
open class PostListFragment() : DialogFragment() {
    val logTag = "###PostListFragment###"
    var postList: ListView? = null
    var baseView: View? = null
    var postsAdapter: PostListAdapter? = null
    var handler: Handler? = null

    object Maker {
        fun Create(): PostListFragment {
            return PostListFragment()
        }
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        baseView = inflater?.inflate(R.layout.fragment_post_list, container, false)
        postList = baseView?.findViewById(R.id.post_list) as ListView
        postsAdapter = PostListAdapter(getActivity(), postList as AbsListView)
        postList.setAdapter(postsAdapter)
        handler = Handler()
        return baseView
    }

    override fun onResume() {
        super.onResume()
        Post.Maker.GetList {
            Log.d(logTag,"Testing get list =&gt;" + it.get(0).post)
            postsAdapter?.postList?.addAll(it)
            handler?.post {
                this.postsAdapter?.notifyDataSetChanged()
            }
        }
    }
}

Things I loved

  • The biggest advantage Kotlin gives you is passing functions as parameter (Functions are first class citizens). And this helps us to remove all those interfaces we end up creating to circumvent this problem in Java. (Look at the Post.Maker.GetList)
  • In Kotlin, you have to write all the static methods separately inside an object. Most of the times the static methods are factory methods, and this helped me to make the code more readable. So I call each of the object inside the class as Maker, and write different factory method inside it. This is completely possible with Java by writing inner classes, but this being the semantics of the language, forces us to write the factory methods like this.

Things I hated

  • ‘?’ comes really handy to avoid null pointer exceptions. But I feel that also is a problematic. In most of the cases during development, null pointer exceptions help us to fix issues quickly. Now as this was a small skit, I never faced any null pointer exception but while writing some complex code this could make a few things really tricky.

Post
This is the model class containing info related with each post.

Post Model
1
2
3
4
5
6
7
8
9
10
11
data class Post(val id: Long, val post: String, val by: String, val postedAt: Date) {
    object Maker {
        fun GetList(onSuccess: (posts: List) -&gt; Unit) {
            val posts = ArrayList()
            for (a in 1..10) {
                posts.add(Post(a.toLong(), "Test", "Akshay", Date()))
            }
            onSuccess(posts)
        }
    }
}

Things I loved

  • Making class as a data class makes it easy to debug. Using instance of this class in logs prints the values of each of the field of the class in readable format. I feel this is one of the best thing. This can be done in Java by overriding toString() method.
  • The way you define members of the class, is much much compact and better. Now my model classes will just have the Maker which will have the factory methods.
  • Functions are first class citizens! Look at the way GetList is coded. It accepts a function, that takes up List of Post as param and returns nothing. (Unit in Kotlin means nothing). So forget all the interfaces we used to write for circumventing the problem of passing functions as arguments.

PostListAdapter

This is the list adapter for post list view.

List Adapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
open class PostListAdapter(val context:Context,val listView: AbsListView): BaseAdapter(){

    private val logTag: String = "###PostListAdapter###"
    var postList = ArrayList()

    override fun getCount(): Int {
        return postList.size
    }

    override fun getItem(position: Int): Any? {
        if(position &gt;= postList.size)
            return null
        return postList.get(position)
    }

    override fun getItemId(p0: Int): Long {
        if(p0 &gt;= postList.size)
            return -1
        return postList.get(p0).id
    }

    override fun getView(position: Int, view: View?, parent: ViewGroup): View? {
        var cachedItem: ItemCache?
        var contentView: View ? = view
        when (contentView) {
            null -&gt; {
                contentView = LayoutInflater.from(context).inflate(R.layout.layout_post_item, parent, false)
                cachedItem = ItemCache(textView = contentView?.findViewById(R.id.post) as TextView)
                contentView?.setTag(cachedItem)
            }
            else -&gt; {
                cachedItem = contentView?.getTag() as ItemCache
            }
        }
        val currentPost = getItem(position) as Post
        cachedItem?.textView?.setText(currentPost.post)
        return contentView
    }

    class ItemCache(val textView: TextView?)
}

This all works well. The source code is available at https://github.com/akshaydeo/kotlin_android.

Some analysis of APK

  • The very very important thing to look at is, the number of classes inside your APK. Don’t worry, its gonna be huge that the APK you will create with same functionality using JAVA. For example the application that I have created contains 3561 classes. (I am planning to write the same app using Java and compare the results with Kotlin app). So running proguard during the debug builds is very essential in this case.
  • Also I faced gradle crashing problem because of the default VM params. So you will have to change them using preferences. I changed them to
    -XX:MaxPermSize=1024m -XX:MaxHeapSize=256m -Xmx256m
    

Important Links for Kotlin

I will be updating this post as I progress in analysing the side effects of using Kotlin for Android development.

Creating .keystore From .cert and .key

A quick not for creating .keystore using a set of certificates including root, intermediate and domain certs.

First create a .p12 using private key and domain certificate.
openssl pkcs12 -export -in <domain_cert> -inkey <private_key> -certfile <domain_cert> -name "<name>" -out <p12_name>.p12

Create keystore file using
keytool -importkeystore -destkeystore <keystore_name>.keystore -srckeystore <p12_name>.p12 -srcstoretype pkcs12 -alias <alias_name>

Then import the other certificates using
keytool -import -trustcacerts -alias root -file <cert_file> -keystore <keystore_name>.keystore

Limits (Mathamatics)

[Warning: These are more like my notes which I make public so that if at all there are some stupid people like me, these would come handy]

Mathematical definition:
A limit is the value that a function or sequence “approaches” as the input or index approaches some value.[1]

Limit of function

Mathematical Representation:

<br /> lim ƒ(x) = L<br /> x->c<br />

This says that L is the value of function ƒ(x) when x reaches to value c.

There is one more definition which is kind of logical evidence to this is L is the limit of function ƒ(x) with ε as a small positive real number then value of ƒ(x) lies in (L-ε,L+ε) or |ƒ(x) – L| < ε

Reallife examples:
I found this amazing question

Adding the most upvoted example here

The reading of your speedometer (e.g., 85 km/h) is a limit in the real world. Maybe you think speed is speed, why not 85 km/h. But in fact your speed is changing continuously during time, and the only “solid”, i.e., “limitless” data you have is that it took you exactly 2 hours to drive the 150 km from A to B. The figure your speedometer gives you is at each instant t0 of your travel the limit

<br /> v(t0):= lim (t0)−s(t0−Δt)/Δt<br /> Δt→0s<br />

where s(t) denotes the distance travelled up to time t.

And the most simple to understand (for me)

If I keep tossing a coin as long as it takes, how likely am I to never toss a head?

Possibility of getting a head is (½) if I toss the coin once.

Now if I toss N times P(N) = (½)N
Now this probability would be zero if N is ∞.

Mathematical representation of the question is

P(N) = (½)N find N for which P(N) = 0

and Answer to the given question is
<br /> lim P(N)<br /> N->∞<br />

setOnItemClickListener() Is Not Working…and Its’ Because Your Layout Structure Is Wrong

This is gonna be a quick post. I spent like last 45 mins solving a very dumb issue, related to setOnItemClickListener of a custom class extending GridView.

Layout of GridView item:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="@dimen/fragment_application_grid_item_width"
                android:layout_height="@dimen/fragment_application_grid_item_height"
                android:background="@drawable/fragment_application_list_item_background_drawable"
                android:orientation="vertical">

        



        <View
            android:id="@+id/application_price_indicator"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.05"
            android:background="@drawable/fragment_application_grid_item_free_indicator"/>

        <ImageButton
            android:id="@+id/add_to_list_menu"
            android:layout_width="0dp"
            android:layout_height="@dimen/fragment_application_grid_item_menu_dimen"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:src="@drawable/fragment_application_grid_item_add_to_list_icon"/>

        <ImageButton
            android:id="@+id/install_app_icon"
            android:layout_width="0dp"
            android:layout_height="@dimen/fragment_application_grid_item_menu_dimen"
            android:background="@android:color/transparent"
            android:layout_weight="1"
            android:src="@drawable/fragment_application_grid_item_install_icon"/>

        <ImageButton
            android:id="@+id/share_app_icon"
            android:layout_width="0dp"
            android:layout_height="@dimen/fragment_application_grid_item_menu_dimen"
            android:background="@android:color/transparent"
            android:layout_weight="1"
            android:src="@drawable/fragment_application_grid_item_share_icon"/>

    </LinearLayout>

</RelativeLayout>

The issue here is, ImageButton is the part of this view. So whenever onClick event happens, Android system will try to find onClickListener related to ImageButtons in the view. So There are only two solutions to this :

  1. Either change ImageButton to ImageView
  2. Override onTouch of ImageButton and return False.

Why BATTERY_STATS Permission Is Not Gonna Work in KITKAT

I am working on an internal project which requires battery stats. A quick search would guide you to use android.permission.BATTERY_STATS, and you would be able to access battery stats directly. But you would notice that this code wont work in KITKAT.

Why ?

If you are coding in Android you must be familiar with the concept of permission group. Every of the android.permission.* is associated with a permissions group. And each of these permission groups have a protection level associated with them. There are six types of the protection levels.

Constant Value Description
normal A lower-risk permission that gives an application access to isolated
application-level features, with minimal risk to other applications,
the system, or the user. The system automatically grants this type
of permission to a requesting application at installation, without
asking for the user’s explicit approval (though the user always
has the option to review these permissions before installing).
dangerous 1 A higher-risk permission that would give a requesting application
access to private user data or control over the device that can
negatively impact the user. Because this type of permission
introduces potential risk, the system may not automatically
grant it to the requesting application. For example, any dangerous
permissions requested by an application may be displayed to the
user and require confirmation before proceeding, or some other
approach may be taken to avoid the user automatically allowing
the use of such facilities.
signature 2 A permission that the system is to grant only if the requesting
application is signed with the same certificate as the application
that declared the permission. If the certificates match, the system
automatically grants the permission without notifying the user or
asking for the user’s explicit approval.
signatureOrSystem 3 A permission that the system is to grant only to packages in the
Android system image or that are signed with the same
certificates. Please avoid using this option, as the
signature protection level should be sufficient for most needs and
works regardless of exactly where applications are installed. This
permission is used for certain special situations where multiple
vendors have applications built in to a system image which need
to share specific features explicitly because they are being built
together.
system 0x10 Additional flag from base permission type: this permission can also
be granted to any applications installed on the system image.
Please avoid using this option, as the
signature protection level should be sufficient for most needs and
works regardless of exactly where applications are installed. This
permission flag is used for certain special situations where multiple
vendors have applications built in to a system image which need
to share specific features explicitly because they are being built
together.
development 0x20 Additional flag from base permission type: this permission can also
(optionally) be granted to development applications.

Earlier BATTERY_STATS was having protection level as dangerous but from kitkat they have changed it to signature/system so unless your app is running as system app or your app is signed with same certificate as system apps, your app wont be able to access battery stats !!

Have a good day coders :)

Consumer Producer Implementation With Golang

Today I invested my whole day in evaluating GoLang for one of our component implementations. Since the first document, I got impressed by it’s construct. Till I reached the ultimate problem solution for our case, I fell in love with this new language. It gives you the power of OS level constructs keeping a lot of overheads hidden from you. I was playing with a lot of simple problems while reading the documentation and here I am sharing one of those implementation and will discuss how simple it is to implement pretty complex problems with GoLang.

Classic Consumer-Producer problem

You can find some classic implemetations here.

GoLang implementation:

package main

import (
    "fmt"
    "strconv" // added for giving proper name to job
    "time"    // added for delaying consecutive inserts into job queue
)

func main() {

    jobQueue := make(chan string, 200)
    done := make(chan bool,1)
    // consumer
    go func() {
        for {
            job := <-jobQueue
            fmt.Println("Got job:", job)
        }
        done <- true
    }()
    // producer
    go func() {
        i := int64(0)
        for {
            jobQueue <- strconv.FormatInt(i, 10)
            time.Sleep(time.Second * 5) // to wait for 5 seconds before adding one more job
            i++
        }
        done <- true
    }()
    <-done
}

Components

Channels
Channels are the basic inter go-routine communication model. If you are a Java developer you can consider it as array blocking queue, which is mostly used for inter-thread communication.

Go routine
Go routines are the lightweight Thread implementation for Go.

You can learn Go with some amazing examples here. Initial impressions of Go are pretty good. Will share more info as we discover it during our implementations.

Be Careful While Using Executors.newCachedThreadPool()

I am currently writing a scalable backend server for AppSurfer, which does routing work for different components associated with a session. Recently I went into a trouble with the new version of the server. Even for a single connection CPU load was almost 100%. I was clueless about the issue and started digging into it.

My setup

  • netty4x using our home cooked wrapper Transporter.
  • JDK with latest update number 40.
  • CentOS.

CaseStudy

We at AppSurfer solve a very tricky problem on our technology side. Our servers need to respond almost real time with minimilstic load on the server as it might affect other sessions. For better scaling we shifted onto netty (i.e nio) from standard io. It really helped us to improve overall response time as well as efficient usage of resources. But during shift we faced one critical issue related to 100% CPU usage. Some initial googling introduced me to this JVM issue. But this issue was tackled very well in netty3x as well as netty 4x. Then I started digging into this issue more.

CPU usage shown by HTOP command :
Screenshot from 2013-09-28 21:06:24
CPU usage shown by TOP command :
Screenshot from 2013-09-28 21:12:48

I was clueless about what was going wrong after adding all the fixed related to nio. Then yourkit output indicated one strange thing related to garbage collection. There was a lot of garbage collection happening during session runs. Then digging into it more revealed that the main issue was related with Executors.newCachedThreadPool().

Garbage collection output:
Screenshot from 2013-10-06 23:36:15

What I read about newCachedThreadPool in Java documentation was:

“Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.”

But I missed one of the important crux of this line. If no existing thread is available, a new thread will be created and added to the pool. This means if my tasks are getting created at a very faster rate for lets say 2-3 mins (and in our case its throughout the session run), you will end up with thousands of threads created and destroyed (to be precise during our single session for 50 sec, number of threads shown by yourkit was around 10500). I always knew that, in our case there are going to be a lot of short duration tasks will be submitted to the workers but in the java documentation they have mentioned that These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. But what they mean by that is, when you know the number of tasks are under control and they not virtually concurrently submitted.

Solution

We wrote our custom thread pool executor to control the number of threads getting spawned at the runtime, and now cpu usage is like 1% or 2% throughtout the lifetime.

public static ThreadPoolExecutor get(final int coreSize, final int maxSize, final int idleTimeout,
final TimeUnit timeUnit, final int queueSize, final String namePrefix) {
        return new EfficientThreadPoolExecutor(coreSize, // core size
                maxSize, // max size
                idleTimeout, // idle timeout
                timeUnit,
                new ArrayBlockingQueue(queueSize), // queue with a size
                new PriorityThreadFactory(namePrefix, Thread.NORM_PRIORITY));
    }

Thanks to Norman Maurer, most active contributor of netty, for helping me out in solving this issue.