Mobil Mewah

Salah satu sumber Inspirasi.

Mobil Sport terbaik

Anda pasti bisa memilikinya.

Bermain dengan pesawat

Salah satu ide yang gila, Balapan di udara.

Bermain di angkasa

Apakah ini salah satu Goals dalam hidup anda? anda pasti bisa mencapainya

Tuesday, 12 September 2017

Javascript promise error handling

Javascript promise error handling
On web application, the standard will have javascript code. Especially with ajax call method and event driven application design.

One of my experience just now was with using promise in javascript. It not catch the error status code returned from the server backend, but in debug mode in the browser status returned was 401 or unauthorized. I using Marionette JS as my development framework and my backend using Chicagoboss.

So the story was that user need to login before access the API. When request coming without authorization code, it will return 401 status code. My backend API try to be Restfull. And i want my web application automatically cacth the 401 error globally and will show login form when 401 status code encountered within the app run time. So the code was like below :

            var defer = $.Deferred();
            options || (options = {});
            options.reset = true;
            defer.then(options.success, options.error);
            var response = data.fetch(_.omit(options, 'success', 'error'));
            response.done(function(){
                defer.resolveWith(response, [edonationboxes]);
            });
            response.fail(function(response){
                defer.reject(response);
            });

And i have the ajax setup when the webapp start to detect the 401 globally.

    $.ajaxSetup({
        statusCode: {
            401: function(){
                // Redirec the to the login page.
                window.location.replace('#login');
            },
            403: function() {
                // 403 -- Access denied
                window.location.replace('/#denied');
            }
        },
    });

So the problem is when i refresh page, it correctly detect the 401 and login page shown. But when application run and the request triggered within the app, by accessing a menu, it not cacthing the 401 error.

When debugging, the statusCode was 0. So google for a while, seems issue with the API backend which not returning correct headers for CORS request. So when fixed the app behave correctly. Login page show when 401 detected.

So make sure in the API response header return this : access-control-allow-origin:* 
With javascript promise, it wont behave correctly if CORS involved and not set correctly, especially in SPA web application.

Hope this helps someone, as i have debug this almost 3 hours.

Saturday, 29 July 2017

Update lates gcc in centos 6.x

Opensource is great as we can compile it from source by our self. it just a command to be used like configure , make and make install. You got your new software ready to use.

But sometimes when compiling, some errors can happend and the cause is the compiler is outdated, especially if you want to try new software, and the software constantly updated with new technology being used especially the complier.

So we must upgrade our complier like gcc which is the default compiler in linux world.

My experience also show error when compiling, when using Centos 6.x , the gcc still in version 4.4.7 .
We will update our gcc in centos 6.x by following these steps :

# yum install -y centos-release-scl

# yum install -y devtoolset-3-toolchain

# scl enable devtoolset-3 bash

# gcc --version

With this you will got gcc 4.9.2 ready to use for compiling you source code.


Saturday, 12 November 2016

CouchDB test performance using httperf

After test an erlang framework to serve JSON API from a postgreSQL database, i remember that had installed CouchDB in my laptop, and why not use it with same data and try the performance load using the same httperf command.

The result of json from couchdb :

{
  "total_rows": 2,
  "offset": 0,
  "rows": [
    {
      "id": "2f9bc9fb62f3e8fa19ace932b9000d9f",
      "key": "2f9bc9fb62f3e8fa19ace932b9000d9f",
      "value": {
        "_id": "2f9bc9fb62f3e8fa19ace932b9000d9f",
        "_rev": "1-0a77ba71f874dc7ca2b7d22893cf4882",
        "task": "learn",
        "status": "not done"
      }
    },
    {
      "id": "2f9bc9fb62f3e8fa19ace932b90013d9",
      "key": "2f9bc9fb62f3e8fa19ace932b90013d9",
      "value": {
        "_id": "2f9bc9fb62f3e8fa19ace932b90013d9",
        "_rev": "1-6127c1359f9d34d2733943876931e7d4",
        "task": "erlang",
        "status": "not done"
      }
    }
  ]
}

And the design document to retreive the data save with path /todo/_design/todo/_view/list

function(doc) {
  emit(doc._id, doc);
}


so here are the result with same data.


