Bug-7614: LocRibWriter does not recover well from transaction chain failure 63/52563/1
authorAjay <ajayl.bro@gmail.com>
Tue, 14 Feb 2017 06:59:09 +0000 (06:59 +0000)
committerAjay <ajayl.bro@gmail.com>
Wed, 1 Mar 2017 22:06:31 +0000 (22:06 +0000)
If transaction chain associated with LocRibWriter fails, create a
new transaction chain and re-initialize it with the new chain.

Change-Id: Ibbb60c41594ad6b3602cabac18f5e4bccaa093bb
Signed-off-by: Ajay <ajayl.bro@gmail.com>
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
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/RIBImpl.java

index 3cbd173c11b78eaa91231ea076e590824f3bb973..9225c5254225e13a61e021e5d81f7849f6c72367 100644 (file)
@@ -65,13 +65,16 @@ final class LocRibWriter implements AutoCloseable, ClusteredDOMDataTreeChangeLis
 
     private final Map<PathArgument, RouteEntry> routeEntries = new HashMap<>();
     private final YangInstanceIdentifier locRibTarget;
-    private final DOMTransactionChain chain;
+    private final NodeIdentifierWithPredicates tableKey;
+    private DOMTransactionChain chain;
     private final ExportPolicyPeerTracker exportPolicyPeerTracker;
     private final NodeIdentifier attributesIdentifier;
     private final Long ourAs;
     private final RIBSupport ribSupport;
     private final TablesKey localTablesKey;
-    private final ListenerRegistration<LocRibWriter> reg;
+    private final YangInstanceIdentifier target;
+    private final DOMDataTreeChangeService service;
+    private ListenerRegistration<LocRibWriter> reg;
     private final PathSelectionMode pathSelectionMode;
     private final UnsignedInt32Counter routeCounter;
 
@@ -79,24 +82,33 @@ final class LocRibWriter implements AutoCloseable, ClusteredDOMDataTreeChangeLis
         final Long ourAs, final DOMDataTreeChangeService service, final ExportPolicyPeerTracker exportPolicyPeerTracker, final TablesKey tablesKey,
         @Nonnull final PathSelectionMode pathSelectionMode, final UnsignedInt32Counter routeCounter) {
         this.chain = Preconditions.checkNotNull(chain);
+        this.target = Preconditions.checkNotNull(target);
         final NodeIdentifierWithPredicates tableKey = RibSupportUtils.toYangTablesKey(tablesKey);
         this.localTablesKey = tablesKey;
+        this.tableKey = tableKey;
         this.locRibTarget = YangInstanceIdentifier.create(target.node(LocRib.QNAME).node(Tables.QNAME).node(tableKey).getPathArguments());
         this.ourAs = Preconditions.checkNotNull(ourAs);
+        this.service = Preconditions.checkNotNull(service);
         this.ribSupport = registry.getRIBSupportContext(tablesKey).getRibSupport();
         this.attributesIdentifier = this.ribSupport.routeAttributesIdentifier();
         this.exportPolicyPeerTracker = exportPolicyPeerTracker;
         this.pathSelectionMode = pathSelectionMode;
         this.routeCounter = routeCounter;
 
+        init();
+    }
+
+    private synchronized void init() {
         final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
         tx.merge(LogicalDatastoreType.OPERATIONAL, this.locRibTarget.node(Routes.QNAME), this.ribSupport.emptyRoutes());
-        tx.merge(LogicalDatastoreType.OPERATIONAL, this.locRibTarget.node(Attributes.QNAME).node(ATTRIBUTES_UPTODATE_TRUE.getNodeType()), ATTRIBUTES_UPTODATE_TRUE);
+        tx.merge(LogicalDatastoreType.OPERATIONAL, this.locRibTarget.node(Attributes.QNAME)
+            .node(ATTRIBUTES_UPTODATE_TRUE.getNodeType()), ATTRIBUTES_UPTODATE_TRUE);
         tx.submit();
 
-        final YangInstanceIdentifier tableId = target.node(Peer.QNAME).node(Peer.QNAME).node(EffectiveRibIn.QNAME).node(Tables.QNAME).node(tableKey);
+        final YangInstanceIdentifier tableId = this.target.node(Peer.QNAME).node(Peer.QNAME).node(EffectiveRibIn.QNAME)
+            .node(Tables.QNAME).node(this.tableKey);
         final DOMDataTreeIdentifier wildcard = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, tableId);
