BUG-981: improve waitForSchema() reliability 82/6882/1
authorRobert Varga <rovarga@cisco.com>
Sun, 11 May 2014 20:29:29 +0000 (22:29 +0200)
committerRobert Varga <rovarga@cisco.com>
Sun, 11 May 2014 20:33:13 +0000 (22:33 +0200)
SchemaLock.waitForSchema() should not check presence of codecs, but
rather only look at available definitions. Rework it such that it is
more useful.

Change-Id: I0e6240dad78d1e4987b3c7e8b4171b0e117e4792
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/RuntimeGeneratedMappingServiceImpl.java
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/SchemaLock.java

index 28027f025939936491c67f23ed13737ccac0fe44..a6bf37629818d798ebf459ee1da9282a7b594a3b 100644 (file)
@@ -1374,32 +1374,7 @@ class LazyGeneratedCodecRegistry implements //
 
     }
 
-    public boolean isCodecAvailable(final Class<?> cls) {
-        // FIXME: enforce type?
-        // Preconditions.checkArgument(DataContainer.class.isAssignableFrom(cls));
-
-        if (containerCodecs.containsKey(cls)) {
-            return true;
-        }
-        if (identifierCodecs.containsKey(cls)) {
-            return true;
-        }
-        if (choiceCodecs.containsKey(cls)) {
-            return true;
-        }
-        if (caseCodecs.containsKey(cls)) {
-            return true;
-        }
-        if (augmentableCodecs.containsKey(cls)) {
-            return true;
-        }
-        if (augmentationCodecs.containsKey(cls)) {
-            return true;
-        }
-        return false;
-    }
-
-    public static final Type referencedType(final Class<?> augmentableType) {
+    private static final Type referencedType(final Class<?> augmentableType) {
         return new ReferencedTypeImpl(augmentableType.getPackage().getName(), augmentableType.getSimpleName());
     }
 }
index 8ac4a9441a9fbb43f0171b7deb6917ea1a0c34ac..e6dc334e3ea566f48979d4a696b23a2ef403bb22 100644 (file)
@@ -71,6 +71,7 @@ import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
+import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 
 public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMappingService, SchemaContextListener,
@@ -268,14 +269,15 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
 
     @Override
     public void waitForSchema(final Class<?> cls) {
-        if (!registry.isCodecAvailable(cls)) {
-            final Type ref = Types.typeForClass(cls);
+        final ListenableFuture<Type> f = getSchemaDefinition(cls);
+        if (f != null) {
             try {
-                getSchemaWithRetry(ref);
+                f.get();
             } catch (InterruptedException | ExecutionException e) {
                 LOG.warn("Waiting for schema for class {} failed", cls, e);
                 throw new IllegalStateException(String.format("Failed to get schema for %s", cls), e);
             }
+            LOG.info("Schema for {} became available, thread unblocked", cls);
         }
     }
 
@@ -365,21 +367,18 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
         return serviceRef;
     }
 
-    private void getSchemaWithRetry(final Type type) throws InterruptedException, ExecutionException {
-        final SettableFuture<Type> f;
-
+    private ListenableFuture<Type> getSchemaDefinition(final Class<?> cls) {
+        final Type type = Types.typeForClass(cls);
         synchronized (this) {
             if (typeToDefinition.containsKey(type)) {
-                return;
+                return null;
             }
 
-            LOG.info("Thread blocked waiting for schema for: {}", type.getFullyQualifiedName());
-            f = SettableFuture.create();
+            LOG.info("Thread is going to wait for schema for: {}", type.getFullyQualifiedName());
+            final SettableFuture<Type> f = SettableFuture.create();
             promisedTypes.put(type, f);
+            return f;
         }
-
-        f.get();
-        LOG.info("Schema for {} became available, thread unblocked", type.getFullyQualifiedName());
     }
 
     @GuardedBy("this")
index be0bb45cfb90c982d6c70b8c3b6e0134349391f6..4633a22caaff3a95161a836710ae2313418346ca 100644 (file)
@@ -1,7 +1,12 @@
+/**
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.yangtools.sal.binding.generator.impl;
 
-public interface SchemaLock {
-
-    public void waitForSchema(Class<?> cls);
-    
+interface SchemaLock {
+    void waitForSchema(Class<?> cls);
 }