Sunday, 29 December 2013

MySQL Architecture



MySQL Architecture

It is very import to understand the MySQL architecture.  MySQL Architecture can be shown as below:

key components

Connector:  that is usually on the client side. That is the SQL/database driver. For example, the JDBC driver for mysql privides an interface between JAVA program and the database SQL operations.
Management Service and utilities: the tools used by DBA to control the database , HA and improve the performance.
Connection pool:  this component is to manage the connections from multiple clients, dispatching the requests and response.
SQL Interface: it accepts the SQL from the client and returns the results.
Parser:  it analyses the SQL language and make a special data structure (SQL tree). The parser is a long script written by Lex and YACC.
Optimizer: the SQL will be optimised before execution by the optimizer.it is done by ‘select-cast-join’ operations.
Cache/Buffer: the cached results for SQL query, stored in memory for next time usage.
Storage Engine:  the interface to file system. MySQL storage engineer is pluggable. There are quite a few engine available.
File system: the OS file system to provide persistent storage for MySQL engine.

Linux Virtual Server (LVS) introduction



Linux Virtual Server (LVS) introduction

LVS is a linux-based load balance technology, it can provide high performance, LVS is originated from a Chinese open source project in 1998 and now it is probably thought as the most powerful load balance solution in the world. For example, the world largest e-economic sites www.taobao.com used LVS to handle over 1 million concurrent http connections. No wonder, the Founder of LVS , Dr Zhang is the CTO of ‘taobao’.

LVS architecture

There are 3 layers of a traditional LVS system.

Loader balancer (Director Server):  it accepts the request from the client and dispatch it to the backend Server Array.
Server Cluster (Real Server): the servers serve the request and provide the response to the client.
Shared Storage (optional): the layer provides a consistent content to the client. In some simple system, the layer does not existing.  

The basic idea of LVS is when the request comes to the Director Server ‘s VIP, Director Server will pick up a real server and forward the request to it. Then how the real server is selected (load balance algorithm) and how the request/response are dispatched(load balance mode) are the two major topics in LVS

Load balance mode:

VS/NAT(Virtual Server via Network Address Translation): When a request packet destined for virtual IP address (the external IP address for the load balancer) arrives at the load balancer. The load balancer will choose a real server by a scheduling algorithm, and the connection is added into the hash table which record the established connection. Then, the destination address (IP) and the port (optional) of the packet are rewritten to those of the chosen server, and the packet is forwarded to the server. When the incoming packet belongs to this connection and the chosen server can be found in the hash table, the packet will be rewritten and forwarded to the chosen server. When the reply packets come back, the load balancer rewrites the source address (IP) and port (optional) of the packets to those of the virtual service. After the connection terminates or timeouts, the connection record will be removed in the hash table.
It is called NAT is because the IP(and port) will be rewritten then the request dispatched and when the response coming back.



VS/TUN(Virtual Server via IP Tunnelling): When a request packet destined for virtual IP address (the external IP address for the load balancer) arrives at the load balancer. The load balancer will choose a real server by a scheduling algorithm, and the connection is added into the hash table which record the established connection. Then, the load balancer encapsulates the packet within an IP datagram and forwards it to the chosen server.  When an incoming packet belongs to this connection and the chosen server can be found in the hash table, the packet will be again encapsulated and forwarded to that server. When the real server receives the encapsulated packet, it decapsulates the packet and processes the request, finally return the result directly to the user according to its own routing table. After a connection terminates or timeouts, the connection record will be removed from the hash table.


The key technology is ‘encapsulation’ the requests to the IP tunnel.


VS/DR(Virtual Server via Direct Routing): When a request packet destined for virtual IP address (the external IP address for the load balancer) arrives at the load balancer. The load balancer will choose a real server by a scheduling algorithm, and the connection is added into the hash table which record the established connection. Then, the load balancer directly forwards it to the chosen server (layer 2 forwarding). When the incoming packet belongs to this connection and the chosen server can be found in the hash table, the packet will be again directly routed to the server. When the server receives the forwarded packet, the server finds that the packet is for the address on its alias interface or for a local socket, so it processes the request and returns the result directly to the user. After a connection terminates or timeouts, the connection record will be removed from the hash table.

Note: the essential idea is LD and RS must in the same LAN and the response is back to the user directly.



There is a short comparation about the three modes

