BUG-9141: Fix Stateful07TopologySessionListener failing test 62/62962/1
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Mon, 11 Sep 2017 09:57:07 +0000 (11:57 +0200)
committerClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Mon, 11 Sep 2017 11:58:20 +0000 (13:58 +0200)
Some of the timers are rounding down, which is ending
on some statistics results in 0, when we have better
performance than expected.  Update the code to round up.

Use LongAdder instead of long for counters

Change-Id: I958f1c252ba94f8cd392979fe469555aee3f8d48
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
pcep/topology-provider/src/main/java/org/opendaylight/bgpcep/pcep/topology/provider/PCEPRequest.java
pcep/topology-provider/src/main/java/org/opendaylight/bgpcep/pcep/topology/provider/SessionListenerState.java

index e681197b250b8aa23b9f9d6b9df7f4300076f486..4f8e15d8d56a543555497185269dd5f82cb5f9cd 100755 (executable)
@@ -12,22 +12,28 @@ import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.Timer;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.lsp.metadata.Metadata;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 final class PCEPRequest {
+
+    private static final Logger LOG = LoggerFactory.getLogger(PCEPRequest.class);
+
+    private static final long MINIMUM_ELAPSED_TIME = 1;
+
     enum State {
         UNSENT,
         UNACKED,
         DONE,
     }
 
-    private static final Logger LOG = LoggerFactory.getLogger(PCEPRequest.class);
     private final SettableFuture<OperationResult> future;
     private final Metadata metadata;
     private volatile State state;
+    @GuardedBy("this")
     private final Stopwatch stopwatch;
     private final Timer timer;
 
@@ -88,7 +94,12 @@ final class PCEPRequest {
         }
     }
 
-    long getElapsedMillis() {
-        return this.stopwatch.elapsed(TimeUnit.MILLISECONDS);
+    synchronized long getElapsedMillis() {
+        final long elapsedNanos = this.stopwatch.elapsed().toNanos();
+        final long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(elapsedNanos);
+        if (elapsedMillis == 0 && elapsedNanos > 0) {
+            return  MINIMUM_ELAPSED_TIME;
+        }
+        return elapsedMillis;
     }
 }
index 340cde0438a3a468665bb7c915bc45571410dcf0..643cf829562f393f1a79f011864f86be4bd15aa8 100644 (file)
@@ -8,9 +8,11 @@
 
 package org.opendaylight.bgpcep.pcep.topology.provider;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.base.Stopwatch;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.LongAdder;
 import org.opendaylight.controller.config.yang.pcep.topology.provider.ErrorMessages;
 import org.opendaylight.controller.config.yang.pcep.topology.provider.LastReceivedError;
 import org.opendaylight.controller.config.yang.pcep.topology.provider.LastSentError;
