Wednesday, 26 March 2014

Regular Expression

this is the study note to
http://qntm.org/files/re/re.html



1.       literal characters, which just represent themselves. Such as ‘c’, ‘ab’
2.       the dot  --- any single character
3.       Any meta character can be escaped using a backslash, \. This turns it back into a literal.
4.       A character class is a collection of characters in square brackets. This means, "find any one of these characters". eg [0-9] means any digital, [a-z] means any lower case characters. [^a] means any characters except a
5.       Special means
a.       \d – digital           \D – non-digital
b.      \w - [0-9A-Za-z_]: find a word character.  \W – non-word character
c.       \s find a space(tab, space, carriage return or line feed) character. \S non-space character.

6.       Multipliers use braces to put a multiplier after a literal or a character class.
a{3} means "find an a followed by an a followed by an a".
a{3,5} means "find aaaaa or aaaa or aaa".
? means none or once
* means any times (none, once, or more than once)
+ means one or more than once



Alternation with group: alternation is devided by |, eg: ‘cat|dog’ means cat or dog. You may use () to put your matching pattern there for example: (Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day means the day’s

\b means the word boundary
^ and $ from "start-of-line" and "end-of-line"

Advanced topics about Regular Expression
Capturing : used referring for () group. (\w+) had a (\w+)  (\w+)
And we can use back-references to refer to the captured pattern such as regular expression ([abc])\1 means "find aa or bb or cc". ([abc])\1\1 means "find aaa or bbb or ccc"
 

Monday, 24 March 2014

MySQL data files



 
The MySQL database stores the data in disks as other databases.  The default datastore will be determined by datadir variable
>show variables like "%datadir%";
For example my output is /var/lib/mysql/

Check the directories in
mysql> system ls -ld /var/lib/mysql/*/
drwx------. 2 mysql mysql 4096 Mar 21 22:09 /var/lib/mysql/mydatabase/
drwx--x--x. 2 mysql mysql 4096 Dec 29 10:33 /var/lib/mysql/mysql/
drwx------. 2 mysql mysql 4096 Dec 29 10:33 /var/lib/mysql/performance_schema/
drwx------. 2 mysql mysql 4096 Jan  1 21:08 /var/lib/mysql/test/
at the moment we got 5 databases on my databases(note file information_schema does not have a real data file storage)
mysql> show databases;
| information_schema      |
| mydatabase                    |
| mysql                             |
| performance_schema    |
| test                                 |

data file in  MyISAM

When I create a table using MyISAM engine.
mysql> create table mytable(id int ,name varchar(20)) engine=myisam;
Query OK, 0 rows affected (0.18 sec)
You will see there are three files related with the table
-rw-rw----. 1 mysql mysql 8586 Mar 25 14:38 mytable.frm
-rw-rw----. 1 mysql mysql    0 Mar 25 14:38 mytable.MYD
-rw-rw----. 1 mysql mysql 1024 Mar 25 14:38 mytable.MYI

  • *.frm file contains the table related meta files including the table’s definition information
  • *.MYD file contains the data for the table.
  • *.MYI file contains the index information for the table.

data file in innodb



When we create a table using innodb engine, there is a option called innodb_file_per_table which will decide if every table has single files or shared file. Please use the command
> show variables like ‘%per_table%’;
To verify the setting

To change the settings,  innodb_file_per_table=0 should be in my.cnf’s [mysqld] section then restart the database.

  • innodb_file_per_table – on : each table will create a single innodb file for individual table
  • innodb_file_per_table – off: tables will share the innodb files

when innodb_file_per_table is on

mysql> create table intable(id int ,name varchar(20)) engine=innodb;
Query OK, 0 rows affected (0.26 sec)
You may see two files for the table
-rw-rw----. 1 mysql mysql  8586 Mar 25 14:43 intable.frm (the meta file)
-rw-rw----. 1 mysql mysql 98304 Mar 25 14:43 intable.ibd (the data file)

when innodb_file_per_table is off.

All the table will share the same file for example ibdata1(datafile) and ib_logfile0 (logfile) but you should still be able to see the meta file
mysql> create table intablesinglefile1(id int ,name varchar(20)) engine=innodb;
Query OK, 0 rows affected (0.19 sec)
mysql> create table intablesinglefile2(id int ,name varchar(20)) engine=innodb;
Query OK, 0 rows affected (0.08 sec)
-rw-rw----. 1 mysql mysql  8586 Mar 25 15:04 intablesinglefile1.frm
-rw-rw----. 1 mysql mysql  8586 Mar 25 15:04 intablesinglefile2.frm

Nginx process structure




Usually Nginx is working in multiple thread mode, there will be a master process, it will prefork multiple children processes called worker processes. 

  • Master is not worked for web client connection and session management. It only manages the children processes.
  • Worker process handles the requests from clients (usually http/https) and servers them. The number of worker processes are decided by configuration

Configuration Syntax

user userid;
worker_processes  [number];   #define how many worker processes
events{
worker_connections [number];                #define how many connection one process can server
}

Sample configuration
user  nginx;
worker_processes  10;

the processes output

root      5031     1  0 13:41 ?        00:00:00 nginx: master process ./nginx
nginx     5032  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5033  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5034  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5035  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5036  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5037  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5038  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5039  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5040  5031  0 13:41 ?        00:00:00 nginx: worker process
nginx     5041  5031  0 13:41 ?        00:00:00 nginx: worker process

process 5031 is the master(parent) process
processes 5032-5041 are the worker (children) process

hardware and core configuration

low traffic website
medium traffic website
high traffic website

CPU : 2 cores,
memory 2GB,
visits ~ 1/s
CPU : 4 cores,
memory 4GB,
visits ~ 50/s
CPU : 8 cores,
memory 12GB,
visits ~ 1000/s




worker_processor 2;
worker_priority -4;
worker_cpu_affinity 01 10
events{
     worker_connections 128;
}
worker_processor 4;
worker_priority 0;
worker_cpu_affinity 0001 0010 0100 1000
events{
     worker_connections 1024;
}
worker_processor 8;
worker_priority 0;
events{
     worker_connections 8192;
}