-        this.reg = service.registerDataTreeChangeListener(wildcard, this);
+        this.reg = this.service.registerDataTreeChangeListener(wildcard, this);
     }
 
     public static LocRibWriter create(@Nonnull final RIBSupportContextRegistry registry, @Nonnull final TablesKey tablesKey, @Nonnull final DOMTransactionChain chain,
@@ -105,10 +117,22 @@ final class LocRibWriter implements AutoCloseable, ClusteredDOMDataTreeChangeLis
         return new LocRibWriter(registry, chain, target, ourAs.getValue(), service, ep, tablesKey, pathSelectionStrategy, routeCounter);
     }
 
+    /**
+     * Re-initialize this LocRibWriter with new transaction chain.
+     *
+     * @param newChain new transaction chain
+     */
+    synchronized void restart(@Nonnull final DOMTransactionChain newChain) {
+        Preconditions.checkNotNull(newChain);
+        close();
+        this.chain = newChain;
+        init();
+    }
+
     @Override
     public void close() {
         this.reg.close();
-        // FIXME: wait for the chain to close? unfortunately RIBImpl is the listener, so that may require some work
+        this.reg = null;
         this.chain.close();
     }
 
index f48740f25016de8513b241d704c6ab66903b64cb..099bce0b0a3686f381afaead9bb7081cce2cf1a7 100755 (executable)
@@ -15,7 +15,7 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -109,7 +109,7 @@ public final class RIBImpl extends DefaultRibReference implements ClusterSinglet
     private final BgpDeployer.WriteConfiguration configurationWriter;
     private ClusterSingletonServiceRegistration registration;
     private final DOMDataBrokerExtension service;
-    private final List<LocRibWriter> locRibs = new ArrayList<>();
+    private final Map<TransactionChain, LocRibWriter> txChainToLocRibWriter = new HashMap<>();
     private final BGPConfigModuleTracker configModuleTracker;
     private final BGPOpenConfigProvider openConfigProvider;
     private final Map<TablesKey, PathSelectionMode> bestPathSelectionStrategies;
@@ -199,14 +199,21 @@ public final class RIBImpl extends DefaultRibReference implements ClusterSinglet
         } catch (final TransactionCommitFailedException e1) {
             LOG.error("Failed to initiate LocRIB for key {}", key, e1);
         }
+        createLocRibWriter(key);
+    }
 
+    private synchronized void createLocRibWriter(final TablesKey key) {
+        LOG.debug("Creating LocRIB writer for key {}", key);
+        final DOMTransactionChain txChain = createPeerChain(this);
         PathSelectionMode pathSelectionStrategy = this.bestPathSelectionStrategies.get(key);
         if (pathSelectionStrategy == null) {
             pathSelectionStrategy = BasePathSelectionModeFactory.createBestPathSelectionStrategy();
         }
 
-        this.locRibs.add(LocRibWriter.create(this.ribContextRegistry, key, createPeerChain(this), getYangRibId(), this.localAs, getService(),
-            this.exportPolicyPeerTrackerMap.get(key), pathSelectionStrategy, this.renderStats.getLocRibRouteCounter().init(key)));
+        final LocRibWriter locRibWriter = LocRibWriter.create(this.ribContextRegistry, key, txChain,
+            getYangRibId(), this.localAs, getService(), this.exportPolicyPeerTrackerMap.get(key),
+            pathSelectionStrategy, this.renderStats.getLocRibRouteCounter().init(key));
+        this.txChainToLocRibWriter.put(txChain, locRibWriter);
     }
 
     @Override
@@ -248,8 +255,14 @@ public final class RIBImpl extends DefaultRibReference implements ClusterSinglet
     }
 
     @Override
-    public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
+    public synchronized void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
         LOG.error("Broken chain in RIB {} transaction {}", getInstanceIdentifier(), transaction != null ? transaction.getIdentifier() : null, cause);
+        if (this.txChainToLocRibWriter.containsKey(chain)) {
+            final LocRibWriter locRibWriter = this.txChainToLocRibWriter.remove(chain);
+            final DOMTransactionChain newChain = createPeerChain(this);
+            locRibWriter.restart(newChain);
+            this.txChainToLocRibWriter.put(newChain, locRibWriter);
+        }
     }
 
     @Override
@@ -359,10 +372,10 @@ public final class RIBImpl extends DefaultRibReference implements ClusterSinglet
     }
 
     @Override
-    public ListenableFuture<Void> closeServiceInstance() {
-        LOG.info("Close RIB Singleton Service {}", getIdentifier());
-        this.locRibs.forEach(LocRibWriter::close);
-        this.locRibs.clear();
+    public synchronized ListenableFuture<Void> closeServiceInstance() {
+        LOG.info("RIB {} closing instance", this.ribId.getValue());
+        this.txChainToLocRibWriter.values().forEach(LocRibWriter::close);
+        this.txChainToLocRibWriter.clear();
 
         this.renderStats.getLocRibRouteCounter().resetAll();