@@ -28,19 +30,19 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.iet
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
 
 final class SessionListenerState {
-    private long lastReceivedRptMsgTimestamp = 0;
-    private long receivedRptMsgCount = 0;
-    private long sentUpdMsgCount = 0;
-    private long sentInitMsgCount = 0;
+    private final LongAdder lastReceivedRptMsgTimestamp = new LongAdder();
+    private final LongAdder receivedRptMsgCount = new LongAdder();
+    private final LongAdder sentUpdMsgCount = new LongAdder();
+    private final LongAdder sentInitMsgCount = new LongAdder();
     private PeerCapabilities capa;
     private LocalPref localPref;
     private PeerPref peerPref;
     private final Stopwatch sessionUpDuration;
 
-    private long minReplyTime = 0;
-    private long maxReplyTime = 0;
-    private long totalTime = 0;
-    private long reqCount = 0;
+    private final LongAdder minReplyTime = new LongAdder();
+    private final LongAdder maxReplyTime = new LongAdder();
+    private final LongAdder totalTime = new LongAdder();
+    private final LongAdder reqCount = new LongAdder();
 
     public SessionListenerState() {
         this.sessionUpDuration = Stopwatch.createUnstarted();
@@ -48,58 +50,59 @@ final class SessionListenerState {
     }
 
     public void init(final PCEPSession session) {
-        Preconditions.checkNotNull(session);
+        requireNonNull(session);
         this.localPref = getLocalPref(session.getLocalPref());
         this.peerPref = getPeerPref(session.getPeerPref());
         this.sessionUpDuration.start();
     }
 
-    public void processRequestStats(final long duration) {
-        if (this.minReplyTime == 0) {
-            this.minReplyTime = duration;
-        } else {
-            if (duration < this.minReplyTime) {
-                this.minReplyTime = duration;
-            }
+    public synchronized void processRequestStats(final long duration) {
+        if (this.minReplyTime.longValue() == 0) {
+            this.minReplyTime.reset();
+            this.minReplyTime.add(duration);
+        } else if (duration < this.minReplyTime.longValue()) {
+            this.minReplyTime.reset();
+            this.minReplyTime.add(duration);
         }
-        if (duration > this.maxReplyTime) {
-            this.maxReplyTime = duration;
+        if (duration > this.maxReplyTime.longValue()) {
+            this.maxReplyTime.reset();
+            this.maxReplyTime.add(duration);
         }
-        this.totalTime += duration;
-        this.reqCount++;
+        this.totalTime.add(duration);
+        this.reqCount.increment();
     }
 
-    public StatefulMessages getStatefulMessages() {
+    public synchronized StatefulMessages getStatefulMessages() {
         final StatefulMessages msgs = new StatefulMessages();
-        msgs.setLastReceivedRptMsgTimestamp(this.lastReceivedRptMsgTimestamp);
-        msgs.setReceivedRptMsgCount(this.receivedRptMsgCount);
-        msgs.setSentInitMsgCount(this.sentInitMsgCount);
-        msgs.setSentUpdMsgCount(this.sentUpdMsgCount);
+        msgs.setLastReceivedRptMsgTimestamp(this.lastReceivedRptMsgTimestamp.longValue());
+        msgs.setReceivedRptMsgCount(this.receivedRptMsgCount.longValue());
+        msgs.setSentInitMsgCount(this.sentInitMsgCount.longValue());
+        msgs.setSentUpdMsgCount(this.sentUpdMsgCount.longValue());
         return msgs;
     }
 
-    public void resetStats(final PCEPSession session) {
-        Preconditions.checkNotNull(session);
-        this.receivedRptMsgCount = 0;
-        this.sentInitMsgCount = 0;
-        this.sentUpdMsgCount = 0;
-        this.lastReceivedRptMsgTimestamp = 0;
-        this.maxReplyTime = 0;
-        this.minReplyTime = 0;
-        this.totalTime = 0;
-        this.reqCount = 0;
+    public synchronized void resetStats(final PCEPSession session) {
+        requireNonNull(session);
+        this.receivedRptMsgCount.reset();
+        this.sentInitMsgCount.reset();
+        this.sentUpdMsgCount.reset();
+        this.lastReceivedRptMsgTimestamp.reset();
+        this.maxReplyTime.reset();
+        this.minReplyTime.reset();
+        this.totalTime.reset();
+        this.reqCount.reset();
         session.resetStats();
     }
 
-    public ReplyTime getReplyTime() {
+    public synchronized ReplyTime getReplyTime() {
         final ReplyTime time = new ReplyTime();
         long avg = 0;
-        if (this.reqCount != 0) {
-            avg = this.totalTime / this.reqCount;
+        if (this.reqCount.longValue() != 0) {
+            avg = Math.round((double)this.totalTime.longValue()/this.reqCount.longValue());
         }
         time.setAverageTime(avg);
-        time.setMaxTime(this.maxReplyTime);
-        time.setMinTime(this.minReplyTime);
+        time.setMaxTime(this.maxReplyTime.longValue());
+        time.setMinTime(this.minReplyTime.longValue());
         return time;
     }
 
@@ -108,7 +111,7 @@ final class SessionListenerState {
     }
 
     public SessionState getSessionState(final PCEPSession session) {
-        Preconditions.checkNotNull(session);
+        requireNonNull(session);
         final SessionState state = new SessionState();
         state.setLocalPref(this.localPref);
         state.setPeerPref(this.peerPref);
@@ -118,23 +121,25 @@ final class SessionListenerState {
     }
 
     public void setPeerCapabilities(final PeerCapabilities capabilities) {
-        this.capa = Preconditions.checkNotNull(capabilities);
+        this.capa = requireNonNull(capabilities);
     }
 
     public void updateLastReceivedRptMsg() {
-        this.lastReceivedRptMsgTimestamp = StatisticsUtil.getCurrentTimestampInSeconds();
-        this.receivedRptMsgCount++;
+        this.lastReceivedRptMsgTimestamp.reset();
+        this.lastReceivedRptMsgTimestamp.add(StatisticsUtil.getCurrentTimestampInSeconds());
+        this.receivedRptMsgCount.increment();
     }
 
     public void updateStatefulSentMsg(final Message msg) {
         if (msg instanceof Pcinitiate) {
-            this.sentInitMsgCount++;
+            this.sentInitMsgCount.increment();
         } else if (msg instanceof Pcupd) {
-            this.sentUpdMsgCount++;
+            this.sentUpdMsgCount.increment();
         }
     }
 
-    private static LocalPref getLocalPref(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.LocalPref localPref) {
+    private static LocalPref getLocalPref(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
+        .controller.pcep.stats.rev141006.pcep.session.state.LocalPref localPref) {
         final LocalPref local = new LocalPref();
         local.setDeadtimer(localPref.getDeadtimer());
         local.setIpAddress(localPref.getIpAddress());
@@ -143,7 +148,8 @@ final class SessionListenerState {
         return local;
     }
 
-    private static PeerPref getPeerPref(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.PeerPref peerPref) {
+    private static PeerPref getPeerPref(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
+        .controller.pcep.stats.rev141006.pcep.session.state.PeerPref peerPref) {
         final PeerPref peer = new PeerPref();
         peer.setDeadtimer(peerPref.getDeadtimer());
         peer.setIpAddress(peerPref.getIpAddress());
@@ -152,7 +158,8 @@ final class SessionListenerState {
         return peer;
     }
 
-    private static Messages getMessageStats(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.Messages messages) {
+    private static Messages getMessageStats(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
+        .controller.pcep.stats.rev141006.pcep.session.state.Messages messages) {
         final LastReceivedError lastReceivedError = new LastReceivedError();
         lastReceivedError.setErrorType(messages.getErrorMessages().getLastReceivedError().getErrorType());
         lastReceivedError.setErrorValue(messages.getErrorMessages().getLastReceivedError().getErrorValue());