Fix race conditions between config-manager and persister.
[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 org.opendaylight.controller.config.api.ConflictingVersionException;
11 import org.opendaylight.controller.config.api.jmx.CommitStatus;
12 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
13 import org.opendaylight.controller.config.spi.ModuleFactory;
14 import org.osgi.framework.ServiceReference;
15 import org.osgi.util.tracker.ServiceTrackerCustomizer;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import javax.management.ObjectName;
20
21 /**
22  * Every time factory is added or removed, blank transaction is triggered to handle
23  * {@link org.opendaylight.controller.config.spi.ModuleFactory#getDefaultModules(org.opendaylight.controller.config.api.DependencyResolverFactory, org.osgi.framework.BundleContext)}
24  * functionality.
25  */
26 public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<ModuleFactory, Object> {
27     private static final Logger logger = LoggerFactory.getLogger(BlankTransactionServiceTracker.class);
28
29     private final ConfigRegistryImpl configRegistry;
30
31     public BlankTransactionServiceTracker(ConfigRegistryImpl configRegistry) {
32         this.configRegistry = configRegistry;
33     }
34
35     @Override
36     public Object addingService(ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
37         blankTransaction();
38         return null;
39     }
40
41     private synchronized void blankTransaction() {
42         // race condition check: config-persister might push new configuration while server is starting up.
43         ConflictingVersionException lastException = null;
44         for (int i = 0; i < 10; i++) {
45             try {
46                 // create transaction
47                 ObjectName tx = configRegistry.beginConfig();
48                 CommitStatus commitStatus = configRegistry.commitConfig(tx);
49                 logger.debug("Committed blank transaction with status {}", commitStatus);
50                 return;
51             } catch (ConflictingVersionException e) {
52                 lastException = e;
53                 try {
54                     Thread.sleep(1000);
55                 } catch (InterruptedException interruptedException) {
56                     Thread.currentThread().interrupt();
57                     throw new IllegalStateException(interruptedException);
58                 }
59             }
60         }
61         throw lastException;
62     }
63
64     @Override
65     public void modifiedService(ServiceReference <ModuleFactory> moduleFactoryServiceReference, Object o) {
66         blankTransaction();
67     }
68
69     @Override
70     public void removedService(ServiceReference<ModuleFactory> moduleFactoryServiceReference, Object o) {
71         blankTransaction();
72     }
73 }