Fix bug 2450 - Statistics collection slow - performance
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / impl / StatListenCommitMeter.java
index 9c9de59a6ad8f53b562add5a7c8255cc2a54da19..2d5be71dcf914e41f0d8b9d61db9d5d7429ad67d 100644 (file)
@@ -16,6 +16,7 @@ import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.controller.md.statistics.manager.StatPermCollector.StatCapabTypes;
 import org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation;
@@ -36,7 +37,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeatures;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
@@ -45,11 +48,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 
 /**
  * statistics-manager
@@ -90,68 +95,102 @@ public class StatListenCommitMeter extends StatAbstractListenCommit<Meter, Opend
             LOG.debug("STAT-MANAGER - MeterConfigStatsUpdated: unregistred notification detect TransactionId {}", transId);
             return;
         }
+        manager.getRpcMsgManager().addNotification(notification, nodeId);
         if (notification.isMoreReplies()) {
-            manager.getRpcMsgManager().addNotification(notification, nodeId);
             return;
         }
-        final List<MeterConfigStats> meterConfStat = notification.getMeterConfigStats() != null
-                ? new ArrayList<>(notification.getMeterConfigStats()) : new ArrayList<MeterConfigStats>(10);
-        final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
-        if (txContainer.isPresent()) {
-            final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
-            for (final TransactionAware notif : cacheNotifs) {
-                if (notif instanceof MeterConfigStatsUpdated) {
-                    meterConfStat.addAll(((MeterConfigStatsUpdated) notif).getMeterConfigStats());
-                }
-            }
-        }
-        final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+
+        /* Don't block RPC Notification thread */
         manager.enqueue(new StatDataStoreOperation() {
             @Override
             public void applyOperation(final ReadWriteTransaction tx) {
+
+                final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
+                        .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+
+                /* Validate exist FlowCapableNode */
+                final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
+                Optional<FlowCapableNode> fNode = Optional.absent();
+                try {
+                    fNode = tx.read(LogicalDatastoreType.OPERATIONAL,fNodeIdent).checkedGet();
+                }
+                catch (final ReadFailedException e) {
+                    LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
+                }
+                if ( ! fNode.isPresent()) {
+                    return;
+                }
+                /* Get and Validate TransactionCacheContainer */
+                final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
+                if ( ! isTransactionCacheContainerValid(txContainer)) {
+                    return;
+                }
+                /* Prepare List actual Meters and not updated Meters will be removed */
+                final List<Meter> existMeters = fNode.get().getMeter() != null
+                        ? fNode.get().getMeter() : Collections.<Meter> emptyList();
+                final List<MeterKey> existMeterKeys = new ArrayList<>();
+                for (final Meter meter : existMeters) {
+                    existMeterKeys.add(meter.getKey());
+                }
+                /* MeterConfig processing */
+                comitConfMeterStats(txContainer, tx, fNodeIdent, existMeterKeys);
+                /* Delete all not presented Meter Nodes */
+                deleteAllNotPresentedNodes(fNodeIdent, tx, Collections.unmodifiableList(existMeterKeys));
                 /* Notification for continue collecting statistics */
-                notifyToCollectNextStatistics(nodeIdent);
-                comitConfMeterStats(meterConfStat, nodeIdent, tx);
+                notifyToCollectNextStatistics(nodeIdent, transId);
             }
         });
     }
 
     @Override
     public void onMeterFeaturesUpdated(final MeterFeaturesUpdated notification) {
+        Preconditions.checkNotNull(notification);
         final TransactionId transId = notification.getTransactionId();
         final NodeId nodeId = notification.getId();
         if ( ! isExpectedStatistics(transId, nodeId)) {
             LOG.debug("STAT-MANAGER - MeterFeaturesUpdated: unregistred notification detect TransactionId {}", transId);
             return;
         }
+        manager.getRpcMsgManager().addNotification(notification, nodeId);
         if (notification.isMoreReplies()) {
-            manager.getRpcMsgManager().addNotification(notification, nodeId);
             return;
         }
-        final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
-        if ( ! txContainer.isPresent()) {
-            return;
-        }
-        final MeterFeatures stats = new MeterFeaturesBuilder(notification).build();
-        final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
-                .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
-        final InstanceIdentifier<MeterFeatures> meterFeatureIdent = nodeIdent
-                .augmentation(NodeMeterFeatures.class).child(MeterFeatures.class);
 
+        /* Don't block RPC Notification thread */
         manager.enqueue(new StatDataStoreOperation() {
             @Override
             public void applyOperation(final ReadWriteTransaction tx) {
-                /* Notification for continue collecting statistics */
-                notifyToCollectNextStatistics(nodeIdent);
-                Optional<Node> node = Optional.absent();
-                try {
-                    node = tx.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
-                }
-                catch (final ReadFailedException e) {
-                    LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
+                /* Get and Validate TransactionCacheContainer */
+                final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
+                if ( ! isTransactionCacheContainerValid(txContainer)) {
+                    return;
                 }
-                if (node.isPresent()) {
-                    tx.put(LogicalDatastoreType.OPERATIONAL, meterFeatureIdent, stats);
+
+                final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
+                        .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+
+                final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
+                for (final TransactionAware notif : cacheNotifs) {
+                    if ( ! (notif instanceof MeterFeaturesUpdated)) {
+                        break;
+                    }
+                    final MeterFeatures stats = new MeterFeaturesBuilder((MeterFeaturesUpdated)notif).build();
+                    final InstanceIdentifier<NodeMeterFeatures> nodeMeterFeatureIdent =
+                            nodeIdent.augmentation(NodeMeterFeatures.class);
+                    final InstanceIdentifier<MeterFeatures> meterFeatureIdent = nodeMeterFeatureIdent
+                            .child(MeterFeatures.class);
+                    Optional<Node> node = Optional.absent();
+                    try {
+                        node = tx.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
+                    }
+                    catch (final ReadFailedException e) {
+                        LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
+                    }
+                    if (node.isPresent()) {
+                        tx.merge(LogicalDatastoreType.OPERATIONAL, nodeMeterFeatureIdent, new NodeMeterFeaturesBuilder().build(), true);
+                        tx.put(LogicalDatastoreType.OPERATIONAL, meterFeatureIdent, stats);
+                        manager.registerAdditionalNodeFeature(nodeIdent, StatCapabTypes.METER_STATS);
+                    }
                 }
             }
         });
@@ -159,123 +198,149 @@ public class StatListenCommitMeter extends StatAbstractListenCommit<Meter, Opend
 
     @Override
     public void onMeterStatisticsUpdated(final MeterStatisticsUpdated notification) {
+        Preconditions.checkNotNull(notification);
         final TransactionId transId = notification.getTransactionId();
         final NodeId nodeId = notification.getId();
         if ( ! isExpectedStatistics(transId, nodeId)) {
             LOG.debug("STAT-MANAGER - MeterStatisticsUpdated: unregistred notification detect TransactionId {}", transId);
             return;
         }
+        manager.getRpcMsgManager().addNotification(notification, nodeId);
         if (notification.isMoreReplies()) {
-            manager.getRpcMsgManager().addNotification(notification, nodeId);
             return;
         }
-        final List<MeterStats> meterStat = notification.getMeterStats() != null
-                ? new ArrayList<>(notification.getMeterStats()) : new ArrayList<MeterStats>(10);
-        final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
-        if (txContainer.isPresent()) {
-            final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
-            for (final TransactionAware notif : cacheNotifs) {
-                if (notif instanceof MeterConfigStatsUpdated) {
-                    meterStat.addAll(((MeterStatisticsUpdated) notif).getMeterStats());
-                }
-            }
-        }
-        final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+
+        /* Don't block RPC Notification thread */
         manager.enqueue(new StatDataStoreOperation() {
             @Override
             public void applyOperation(final ReadWriteTransaction tx) {
-                statMeterCommit(meterStat, nodeIdent, tx);
-                /* Notification for continue collecting statistics */
-                notifyToCollectNextStatistics(nodeIdent);
+
+                final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
+                        .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+                /* Node exist check */
+                Optional<Node> node = Optional.absent();
+                try {
+                    node = tx.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
+                }
+                catch (final ReadFailedException e) {
+                    LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
+                }
+                if ( ! node.isPresent()) {
+                    return;
+                }
+
+                /* Get and Validate TransactionCacheContainer */
+                final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
+                if ( ! isTransactionCacheContainerValid(txContainer)) {
+                    return;
+                }
+                final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
+
+                Optional<Meter> notifMeter = Optional.absent();
+                final Optional<? extends DataObject> inputObj = txContainer.get().getConfInput();
+                if (inputObj.isPresent() && inputObj.get() instanceof Meter) {
+                    notifMeter = Optional.<Meter> of((Meter)inputObj.get());
+                }
+                for (final TransactionAware notif : cacheNotifs) {
+                    if ( ! (notif instanceof MeterStatisticsUpdated)) {
+                        break;
+                    }
+                    statMeterCommit(((MeterStatisticsUpdated) notif).getMeterStats(), nodeIdent, tx);
+                }
+                if ( ! notifMeter.isPresent()) {
+                    notifyToCollectNextStatistics(nodeIdent, transId);
+                }
             }
         });
     }
 
     private void statMeterCommit(final List<MeterStats> meterStats,
-            final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction trans) {
+            final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction tx) {
+
+        Preconditions.checkNotNull(meterStats);
+        Preconditions.checkNotNull(nodeIdent);
+        Preconditions.checkNotNull(tx);
 
         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
+
         for (final MeterStats mStat : meterStats) {
             final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
 
-            final MeterKey mKey = new MeterKey(mStat.getMeterId());
-            final InstanceIdentifier<MeterStatistics> msIdent = fNodeIdent
-                    .child(Meter.class, mKey).augmentation(NodeMeterStatistics.class)
-                    .child(MeterStatistics.class);
+            final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(mStat.getMeterId()));
+            final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
+                    .augmentation(NodeMeterStatistics.class);
+            final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
             /* Meter Statistics commit */
-            Optional<FlowCapableNode> fNode = Optional.absent();
+            Optional<Meter> meter = Optional.absent();
             try {
-                fNode = trans.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
+                meter = tx.read(LogicalDatastoreType.OPERATIONAL, meterIdent).checkedGet();
             }
             catch (final ReadFailedException e) {
                 LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
             }
-            if (fNode.isPresent()) {
-                trans.put(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
+            if (meter.isPresent()) {
+                tx.merge(LogicalDatastoreType.OPERATIONAL, nodeMeterStatIdent, new NodeMeterStatisticsBuilder().build(), true);
+                tx.put(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
             }
         }
     }
 
-    private void comitConfMeterStats(final List<MeterConfigStats> meterConfStat,
-            final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction trans) {
+    private void comitConfMeterStats(final Optional<TransactionCacheContainer<?>> txContainer, final ReadWriteTransaction tx,
+            final InstanceIdentifier<FlowCapableNode> fNodeIdent, final List<MeterKey> existMeterKeys) {
 
-        final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
-        final List<MeterKey> deviceMeterKeys = new ArrayList<>();
-
-        for (final MeterConfigStats meterConf : meterConfStat) {
-            final MeterBuilder meterBuilder = new MeterBuilder(meterConf);
-            if (meterConf.getMeterId() != null) {
-                final MeterKey meterKey = new MeterKey(meterConf.getMeterId());
-                meterBuilder.setKey(meterKey);
-                final InstanceIdentifier<Meter> meterRef = nodeIdent
-                        .augmentation(FlowCapableNode.class).child(Meter.class,meterKey);
-                final NodeMeterConfigStatsBuilder meterConfig = new NodeMeterConfigStatsBuilder();
-                meterConfig.setMeterConfigStats(new MeterConfigStatsBuilder(meterConf).build());
-                //Update augmented data
-                meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
-                deviceMeterKeys.add(meterKey);
-                Optional<FlowCapableNode> fNode = Optional.absent();
-                try {
-                    fNode = trans.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
-                }
-                catch (final ReadFailedException e) {
-                    LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
-                }
-                if (fNode.isPresent()) {
-                    trans.put(LogicalDatastoreType.OPERATIONAL, meterRef, meterBuilder.build());
+        Preconditions.checkNotNull(existMeterKeys);
+        Preconditions.checkNotNull(txContainer);
+        Preconditions.checkNotNull(fNodeIdent);
+        Preconditions.checkNotNull(tx);
+
+        final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
+        for (final TransactionAware notif : cacheNotifs) {
+            if ( ! (notif instanceof MeterConfigStatsUpdated)) {
+                break;
+            }
+            final List<MeterConfigStats> meterStats = ((MeterConfigStatsUpdated) notif).getMeterConfigStats();
+            if (meterStats == null) {
+                break;
+            }
+            for (final MeterConfigStats meterStat : meterStats) {
+                if (meterStat.getMeterId() != null) {
+                    final MeterBuilder meterBuilder = new MeterBuilder(meterStat);
+                    final MeterKey meterKey = new MeterKey(meterStat.getMeterId());
+                    final InstanceIdentifier<Meter> meterRef = fNodeIdent.child(Meter.class, meterKey);
+
+                    final NodeMeterConfigStatsBuilder meterConfig = new NodeMeterConfigStatsBuilder();
+                    meterConfig.setMeterConfigStats(new MeterConfigStatsBuilder(meterStat).build());
+                    //Update augmented data
+                    meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
+                    existMeterKeys.remove(meterKey);
+                    tx.put(LogicalDatastoreType.OPERATIONAL, meterRef, meterBuilder.build());
                 }
             }
         }
-        /* Delete all not presented Meter Nodes */
-        deleteAllNotPresentedNodes(fNodeIdent, trans, deviceMeterKeys);
     }
 
     private void deleteAllNotPresentedNodes(final InstanceIdentifier<FlowCapableNode> fNodeIdent,
-            final ReadWriteTransaction trans, final List<MeterKey> deviceMeterKeys) {
-        /* Delete all not presented meters */
-        final Optional<FlowCapableNode> fNode = readLatestConfiguration(fNodeIdent);
+            final ReadWriteTransaction tx, final List<MeterKey> deviceMeterKeys) {
 
-        if ( ! fNode.isPresent()) {
-            LOG.trace("Read Operational/DS for FlowCapableNode fail! Node {} doesn't exist.", fNodeIdent);
+        Preconditions.checkNotNull(fNodeIdent);
+        Preconditions.checkNotNull(tx);
+
+        if (deviceMeterKeys == null) {
             return;
         }
-        final List<Meter> existMeters = fNode.get().getMeter() != null
-                ? fNode.get().getMeter() : Collections.<Meter> emptyList();
-        /* Add all existed groups paths - no updated paths has to be removed */
-        for (final Meter meter : existMeters) {
-            if (deviceMeterKeys.remove(meter.getKey())) {
-                break; // Meter still exist on device
-            }
-            final InstanceIdentifier<Meter> delMeterIdent = fNodeIdent.child(Meter.class, meter.getKey());
+
+        for (final MeterKey key : deviceMeterKeys) {
+            final InstanceIdentifier<Meter> delMeterIdent = fNodeIdent.child(Meter.class, key);
+            LOG.trace("Meter {} has to removed.", key);
             Optional<Meter> delMeter = Optional.absent();
             try {
-                delMeter = trans.read(LogicalDatastoreType.OPERATIONAL, delMeterIdent).checkedGet();
+                delMeter = tx.read(LogicalDatastoreType.OPERATIONAL, delMeterIdent).checkedGet();
             }
             catch (final ReadFailedException e) {
                 // NOOP - probably another transaction delete that node
             }
             if (delMeter.isPresent()) {
-                trans.delete(LogicalDatastoreType.OPERATIONAL, delMeterIdent);
+                tx.delete(LogicalDatastoreType.OPERATIONAL, delMeterIdent);
             }
         }
     }