a15820556c4a9a76ac45f80ccf89a317df646d53
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BlankTransactionServiceTracker.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl.osgi;
9
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;
24
25 /**
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)}
28  * functionality.
29  */
30 public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<ModuleFactory, Object> {
31     private static final Logger LOG = LoggerFactory.getLogger(BlankTransactionServiceTracker.class);
32
33     public static final int DEFAULT_MAX_ATTEMPTS = 10;
34
35     private final BlankTransaction blankTransaction;
36     private final ExecutorService txExecutor;
37     private final int maxAttempts;
38
39     public BlankTransactionServiceTracker(final ConfigRegistryImpl configRegistry) {
40         this(() -> {
41             ObjectName tx = configRegistry.beginConfig(true);
42             return configRegistry.commitConfig(tx);
43          });
44     }
45
46     public BlankTransactionServiceTracker(final BlankTransaction blankTransaction) {
47         this(blankTransaction, DEFAULT_MAX_ATTEMPTS, Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
48                 .setNameFormat("config-blank-txn-%d").build()));
49     }
50
51     @VisibleForTesting
52     BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts,
53             final ExecutorService txExecutor) {
54         this.blankTransaction = blankTx;
55         this.maxAttempts = maxAttempts;
56         this.txExecutor = txExecutor;
57     }
58
59     @Override
60     public Object addingService(ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
61         blankTransactionAsync();
62         return null;
63     }
64
65     private void blankTransactionAsync() {
66         txExecutor.execute(this::blankTransactionSync);
67     }
68
69     void blankTransactionSync() {
70         // race condition check: config-persister might push new configuration while server is starting up.
71         ConflictingVersionException lastException = null;
72         for (int i = 0; i < maxAttempts; i++) {
73             try {
74                 // create transaction
75                 CommitStatus commitStatus = blankTransaction.hit();
76                 LOG.debug("Committed blank transaction with status {}", commitStatus);
77                 return;
78             } catch (ConflictingVersionException e) {
79                 lastException = e;
80                 try {
81                     Thread.sleep(1000);
82                 } catch (InterruptedException interruptedException) {
83                     Thread.currentThread().interrupt();
84                     LOG.debug("blankTransactionSync was interrupted");
85                     return;
86                 }
87             } catch (ValidationException e) {
88                 LOG.error("Validation exception while running blank transaction indicates programming error", e);
89             }
90         }
91
92         LOG.error("Maximal number of attempts reached and still cannot get optimistic lock from config manager",
93                 lastException);
94     }
95
96     @Override
97     public void modifiedService(ServiceReference <ModuleFactory> moduleFactoryServiceReference, Object o) {
98         blankTransactionAsync();
99     }
100
101     @Override
102     public void removedService(ServiceReference<ModuleFactory> moduleFactoryServiceReference, Object o) {
103         blankTransactionAsync();
104     }
105
106     @VisibleForTesting
107     interface BlankTransaction {
108         CommitStatus hit() throws ValidationException, ConflictingVersionException;
109     }
110 }