Monthly Archives: January 2018

Java Coding Style

Guava: Preconditions.checkNotNull
Guava: Preconditions.checkArgument
JDK: Objects.requireNotNon
NullPointerException or IllegalArgumentException
1. Many APIs in the JDK pro-actively throws NullPointerException. This suggests that throwing NPE is a Java convention.
2. Effective Java Item 60: “Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states. If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException. Similarly, if a caller passes an out-of-range value in a parameter representing an index into a sequence, IndexOutOfBoundsException should be thrown rather than IllegalArgumentException.”
3. Preconditions.checkNotNull(parameter) is clearer than Preconditions.checkArgument(parameter != null, “parameter can’t be null”)

static import takes first place. Because there was a Bug prior to JDK8.

Prefer dvdPlayer to DVDPlayer. Because you can’t tell HTTPSID(httpSid or httpsId)

Prefer 1TBS to K&R. Because moving codes in K&R may lead messy. 1TBS always has bracket to be clear.
int i;
for (i = 0; i < 10; i++) {

int i;
for (i = 0; i < 10; i++)

stylistic convention

2PC, 3PC

There are two basic distributed transaction protocols: two-phases-commit and three-phases-commit.


Assumption, coordinator and cohorts are never down.

If coordinator is down, all other cohorts will be blocking and waiting for its recover. If a cohort is down and when it wakes up, it will get notified by coordinator(commit or rollback).

Note: 2PC only guarantees in atomic commit for current current transaction. It doesn’t guarantee data consistency. Client may read data from different cohorts. Some cohort may have data committed, some may not. In this case, client read stale data or client thinks data is inconsistent. In such case, an option is that the read waits until the commit is done, then return data. Read this article.

princeton 2PC, check the Server termination protocol. It shows 2PC is blocking protocol. After all cohorts ACKed for pre-commit, they entered a blocking state, until an answer from coordinator is sent out. If in any case coordinator is down, then cohorts need to wait for coordinator to recover.

Only at preCommit phase, coordinator can sense timeout.


For 3PC, coordinators asks for canCommit to all nodes. Only if all nodes answer YES, coordinate enters prepared phase and send preCommit to all nodes.

When majority nodes ACKed preCommit, coordinator sends commit to all nodes. It enters commit state.

In above phases, if coordinator/cohorts failed, timeout, then it sends abort message.

When coordinator fails, new coordinator comes back. If new coordinator found any cohort has committed, it can guess the previous transaction is done. Then new coordinator sends out commit message. If new coordinator didn’t find any cohort in pre-commit state, then coordinator will restart from canCommit/preCommit state. Check this video.


also compare with zookeeper protocol