2 * Copyright IBM Corporation, 2013. All rights reserved.
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
8 package org.opendaylight.controller.md.statistics.manager;
10 import java.util.Collections;
11 import java.util.Iterator;
12 import java.util.LinkedHashMap;
14 import java.util.Timer;
15 import java.util.TimerTask;
16 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
19 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
20 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction.DataTransactionListener;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Main responsibility of the class is to check the MD-SAL data store read/write
26 * transaction accumulation level and send statistics request if number of pending
27 * read/write transactions are zero.
28 * @author avishnoi@in.ibm.com
31 @SuppressWarnings("rawtypes")
32 public class StatisticsRequestScheduler implements DataTransactionListener {
34 private static final Logger srsLogger = LoggerFactory.getLogger(StatisticsRequestScheduler.class);
35 private final Timer timer = new Timer("request-monitor", true);
37 // We need ordered retrieval, and O(1) contains operation
38 private final Map<AbstractStatsTracker,Integer> requestQueue =
39 Collections.synchronizedMap(new LinkedHashMap<AbstractStatsTracker,Integer>());
41 private Long PendingTransactions;
43 private long lastRequestTime = System.nanoTime();
45 private static final long REQUEST_MONITOR_INTERVAL = 1000;
47 private final TimerTask task = new TimerTask() {
50 long now = System.nanoTime();
51 if(now > lastRequestTime+TimeUnit.MILLISECONDS.toNanos(REQUEST_MONITOR_INTERVAL)){
57 public StatisticsRequestScheduler(){
58 PendingTransactions = (long) 0;
61 public void addRequestToSchedulerQueue(AbstractStatsTracker statsRequest){
62 requestQueue.put(statsRequest, null);
65 public AbstractStatsTracker getNextRequestFromSchedulerQueue(){
66 //Remove first element
67 AbstractStatsTracker stats = null;
68 synchronized(requestQueue){
69 Iterator<Map.Entry<AbstractStatsTracker, Integer>> nodesItr = requestQueue.entrySet().iterator();
70 if(nodesItr.hasNext()){
71 stats = nodesItr.next().getKey();
72 srsLogger.debug("{} chosen up for execution",stats.getNodeRef());
80 private void requestStatistics(){
81 AbstractStatsTracker stats = this.getNextRequestFromSchedulerQueue();
84 stats.increaseRequestCounter();
88 public void onStatusUpdated(DataModificationTransaction transaction, TransactionStatus status) {
90 AbstractStatsTracker stats = null;
91 synchronized(PendingTransactions){
94 this.PendingTransactions++;
98 this.PendingTransactions--;
99 if(PendingTransactions == 0){
100 lastRequestTime = System.nanoTime();
101 stats = this.getNextRequestFromSchedulerQueue();
103 srsLogger.debug("Pending MD-SAL transactions : {} & Scheduler queue size : {}",this.PendingTransactions,this.requestQueue.size());
111 stats.increaseRequestCounter();
116 timer.schedule(task, 0, REQUEST_MONITOR_INTERVAL);