BUG 3125 : Set Rate Limit just before acquiring a permit to avoid contention
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionRateLimitingCallback.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import com.codahale.metrics.Timer;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
14
15 /**
16  * TransactionRateLimitingCallback computes the new transaction rate limit on the successful completion of a
17  * transaction
18  */
19 public class TransactionRateLimitingCallback implements OperationCallback{
20     private final Timer commitTimer;
21     private Timer.Context timerContext;
22
23     TransactionRateLimitingCallback(ActorContext actorContext){
24         commitTimer = actorContext.getOperationTimer(ActorContext.COMMIT);
25     }
26
27     @Override
28     public void run() {
29         timerContext = commitTimer.time();
30     }
31
32     @Override
33     public void success() {
34         Preconditions.checkState(timerContext != null, "Call run before success");
35         timerContext.stop();
36     }
37
38     @Override
39     public void failure() {
40         // This would mean we couldn't get a transaction completed in 30 seconds which is
41         // the default transaction commit timeout. Using the timeout information to figure out the rate limit is
42         // not going to be useful - so we leave it as it is
43     }
44
45 }