From e6122a555939321d03dce91fee5072749d77e4b8 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 13 Feb 2014 08:00:13 +0100 Subject: [PATCH] Start rework of MultipartMessageManager - TxIdEntry should be private static final - Remove unused method - Use System.nanoTime() for timekeeping Change-Id: I51bf99082aadeb1f83055a0bf6a07e320c089db0 Signed-off-by: Robert Varga --- .../manager/MultipartMessageManager.java | 70 ++++++++----------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/opendaylight/md-sal/statistics-manager/src/main/java/org/opendaylight/controller/md/statistics/manager/MultipartMessageManager.java b/opendaylight/md-sal/statistics-manager/src/main/java/org/opendaylight/controller/md/statistics/manager/MultipartMessageManager.java index 6f58708e1b..02b397e78f 100644 --- a/opendaylight/md-sal/statistics-manager/src/main/java/org/opendaylight/controller/md/statistics/manager/MultipartMessageManager.java +++ b/opendaylight/md-sal/statistics-manager/src/main/java/org/opendaylight/controller/md/statistics/manager/MultipartMessageManager.java @@ -7,43 +7,42 @@ */ package org.opendaylight.controller.md.statistics.manager; -import java.util.Date; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; /** - * Main responsibility of the class is to manage multipart response + * Main responsibility of the class is to manage multipart response * for multipart request. It also handles the flow aggregate request - * and response mapping. + * and response mapping. * @author avishnoi@in.ibm.com * */ public class MultipartMessageManager { /* - * Map for tx id and type of request, to keep track of all the request sent - * by Statistics Manager. Statistics Manager won't entertain any multipart - * response for which it didn't send the request. + * Map for tx id and type of request, to keep track of all the request sent + * by Statistics Manager. Statistics Manager won't entertain any multipart + * response for which it didn't send the request. */ - - private static Map txIdToRequestTypeMap = new ConcurrentHashMap(); + private final Map txIdToRequestTypeMap = new ConcurrentHashMap<>(); /* * Map to keep track of the request tx id for flow table statistics request. * Because flow table statistics multi part response do not contains the table id. */ - private static Map txIdTotableIdMap = new ConcurrentHashMap(); - - private final int NUMBER_OF_WAIT_CYCLES =2; + private final Map txIdTotableIdMap = new ConcurrentHashMap<>(); - class TxIdEntry{ + private static final int NUMBER_OF_WAIT_CYCLES =2; + + private static final class TxIdEntry { private final TransactionId txId; private final NodeId nodeId; private final StatsRequestType requestType; - + public TxIdEntry(NodeId nodeId, TransactionId txId, StatsRequestType requestType){ this.txId = txId; this.nodeId = nodeId; @@ -62,7 +61,6 @@ public class MultipartMessageManager { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + getOuterType().hashCode(); result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode()); result = prime * result + ((txId == null) ? 0 : txId.hashCode()); return result; @@ -79,9 +77,7 @@ public class MultipartMessageManager { return false; } TxIdEntry other = (TxIdEntry) obj; - if (!getOuterType().equals(other.getOuterType())) { - return false; - } + if (nodeId == null) { if (other.nodeId != null) { return false; @@ -98,52 +94,42 @@ public class MultipartMessageManager { } return true; } - private MultipartMessageManager getOuterType() { - return MultipartMessageManager.this; - } + @Override public String toString() { return "TxIdEntry [txId=" + txId + ", nodeId=" + nodeId + ", requestType=" + requestType + "]"; } } - public MultipartMessageManager(){} - public Short getTableIdForTxId(NodeId nodeId,TransactionId id){ - return txIdTotableIdMap.get(new TxIdEntry(nodeId,id,null)); - } - + public void setTxIdAndTableIdMapEntry(NodeId nodeId, TransactionId id,Short tableId){ if(id == null) return; txIdTotableIdMap.put(new TxIdEntry(nodeId,id,null), tableId); } - + public boolean isRequestTxIdExist(NodeId nodeId, TransactionId id, Boolean moreRepliesToFollow){ TxIdEntry entry = new TxIdEntry(nodeId,id,null); if(moreRepliesToFollow.booleanValue()){ return txIdToRequestTypeMap.containsKey(entry); }else{ - return txIdToRequestTypeMap.remove(entry)==null?false:true; + return txIdToRequestTypeMap.remove(entry) != null; } } + public void addTxIdToRequestTypeEntry (NodeId nodeId, TransactionId id,StatsRequestType type){ if(id == null) return; TxIdEntry entry = new TxIdEntry(nodeId,id,type); txIdToRequestTypeMap.put(entry, getExpiryTime()); } - public boolean removeTxId(NodeId nodeId, TransactionId id){ - TxIdEntry entry = new TxIdEntry(nodeId,id,null); - return txIdToRequestTypeMap.remove(entry)==null?false:true; - } - - private Date getExpiryTime(){ - Date expires = new Date(); - expires.setTime(expires.getTime()+StatisticsProvider.STATS_THREAD_EXECUTION_TIME*NUMBER_OF_WAIT_CYCLES); - return expires; + + private static Long getExpiryTime(){ + return System.nanoTime() + TimeUnit.MILLISECONDS.toNanos( + StatisticsProvider.STATS_THREAD_EXECUTION_TIME*NUMBER_OF_WAIT_CYCLES); } public enum StatsRequestType{ @@ -157,16 +143,18 @@ public class MultipartMessageManager { GROUP_DESC, METER_CONFIG } - + public void cleanStaleTransactionIds(){ + final long now = System.nanoTime(); + for (Iterator it = txIdToRequestTypeMap.keySet().iterator();it.hasNext();){ TxIdEntry txIdEntry = it.next(); - Date now = new Date(); - Date expiryTime = txIdToRequestTypeMap.get(txIdEntry); - if(now.after(expiryTime)){ + + Long expiryTime = txIdToRequestTypeMap.get(txIdEntry); + if(now > expiryTime){ it.remove(); txIdTotableIdMap.remove(txIdEntry); - } + } } } } -- 2.36.6