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.Map;
import java.util.Map.Entry;
import java.util.Set;
+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.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.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> revisionCache = CacheBuilder.newBuilder()
+ private final Cache<Collection<SourceIdentifier>, EffectiveModelContext> revisionCache = CacheBuilder.newBuilder()
.weakValues().build();
- private final Cache<Collection<SourceIdentifier>, SchemaContext> semVerCache = CacheBuilder.newBuilder()
+ private final Cache<Collection<SourceIdentifier>, EffectiveModelContext> semVerCache = CacheBuilder.newBuilder()
.weakValues().build();
private final @NonNull SchemaRepository repository;
private final @NonNull SchemaContextFactoryConfiguration config;
}
@Override
- public @NonNull ListenableFuture<SchemaContext> createSchemaContext(
+ public @NonNull ListenableFuture<EffectiveModelContext> createEffectiveModelContext(
final @NonNull Collection<SourceIdentifier> requiredSources) {
return createSchemaContext(requiredSources,
config.getStatementParserMode() == StatementParserMode.SEMVER_MODE ? semVerCache : revisionCache,
new AssembleSources(config));
}
- private @NonNull ListenableFuture<SchemaContext> createSchemaContext(
+ private @NonNull ListenableFuture<EffectiveModelContext> createSchemaContext(
final Collection<SourceIdentifier> requiredSources,
- final Cache<Collection<SourceIdentifier>, SchemaContext> cache,
- final AsyncFunction<List<ASTSchemaSource>, SchemaContext> assembleSources) {
+ 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 immediateFluentFuture(existing);
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(final Throwable cause) {
LOG.debug("Failed to assemble sources", cause);
+ rf.setException(cause);
}
}, MoreExecutors.directExecutor());
- return cf;
+ return rf;
}
private ListenableFuture<ASTSchemaSource> requestSource(final @NonNull SourceIdentifier identifier) {
}
}
- private static final class AssembleSources implements AsyncFunction<List<ASTSchemaSource>, SchemaContext> {
+ private static final class AssembleSources implements AsyncFunction<List<ASTSchemaSource>, EffectiveModelContext> {
private final @NonNull SchemaContextFactoryConfiguration config;
private final @NonNull Function<ASTSchemaSource, SourceIdentifier> getIdentifier;
}
@Override
- public FluentFuture<SchemaContext> apply(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 =
ast.getSymbolicName().orElse(null)));
}
- final SchemaContext schemaContext;
+ final EffectiveModelContext schemaContext;
try {
schemaContext = reactor.buildEffective();
} catch (final ReactorException ex) {