Replace UnsignedInt32Counter by LongAdder 26/49926/3
authorClaudio D. Gasparini <cgaspari@cisco.com>
Mon, 19 Dec 2016 22:36:09 +0000 (23:36 +0100)
committerClaudio D. Gasparini <cgaspari@cisco.com>
Thu, 5 Jan 2017 17:52:17 +0000 (18:52 +0100)
Replace UnsignedInt32Counter, which is not
efficient and only provides of logs which are of
not use for a more suitable counter(LongAdder).

Change-Id: Iaad4879c9c3a783ac45f21d47f3379b22f22e3e4
Signed-off-by: Claudio D. Gasparini <cgaspari@cisco.com>
16 files changed:
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AdjRibOutListener.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/ApplicationPeer.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/CountersUtil.java [new file with mode: 0644]
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/EffectiveRibInWriter.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/LocRibWriter.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/UnsignedInt32Counter.java [deleted file]
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/peer/BGPPeerStats.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/peer/BGPPeerStatsImpl.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/peer/route/PerTableTypeRouteCounter.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/rib/impl/BGPRenderStats.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/rib/impl/BGPRenderStatsImpl.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/rib/impl/RIBImplRuntimeMXBeanImpl.java
bgp/rib-impl/src/test/java/org/opendaylight/controller/config/yang/bgp/rib/impl/AbstractRIBImplModuleTest.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/config/AbstractConfig.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/stats/rib/impl/BGPRenderStatsImplTest.java

index beb1177e645a496a100a434047f6c700ec2479c9..4dddf38128a663b45903e916e11a15a946da161a 100644 (file)
@@ -12,6 +12,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.atomic.LongAdder;
 import javax.annotation.Nonnull;
 import javax.annotation.concurrent.NotThreadSafe;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -20,7 +21,6 @@ import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.protocol.bgp.rib.impl.spi.Codecs;
 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
