import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction.DataTransactionListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Main responsibility of the class is to check the MD-SAL data store read/write
- * transaction accumulation level and send statistics request if number of pending
+ * transaction accumulation level and send statistics request if number of pending
* read/write transactions are zero.
* @author avishnoi@in.ibm.com
*
private final Timer timer = new Timer("request-monitor", true);
// We need ordered retrieval, and O(1) contains operation
- private final Map<AbstractStatsTracker,Integer> requestQueue =
+ private final Map<AbstractStatsTracker,Integer> requestQueue =
Collections.synchronizedMap(new LinkedHashMap<AbstractStatsTracker,Integer>());
-
+
private Long PendingTransactions;
-
+
private long lastRequestTime = System.nanoTime();
-
+
private static final long REQUEST_MONITOR_INTERVAL = 1000;
-
+
private final TimerTask task = new TimerTask() {
@Override
public void run() {
- long now = System.nanoTime();
- if(now > lastRequestTime+TimeUnit.MILLISECONDS.toNanos(REQUEST_MONITOR_INTERVAL)){
- requestStatistics();
+ try{
+ long now = System.nanoTime();
+ if(now > lastRequestTime+TimeUnit.MILLISECONDS.toNanos(REQUEST_MONITOR_INTERVAL)){
+ requestStatistics();
+ }
+ }catch (IllegalArgumentException | IllegalStateException | NullPointerException e){
+ srsLogger.warn("Exception occured while sending statistics request : {}",e);
}
}
};
public StatisticsRequestScheduler(){
PendingTransactions = (long) 0;
}
-
+
public void addRequestToSchedulerQueue(AbstractStatsTracker statsRequest){
requestQueue.put(statsRequest, null);
}
-
+
+ public void removeRequestsFromSchedulerQueue(NodeRef node){
+ AbstractStatsTracker stats = null;
+ synchronized(requestQueue){
+ Iterator<Map.Entry<AbstractStatsTracker, Integer>> nodesItr = requestQueue.entrySet().iterator();
+ while(nodesItr.hasNext()){
+ stats = nodesItr.next().getKey();
+ if(stats.getNodeRef().equals(node)){
+ nodesItr.remove();
+ }
+ }
+ }
+
+ }
public AbstractStatsTracker getNextRequestFromSchedulerQueue(){
//Remove first element
AbstractStatsTracker stats = null;
private void requestStatistics(){
AbstractStatsTracker stats = this.getNextRequestFromSchedulerQueue();
- if(stats != null) {
- stats.request();
- stats.increaseRequestCounter();
- }
+ sendStatsRequest(stats);
}
@Override
public void onStatusUpdated(DataModificationTransaction transaction, TransactionStatus status) {
-
+
AbstractStatsTracker stats = null;
synchronized(PendingTransactions){
switch(status){
break;
}
}
+ sendStatsRequest(stats);
+ }
+
+ private void sendStatsRequest(AbstractStatsTracker stats){
if(stats != null){
- stats.request();
- stats.increaseRequestCounter();
+ try{
+ stats.request();
+ stats.increaseRequestCounter();
+ }catch(Exception e){
+ srsLogger.warn("Statistics request was not sent successfully. Reason : {}",e.getMessage());
+ }
}
}
-
public void start(){
timer.schedule(task, 0, REQUEST_MONITOR_INTERVAL);
}