Hồi phục… IT Band

Cuối tuần vừa rồi mình đã có 2 buổi chạy bộ liên tiếp ngay gần nhà. Mình chủ động tính toán thời gian để buổi chạy không vượt quá 1 giờ. Chấn thương khiến mình cẩn trọng trong từng bước chạy, cảm nhận trọng lực dồn đều từ bàn chân lên cơ tứ đầu, hamstring.

Mình cảm nhận được rõ điều này vì toàn bộ các nhóm cơ chân tới mông của mình vẫn còn khá căng cứng. Hậu quả của Leg day từ hôm thứ 4 tuần trước, đáng lẽ mình không nên chạy bộ vào cuối tuần này tuy nhiên mình vẫn quyết định xỏ giầy. “Mình đã nghỉ 1 tuần rồi. Cùng lắm đau 1 chút sẽ nghỉ 1 tuần nữa”, mình đã nghĩ vậy.

Hôm nay là ngày thứ 2 sau buổi chạy chân cẳng mình vẫn ổn. Có vẻ đầu gối của mình đã dần ổn định trở lại :). Để nói về cái việc này có ý nghĩa như thế nào thì phải quay trở lại buổi chạy 1 tuần trước. Mình bị đau IT band ngay sau 2 bài chạy nhỏ (20phút/bài) và tuần trước nữa đó thì mình không thể chạy nổi 3km mặc dù đã lê lết ở pace 8:50-9.

Cái chấn thương IT Band này đã đeo đẳng với mình suốt từ tháng 8 đến giờ.  Để chuẩn bị cho Tràng An Marathon mình đã tăng số buổi chạy trong tuần lên từ tháng 7. Giữa tuần có 1 buổi Interval, cuối tuần là Long run. Có điều vì lười dậy sớm ra công viên nên mình đã tập Interval trên treatmill tại phòng gym. Chạy trên treadmill mình không thể đẩy tốc độ lên cao ngay lập tức, nguyên tắc an toàn treadmill là sau khi tăng speed thì tốc độ của máy mới được đẩy lên sau vài giây. Do vậy mà nhịp chạy nhanh của mình chỉ ở mức bình thường, hear rate trung bình loanh quanh ở cuối zone 3.  Kết quả cải thiện đáng kể, ở buổi Long run cuối tuần mình chạy khá ổn và mình quyết định tăng pace vào mỗi buổi Long run cuối tuần. Cần phải nói thêm là mình vẫn duy trì Leg day mỗi tuần ở phòng Gym. Nghĩ lại giai đoạn này đôi chân của mình bị hành hạ tương đối với mỗi tuần 2 buổi chạy treadmill + 1 buổi full leg + 1 long run vào cuối tuần.

Và kết quả là chấn thương ập đến.

 

 

Advertisements

Lambda Expression

First snippets

List names = Arrays.asList("Nguyen Thi A", "Tran Thi B", "Le Thi C");

for (String name : names) {
    if (name.contains("Tran"))
        System.out.println(name);
}

And

List names = Arrays.asList("Nguyen Thi A", "Tran Thi B", "Le Thi C");

Predicate predicate = s -> s.contains("Tran");
names.stream().filter(predicate).forEach(System.out::println);

It’s an example filter use Predicate

Create data use Supplier

Supplier supplier = () -> (int) (Math.random() * 1000);
for (int i = 0; i < 10; i++)
    System.out.println(supplier.get());

Performs operation on the give argument with Consumer

Consumer c = (x) -> System.out.println(x + 100);
Consumer c2 = (x) -> System.out.println(x + 1);
c.accept(123);
c.andThen(c2).accept(100);

Represents a function that accepts one argument and produces a result.

Function<String, Integer> toInt = Integer::parseInt;
int val = toInt.apply("4");
System.out.println(val);

 

Difference between Sharding And Replication on MongoDB

Replica-Set means that you have multiple instances of MongoDB which each mirror all the data of each other. A replica-set consists of one Master (also called “Primary”) and one or more Slaves (aka Secondary). Read-operations can be served by any slave, so you can increase read-performance by adding more slaves to the replica-set. But write-operations always take place on the master of the replica-set and are then propagated to the slaves, so writes won’t get faster when you add more slaves.

Replica-sets also offer fault-tolerance. When one of the members of the replica-set goes down, the others take over. When the master goes down, the slaves will elect a new master. For that reason it is suggested for productive deployment to always use MongoDB as a replica-set of at least three servers, two of them holding data (the third one is a data-less “arbiter” which is required for determining a new master when one of the slaves goes down).

Sharded Cluster means that each shard of the cluster (which can also be a replica-set) takes care of a part of the data. Each request, both reads and writes, is served by the cluster where the data resides, This means that both read- and write performance can be increased by adding more shards to a cluster. Which document resides on which shard is determined by the shard key of each collection. It should be chosen in a way that the data can be evenly distributed on all clusters and so that it is clear for the most common queries where the shard-key resides (example: when you frequently query by user_name, your shard-key should include the field user_name so each query can be delegated to only the one shard which has that document).

The drawback is that the fault-tolerance suffers. When one shard of the cluster goes down, any data on it is inaccessible. For that reason each member of the cluster should also be a replica-set. This is not required. When you don’t care about high-availability, a shard can also be a single mongod instance without replication. But for production-use you should always use replication.

So what does that mean for your example?
                            Sharded Cluster             
             /                    |                    \
      Shard A                  Shard B                  Shard C
        / \                      / \                      / \
+-------+ +---------+    +-------+ +---------+    +-------+ +---------+
|Primary| |Secondary|    |Primary| |Secondary|    |Primary| |Secondary|
|  25GB |=| 25GB    |    | 25 GB |=| 25 GB   |    | 25GB  |=| 25GB    |   
+-------+ +---------+    +-------+ +---------+    +-------+ +---------+

