Skip to content

Commit

Permalink
Refresh all repositories when performing an update of IU container
Browse files Browse the repository at this point in the history
Currently the IUBundleContainer#update only queries the current P2
metadata that might be cached. This can lead to situations where when a
repository has "just changed" it does not see the latest metadata.

As we can assume that if the user mind to hit the update button it is
meant to really get the latest, this now ask the manager to update the
metadata (and possibly colocated artifacts) repository.
  • Loading branch information
laeubi committed Dec 14, 2024
1 parent 51ef6fe commit af1185d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ void synchronizerChanged(ITargetDefinition target) {
public synchronized IUBundleContainer update(Set<String> toUpdate, IProgressMonitor monitor) throws CoreException {
SubMonitor progress = SubMonitor.convert(monitor, 100);
IQueryable<IInstallableUnit> source = P2TargetUtils.getQueryableMetadata(fRepos, isFollowRepositoryReferences(),
progress.split(30));
true, progress.split(30));
boolean updated = false;
List<UnitDeclaration> updatedUnits = new ArrayList<>(fIUs);
SubMonitor loopProgress = progress.split(70).setWorkRemaining(updatedUnits.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class Messages extends NLS {
public static String LocalTargetHandle_3;
public static String LocalTargetHandle_4;
public static String LocalTargetHandle_5;
public static String P2TargetUtils_cant_refresh_artifacts;
public static String P2TargetUtils_cant_refresh_metadata;
public static String P2TargetUtils_ProvisioningSourceTask;
public static String ProfileBundleContainer_0;
public static String ProfileBundleContainer_2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ LocalTargetHandle_2=Unable to generate memento for target platform
LocalTargetHandle_3=Failed to delete target definition: {0}
LocalTargetHandle_4=Error saving target definition {0}
LocalTargetHandle_5=Error creating target file
P2TargetUtils_cant_refresh_artifacts=Can't refresh artifact repository {0}
P2TargetUtils_cant_refresh_metadata=Can't refresh metadata repository {0} reported metadata might be outdated
P2TargetUtils_ProvisioningSourceTask=Provisioning source bundles
ProfileBundleContainer_0=Installation directory does not exist: {0}
ProfileBundleContainer_2=Configuration directory does not exist: {0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import org.eclipse.equinox.p2.repository.artifact.IFileArtifactRepository;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.core.target.ITargetDefinition;
import org.eclipse.pde.core.target.ITargetHandle;
import org.eclipse.pde.core.target.ITargetLocation;
Expand Down Expand Up @@ -984,30 +985,58 @@ private static <T> T getP2Service(Class<T> key, String absentErrorMessage) throw
/**
* Return a queryable on the metadata defined in the given repo locations
*
* @param repos the repos to lookup
* @param followRepositoryReferences whether to follow repository references
* @param monitor the progress monitor
* @param repos
* the repos to lookup
* @param followRepositoryReferences
* whether to follow repository references
* @param monitor
* the progress monitor
* @return the set of metadata repositories found
* @throws CoreException if there is a problem getting the repositories
* @throws CoreException
* if there is a problem getting the repositories
*/
static IQueryable<IInstallableUnit> getQueryableMetadata(Collection<URI> repos, boolean followRepositoryReferences,
IProgressMonitor monitor) throws CoreException {
IMetadataRepositoryManager manager = getRepoManager();
return getQueryableMetadata(repos, followRepositoryReferences, false, monitor);
}

/**
* Return a queryable on the metadata defined in the given repo locations
*
* @param repos
* the repos to lookup
* @param followRepositoryReferences
* whether to follow repository references
* @param forceReload
* forces reloading of repositories
* @param monitor
* the progress monitor
* @return the set of metadata repositories found
* @throws CoreException
* if there is a problem getting the repositories
*/
static IQueryable<IInstallableUnit> getQueryableMetadata(Collection<URI> repos, boolean followRepositoryReferences,
boolean forceReload,
IProgressMonitor monitor) throws CoreException {
IMetadataRepositoryManager metadataRepositoryManager = getRepoManager();
Collection<URI> existing = new LinkedHashSet<>(
Arrays.asList(metadataRepositoryManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL)));
if (repos.isEmpty()) {
repos = Arrays.asList(manager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL));
repos = existing;
}
SubMonitor subMonitor = SubMonitor.convert(monitor, repos.size() * 2);
int work = repos.size() * (forceReload?3: 2);
SubMonitor subMonitor = SubMonitor.convert(monitor, work);

Set<IRepositoryReference> seen = new HashSet<>();
List<IMetadataRepository> result = new ArrayList<>(repos.size());
List<IMetadataRepository> additional = new ArrayList<>();
MultiStatus repoStatus = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories);
for (URI location : repos) {
try {
IMetadataRepository repository = manager.loadRepository(location, subMonitor.split(1));
IMetadataRepository repository = metadataRepositoryManager.loadRepository(location, subMonitor.split(1));
result.add(repository);
if (followRepositoryReferences) {
addReferences(repository, additional, seen, manager, subMonitor.split(1));
addReferences(repository, additional, seen, metadataRepositoryManager, subMonitor.split(1));
}
} catch (ProvisionException e) {
repoStatus.add(e.getStatus());
Expand All @@ -1021,7 +1050,39 @@ static IQueryable<IInstallableUnit> getQueryableMetadata(Collection<URI> repos,
if (result.size() == 1) {
return result.get(0);
}
return QueryUtil.compoundQueryable(new LinkedHashSet<>(result));
LinkedHashSet<IMetadataRepository> unique = new LinkedHashSet<>(result);
if (forceReload && !unique.isEmpty()) {
List<IMetadataRepository> refreshed = new ArrayList<>();
subMonitor.setWorkRemaining(2 * unique.size());
IArtifactRepositoryManager artifactRepositoryManager = getArtifactRepositoryManager();
for (IMetadataRepository metadataRepository : unique) {
URI location = metadataRepository.getLocation();
if (existing.contains(location)) {
try {
refreshed.add(metadataRepositoryManager.refreshRepository(location, subMonitor.split(1)));
} catch (ProvisionException e) {
ILog.get().warn(NLS.bind(Messages.P2TargetUtils_cant_refresh_metadata, location), e);
// if refresh do not work, use the previously loaded
// repository
refreshed.add(metadataRepository);
}
try {
artifactRepositoryManager.refreshRepository(location, subMonitor.split(1));
} catch (ProvisionException e) {
// it might fail here because they are not co-located or
// for
// other reasons but we have done our best
ILog.get().warn(NLS.bind(Messages.P2TargetUtils_cant_refresh_artifacts, location), e);
}
} else {
// the repo was just loaded as part of this called so it has
// to be (almost) fresh
refreshed.add(metadataRepository);
}
}
return QueryUtil.compoundQueryable(refreshed);
}
return QueryUtil.compoundQueryable(unique);
}

private static void addReferences(IMetadataRepository repository, List<IMetadataRepository> result,
Expand Down

0 comments on commit af1185d

Please sign in to comment.