处理写偏斜异常
在6.6节中,我们曾经简单讨论了写偏斜(write skew)以及Clojure STM是如何解决这个问题的。Akka同样提供了处理写偏斜问题的支持,但是需要我们配置一下才能生效。OK,一听到配置这个词可能让你觉得有些提心吊胆,但实际操作起来其实起来还是蛮简单的。下面就让我们首先了解一下Akka在不进行任何配置情况下的默认行为。
让我们回顾一下之前曾经见到过的那个多个账户共享同一个联合余额最低限制例子。首先我们创建了一个名为Portfolio的类来保存支票账户余额和储蓄账户余额。根据银行规定,这两个账户的总余额不得低于$1000。在Portfolio类的代码中我们用Java重新实现了withdraw()函数。在该函数中,我们先读取两个账户的余额,将二者相加得到总余额,并在等待一个故意插进去的延时(引入这个延时的目的是为了人为制造事务冲突的环境)之后,从其中一个账户余额中减掉给定数量的金额(当然,在操作之前需要判断减掉这个数量后总余额不少于$1000)。最后需要注意的是,withdraw()函数是在一个使用了默认设置的事务中完成上述操作的。
01 |
public class Portfolio {
|
02 |
final private Ref<Integer> checkingBalance = new Ref<Integer>( 500 );
|
03 |
final private Ref<Integer> savingsBalance = new Ref<Integer>( 600 );
|
04 |
public int getCheckingBalance() { return checkingBalance.get(); }
|
05 |
public int getSavingsBalance() { return savingsBalance.get(); }
|
07 |
public void withdraw( final boolean fromChecking, final int amount) {
|
08 |
new Atomic<Object>() {
|
09 |
public Object atomically() {
|
10 |
final int totalBalance =
|
11 |
checkingBalance.get() + savingsBalance.get();
|
12 |
try { Thread.sleep( 1000 ); } catch (InterruptedException ex) {}
|
13 |
if (totalBalance - amount >= 1000 ) {
|
15 |
checkingBalance.swap(checkingBalance.get() - amount);
|
17 |
savingsBalance.swap(savingsBalance.get() - amount);
|
21 |
"Sorry, can't withdraw due to constraint violation" );
|
下面让我们创建两个事务来并发地更改账户内的余额:
01 |
public class UsePortfolio {
|
02 |
public static void main( final String[] args) throws InterruptedException {
|
03 |
final Portfolio portfolio = new Portfolio();
|
04 |
int checkingBalance = portfolio.getCheckingBalance();
|
05 |
int savingBalance = portfolio.getSavingsBalance();
|
06 |
System.out.println( "Checking balance is " + checkingBalance);
|
07 |
System.out.println( "Savings balance is " + savingBalance);
|
08 |
System.out.println( "Total balance is " +
|
09 |
(checkingBalance + savingBalance));
|
10 |
final ExecutorService service = Executors.newFixedThreadPool( 10 );
|
11 |
service.execute( new Runnable() {
|
12 |
public void run() { portfolio.withdraw( true , 100 ); }
|
14 |
service.execute( new Runnable() {
|
15 |
public void run() { portfolio.withdraw( false , 100 ); }
|
19 |
checkingBalance = portfolio.getCheckingBalance();
|
20 |
savingBalance = portfolio.getSavingsBalance();
|
21 |
System.out.println( "Checking balance is " + checkingBalance);
|
22 |
System.out.println( "Savings balance is " + savingBalance);
|
23 |
System.out.println( "Total balance is " +
|
24 |
(checkingBalance + savingBalance));
|
25 |
if (checkingBalance + savingBalance < 1000 )
|
26 |
System.out.println( "Oops, broke the constraint!" );
|
正如我们在输出结果中所看到的那样,在默认情况下,Akka没能避免写偏斜问题,两个事务违反了银行的规定,即都从账户里取出了钱。
Checking balance is 500
Savings balance is 600
Total balance is 1100
Checking balance is 400
Savings balance is 500
Total balance is 900
Oops, broke the constraint!
现在到了该彻底解决这个问题的时候了。让我们祭出TransactionFactory这个能帮助我们在程序里对事物进行配置的法宝,在Portfolio类的第9行插入下面这段创建工厂实例的代码:
1 |
akka.stm.TransactionFactory factory = |
2 |
new akka.stm.TransactionFactoryBuilder()
|
在插进来的这几行代码中,我们创建了一个TransactionFactoryBuilder,并将writeSkew和trackReads属性分别设置为false和true。与Clojure STM对于ensure的处理类似,这两个设置项的目的是告诉事务要在其运行过程中对读操作进行追踪,同时也会使事务在读数据的过程中对账户余额变量加读锁直至提交开始为止。
除了上面提到的几处更改之外,Portfolio和UsePortfolio的其他代码都保持不变。而在对事务进行了上述设置之后,其输出结果如下所示:
Checking balance is 500
Savings balance is 600
Total balance is 1100
Sorry, can't withdraw due to constraint violation
Checking balance is 400
Savings balance is 600
Total balance is 1000
由于并发执行的不可预测性,我们不能确定两个事务到底哪个会胜出。但是我们可以从输出结果中看到,在所有操作结束后两个账户的余额是不同的,而在6.6节的Clojure示例中,最终两个账户余额是相同的。我们可以通过多次运行这两个实例来观察二者之间的差异。
在本节我们是用Java完成整个示例的。如果换成Scala,则我们可以使用在6.10节中学习的语法来配置事务的writeSkew和trackReads属性。