httperf --client=0/1 --server=127.0.0.1 --port=5984 --uri=/todo/_design/todo/_view/list --rate=150 --send-buffer=4096 --recv-buffer=16384 --num-conns=27000 --num-calls=1
httperf: warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 27000 requests 27000 replies 27000 test-duration 179.995 s

Connection rate: 150.0 conn/s (6.7 ms/conn, <=13 concurrent connections)
Connection time [ms]: min 0.6 avg 1.1 max 92.4 median 0.5 stddev 2.5
Connection time [ms]: connect 0.0
Connection length [replies/conn]: 1.000

Request rate: 150.0 req/s (6.7 ms/req)
Request size [B]: 90.0

Reply rate [replies/s]: min 149.8 avg 150.0 max 150.0 stddev 0.0 (36 samples)
Reply time [ms]: response 1.1 transfer 0.1
Reply size [B]: header 231.0 content 470.0 footer 2.0 (total 703.0)
Reply status: 1xx=0 2xx=27000 3xx=0 4xx=0 5xx=0

CPU time [s]: user 62.39 system 117.63 (user 34.7% system 65.4% total 100.0%)
Net I/O: 115.9 KB/s (0.9*10^6 bps)

Errors: total 0 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0

As we can see the result is almost the same. no error happening, all request processed with 6.7ms / request.
Test done in 179 s or 3 minutes. I don't know if all erlang use same processing to dump json.

Anyhow, great result and positif about this.

Erlang Chicago Boss JSON API test performance

I am documenting my test on erlang web framework, Chicago Boss and use it for backend API.
My setting for the database using Postgresql 9.2 using 1 table with 2 rows and 4 fields only and return the request using JSON.

I test the performance load using httperf on my debian 8 laptop.

# httperf --server 127.0.0.1 --port 8001 --uri /todo/list --rate 150 --num-conn 27000 --num-call 1

In this simple test, the same page is retrieved repeatedly. The rate at which requests are issued is 150 per second. The test involves initiating a total of 27,000 TCP connections and on each connection one HTTP call is performed (a call consists of sending a request and receiving a reply)

 The result should be like this if access from browser :

{
  "todos": [
    {
      "id": "todo-1",
      "task": "learning",
      "status": "not done",
      "owner": "Voldomore"
    },
    {
      "id": "todo-2",
      "task": "erlang",
      "status": "not done",
      "owner": "Potter"
    }
  ]
}

and the result of httperf : 

httperf --client=0/1 --server=127.0.0.1 --port=8001 --uri=/todo/list --rate=150 --send-buffer=4096 --recv-buffer=16384 --num-conns=27000 --num-calls=1
httperf: warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 27000 requests 27000 replies 27000 test-duration 179.999 s

Connection rate: 150.0 conn/s (6.7 ms/conn, <=13 concurrent connections)
Connection time [ms]: min 3.9 avg 6.6 max 85.4 median 5.5 stddev 4.5
Connection time [ms]: connect 0.0
Connection length [replies/conn]: 1.000

Request rate: 150.0 req/s (6.7 ms/req)
Request size [B]: 71.0

Reply rate [replies/s]: min 149.8 avg 150.0 max 150.2 stddev 0.1 (36 samples)
Reply time [ms]: response 6.6 transfer 0.0
Reply size [B]: header 125.0 content 673.0 footer 0.0 (total 798.0)
Reply status: 1xx=0 2xx=27000 3xx=0 4xx=0 5xx=0

CPU time [s]: user 55.66 system 124.11 (user 30.9% system 68.9% total 99.9%)
Net I/O: 127.3 KB/s (1.0*10^6 bps)

Errors: total 0 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0


What i see are 179 seconds (3 minutes) to complete all the request, and with basic setup. only have a model and controller to list. I want to try it using Django Rest Framework also later on to see how django rest framework performance. But surely Erlang faster. Just want to know how many can be handled by DRF.


Sunday, 21 August 2016

Creating a base view in backbone marianotte js the good way

Marionette JS is based on Backbone JS and a lot of the module extend from basic backbone modules. Here i learn when i extend a module whether it was a view or model or anything you will do like this :

var BaseForm = Marionette.ItemView.extend({

    

      initialize: function(){

           this.title = "title"

      },



});


and to use it on a custom page view base on the BaseForm you will do this :

var contactForm = BaseForm.extend({

    

      initialize: function(){

            this.rows = 2;

      },



});


