2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.applications.topology.manager;
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.BlockingQueue;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 final class OperationProcessor implements AutoCloseable, Runnable, TransactionChainListener {
24 private static final Logger LOG = LoggerFactory.getLogger(OperationProcessor.class);
25 private static final int MAX_TRANSACTION_OPERATIONS = 100;
26 private static final int OPERATION_QUEUE_DEPTH = 500;
28 private final BlockingQueue<TopologyOperation> queue = new LinkedBlockingQueue<>(OPERATION_QUEUE_DEPTH);
29 private final DataBroker dataBroker;
30 private BindingTransactionChain transactionChain;
31 private volatile boolean finishing = false;
33 OperationProcessor(final DataBroker dataBroker) {
34 this.dataBroker = Preconditions.checkNotNull(dataBroker);
35 transactionChain = this.dataBroker.createTransactionChain(this);
38 void enqueueOperation(final TopologyOperation task) {
41 } catch (InterruptedException e) {
42 LOG.warn("Interrupted while submitting task {}", task, e);
50 TopologyOperation op = queue.take();
52 LOG.debug("New {} operation available, starting transaction", op);
54 final ReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
58 op.applyOperation(tx);
61 if (ops < MAX_TRANSACTION_OPERATIONS) {
67 LOG.debug("Next operation {}", op);
70 LOG.debug("Processed {} operations, submitting transaction", ops);
73 tx.submit().checkedGet();
74 } catch (final TransactionCommitFailedException e) {
75 LOG.warn("Stat DataStoreOperation unexpected State!", e);
76 transactionChain.close();
77 transactionChain = dataBroker.createTransactionChain(this);
78 cleanDataStoreOperQueue();
81 } catch (final IllegalStateException e) {
82 LOG.warn("Stat DataStoreOperation unexpected State!", e);
83 transactionChain.close();
84 transactionChain = dataBroker.createTransactionChain(this);
85 cleanDataStoreOperQueue();
86 } catch (final InterruptedException e) {
87 LOG.warn("Stat Manager DS Operation thread interupted!", e);
89 } catch (final Exception e) {
90 LOG.warn("Stat DataStore Operation executor fail!", e);
93 // Drain all events, making sure any blocked threads are unblocked
94 cleanDataStoreOperQueue();
97 private void cleanDataStoreOperQueue() {
98 while (!queue.isEmpty()) {
104 public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
105 LOG.warn("Failed to export Topology manager operations, Transaction {} failed: {}", transaction.getIdentifier(), cause.getMessage());
106 LOG.debug("Failed to export Topology manager operations.. ", cause);
107 transactionChain.close();
108 transactionChain = dataBroker.createTransactionChain(this);
109 cleanDataStoreOperQueue();
113 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
118 public void close() throws Exception {
119 if (transactionChain != null) {
120 transactionChain.close();