When you want to split your data of 75GB into 3 shards of 25GB each, you need at least 6 database servers organized in three replica-sets. Each replica-set consists of two servers who have the same 25GB of data.

You also need servers for the arbiters of the three replica-sets as well as the mongos router and the config server for the shard. The arbiters are very lightweight and are only needed when a replica-set member goes down, so they can usually share the same hardware with something else. But Mongos router and config-server should be redundant and on their own servers.

It’s copy from stackexchange.The source here: http://dba.stackexchange.com/questions/52632/difference-between-sharding-and-replication-on-mongodb

Java Quiz

Question 1. Given this code fragment:

List<String> vitamins = new ArrayList<>();
vitamins.add("A");
vitamins.add("B12");
vitamins.add("C");
vitamins.set(1,"B");
vitamins.add(1,"D");
System.out.println(vitamins);

What is the result?

a. [A, D, B, C]

b. [A, D, C]

c. [D, B, C]

d. [D, B12, C]

Question 2. Given this code fragment:

1. class Engine { }
2. public class App {
3.     public static void main(String[] args) {
4.        Engine e = new Engine();
5.        Engine e1 = e;
6.        e = null;
7.     } 
8. }

Which statement is true about this code?

a. It creates an object and the object is eligible for garbage collection.

b. It creates an object and the object is not eligible for garbage collection.

c. It creates two objects: e and e1. The e object is eligible for garbage collection.

d. It creates two objects and both the objects are not eligible for garbage collection.

Question 3. Given this code fragment:

    double wage = 2.0;
    int weekDays = 5;
    long monDays = weekDays * 4;
    long yearDays = monDays * 12L;
    long totalWage = yearDays * wage; // line n4

Which modification enables the code to compile?

a. Replace line n1 with double wage = 2;
b. Replace line n2 with long monDays = weekDays * 4L;
c. Replace line n3 with long yearDays = monDays * 12;
d. Replace line n4 with long totalWage = yearDays * (long) wage;

Imported tables are not showing up in phpmyadmin

I have imported a .sql file as you did and then I went to phpmyadmin, selected the database and surprise: No tables in the database, but I went to mysql command line and did a SHOW TABLES and they do exist.

So this is what happened in my case, the original database had some VIEWS defined by a user that did not exist in my computer’s mysql users.
Example:

CREATE ALGORITHM=UNDEFINED DEFINER=admin@% SQL SECURITY DEFINER VIEW cantidades AS select (…)

The user phongtn@% was available on the original server from where I’ve exported the database, but not on my computer.

So the fix to this solution was either to add that user, or to drop the views and create them again with a existing user.I have choosen the second option:

DROP VIEW cantidades;
CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER VIEW cantidades AS select (…)

Monitor progress of data

The pv command allows you to see the progress of data through a pipeline.
Because default pv command is not installed. Type the following:

#yum install pv

Ok, try this:

pv dumpfile.sql | mysql -uxxx -pxxx dbname

will show a progress bar as the program runs.

Sample output:

[root@server5]# pv mysql-chicken_war.30-10-2013-14_00_01 | mysql -uroot -p chicken_war
Enter password:
718MB 0:05:21 [2.23MB/s] [=================================================>]100%

Ok, try to login to phpMyadmin. Hmmm, I cant see any tables?
Back to sql command. Type the following:

mysql> show tables;

mysql> SELECT table_name FROM information_schema.tables where table_schema = DATABASE();

Sample output:

ERROR 1143 (42000): SELECT command denied to user ”@’localhost’ for column ‘xxxx’ in table ‘xxxx’

I can see has problem with permission on column ‘xxx’. Try to fix it, problem solved.

Enjoy!

MYSQL: GETTING ROW POSITION IN RESULT SET

In some cases we want to know position of specific row in result set. For example when we use pagination and want to know on which page specific element is. If we don’t use ORDER BY with non-unique field, we can use simple query:

SELECT COUNT(*) FROM TABLE WHERE id <= ELEMENT_ID

The result will be position of element with id equal to ELEMENT_ID, assuming that result is ordered by id field. If we use other field in ORDER BY, we have to change WHERE clause, for instance:

SELECT COUNT(*) FROM TABLE r WHERE creation_time >= ELEMENT_CREATION_TIME_FIELD ORDER BY creation_time DESC

Note that in WHERE clause greater-equal is used instead of less-equal, because DESC instead of ASC (default) is used.

Unfortunately this approach won’t work when we use ORDER BY with non-unique field. In that case we have to use MySQL user variable.

Example: getting row position for row identified by specified id.

SET @i = 0;
SELECT POSITION FROM (
    SELECT id, @i:=@i+1 AS POSITION
    FROM TABLE ti
 ) t WHERE id=ID

Caution: if you use PHP, remember that you have to execute two queries, first for SET and second for SELECT one.
If you want to use SELECT query more than once, remember to set i variable each time.

As you can see above – trick is about incrementing i variable on each row, and fetching position of interesting row.

We can also use this technique when result set is ordered.
Example: getting row position for row identified by specified id in ordered query

SELECT POSITION FROM (
    SELECT id, @i:=@i+1 AS POSITION
    FROM TABLE ti
    ORDER BY creation_time DESC
 ) t WHERE id = ID;

Another thing we can achieve is getting row located on specified position.
Example: Getting row located on position 2

SELECT id FROM (
    SELECT id, @i:=@i+1 AS POSITION
    FROM TABLE ti
    ORDER BY creation_time DESC
 ) t WHERE POSITION = 2;