Tuesday, December 10, 2013

Thread States

When you look at a thread dump or a well structured thread dump using an analyzer you should see threads having a particular format along with its thread states both of which are detailed below -

Threads with format should be analyzed- ajp-10.10.50.23-8009-5 daemon prio=10

Meaning -

ajp - protocol we use to connet Apache to JBoss
10.10.XX.XXX - JBoss Server IP
8009 - Port on which the JBoss AJP port is listening
5 - Thread number.
daemon - its a daemon thread 
prio=10 - Represents the priority of the threads.

Java 6 Thread States and Life Cycle -

1) New - is the thread state for a thread which was created but has not yet started.

2) Runnable - A thread in the runnable state is executing from the JVM point of view but in fact it may be waiting for some resources from the operating system. The thread is occupying the CPU and processing a task

3) Timed waiting is a thread state for a thread waiting with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time: Thread.sleep(sleeptime), Object.wait(timeout), Thread.join(timeout), LockSupport.parkNanos(timeout), LockSupport.parkUntil(timeout)

4) Waiting - A thread is in the waiting state due to the calling one of the following methods Object.wait(), Thread.join(), LockSupport.park() without timeout

5) Blocked - Thread is in the blocked state while waiting for the monitor lock to enter a synchronized block or method or to reenter a synchronized block or method after calling Object.wait().

6) Terminated - After thread has completed execution of run() method, it is moved into terminated state

Other notes - 
Waiting on Monitor:
The thread is either sleeping or waiting on an object for said period of time or waiting to be notified by another thread. This will happen when any of the sleep() methods on Thread object or wait() method on an Object is called.

BLOCKED / WAITING FOR MONITOR ENTRY:
 Thread enters this state when it isn't able to acquire the necessary monitors to enter a synchronized block. A thread waits for other threads to reliquish the monitor. A thread in this state isnt doing any work 

SLEEPING  / WAITING ON CONDITION:
A thread sleeps when it calls Thread.sleep(long). As the name suggests the thread isnt doing anything in this state either

Monday, April 1, 2013

MySQL Performance tip

MySQL performance tip: Watch out for pagination queries

Applications that paginate tend to bring the server to its knees. In showing you a page of results, with a link to go to the next page, these applications typically group and sort in ways that can't use indexes, and they employ a LIMIT and offset that causes the server to do a lot of work generating, then discarding rows.
Optimizations can often be found in the user interface itself. Instead of showing the exact number of pages in the results and links to each page individually, you can just show a link to the next page. You can also prevent people from going to pages too far from the first page.
On the query side, instead of using LIMIT with offset, you can select one more row than you need, and when the user clicks the "next page" link, you can designate that final row as the starting point for the next set of results. For example, if the user viewed a page with rows 101 through 120, you would select row 121 as well; to render the next page, you'd query the server for rows greater than or equal to 121, limit 21.

Refer - http://www.infoworld.com/d/data-management/10-essential-performance-tips-mysql-192815?page=0,1

Wednesday, January 16, 2013

MySQL Tips

1. Use Innodb storage engine as compared to MyISAM for transaction tables
2. Have file per table flag turned on in the my.cnf file (innodb_file_per_table)
3. Put indexes on fields with highest SI
4. Try to avoid any blob entry
5. Leverage Load balance - Writes to Master and Reads (Reports) to Slave
6. max_connections and related per thread buffers need to be set keeping in mind the overall memory allocated
7. Do not use default innodb_buffer_pool, infact. Most places recommend to set this parameter 80% of RAM available.
8. mtop is a poor man''s utility and very handy for a DBA. Monitor - CPU, Memory & Connections
9. Use latest and stable MySQL db version and use native connector
10. Set slow query flag and tune the instances logged
11. Monitor replication lag and attempt to avoid insert... select pattern as they run on Master as well as Slave.
12.Have primary key mandated for every table
13. select count(1) on a table with high no. of records can be slow
14. Consider partitioning of tables that have high growth.
15. Avoid firing any query on information_schema especially when the no. of schemas/tables in that instance is high
16. For log tables, consider NOT having archival tables and later deleting thousands of old records but, instead partition the log tables say on weekly, fortnightly, monthly, quarterly basis depending on data growth/reads and then just drop the old partitions.