2 * Copyright (c) 2013 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 handle
27 * {@link org.opendaylight.controller.config.spi.ModuleFactory#getDefaultModules(org.opendaylight.controller.config.api.DependencyResolverFactory, org.osgi.framework.BundleContext)}
30 public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<ModuleFactory, Object> {
31 private static final Logger LOG = LoggerFactory.getLogger(BlankTransactionServiceTracker.class);
33 public static final int DEFAULT_MAX_ATTEMPTS = 10;
35 private final BlankTransaction blankTransaction;
36 private final ExecutorService txExecutor;
37 private final int maxAttempts;
39 public BlankTransactionServiceTracker(final ConfigRegistryImpl configRegistry) {
40 this(new BlankTransaction() {
42 public CommitStatus hit()
43 throws ValidationException, ConflictingVersionException {
44 ObjectName tx = configRegistry.beginConfig(true);
45 return configRegistry.commitConfig(tx);
50 public BlankTransactionServiceTracker(final BlankTransaction blankTransaction) {
51 this(blankTransaction, DEFAULT_MAX_ATTEMPTS, Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
52 .setNameFormat("config-blank-txn-%d").build()));
56 BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts,
57 final ExecutorService txExecutor) {
58 this.blankTransaction = blankTx;
59 this.maxAttempts = maxAttempts;
60 this.txExecutor = txExecutor;
64 public Object addingService(ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
65 blankTransactionAsync();
69 private void blankTransactionAsync() {
70 txExecutor.execute(() -> { blankTransactionSync(); });
73 void blankTransactionSync() {
74 // race condition check: config-persister might push new configuration while server is starting up.
75 ConflictingVersionException lastException = null;
76 for (int i = 0; i < maxAttempts; i++) {
79 CommitStatus commitStatus = blankTransaction.hit();
80 LOG.debug("Committed blank transaction with status {}", commitStatus);
82 } catch (ConflictingVersionException e) {
86 } catch (InterruptedException interruptedException) {
87 Thread.currentThread().interrupt();
88 LOG.debug("blankTransactionSync was interrupted");
91 } catch (ValidationException e) {
92 LOG.error("Validation exception while running blank transaction indicates programming error", e);
96 LOG.error("Maximal number of attempts reached and still cannot get optimistic lock from config manager",
101 public void modifiedService(ServiceReference <ModuleFactory> moduleFactoryServiceReference, Object o) {
102 blankTransactionAsync();
106 public void removedService(ServiceReference<ModuleFactory> moduleFactoryServiceReference, Object o) {
107 blankTransactionAsync();
111 static interface BlankTransaction {
112 CommitStatus hit() throws ValidationException, ConflictingVersionException;