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.
1TBS:
int i;
for (i = 0; i < 10; i++) {
printf(“Hi”);
}

K&R
int i;
for (i = 0; i < 10; i++)
printf(“Hi.”);

stylistic convention

2PC, 3PC

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

two-phase-commit

Assumption, coordinator and cohorts are never down.
2PC

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).

3-phase-commit

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.


3PC

https://en.wikipedia.org/wiki/Two-phase_commit_protocol
https://en.wikipedia.org/wiki/Three-phase_commit_protocol
also compare with zookeeper protocol