@@ -64,11 +64,11 @@ final class AdjRibOutListener implements ClusteredDOMDataTreeChangeListener {
     private final RIBSupport support;
     private final boolean mpSupport;
     private final ListenerRegistration<AdjRibOutListener> registerDataTreeChangeListener;
-    private final UnsignedInt32Counter routeCounter;
+    private final LongAdder routeCounter;
 
     private AdjRibOutListener(final PeerId peerId, final TablesKey tablesKey, final YangInstanceIdentifier ribId,
         final CodecsRegistry registry, final RIBSupport support, final DOMDataTreeChangeService service,
-        final ChannelOutputLimiter session, final boolean mpSupport, final UnsignedInt32Counter routeCounter) {
+        final ChannelOutputLimiter session, final boolean mpSupport, final LongAdder routeCounter) {
         this.session = Preconditions.checkNotNull(session);
         this.support = Preconditions.checkNotNull(support);
         this.codecs = registry.getCodecs(this.support);
@@ -80,7 +80,7 @@ final class AdjRibOutListener implements ClusteredDOMDataTreeChangeListener {
 
     static AdjRibOutListener create(@Nonnull final PeerId peerId, @Nonnull final TablesKey tablesKey, @Nonnull final YangInstanceIdentifier ribId,
         @Nonnull final CodecsRegistry registry, @Nonnull final RIBSupport support, @Nonnull final DOMDataTreeChangeService service,
-        @Nonnull final ChannelOutputLimiter session, @Nonnull final boolean mpSupport, @Nonnull final UnsignedInt32Counter routeCounter
+        @Nonnull final ChannelOutputLimiter session, @Nonnull final boolean mpSupport, @Nonnull final LongAdder routeCounter
     ) {
         return new AdjRibOutListener(peerId, tablesKey, ribId, registry, support, service, session, mpSupport, routeCounter);
     }
@@ -137,7 +137,7 @@ final class AdjRibOutListener implements ClusteredDOMDataTreeChangeListener {
     }
 
     private Update withdraw(final MapEntryNode route) {
-        this.routeCounter.decreaseCount();
+        this.routeCounter.decrement();
         if (!this.mpSupport) {
             return buildUpdate(Collections.<MapEntryNode>emptyList(), Collections.singleton(route), routeAttributes(route));
         }
@@ -145,7 +145,7 @@ final class AdjRibOutListener implements ClusteredDOMDataTreeChangeListener {
     }
 
     private Update advertise(final MapEntryNode route) {
-        this.routeCounter.increaseCount();
+        this.routeCounter.increment();
         if (!this.mpSupport) {
             return buildUpdate(Collections.singleton(route), Collections.<MapEntryNode>emptyList(), routeAttributes(route));
         }
index bf65fdc7c436eabb3f94f7e23f039c331d2a4c16..cfdedff7e09dc2a16792fcab44c3376173244d3d 100644 (file)
@@ -130,7 +130,7 @@ public class ApplicationPeer implements AutoCloseable, org.opendaylight.protocol
         };
         this.adjRibInWriter = this.adjRibInWriter.transform(peerId, context, localTables, Collections.emptyMap(),
             registerAppPeerListener);
-        this.peerStats = new BGPPeerStatsImpl(this.name, localTables);
+        this.peerStats = new BGPPeerStatsImpl(localTables);
         this.effectiveRibInWriter = EffectiveRibInWriter.create(this.rib.getService(), this.rib.createPeerChain(this), this.peerIId,
             this.rib.getImportPolicyPeerTracker(), context, PeerRole.Internal, this.peerStats.getEffectiveRibInRouteCounters(),
             this.peerStats.getAdjRibInRouteCounters());
index fe62c9efb29c3449d9a4023e8c9c0885e8f040c3..da7655401638bc4e4461f695253415f7b4327c6b 100644 (file)
@@ -114,7 +114,7 @@ public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeer
     private final String name;
     private BGPPeerRuntimeRegistrator registrator;
     private BGPPeerRuntimeRegistration runtimeReg;
-    private final Map<TablesKey, AdjRibOutListener> adjRibOutListenerSet = new HashMap();
+    private final Map<TablesKey, AdjRibOutListener> adjRibOutListenerSet = new HashMap<>();
     private final RpcProviderRegistry rpcRegistry;
     private RoutedRpcRegistration<BgpPeerRpcService> rpcRegistration;
     private final PeerRole peerRole;
@@ -129,7 +129,7 @@ public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeer
         this.rib = Preconditions.checkNotNull(rib);
         this.name = name;
         this.rpcRegistry = rpcRegistry;
-        this.peerStats = new BGPPeerStatsImpl(this.name, this.tables);
+        this.peerStats = new BGPPeerStatsImpl(this.tables);
 
         this.chain = rib.createPeerChain(this);
     }
@@ -140,7 +140,7 @@ public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeer
 
     public void instantiateServiceInstance() {
         // add current peer to "configured BGP peer" stats
-        this.rib.getRenderStats().getConfiguredPeerCounter().increaseCount();
+        this.rib.getRenderStats().getConfiguredPeerCounter().increment();
         this.ribWriter = AdjRibInWriter.create(rib.getYangRibId(), this.peerRole, this.simpleRoutingPolicy, this.chain);
     }
 
@@ -320,7 +320,7 @@ public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeer
         this.ribWriter = this.ribWriter.transform(peerId, this.rib.getRibSupportContext(), this.tables, addPathTableMaps);
 
         // register BGP Peer stats
-        this.peerStats.getSessionEstablishedCounter().increaseCount();
+        this.peerStats.getSessionEstablishedCounter().increment();
         if (this.registrator != null) {
             this.runtimeReg = this.registrator.register(this);
         }
@@ -332,7 +332,7 @@ public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeer
             this.rpcRegistration.registerPath(PeerContext.class, path);
         }
 
-        this.rib.getRenderStats().getConnectedPeerCounter().increaseCount();
+        this.rib.getRenderStats().getConnectedPeerCounter().increment();
     }
 
     private void createAdjRibOutListener(final PeerId peerId) {
@@ -434,7 +434,7 @@ public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeer
             }
             this.session = null;
 
-            this.rib.getRenderStats().getConnectedPeerCounter().decreaseCount();
+            this.rib.getRenderStats().getConnectedPeerCounter().decrement();
         }
     }
 
diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/CountersUtil.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/CountersUtil.java
new file mode 100644 (file)
index 0000000..003c329
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.rib.impl;
+
+import java.util.concurrent.atomic.LongAdder;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides util for increment/decrement counters whenever is not null
+ * Otherwise prints an informative warn.
+ */
+public final class CountersUtil {
+    private static final Logger LOG = LoggerFactory.getLogger(CountersUtil.class);
+
+    private CountersUtil() {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Increments counter by 1 if supported, otherwise produce a warn
+     *
+     * @param counter counter
+     * @param tablesKey tablesKey Type
+     */
+    static void increment(@Nullable final LongAdder counter, @Nonnull TablesKey tablesKey) {
+        if (counter != null) {
+            counter.increment();
+            return;
+        }
+        LOG.warn("Family {} not supported", tablesKey);
+    }
+
+    /**
+     * Increments counter by 1 if supported, otherwise produce a warn
+     *  @param counter counter
+     * @param tablesKey tablesKey Type
+     */
+    static void decrement(@Nullable final LongAdder counter, @Nonnull TablesKey tablesKey) {
+        if (counter != null) {
+            counter.decrement();
+            return;
+        }
+        LOG.warn("Family {} not supported", tablesKey);
+    }
+
+    public static ZeroBasedCounter32 toZeroBasedCounter32(final LongAdder longAdder){
+        return new ZeroBasedCounter32(longAdder.longValue());
+    }
+}
index b62e0ec879c6b74f134de4ef278c193e2d1b1135..bb3dad7e50fa64abe475c8a80534b069df7c632f 100644 (file)
@@ -15,6 +15,7 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.LongAdder;
 import javax.annotation.Nonnull;
 import javax.annotation.concurrent.NotThreadSafe;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -101,7 +102,7 @@ final class EffectiveRibInWriter implements AutoCloseable {
          */
         @Deprecated
         AdjInTracker(final DOMDataTreeChangeService service, final RIBSupportContextRegistry registry, final DOMTransactionChain chain, final YangInstanceIdentifier peerIId) {
-            this(service, registry, chain, peerIId, new PerTableTypeRouteCounter("effective-rib-in"), new PerTableTypeRouteCounter("adj-rib-in"));
+            this(service, registry, chain, peerIId, new PerTableTypeRouteCounter(), new PerTableTypeRouteCounter());
         }
 
         private void updateRoute(@Nonnull final PerTableTypeRouteCounter counter, @Nonnull final Map<TablesKey, Set<YangInstanceIdentifier>> routeMap,
@@ -130,8 +131,9 @@ final class EffectiveRibInWriter implements AutoCloseable {
 
         private void updateRouteCounter(@Nonnull final PerTableTypeRouteCounter counter, @Nonnull final Map<TablesKey, Set<YangInstanceIdentifier>> routeMap,
                 @Nonnull final TablesKey tablesKey) {
-            counter.getCounterOrSetDefault(tablesKey)
-            .setCount(routeMap.getOrDefault(tablesKey, new HashSet<>()).size());
+            final LongAdder tableCounter = counter.getCounterOrSetDefault(tablesKey);
+            tableCounter.reset();
+            tableCounter.add(routeMap.getOrDefault(tablesKey, new HashSet<>()).size());
         }
 
         private void processRoute(final DOMDataWriteTransaction tx, final RIBSupport ribSupport, final AbstractImportPolicy policy, final YangInstanceIdentifier routesPath, final DataTreeCandidateNode route) {
index 3cbd173c11b78eaa91231ea076e590824f3bb973..d6ad73ec11dc13f2cd878eb620196f5230fda660 100644 (file)
@@ -14,6 +14,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.LongAdder;
 import javax.annotation.Nonnull;
 import javax.annotation.concurrent.NotThreadSafe;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -25,7 +26,6 @@ import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
 import org.opendaylight.protocol.bgp.mode.api.RouteEntry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup;
@@ -73,11 +73,11 @@ final class LocRibWriter implements AutoCloseable, ClusteredDOMDataTreeChangeLis
     private final TablesKey localTablesKey;
     private final ListenerRegistration<LocRibWriter> reg;
     private final PathSelectionMode pathSelectionMode;
-    private final UnsignedInt32Counter routeCounter;
+    private final LongAdder routeCounter;
 
     private LocRibWriter(final RIBSupportContextRegistry registry, final DOMTransactionChain chain, final YangInstanceIdentifier target,
         final Long ourAs, final DOMDataTreeChangeService service, final ExportPolicyPeerTracker exportPolicyPeerTracker, final TablesKey tablesKey,
-        @Nonnull final PathSelectionMode pathSelectionMode, final UnsignedInt32Counter routeCounter) {
+        @Nonnull final PathSelectionMode pathSelectionMode, final LongAdder routeCounter) {
         this.chain = Preconditions.checkNotNull(chain);
         final NodeIdentifierWithPredicates tableKey = RibSupportUtils.toYangTablesKey(tablesKey);
         this.localTablesKey = tablesKey;
@@ -101,7 +101,7 @@ final class LocRibWriter implements AutoCloseable, ClusteredDOMDataTreeChangeLis
 
     public static LocRibWriter create(@Nonnull final RIBSupportContextRegistry registry, @Nonnull final TablesKey tablesKey, @Nonnull final DOMTransactionChain chain,
         @Nonnull final YangInstanceIdentifier target, @Nonnull final AsNumber ourAs, @Nonnull final DOMDataTreeChangeService service, @Nonnull final ExportPolicyPeerTracker ep,
-        @Nonnull final PathSelectionMode pathSelectionStrategy, @Nonnull final UnsignedInt32Counter routeCounter) {
+        @Nonnull final PathSelectionMode pathSelectionStrategy, @Nonnull final LongAdder routeCounter) {
         return new LocRibWriter(registry, chain, target, ourAs.getValue(), service, ep, tablesKey, pathSelectionStrategy, routeCounter);
     }
 
@@ -211,7 +211,8 @@ final class LocRibWriter implements AutoCloseable, ClusteredDOMDataTreeChangeLis
      * Update the statistic of loc-rib route
      */
     private void updateRouteCounter() {
-        routeCounter.setCount(this.routeEntries.size());
+        this.routeCounter.reset();
+        this.routeCounter.add(this.routeEntries.size());
     }
 
     private void walkThrough(final DOMDataWriteTransaction tx, final Set<Map.Entry<RouteUpdateKey, RouteEntry>> toUpdate) {
diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/UnsignedInt32Counter.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/stats/UnsignedInt32Counter.java
deleted file mode 100644 (file)
index 50fdd88..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.bgp.rib.impl.stats;
-
-import com.google.common.base.Preconditions;
-import java.util.concurrent.atomic.AtomicLong;
-import javax.annotation.Nonnull;
-import javax.annotation.concurrent.ThreadSafe;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * @author Kevin Wang
- */
-@ThreadSafe
-public class UnsignedInt32Counter {
-    private static final Logger LOG = LoggerFactory.getLogger(UnsignedInt32Counter.class);
-    private static final long MAX_VALUE = 4294967295L;
-
-    private final String counterName;
-    private final AtomicLong count = new AtomicLong();
-
-    public UnsignedInt32Counter(@Nonnull final String counterName) {
-        this.counterName = Preconditions.checkNotNull(counterName);
-    }
-
-    private static long safetyCheck(final long value) {
-        return Math.min(Math.max(0L, value), MAX_VALUE);
-    }
-
-    public long increaseCount(final long change) {
-        final long result;
-        if (change == 0) {
-            result = getCount();
-        } else {
-            Preconditions.checkArgument(change > 0, "Count number %s must be a positive number.", change);
-            result = this.count.addAndGet(change);
-            LOG.debug("Counter [{}] is increased by {}. Current count: {}", this.counterName, change, result);
-            if (result > MAX_VALUE) {
-                LOG.warn("Counter [{}] has exceeded the max allowed value {}. Counter's current value {} is invalid.", this.counterName, MAX_VALUE, result);
-            }
-        }
-        return result;
-    }
-
-    public long increaseCount() {
-        return this.increaseCount(1);
-    }
-
-    public long decreaseCount(final long change) {
-        final long result;
-        if (change == 0) {
-            result = getCount();
-        } else {
-            Preconditions.checkArgument(change > 0, "Count number %s must be a positive number.", change);
-            result = this.count.addAndGet(-change);
-            LOG.debug("Counter [{}] is decreased by {}. Current count: {}", this.counterName, change, result);
-            if (result < 0) {
-                // In most case, we do not want the BGP session get into trouble due to an ERROR in counter
-                // so here we print ERROR log instead of throwing out exception
-                LOG.warn("Counter {} must not be less than 0. Counter's current value {} is invalid.", this.counterName, result);
-            }
-        }
-        return result;
-    }
-
-    public long decreaseCount() {
-        return this.decreaseCount(1);
-    }
-
-    public long getCount() {
-        return this.count.get();
-    }
-
-    public void setCount(final long count) {
-        final long result = getCount();
-        if (count != result) {
-            LOG.debug("Value of counter [{}] changes to {} (previous value is {})", this.counterName, count, result);
-            this.count.set(count);
-        }
-    }
-
-    public void resetCount() {
-        LOG.debug("Value of counter [{}] is reset to 0.", this.counterName);
-        setCount(0L);
-    }
-
-    public ZeroBasedCounter32 getCountAsZeroBasedCounter32() {
-        return new ZeroBasedCounter32(safetyCheck(getCount()));
-    }
-}
index 39494418136f5c83ed8b8f034da9ba6c12a4bb53..ae251e8b0cf0d339ab59d0ce9d265285ad0f323a 100644 (file)
@@ -7,13 +7,10 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl.stats.peer;
 
+import java.util.concurrent.atomic.LongAdder;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
 
-/**
- * @author Kevin Wang
- */
 public interface BGPPeerStats {
     BgpPeerState getBgpPeerState();
 
@@ -23,5 +20,5 @@ public interface BGPPeerStats {
 
     PerTableTypeRouteCounter getEffectiveRibInRouteCounters();
 
-    UnsignedInt32Counter getSessionEstablishedCounter();
+    LongAdder getSessionEstablishedCounter();
 }
index 537a578e2cab08554e06264903580467782cc5c4..df41686296de631f72c522aa765e2fa99eb8e033 100644 (file)
@@ -7,15 +7,17 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl.stats.peer;
 
+import static org.opendaylight.protocol.bgp.rib.impl.CountersUtil.toZeroBasedCounter32;
+
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.atomic.LongAdder;
 import javax.annotation.Nonnull;
 import org.opendaylight.controller.config.api.IdentityAttributeRef;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.RouteTable;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
@@ -26,15 +28,13 @@ public final class BGPPeerStatsImpl implements BGPPeerStats {
     private final PerTableTypeRouteCounter adjRibInRouteCounters;
     private final PerTableTypeRouteCounter adjRibOutRouteCounters;
     private final PerTableTypeRouteCounter effectiveRibInRouteCounters;
-    private final UnsignedInt32Counter sessionEstablishedCounter = new UnsignedInt32Counter("SESSION ESTABLISHED");
-    private final String peerName;
+    private final LongAdder sessionEstablishedCounter = new LongAdder();
 
-    public BGPPeerStatsImpl(@Nonnull final String peerName, @Nonnull final Set<TablesKey> tablesKeySet) {
-        this.peerName = Preconditions.checkNotNull(peerName);
+    public BGPPeerStatsImpl(@Nonnull final Set<TablesKey> tablesKeySet) {
         this.tablesKeySet = Preconditions.checkNotNull(tablesKeySet);
-        this.adjRibInRouteCounters = new PerTableTypeRouteCounter("["+ this.peerName +"] adj-rib-in route", tablesKeySet);
-        this.adjRibOutRouteCounters = new PerTableTypeRouteCounter("["+ this.peerName +"] adj-rib-out route", tablesKeySet);
-        this.effectiveRibInRouteCounters = new PerTableTypeRouteCounter("["+ this.peerName +"] effective-rib-in route", tablesKeySet);
+        this.adjRibInRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
+        this.adjRibOutRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
+        this.effectiveRibInRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
     }
 
     public PerTableTypeRouteCounter getAdjRibInRouteCounters() {
@@ -60,9 +60,9 @@ public final class BGPPeerStatsImpl implements BGPPeerStats {
         routeTable.setAfi(new IdentityAttributeRef(afiQName.toString()));
         routeTable.setSafi(new IdentityAttributeRef(safiQName.toString()));
         // we want to get default counter in case particular route table is not initialized (e.g. adj-rib-out is not initialized in some cases)
-        routeTable.setAdjRibInRoutesCount(adjRibInRouteCounters.getCounterOrDefault(tablesKey).getCountAsZeroBasedCounter32());
-        routeTable.setAdjRibOutRoutesCount(adjRibOutRouteCounters.getCounterOrDefault(tablesKey).getCountAsZeroBasedCounter32());
-        routeTable.setEffectiveRibInRoutesCount(effectiveRibInRouteCounters.getCounterOrDefault(tablesKey).getCountAsZeroBasedCounter32());
+        routeTable.setAdjRibInRoutesCount(toZeroBasedCounter32(this.adjRibInRouteCounters.getCounterOrDefault(tablesKey)));
+        routeTable.setAdjRibOutRoutesCount(toZeroBasedCounter32(this.adjRibOutRouteCounters.getCounterOrDefault(tablesKey)));
+        routeTable.setEffectiveRibInRoutesCount(toZeroBasedCounter32(this.effectiveRibInRouteCounters.getCounterOrDefault(tablesKey)));
 
         return routeTable;
     }
@@ -71,14 +71,14 @@ public final class BGPPeerStatsImpl implements BGPPeerStats {
     public BgpPeerState getBgpPeerState() {
         final BgpPeerState peerState = new BgpPeerState();
         final List<RouteTable> routes = Lists.newArrayList();
-        this.tablesKeySet.stream().forEach(tablesKey -> routes.add(createRouteTable(tablesKey)));
+        this.tablesKeySet.forEach(tablesKey -> routes.add(createRouteTable(tablesKey)));
         peerState.setRouteTable(routes);
-        peerState.setSessionEstablishedCount(this.sessionEstablishedCounter.getCountAsZeroBasedCounter32());
+        peerState.setSessionEstablishedCount(toZeroBasedCounter32(this.sessionEstablishedCounter));
         return peerState;
     }
 
     @Override
-    public UnsignedInt32Counter getSessionEstablishedCounter() {
+    public LongAdder getSessionEstablishedCounter() {
         return this.sessionEstablishedCounter;
     }
 }
index 07bdf06f8e7a08a7d7f8efad930c2d94465d18de..e2af7a3378210916070994ab7945007caca28e61 100644 (file)
@@ -11,8 +11,8 @@ import com.google.common.base.Preconditions;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.LongAdder;
 import javax.annotation.Nonnull;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -20,33 +20,27 @@ import org.slf4j.LoggerFactory;
 public final class PerTableTypeRouteCounter {
     private static final Logger LOG = LoggerFactory.getLogger(PerTableTypeRouteCounter.class);
 
-    private final Map<TablesKey, UnsignedInt32Counter> counters = new ConcurrentHashMap<>();
-    private final String counterName;
+    private final Map<TablesKey, LongAdder> counters = new ConcurrentHashMap<>();
 
-    private final UnsignedInt32Counter createCounter(@Nonnull final TablesKey tablesKey) {
-        return new UnsignedInt32Counter(this.counterName + tablesKey.toString());
-    }
-
-    public PerTableTypeRouteCounter(@Nonnull final String counterName) {
-        this.counterName = Preconditions.checkNotNull(counterName);
+    public PerTableTypeRouteCounter(@Nonnull final Set<TablesKey> tablesKeySet) {
+        init(tablesKeySet);
     }
 
-    public PerTableTypeRouteCounter(@Nonnull final String counterName, @Nonnull final Set<TablesKey> tablesKeySet) {
-        this(counterName);
-        init(tablesKeySet);
+    public PerTableTypeRouteCounter() {
     }
 
-    public final synchronized void init(@Nonnull Set<TablesKey> tablesKeySet) {
-        tablesKeySet.stream().forEach(tablesKey -> init(tablesKey));
+    private synchronized void init(@Nonnull Set<TablesKey> tablesKeySet) {
+        tablesKeySet.forEach(this::init);
     }
 
-    public final synchronized UnsignedInt32Counter init(@Nonnull final TablesKey tablesKey) {
-        UnsignedInt32Counter counter = this.counters.get(Preconditions.checkNotNull(tablesKey));
+    public final synchronized LongAdder init(@Nonnull final TablesKey tablesKey) {
+        LongAdder counter = this.counters.get(Preconditions.checkNotNull(tablesKey));
         if (counter == null) {
-            this.counters.put(tablesKey, counter = createCounter(tablesKey));
+            counter = new LongAdder();
+            this.counters.put(tablesKey, counter);
         }
         LOG.debug("Initializing route counter for tablesKey {}", tablesKey);
-        counter.resetCount();
+        counter.reset();
         return counter;
     }
 
@@ -56,8 +50,8 @@ public final class PerTableTypeRouteCounter {
      * @param tablesKey
      * @return
      */
-    @Nonnull public final UnsignedInt32Counter getCounterOrDefault(@Nonnull final TablesKey tablesKey) {
-        return this.counters.getOrDefault(Preconditions.checkNotNull(tablesKey), createCounter(tablesKey));
+    @Nonnull public final LongAdder getCounterOrDefault(@Nonnull final TablesKey tablesKey) {
+        return this.counters.getOrDefault(Preconditions.checkNotNull(tablesKey), new LongAdder());
     }
 
     /**
@@ -66,7 +60,7 @@ public final class PerTableTypeRouteCounter {
      * @param tablesKey
      * @return
      */
-    public final UnsignedInt32Counter getCounterOrSetDefault(@Nonnull final TablesKey tablesKey) {
+    public final LongAdder getCounterOrSetDefault(@Nonnull final TablesKey tablesKey) {
         if (!this.counters.containsKey(tablesKey)) {
             return init(tablesKey);
         } else {
@@ -74,12 +68,12 @@ public final class PerTableTypeRouteCounter {
         }
     }
 
-    public final Map<TablesKey, UnsignedInt32Counter> getCounters() {
+    public final Map<TablesKey, LongAdder> getCounters() {
         return this.counters;
     }
 
     public final void resetAll() {
         LOG.debug("Resetting all route counters..");
-        this.counters.values().stream().forEach(v -> v.resetCount());
+        this.counters.values().forEach(LongAdder::reset);
     }
 }
index 6648bf9b1c74946dbd4ed7a5628b7584a1425f8e..0d2de4fc921d3999ff0c0c74c0f95e3d71ebd6f4 100644 (file)
@@ -7,18 +7,15 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl;
 
+import java.util.concurrent.atomic.LongAdder;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.RIBImplRuntimeMXBean;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
 
-/**
- * @author Kevin Wang
- */
 public interface BGPRenderStats extends RIBImplRuntimeMXBean {
 
     PerTableTypeRouteCounter getLocRibRouteCounter();
 
-    UnsignedInt32Counter getConfiguredPeerCounter();
+    LongAdder getConfiguredPeerCounter();
 
-    UnsignedInt32Counter getConnectedPeerCounter();
+    LongAdder getConnectedPeerCounter();
 }
index c5495ccb9e003117465571ec7a354a136cc9d725..a84662a95edc7e93f786da77c79888ad292c5510 100644 (file)
@@ -7,16 +7,18 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl;
 
+import static org.opendaylight.protocol.bgp.rib.impl.CountersUtil.toZeroBasedCounter32;
+
 import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.LongAdder;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.controller.config.api.IdentityAttributeRef;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpRenderState;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.LocRibRouteTable;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
@@ -27,21 +29,21 @@ import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
 import org.opendaylight.yangtools.yang.common.QName;
 
 public class BGPRenderStatsImpl implements BGPRenderStats {
-    private final PerTableTypeRouteCounter locRibRouteCounter = new PerTableTypeRouteCounter("loc-rib route");
+    private final PerTableTypeRouteCounter locRibRouteCounter = new PerTableTypeRouteCounter();
     private final BgpId bgpId;
     private final RibId ribId;
     private final ClusterIdentifier clusterId;
     private final AsNumber localAs;
-    private final UnsignedInt32Counter configuredPeerCounter;
-    private final UnsignedInt32Counter connectedPeerCounter;
+    private final LongAdder configuredPeerCounter;
+    private final LongAdder connectedPeerCounter;
 
     public BGPRenderStatsImpl(@Nonnull final BgpId bgpId, @Nonnull final RibId ribId, @Nonnull final AsNumber localAs, @Nullable final ClusterIdentifier clusterId) {
         this.bgpId = Preconditions.checkNotNull(bgpId);
         this.ribId = Preconditions.checkNotNull(ribId);
         this.localAs = localAs;
         this.clusterId = clusterId;
-        this.configuredPeerCounter = new UnsignedInt32Counter("Configured Peer of BGP-RIB " + this.ribId.getValue());
-        this.connectedPeerCounter = new UnsignedInt32Counter("Connected Peer of BGP-RIB " + this.ribId.getValue());
+        this.configuredPeerCounter = new LongAdder();
+        this.connectedPeerCounter = new LongAdder();
     }
 
     @Override
@@ -51,27 +53,27 @@ public class BGPRenderStatsImpl implements BGPRenderStats {
         renderState.setBgpRibId(this.bgpId);
         renderState.setClusterId(this.clusterId);
         renderState.setLocalAs(this.localAs);
-        renderState.setConfiguredPeerCount(this.configuredPeerCounter.getCountAsZeroBasedCounter32());
-        renderState.setConnectedPeerCount(this.connectedPeerCounter.getCountAsZeroBasedCounter32());
+        renderState.setConfiguredPeerCount(toZeroBasedCounter32(this.configuredPeerCounter));
+        renderState.setConnectedPeerCount(toZeroBasedCounter32(this.connectedPeerCounter));
         // fill in the the statistic part
-        final UnsignedInt32Counter totalRouteCount = new UnsignedInt32Counter("Total Loc-Rib Route Count");
+        final LongAdder totalRouteCount = new LongAdder();
         final List<LocRibRouteTable> locRibRouteTableList = new ArrayList<>();
-        this.locRibRouteCounter.getCounters().entrySet().stream().forEach(e -> generateCounters(e, locRibRouteTableList, totalRouteCount));
+        this.locRibRouteCounter.getCounters().entrySet().forEach(e -> generateCounters(e, locRibRouteTableList, totalRouteCount));
         renderState.setLocRibRouteTable(locRibRouteTableList);
-        renderState.setLocRibRoutesCount(totalRouteCount.getCountAsZeroBasedCounter32());
+        renderState.setLocRibRoutesCount(toZeroBasedCounter32(totalRouteCount));
         return renderState;
     }
 
-    private void generateCounters(final Map.Entry<TablesKey, UnsignedInt32Counter> e, final List<LocRibRouteTable> locRibRouteTableList,
-        final UnsignedInt32Counter totalRouteCount) {
+    private void generateCounters(final Map.Entry<TablesKey, LongAdder> e, final List<LocRibRouteTable> locRibRouteTableList,
+        final LongAdder totalRouteCount) {
         final LocRibRouteTable table = new LocRibRouteTable();
         final QName afi = BindingReflections.getQName(e.getKey().getAfi()).intern();
         final QName safi = BindingReflections.getQName(e.getKey().getSafi()).intern();
         table.setAfi(new IdentityAttributeRef(afi.toString()));
         table.setSafi(new IdentityAttributeRef(safi.toString()));
-        table.setRoutesCount(e.getValue().getCountAsZeroBasedCounter32());
+        table.setRoutesCount(toZeroBasedCounter32(e.getValue()));
         locRibRouteTableList.add(table);
-        totalRouteCount.increaseCount(e.getValue().getCount());
+        totalRouteCount.add(e.getValue().longValue());
 
     }
 
@@ -81,12 +83,12 @@ public class BGPRenderStatsImpl implements BGPRenderStats {
     }
 
     @Override
-    public UnsignedInt32Counter getConfiguredPeerCounter() {
+    public LongAdder getConfiguredPeerCounter() {
         return this.configuredPeerCounter;
     }
 
     @Override
-    public UnsignedInt32Counter getConnectedPeerCounter() {
+    public LongAdder getConnectedPeerCounter() {
         return this.connectedPeerCounter;
     }
 }
index 4d9894f5a2cd820160b51074b50f8a1d9463bc0e..01e8c7ceacc29df1b4741672ed581f699513cde5 100644 (file)
@@ -7,10 +7,10 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl;
 
+import java.util.concurrent.atomic.LongAdder;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpRenderState;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
@@ -38,12 +38,12 @@ public class RIBImplRuntimeMXBeanImpl implements BGPRenderStats {
     }
 
     @Override
-    public UnsignedInt32Counter getConfiguredPeerCounter() {
+    public LongAdder getConfiguredPeerCounter() {
         return this.renderStats.getConfiguredPeerCounter();
     }
 
     @Override
-    public UnsignedInt32Counter getConnectedPeerCounter() {
+    public LongAdder getConnectedPeerCounter() {
         return this.renderStats.getConnectedPeerCounter();
     }
 }
index 3075b68cb749fa515f051ff15b287e9a9a2c6939..9ea90e9061feb635a3c776869786a2155055cebd 100755 (executable)
@@ -30,6 +30,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.atomic.LongAdder;
 import javax.management.InstanceAlreadyExistsException;
 import javax.management.InstanceNotFoundException;
 import javax.management.ObjectName;
@@ -92,7 +93,6 @@ import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer;
 import org.opendaylight.protocol.bgp.rib.impl.spi.ImportPolicyPeerTracker;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl.BGPRenderStats;
 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
@@ -286,7 +286,7 @@ public abstract class AbstractRIBImplModuleTest extends AbstractConfigTest {
         doReturn(new AsNumber(123456l)).when(this.mockedRIB).getLocalAs();
         doReturn(BGP_ID).when(this.mockedRIB).getBgpIdentifier();
         final BGPRenderStats mockedRenderStats = mock(BGPRenderStats.class);
-        doReturn(new UnsignedInt32Counter("counter")).when(mockedRenderStats).getConfiguredPeerCounter();
+        doReturn(new LongAdder()).when(mockedRenderStats).getConfiguredPeerCounter();
         doReturn(mockedRenderStats).when(this.mockedRIB).getRenderStats();
         final DOMDataTreeChangeService mockedTreeChangeService = mock(DOMDataTreeChangeService.class);
         final ListenerRegistration<?> mockedListenerReg = mock(ListenerRegistration.class);
index 2755a180a660008ac33dab5ddbe6b2a0447957ed..ef025eaed379f0548af38a53e3e440d0a47afa12 100644 (file)
@@ -19,6 +19,7 @@ import io.netty.util.concurrent.Future;
 import java.net.InetSocketAddress;
 import java.util.Collections;
 import java.util.concurrent.Executor;
+import java.util.concurrent.atomic.LongAdder;
 import org.junit.Before;
 import org.mockito.Mock;
 import org.mockito.Mockito;
@@ -43,7 +44,6 @@ import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer;
 import org.opendaylight.protocol.bgp.rib.impl.spi.ImportPolicyPeerTracker;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
-import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
 import org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl.BGPRenderStats;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
@@ -99,7 +99,7 @@ class AbstractConfig {
             this.singletonService = (ClusterSingletonService) invocationOnMock.getArguments()[0];
             return this.singletonServiceRegistration;
         }).when(this.rib).registerClusterSingletonService(any(ClusterSingletonService.class));
-        Mockito.doReturn(new UnsignedInt32Counter("counter")).when(this.render).getConfiguredPeerCounter();
+        Mockito.doReturn(new LongAdder()).when(this.render).getConfiguredPeerCounter();
         Mockito.doReturn(this.render).when(this.rib).getRenderStats();
         Mockito.doReturn(this.domTx).when(this.rib).createPeerChain(any(TransactionChainListener.class));
         Mockito.doReturn(AS).when(this.rib).getLocalAs();
index ee505561078c5d3fea2dd360de9dd6e5168ca167..e95f5948d9926db228b0a8186be9258e61b1226c 100644 (file)
@@ -12,6 +12,7 @@ import static org.junit.Assert.assertEquals;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.LongAdder;
 import org.junit.Test;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpRenderState;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.LocRibRouteTable;
@@ -47,8 +48,15 @@ public class BGPRenderStatsImplTest {
         renderStateExpected.setLocRibRoutesCount(COUTER);
 
         assertEquals(renderStateExpected, render.getBgpRenderState());
-        assertEquals(1L, render.getLocRibRouteCounter().init(new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class)).increaseCount());
-        assertEquals(1L, render.getConfiguredPeerCounter().increaseCount());
-        assertEquals(1L, render.getConnectedPeerCounter().increaseCount());
+        LongAdder counter = render.getLocRibRouteCounter().init(new TablesKey(Ipv4AddressFamily.class,
+            UnicastSubsequentAddressFamily.class));
+        counter.increment();
+        assertEquals(1L, counter.longValue());
+        counter = render.getConfiguredPeerCounter();
+        counter.increment();
+        assertEquals(1L, counter.longValue());
+        counter = render.getConnectedPeerCounter();
+        counter.increment();
+        assertEquals(1L, counter.longValue());
     }
 }
\ No newline at end of file