NAT is supported by almost all OS and the real server can use the private IP addresses. But since every package (including the response packages), so performance is a major issue.
Tunneling provides a good performance capacity but it needs the client OS to support "IP Tunneling" (IP Encapsulation) protocol.
DR supported by all OS, the performance is very high. But it needs the RS and DR in a same subnet which is a major limitation for it.

Load balance algorithm:

The algorithm decides how the real server is selected.

  • Round-Robin Scheduling: The round-robin scheduling algorithm sends each incoming request equally to the next server in its list
  • Weighted Round-Robin Scheduling: Each server can be assigned a weight, an integer value that indicates the processing capacity; the server is selected based on the weighted round-robin.
  • Least-Connection Scheduling: The least-connection scheduling algorithm directs network connections to the server with the least number of established connections
  • Weighted Least-Connection Scheduling: The weighted least-connection scheduling is a superset of the least-connection scheduling, in which you can assign a performance weight to each real server. The servers with a higher weight value will receive a larger percentage of live connections at any one time.


There are other algorithms used in LVS.

Thursday, 26 December 2013

LVM(Logical Volume Manager) introduction



LVM(Logical Volume Manager) is a useful tool in linux to manage the disk volume. The main purpose is to dynamically manage the disks in linux. You can use LVM to extend or shrink the disks in OS without rebooting.
Key components:
Physical volume (PV): Partition on hard disk (or even hard disk itself or loopback file) on which you can have volume groups
Volume group (VG): Group of physical volumes that are used as storage volume.
Logical volume (LV): A "virtual/logical partition" that resides in a volume group, it can be used to as a traditional partition and mounted to the mount point.
The picture is from internet.



0. Create a partition using fdisk. 

(this is omitted as not related with LVM), but make sure it’s type set to Linux LVM (8e)




   1. Create PV from disk partition:

commands: pvcreate [device partition]
 



2. Create the VG.

Then you need to create the volume group using the PV.
Commands:
vgcreate [volume ground name] PV        //create the new VG
vgcreate [volume ground name] PV        //add the PV to new VG
eg:
vgcreate newvg /dev/sdb1
vgextend newvg /dev/sdc1



3. Create the logic volume (LV) from VG.

Commands:
#lvcreate –L size [VG] –n [LVname]



 4. view the device 

Now you can view the device by
#ls –l /dev/mapper/[VG]-[LV]
#ls –l /dev/[VG]/[LV]


   5. Make file system and mount the files

mkfs.[filesystem format] [LV]
mount [LV] [mount-point]
   

6. Extend the LV size (expand the file system capacity as well)

Commands:
Lvextend –L [size] [lv]
Resize2fs [lv]
Eg:
lvextend -L 200m /dev/newvg/lvhome
resize2fs /dev/newvg/lvhome


This is the basic usage of LVM, there are other advanced usages such as shrink, remove PV, snapshots, you can refer to other online documents.

linux memory buffers vs cached



in the memory commands output such as 'vmstat', the item buffers or cached is sometimes confusing.

let's do some testings to show what is the difference:

what is buffers:





Here is the output after I did the ‘ls –al ’ command. You will see the buffer information increased a little. It is because the ls –al will get the directory content (not the file content) which is the meta data of the file system.

Another example is 



 free                                         //check the current memory usage including buffers and cached
 time find . > /dev/null                //print whole files name and checked the time usage
 free                                         //check the current memory usage again
 time find . > /dev/null                // print whole files name and checked the time usage again
 free                                         //check the current memory usage again

here are the testing results:
1.     In step 2, lots of buffers are used and the find command used about 9 seconds. No cached memory is used.
2.     In step 4, same commands neither buffers nor cached is used as the results is stored in buffers. The find command is much quicker (0.2 seconds).

what is cached:


Then let’s check how the cached is used. Testing scripts are
free                  //check the current memory usage including buffers and cached
man pwd           // check the man page for ps command
free                  //check the current memory usage including buffers and cached again
man pwd           // check the man page for ps command again
free                  //check the current memory usage including buffers and cached again



here are the testing results:
1.     In step 2, a little cached is used no buffers memory is used.
2.     In step 4, same commands neither buffers nor cached is used as the results is stored in cached.

So there is the conclusion:
Buffers are associated with a specific block device, and cover caching of filesystem metadata as well as tracking in-flight pages. The cache only contains parked file data. That is, the buffers remember what's in directories, what file permissions are, and keep track of what memory is being written from or read to for a particular block device. The cache only contains the contents of the files themselves.