Above problem is the main object title from the BaseForm is gone, except you do this inside contactForm :

       initialize: function(){

             BaseForm.prototype.initialize.call(this);

             this.rows = 2;

      },


With the above method, anything initialized on the BaseForm will still be available.

But this create problem that everytime you override the initialize, you need call the prototype. How if we not touch the initialize in the BaseForm, and just create a new method, that will be checked by the initialize method on the BaseForm.
So here are the new way on doing this :

//BaseForm default initialize

var BaseForm = Marionette.ItemView.extend({

    

      initialize: function(){

           this.title = "title",



          if (this.additionalInit){

              this.additionalInit();

          },



      },



});

//we extend from BaseForm without touch initialization code
var contactForm = BaseForm.extend({
   
      additionalInit: function(){
            this.rows = 2;
      },
});

The benefit of this, any developer want to use the module, no need break the module by override the initialization, or if do want additional custom function running after the initialization base modul load, provide the function that the initialization checks, in this case additionalInit function.


Saturday, 20 August 2016

GlusterFS for scalable storage

When look for a storage, a simple thing for system admin was a NAS. With a NAS appliance, your life is easier. Plug and forget, if ever happend to the harddisk, just replace it as already have RAID system. But still, the storage size is limited to the disk bay provided and a disk size available, also with RAID, disk size is limited to the smallest disk available.

So when we need a future expanding of the storage size, NAS is not an easy way to do the storage expansion. Come the GlusterFS for storage cluster. With GlusterFS you can easily expand / scale out your storage size. just add a server and a disk storage, put it on the glusterFS cluster, your storage size is bigger.

Some of notes when using GlusterFS :
1. Run on Linux server
2. Use XFS file system (recommended by Redhat)
3. Use LVM system
4. Use Hardware RAID for disk level redundancy
5. Use native glusterFS client

The important thing to highlight is use the native glusterFS client as it will provide redundancy, because all the data stored on the storage nodes, the client will fetch it automaticly by connecting to all the nodes inside the storage cluster. The initial connection only to the mount point node of the storage cluster. This will provide load balance of load inside the storage.

Also the RAID on hardware level. If Disk problem, just swap it as provided by the RAID hardware. Easier to do than setup RAID on the OS level.

For the default configuration, GlusterFS distributed your file to each storage nodes. You can have other options in GlusterFS base on your needs as :

1. Distributed Volumes
2. Replicated Volumes
3. Striped Volumes
4. Distributed Striped Volumes
5. Distributed Replicated Volumes
6. Distributed Striped Replicated Volumes
7. Striped Replicated Volumes
8. Dispersed Volumes

To provide redundancy and availablility, i am using Distributed Replicated setup, where i set 2 replica and distributed the store to every nodes.
The minimum brick required are 4 (assumes each server have 1 brick only).
So we distribute the data to 4 storage node, with 2 replica on each data.

GlusterFS Distributed Replicated Volume. Source: GlusterFS Documentation

This storage setup i used for Owncloud storage backend.
With this, we able to expand the storage size anytime with adding more VM or server node to the cluster.

Wednesday, 17 August 2016

Marionette JS the basic of javascript Front End

When talk about Front End development, developers will look how to build fast and update fast. With basic javascript, you can build a frond end app, but will have a boiler plate code. So people look for frameworks and library. As my journey on developing Front End web apps, first look to using plain HTML + CSS + Javascript , which will use Jquery of course.

Move on to the next level, developer tends will use a framework. There are plenty of frameworks, like AngularJS, EmberJS, BackboneJS. Each have their own benefits. I have try angularJS, and with current angularJS status, i don't fell like it. It breaks everything, and you will have to follow if you still want to use AngularJS. Do they even think about their users? How about my apps ?

I try to use the basic with javascript + Jquery, and see on backboneJS is trying help on dealing with javascript better, and also support Jquery. Even better with the available Marionette JS. I like the concept of region and layout, which every application will have that thing and you have full control over it as developer.

So now my journey on Backbone JS + Marionette JS will evolve, and with the backend will use Django as the horse power.

Like many framework, you need to see not only how to create apps faster, but manageable and reusable code. After using Django for web development, you will want the reusable and manageable code in your repository.

Front End web App is the way to a distribute work load, processing on client side.

My way is Backbone JS with Marionette JS.




Twitter Delicious Facebook Digg Stumbleupon Favorites More