2 * Copyright (c) 2013, 2017 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.controller.config.manager.impl.osgi;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.ThreadFactoryBuilder;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import javax.management.ObjectName;
15 import org.opendaylight.controller.config.api.ConflictingVersionException;
16 import org.opendaylight.controller.config.api.ValidationException;
17 import org.opendaylight.controller.config.api.jmx.CommitStatus;
18 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
19 import org.opendaylight.controller.config.spi.ModuleFactory;
20 import org.osgi.framework.ServiceReference;
21 import org.osgi.util.tracker.ServiceTrackerCustomizer;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Every time factory is added or removed, blank transaction is triggered to
29 public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<ModuleFactory, Object> {
30 private static final Logger LOG = LoggerFactory.getLogger(BlankTransactionServiceTracker.class);
32 public static final int DEFAULT_MAX_ATTEMPTS = 10;
34 private final BlankTransaction blankTransaction;
35 private final ExecutorService txExecutor;
36 private final int maxAttempts;
38 public BlankTransactionServiceTracker(final ConfigRegistryImpl configRegistry) {
40 ObjectName tx = configRegistry.beginConfig(true);
41 return configRegistry.commitConfig(tx);
45 public BlankTransactionServiceTracker(final BlankTransaction blankTransaction) {
46 this(blankTransaction, DEFAULT_MAX_ATTEMPTS, Executors
47 .newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("config-blank-txn-%d").build()));
51 BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts,
52 final ExecutorService txExecutor) {
53 this.blankTransaction = blankTx;
54 this.maxAttempts = maxAttempts;
55 this.txExecutor = txExecutor;
59 public Object addingService(final ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
60 blankTransactionAsync();
64 private void blankTransactionAsync() {
65 txExecutor.execute(this::blankTransactionSync);
68 void blankTransactionSync() {
69 // race condition check: config-persister might push new configuration while
70 // server is starting up.
71 ConflictingVersionException lastException = null;
72 for (int i = 0; i < maxAttempts; i++) {
75 CommitStatus commitStatus = blankTransaction.hit();
76 LOG.debug("Committed blank transaction with status {}", commitStatus);
78 } catch (final ConflictingVersionException e) {
82 } catch (final InterruptedException interruptedException) {
83 Thread.currentThread().interrupt();
84 LOG.debug("blankTransactionSync was interrupted");
87 } catch (final ValidationException e) {
88 LOG.error("Validation exception while running blank transaction indicates programming error", e);
92 LOG.error("Maximal number of attempts reached and still cannot get optimistic lock from config manager",
97 public void modifiedService(final ServiceReference<ModuleFactory> moduleFactoryServiceReference,
98 final Object object) {
99 blankTransactionAsync();
103 public void removedService(final ServiceReference<ModuleFactory> moduleFactoryServiceReference,
104 final Object object) {
105 blankTransactionAsync();
109 interface BlankTransaction {
110 CommitStatus hit() throws ValidationException, ConflictingVersionException;