finish lockmanager's managed transaction conversions 06/74506/3
authorMichael Vorburger <vorburger@redhat.com>
Thu, 26 Jul 2018 12:04:21 +0000 (14:04 +0200)
committerSam Hague <shague@redhat.com>
Sat, 28 Jul 2018 12:02:52 +0000 (12:02 +0000)
by also switching LockManagerServiceImpl's readWriteLock() to
RetryingManagedNewTransactionRunner, which is easy now that
ManagedNewTransactionRunner has not only the initial call* methods but
also the new apply* methods which can return a value.

Strictly speaking this isn't 100% equivalent to before, because now
readWriteLock() itself will already do 3 retries, whereas before that
happened (and still does) only in the caller.  I can't see how that
would be a problem though - on the contrary.  (E.g. if I am ever
motivated to finish up the infrautils.metrics based monitoring I've long
been meaning to add to the RetryingManagedNewTransactionRunner, then it
would monitor retries in lockmanager also.)

We now also enforce use of (only) managed transactions in lockmanager.

JIRA: GENIUS-183
Change-Id: I766f507b3ad047ca8480b0669ca740ab000eacc2
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
lockmanager/lockmanager-impl/pom.xml
lockmanager/lockmanager-impl/src/main/java/org/opendaylight/genius/lockmanager/impl/LockManagerServiceImpl.java

index 44e89fcc2c21f3ff869a6f56511a03d53db8327b..2c1c18f49ce232bd618eb9a9134eb9e85749f679 100644 (file)
@@ -72,6 +72,38 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
     </dependency>
   </dependencies>
   <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <artifactId>maven-checkstyle-plugin</artifactId>
+          <dependencies>
+            <dependency>
+              <groupId>org.opendaylight.genius</groupId>
+              <artifactId>checkstyle</artifactId>
+              <version>${project.version}</version>
+            </dependency>
+          </dependencies>
+          <executions>
+            <execution>
+              <id>check-databroker</id>
+              <goals>
+                <goal>check</goal>
+              </goals>
+              <phase>process-sources</phase>
+              <configuration>
+                <configLocation>databroker-checks.xml</configLocation>
+                <includeResources>false</includeResources>
+                <includeTestSourceDirectory>false</includeTestSourceDirectory>
+                <includeTestResources>false</includeTestResources>
+                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
+                <failsOnError>true</failsOnError>
+                <consoleOutput>true</consoleOutput>
+              </configuration>
+            </execution>
+          </executions>
+        </plugin>
+      </plugins>
+    </pluginManagement>
     <plugins>
       <plugin>
         <groupId>org.apache.aries.blueprint</groupId>
index 0d3118bd2d25e6dbda0d72b64842b51e5fcb781c..7068946bec673bc55efeb72359da18a3ae0a1be3 100644 (file)
@@ -20,9 +20,9 @@ import javax.inject.Inject;
 import javax.inject.Singleton;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
+import org.opendaylight.genius.infra.Datastore;
 import org.opendaylight.genius.infra.RetryingManagedNewTransactionRunner;
 import org.opendaylight.serviceutils.tools.mdsal.rpc.FutureRpcResults;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInput;
@@ -53,13 +53,11 @@ public class LockManagerServiceImpl implements LockManagerService {
 
     private static final Logger LOG = LoggerFactory.getLogger(LockManagerServiceImpl.class);
 
-    private final DataBroker broker;
     private final RetryingManagedNewTransactionRunner txRunner;
     private final LockManagerUtils lockManagerUtils;
 
     @Inject
     public LockManagerServiceImpl(final @OsgiService DataBroker dataBroker, final LockManagerUtils lockManagerUtils) {
-        this.broker = dataBroker;
         this.lockManagerUtils = lockManagerUtils;
         this.txRunner = new RetryingManagedNewTransactionRunner(dataBroker);
     }
@@ -212,22 +210,21 @@ public class LockManagerServiceImpl implements LockManagerService {
             throws InterruptedException, ExecutionException {
         String lockName = lockData.getLockName();
         synchronized (lockName.intern()) {
-            ReadWriteTransaction tx = broker.newReadWriteTransaction();
-            Optional<Lock> result = tx.read(LogicalDatastoreType.OPERATIONAL, lockInstanceIdentifier).get();
-            if (!result.isPresent()) {
-                LOG.debug("Writing lock lockData {}", lockData);
-                tx.put(LogicalDatastoreType.OPERATIONAL, lockInstanceIdentifier, lockData, true);
-                tx.commit().get();
-                return true;
-            } else {
-                String lockDataOwner = result.get().getLockOwner();
-                String currentOwner = lockData.getLockOwner();
-                if (currentOwner.equals(lockDataOwner)) {
+            return txRunner.applyWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL, tx -> {
+                Optional<Lock> result = tx.read(lockInstanceIdentifier).get();
+                if (!result.isPresent()) {
+                    LOG.debug("Writing lock lockData {}", lockData);
+                    tx.put(lockInstanceIdentifier, lockData, true);
                     return true;
+                } else {
+                    String lockDataOwner = result.get().getLockOwner();
+                    String currentOwner = lockData.getLockOwner();
+                    if (currentOwner.equals(lockDataOwner)) {
+                        return true;
+                    }
                 }
-            }
-            tx.cancel();
-            return false;
+                return false;
+            }).get();
         }
     }
 }