Deduplicate concurrent SchemaContext loads
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaContextFactory.java
index 0ce69cfbd26e98d531ecf50c91c1023a6da0adcf..10a81fa01e32e5f8edaf388e0b66cefccbe26b94 100644 (file)
@@ -7,8 +7,11 @@
  */
 package org.opendaylight.yangtools.yang.parser.repo;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
+
 import com.google.common.base.Function;
-import com.google.common.base.Preconditions;
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.collect.Collections2;
@@ -16,26 +19,28 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import com.google.common.util.concurrent.AsyncFunction;
+import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.util.concurrent.SettableFuture;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 import java.util.Set;
-import javax.annotation.Nonnull;
+import java.util.concurrent.ExecutionException;
 import org.antlr.v4.runtime.ParserRuleContext;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
 import org.opendaylight.yangtools.yang.parser.impl.DefaultReactors;
@@ -47,42 +52,41 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementR
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-final class SharedSchemaContextFactory implements SchemaContextFactory {
+final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
     private static final Logger LOG = LoggerFactory.getLogger(SharedSchemaContextFactory.class);
 
-    private final Cache<Collection<SourceIdentifier>, SchemaContext> cache = CacheBuilder.newBuilder().weakValues()
-            .build();
-    private final Cache<Collection<SourceIdentifier>, SchemaContext> semVerCache = CacheBuilder.newBuilder()
+    private final Cache<Collection<SourceIdentifier>, EffectiveModelContext> revisionCache = CacheBuilder.newBuilder()
+            .weakValues().build();
+    private final Cache<Collection<SourceIdentifier>, EffectiveModelContext> semVerCache = CacheBuilder.newBuilder()
             .weakValues().build();
-    private final SharedSchemaRepository repository;
-    // FIXME: ignored right now
-    private final SchemaSourceFilter filter;
-
-    // FIXME SchemaRepository should be the type for repository parameter instead of SharedSchemaRepository
-    //       (final implementation)
-    SharedSchemaContextFactory(final SharedSchemaRepository repository, final SchemaSourceFilter filter) {
-        this.repository = Preconditions.checkNotNull(repository);
-        this.filter = Preconditions.checkNotNull(filter);
+    private final @NonNull SchemaRepository repository;
+    private final @NonNull SchemaContextFactoryConfiguration config;
+
+    SharedSchemaContextFactory(final @NonNull SchemaRepository repository,
+        final @NonNull SchemaContextFactoryConfiguration config) {
+        this.repository = requireNonNull(repository);
+        this.config = requireNonNull(config);
     }
 
     @Override
-    public ListenableFuture<SchemaContext> createSchemaContext(final Collection<SourceIdentifier> requiredSources,
-            final StatementParserMode statementParserMode, final Set<QName> supportedFeatures) {
+    public @NonNull ListenableFuture<EffectiveModelContext> createEffectiveModelContext(
+            final @NonNull Collection<SourceIdentifier> requiredSources) {
         return createSchemaContext(requiredSources,
-                statementParserMode == StatementParserMode.SEMVER_MODE ? this.semVerCache : this.cache,
-                new AssembleSources(Optional.ofNullable(supportedFeatures), statementParserMode));
+                config.getStatementParserMode() == StatementParserMode.SEMVER_MODE ? semVerCache : revisionCache,
+                new AssembleSources(config));
     }
 
-    private ListenableFuture<SchemaContext> createSchemaContext(final Collection<SourceIdentifier> requiredSources,
-            final Cache<Collection<SourceIdentifier>, SchemaContext> cache,
-            final AsyncFunction<List<ASTSchemaSource>, SchemaContext> assembleSources) {
+    private @NonNull ListenableFuture<EffectiveModelContext> createSchemaContext(
+            final Collection<SourceIdentifier> requiredSources,
+            final Cache<Collection<SourceIdentifier>, EffectiveModelContext> cache,
+            final AsyncFunction<List<ASTSchemaSource>, EffectiveModelContext> assembleSources) {
         // Make sources unique
         final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);
 
-        final SchemaContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
+        final EffectiveModelContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
         if (existing != null) {
             LOG.debug("Returning cached context {}", existing);
-            return Futures.immediateFuture(existing);
+            return immediateFluentFuture(existing);
         }
 
         // Request all sources be loaded
@@ -96,26 +100,37 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
             MoreExecutors.directExecutor());
 
         // Assemble sources into a schema context
-        final ListenableFuture<SchemaContext> cf = Futures.transformAsync(sf, assembleSources,
+        final ListenableFuture<EffectiveModelContext> cf = Futures.transformAsync(sf, assembleSources,
             MoreExecutors.directExecutor());
 
-        // Populate cache when successful
-        Futures.addCallback(cf, new FutureCallback<SchemaContext>() {
+        final SettableFuture<EffectiveModelContext> rf = SettableFuture.create();
+        Futures.addCallback(cf, new FutureCallback<EffectiveModelContext>() {
             @Override
-            public void onSuccess(final SchemaContext result) {
-                cache.put(uniqueSourceIdentifiers, result);
+            public void onSuccess(final EffectiveModelContext result) {
+                // Deduplicate concurrent loads
+                final EffectiveModelContext existing;
+                try {
+                    existing = cache.get(uniqueSourceIdentifiers, () -> result);
+                } catch (ExecutionException e) {
+                    LOG.warn("Failed to recheck result with cache, will use computed value", e);
+                    rf.set(result);
+                    return;
+                }
+
+                rf.set(existing);
             }
 
             @Override
-            public void onFailure(@Nonnull final Throwable cause) {
+            public void onFailure(final Throwable cause) {
                 LOG.debug("Failed to assemble sources", cause);
+                rf.setException(cause);
             }
         }, MoreExecutors.directExecutor());
 
-        return cf;
+        return rf;
     }
 
-    private ListenableFuture<ASTSchemaSource> requestSource(final SourceIdentifier identifier) {
+    private ListenableFuture<ASTSchemaSource> requestSource(final @NonNull SourceIdentifier identifier) {
         return repository.getSchemaSource(identifier, ASTSchemaSource.class);
     }
 
@@ -141,7 +156,7 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
         private final List<SourceIdentifier> sourceIdentifiers;
 
         SourceIdMismatchDetector(final List<SourceIdentifier> sourceIdentifiers) {
-            this.sourceIdentifiers = Preconditions.checkNotNull(sourceIdentifiers);
+            this.sourceIdentifiers = requireNonNull(sourceIdentifiers);
         }
 
         @Override
@@ -170,17 +185,13 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
         }
     }
 
-    private static final class AssembleSources implements AsyncFunction<List<ASTSchemaSource>, SchemaContext> {
-
-        private final Optional<Set<QName>> supportedFeatures;
-        private final StatementParserMode statementParserMode;
-        private final Function<ASTSchemaSource, SourceIdentifier> getIdentifier;
+    private static final class AssembleSources implements AsyncFunction<List<ASTSchemaSource>, EffectiveModelContext> {
+        private final @NonNull SchemaContextFactoryConfiguration config;
+        private final @NonNull Function<ASTSchemaSource, SourceIdentifier> getIdentifier;
 
-        private AssembleSources(final Optional<Set<QName>> supportedFeatures,
-                final StatementParserMode statementParserMode) {
-            this.supportedFeatures = supportedFeatures;
-            this.statementParserMode = Preconditions.checkNotNull(statementParserMode);
-            switch (statementParserMode) {
+        private AssembleSources(final @NonNull SchemaContextFactoryConfiguration config) {
+            this.config = config;
+            switch (config.getStatementParserMode()) {
                 case SEMVER_MODE:
                     this.getIdentifier = ASTSchemaSource::getSemVerIdentifier;
                     break;
@@ -190,7 +201,7 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
         }
 
         @Override
-        public ListenableFuture<SchemaContext> apply(@Nonnull final List<ASTSchemaSource> sources)
+        public FluentFuture<EffectiveModelContext> apply(final List<ASTSchemaSource> sources)
                 throws SchemaResolutionException, ReactorException {
             final Map<SourceIdentifier, ASTSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
             final Map<SourceIdentifier, YangModelDependencyInfo> deps =
@@ -198,7 +209,8 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
 
             LOG.debug("Resolving dependency reactor {}", deps);
 
-            final DependencyResolver res = this.statementParserMode == StatementParserMode.SEMVER_MODE
+            final StatementParserMode statementParserMode = config.getStatementParserMode();
+            final DependencyResolver res = statementParserMode == StatementParserMode.SEMVER_MODE
                     ? SemVerDependencyResolver.create(deps) : RevisionDependencyResolver.create(deps);
             if (!res.getUnresolvedSources().isEmpty()) {
                 LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(),
@@ -208,28 +220,27 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
             }
 
             final BuildAction reactor = DefaultReactors.defaultReactor().newBuild(statementParserMode);
-            if (supportedFeatures.isPresent()) {
-                reactor.setSupportedFeatures(supportedFeatures.get());
-            }
+            config.getSupportedFeatures().ifPresent(reactor::setSupportedFeatures);
+            config.getModulesDeviatedByModules().ifPresent(reactor::setModulesWithSupportedDeviations);
 
             for (final Entry<SourceIdentifier, ASTSchemaSource> e : srcs.entrySet()) {
                 final ASTSchemaSource ast = e.getValue();
                 final ParserRuleContext parserRuleCtx = ast.getAST();
-                Preconditions.checkArgument(parserRuleCtx instanceof StatementContext,
-                        "Unsupported context class %s for source %s", parserRuleCtx.getClass(), e.getKey());
+                checkArgument(parserRuleCtx instanceof StatementContext, "Unsupported context class %s for source %s",
+                    parserRuleCtx.getClass(), e.getKey());
 
                 reactor.addSource(YangStatementStreamSource.create(e.getKey(), (StatementContext) parserRuleCtx,
                     ast.getSymbolicName().orElse(null)));
             }
 
-            final SchemaContext schemaContext;
+            final EffectiveModelContext schemaContext;
             try {
                 schemaContext = reactor.buildEffective();
             } catch (final ReactorException ex) {
                 throw new SchemaResolutionException("Failed to resolve required models", ex.getSourceIdentifier(), ex);
             }
 
-            return Futures.immediateFuture(schemaContext);
+            return immediateFluentFuture(schemaContext);
         }
     }
 }