Skip to content

Commit

Permalink
[MRESOLVER-628] Explicit cache key for prioritized components (#601)
Browse files Browse the repository at this point in the history
As original solution using map.hashCode may clash, and in
that case runtime "just in time" class clast issue happens.

Rather provide explicit discriminator class "what is in maps"
and use that to create key to cache in session.

---

https://issues.apache.org/jira/browse/MRESOLVER-628
  • Loading branch information
cstamas authored Nov 14, 2024
1 parent 30f95a1 commit c32b8c9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession
requireNonNull(repository, "repository cannot be null");

PrioritizedComponents<LocalRepositoryManagerFactory> factories = PrioritizedComponents.reuseOrCreate(
session, localRepositoryManagerFactories, LocalRepositoryManagerFactory::getPriority);
session,
LocalRepositoryManagerFactory.class,
localRepositoryManagerFactories,
LocalRepositoryManagerFactory::getPriority);

List<NoLocalRepositoryManagerException> errors = new ArrayList<>();
for (PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public RepositoryConnector newRepositoryConnector(RepositorySystemSession sessio
}

PrioritizedComponents<RepositoryConnectorFactory> factories = PrioritizedComponents.reuseOrCreate(
session, connectorFactories, RepositoryConnectorFactory::getPriority);
session, RepositoryConnectorFactory.class, connectorFactories, RepositoryConnectorFactory::getPriority);

RemoteRepositoryFilter filter = remoteRepositoryFilterManager.getRemoteRepositoryFilter(session);
List<NoRepositoryConnectorException> errors = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public RepositoryLayout newRepositoryLayout(RepositorySystemSession session, Rem
requireNonNull(session, "session cannot be null");
requireNonNull(repository, "remote repository cannot be null");

PrioritizedComponents<RepositoryLayoutFactory> factories =
PrioritizedComponents.reuseOrCreate(session, layoutFactories, RepositoryLayoutFactory::getPriority);
PrioritizedComponents<RepositoryLayoutFactory> factories = PrioritizedComponents.reuseOrCreate(
session, RepositoryLayoutFactory.class, layoutFactories, RepositoryLayoutFactory::getPriority);

List<NoRepositoryLayoutException> errors = new ArrayList<>();
for (PrioritizedComponent<RepositoryLayoutFactory> factory : factories.getEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public Transporter newTransporter(RepositorySystemSession session, RemoteReposit
requireNonNull(session, "session cannot be null");
requireNonNull(repository, "repository cannot be null");

PrioritizedComponents<TransporterFactory> factories =
PrioritizedComponents.reuseOrCreate(session, transporterFactories, TransporterFactory::getPriority);
PrioritizedComponents<TransporterFactory> factories = PrioritizedComponents.reuseOrCreate(
session, TransporterFactory.class, transporterFactories, TransporterFactory::getPriority);

List<NoTransporterException> errors = new ArrayList<>();
for (PrioritizedComponent<TransporterFactory> factory : factories.getEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
*/
public final class PrioritizedComponents<T> {
/**
* Reuses or creates and stores (if session data does not contain yet) prioritized components under this key into
* given session. Same session is used to configure prioritized components.
* Reuses or creates and stores (if session data does not contain yet) prioritized components under certain key into
* given session. Same session is used to configure prioritized components, so priority sorted components during
* session are immutable and reusable.
* <p>
* The {@code components} are expected to be Sisu injected {@link Map<String, C>}-like component maps. There is a
* simple "change detection" in place, as injected maps are dynamic, they are atomically expanded or contracted
Expand All @@ -46,11 +47,14 @@ public final class PrioritizedComponents<T> {
*/
@SuppressWarnings("unchecked")
public static <C> PrioritizedComponents<C> reuseOrCreate(
RepositorySystemSession session, Map<String, C> components, Function<C, Float> priorityFunction) {
RepositorySystemSession session,
Class<C> discriminator,
Map<String, C> components,
Function<C, Float> priorityFunction) {
boolean cached = ConfigUtils.getBoolean(
session, ConfigurationProperties.DEFAULT_CACHED_PRIORITIES, ConfigurationProperties.CACHED_PRIORITIES);
if (cached) {
String key = PrioritizedComponents.class.getName() + ".pc" + Integer.toHexString(components.hashCode());
String key = PrioritizedComponents.class.getName() + ".pc." + discriminator.getName();
return (PrioritizedComponents<C>)
session.getData().computeIfAbsent(key, () -> create(session, components, priorityFunction));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ private Utils() {}

private static PrioritizedComponents<ArtifactDecoratorFactory> sortArtifactDecoratorFactories(
RepositorySystemSession session, Map<String, ArtifactDecoratorFactory> factories) {
return PrioritizedComponents.reuseOrCreate(session, factories, ArtifactDecoratorFactory::getPriority);
return PrioritizedComponents.reuseOrCreate(
session, ArtifactDecoratorFactory.class, factories, ArtifactDecoratorFactory::getPriority);
}

public static List<? extends ArtifactDecorator> getArtifactDecorators(
Expand All @@ -67,7 +68,8 @@ public static List<? extends ArtifactDecorator> getArtifactDecorators(

private static PrioritizedComponents<ArtifactGeneratorFactory> sortArtifactGeneratorFactories(
RepositorySystemSession session, Map<String, ArtifactGeneratorFactory> factories) {
return PrioritizedComponents.reuseOrCreate(session, factories, ArtifactGeneratorFactory::getPriority);
return PrioritizedComponents.reuseOrCreate(
session, ArtifactGeneratorFactory.class, factories, ArtifactGeneratorFactory::getPriority);
}

private static List<? extends ArtifactGenerator> doGetArtifactGenerators(
Expand Down Expand Up @@ -107,7 +109,8 @@ public static List<? extends ArtifactGenerator> getArtifactGenerators(

private static PrioritizedComponents<MetadataGeneratorFactory> sortMetadataGeneratorFactories(
RepositorySystemSession session, Map<String, MetadataGeneratorFactory> factories) {
return PrioritizedComponents.reuseOrCreate(session, factories, MetadataGeneratorFactory::getPriority);
return PrioritizedComponents.reuseOrCreate(
session, MetadataGeneratorFactory.class, factories, MetadataGeneratorFactory::getPriority);
}

private static List<? extends MetadataGenerator> doGetMetadataGenerators(
Expand Down

0 comments on commit c32b8c9

Please sign in to comment.