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 public 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 final Thread thread;
31 private BindingTransactionChain transactionChain;
32 private volatile boolean finishing = false;
34 public OperationProcessor(final DataBroker dataBroker) {
35 this.dataBroker = Preconditions.checkNotNull(dataBroker);
36 transactionChain = this.dataBroker.createTransactionChain(this);
38 thread = new Thread(this);
39 thread.setDaemon(true);
40 thread.setName("FlowCapableTopologyExporter-" + FlowCapableTopologyProvider.TOPOLOGY_ID);
43 void enqueueOperation(final TopologyOperation task) {
46 } catch (InterruptedException e) {
47 LOG.warn("Interrupted while submitting task {}", task, e);
59 TopologyOperation op = queue.take();
61 LOG.debug("New {} operation available, starting transaction", op);
63 final ReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
67 op.applyOperation(tx);
70 if (ops < MAX_TRANSACTION_OPERATIONS) {
76 LOG.debug("Next operation {}", op);
79 LOG.debug("Processed {} operations, submitting transaction", ops);
80 submitTransaction(tx);
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 // This should mean we're shutting down.
88 LOG.debug("Stat Manager DS Operation thread interrupted!", e);
90 } catch (final Exception e) {
91 LOG.warn("Stat DataStore Operation executor fail!", e);
94 // Drain all events, making sure any blocked threads are unblocked
95 cleanDataStoreOperQueue();
98 private void submitTransaction(ReadWriteTransaction tx) {
100 tx.submit().checkedGet();
101 } catch (final TransactionCommitFailedException e) {
102 LOG.warn("Stat DataStoreOperation unexpected State!", e);
103 transactionChain.close();
104 transactionChain = dataBroker.createTransactionChain(this);
105 cleanDataStoreOperQueue();
109 private void cleanDataStoreOperQueue() {
110 while (!queue.isEmpty()) {
116 public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
117 LOG.warn("Failed to export Topology manager operations, Transaction {} failed: {}", transaction.getIdentifier(), cause.getMessage());
118 LOG.debug("Failed to export Topology manager operations.. ", cause);
119 transactionChain.close();
120 transactionChain = dataBroker.createTransactionChain(this);
121 cleanDataStoreOperQueue();
125 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
130 public void close() {
134 } catch(InterruptedException e) {
135 LOG.debug("Join of thread {} was interrupted", thread.getName(), e);
138 if (transactionChain != null) {
139 transactionChain.close();
142 LOG.debug("OperationProcessor closed");