Search in sources :

Example 51 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class GroupPromoteAndRollbackAsyncTest method run.

@Test
public void run() throws Exception {
    GroupPromoteResult result = client.module(IndyPromoteClientModule.class).promoteToGroup(getAsyncRequest(new GroupPromoteRequest(source.getKey(), target.getName())));
    assertEquals(result.getResultCode(), ACCEPTED);
    result = getAsyncPromoteResult(GroupPromoteResult.class);
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTarget().getName(), equalTo(target.getName()));
    assertThat(result.getError(), nullValue());
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), first), equalTo(true));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), second), equalTo(true));
    Group g = client.stores().load(StoreType.group, target.getName(), Group.class);
    assertThat(g.getConstituents().contains(source.getKey()), equalTo(true));
    // now rollback.
    result = client.module(IndyPromoteClientModule.class).rollbackGroupPromote(result);
    assertThat(result.getError(), nullValue());
    // wait for events to propagate and remove indexed values.
    Thread.sleep(2000);
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), first), equalTo(false));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), second), equalTo(false));
    g = client.stores().load(StoreType.group, target.getName(), Group.class);
    assertThat(g.getConstituents().contains(source.getKey()), equalTo(false));
}
Also used : GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) IndyPromoteClientModule(org.commonjava.indy.promote.client.IndyPromoteClientModule) Test(org.junit.Test)

Example 52 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class PromotionManager method runPathPromotions.

private PathsPromoteResult runPathPromotions(final PathsPromoteRequest request, final Set<String> pending, final List<Transfer> contents, final ValidationResult validation) {
    long begin = System.currentTimeMillis();
    PromotionHelper.PromotionRepoRetrievalResult checkResult = promotionHelper.checkAndRetrieveSourceAndTargetRepos(request);
    if (checkResult.hasErrors()) {
        return new PathsPromoteResult(request, pending, emptySet(), emptySet(), StringUtils.join(checkResult.errors, "\n"), validation);
    }
    final ArtifactStore targetStore = checkResult.targetStore;
    StoreKey targetKey = targetStore.getKey();
    logger.info("Run promotion from: {} to: {}, paths: {}", request.getSource(), targetKey, pending);
    Set<Group> affectedGroups;
    try {
        affectedGroups = storeManager.query().getGroupsAffectedBy(targetKey);
        logger.info("Calculate affected groups, target: {}, affected-groups: {}", targetKey, affectedGroups);
    } catch (IndyDataException e) {
        logger.error("Get affected groups failed", e);
        return new PathsPromoteResult(request, pending, emptySet(), emptySet(), "Get affected groups failed, " + e.getMessage(), validation);
    }
    DrainingExecutorCompletionService<Set<PathTransferResult>> svc = new DrainingExecutorCompletionService<>(transferService);
    int corePoolSize = transferService.getCorePoolSize();
    int size = contents.size();
    int batchSize = getParalleledBatchSize(size, corePoolSize);
    logger.info("Execute parallel on collection, size: {}, batch: {}", size, batchSize);
    Collection<Collection<Transfer>> batches = batch(contents, batchSize);
    final List<String> errors = new ArrayList<>();
    try {
        detectOverloadVoid(() -> batches.forEach(batch -> svc.submit(newPathPromotionsJob(batch, targetStore, request, affectedGroups))));
    } catch (IndyWorkflowException e) {
        // might be PoolOverloadException. Log it and continue to revert any completed paths
        String msg = String.format("Failed to submit all path promotion jobs. Error: %s", e.toString());
        logger.error(msg, e);
        errors.add(msg);
    }
    final Set<PathTransferResult> results = new HashSet<>();
    try {
        svc.drain(results::addAll);
    } catch (InterruptedException | ExecutionException e) {
        String msg = String.format("Error waiting for promotion of: %s to: %s", request.getSource(), request.getTarget());
        logger.error(msg, e);
        errors.add(msg);
    }
    final Set<String> completed = new HashSet<>();
    final Set<String> skipped = new HashSet<>();
    results.forEach(result -> {
        if (result.error != null) {
            errors.add(result.error);
        } else if (result.skipped) {
            skipped.add(result.path);
        } else {
            completed.add(result.path);
        }
    });
    PathsPromoteResult result;
    if (!errors.isEmpty()) {
        List<String> rollbackErrors = promotionHelper.deleteFromStore(completed, targetStore);
        errors.addAll(rollbackErrors);
        result = new PathsPromoteResult(request, pending, emptySet(), emptySet(), StringUtils.join(errors, "\n"), validation);
    } else {
        result = new PathsPromoteResult(request, emptySet(), completed, skipped, null, validation);
        final String name = String.format("PromoteNFCClean-method(%s)-source(%s)-target(%s)", "runPathPromotions", request.getSource(), targetStore.getKey());
        final String context = String.format("Class: %s, method: %s, source: %s, target: %s", this.getClass().getName(), "runPathPromotions", request.getSource(), targetStore.getKey());
        storeManager.asyncGroupAffectedBy(new StoreDataManager.ContextualTask(name, context, () -> promotionHelper.clearStoreNFC(completed, targetStore, affectedGroups)));
        if (request.isFireEvents()) {
            fireEvent(promoteCompleteEvent, new PathsPromoteCompleteEvent(result));
        }
    }
    logger.info("Promotion completed, promotionId: {}, timeInSeconds: {}", request.getPromotionId(), timeInSeconds(begin));
    return result;
}
Also used : GroupMembershipLocks(org.commonjava.indy.core.inject.GroupMembershipLocks) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) LoggerFactory(org.slf4j.LoggerFactory) PathsPromoteCompleteEvent(org.commonjava.indy.promote.change.event.PathsPromoteCompleteEvent) StringUtils(org.apache.commons.lang3.StringUtils) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) WeftExecutorService(org.commonjava.cdi.util.weft.WeftExecutorService) Batcher.getParalleledBatchSize(org.commonjava.indy.promote.util.Batcher.getParalleledBatchSize) Future(java.util.concurrent.Future) WeftManaged(org.commonjava.cdi.util.weft.WeftManaged) Map(java.util.Map) StoreKey(org.commonjava.indy.model.core.StoreKey) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) PromotionCallbackHelper(org.commonjava.indy.promote.callback.PromotionCallbackHelper) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StoreType(org.commonjava.indy.model.core.StoreType) Set(java.util.Set) ExceptionUtils.getStackTrace(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace) Collectors(java.util.stream.Collectors) ContentManager(org.commonjava.indy.content.ContentManager) List(java.util.List) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) PromotionHelper.throwProperException(org.commonjava.indy.promote.data.PromotionHelper.throwProperException) GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StoreType.hosted(org.commonjava.indy.model.core.StoreType.hosted) IndyDataException(org.commonjava.indy.data.IndyDataException) PoolUtils.detectOverloadVoid(org.commonjava.indy.core.ctl.PoolUtils.detectOverloadVoid) AFFECTED_GROUPS(org.commonjava.indy.data.StoreDataManager.AFFECTED_GROUPS) TARGET_STORE(org.commonjava.indy.data.StoreDataManager.TARGET_STORE) ValidationRequest(org.commonjava.indy.promote.validate.model.ValidationRequest) Locker(org.commonjava.cdi.util.weft.Locker) Callable(java.util.concurrent.Callable) Batcher.batch(org.commonjava.indy.promote.util.Batcher.batch) AtomicReference(java.util.concurrent.atomic.AtomicReference) UPLOAD(org.commonjava.maven.galley.model.TransferOperation.UPLOAD) ThreadContext(org.commonjava.cdi.util.weft.ThreadContext) Group(org.commonjava.indy.model.core.Group) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Transfer(org.commonjava.maven.galley.model.Transfer) Measure(org.commonjava.o11yphant.metrics.annotation.Measure) ExecutorConfig(org.commonjava.cdi.util.weft.ExecutorConfig) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) PromotionValidator(org.commonjava.indy.promote.validate.PromotionValidator) EventUtils.fireEvent(org.commonjava.indy.change.EventUtils.fireEvent) DrainingExecutorCompletionService(org.commonjava.cdi.util.weft.DrainingExecutorCompletionService) DownloadManager(org.commonjava.indy.content.DownloadManager) Event(javax.enterprise.event.Event) PoolUtils.detectOverload(org.commonjava.indy.core.ctl.PoolUtils.detectOverload) PromoteCompleteEvent(org.commonjava.indy.promote.change.event.PromoteCompleteEvent) PromotionHelper.timeInMillSeconds(org.commonjava.indy.promote.data.PromotionHelper.timeInMillSeconds) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) PromotionValidationException(org.commonjava.indy.promote.validate.PromotionValidationException) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) Collections.emptySet(java.util.Collections.emptySet) PromoteConfig(org.commonjava.indy.promote.conf.PromoteConfig) SpecialPathManager(org.commonjava.maven.galley.spi.io.SpecialPathManager) IOException(java.io.IOException) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) ValuePipe(org.commonjava.indy.util.ValuePipe) NotFoundCache(org.commonjava.maven.galley.spi.nfc.NotFoundCache) ExecutionException(java.util.concurrent.ExecutionException) IGNORE_READONLY(org.commonjava.indy.data.StoreDataManager.IGNORE_READONLY) HostedRepository(org.commonjava.indy.model.core.HostedRepository) PromotionHelper.timeInSeconds(org.commonjava.indy.promote.data.PromotionHelper.timeInSeconds) RequestContextHelper(org.commonjava.indy.util.RequestContextHelper) StoreDataManager(org.commonjava.indy.data.StoreDataManager) InputStream(java.io.InputStream) Group(org.commonjava.indy.model.core.Group) Set(java.util.Set) HashSet(java.util.HashSet) Collections.emptySet(java.util.Collections.emptySet) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) ArrayList(java.util.ArrayList) DrainingExecutorCompletionService(org.commonjava.cdi.util.weft.DrainingExecutorCompletionService) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) StoreDataManager(org.commonjava.indy.data.StoreDataManager) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) StoreKey(org.commonjava.indy.model.core.StoreKey) IndyDataException(org.commonjava.indy.data.IndyDataException) PathsPromoteCompleteEvent(org.commonjava.indy.promote.change.event.PathsPromoteCompleteEvent) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) Collection(java.util.Collection)

Example 53 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class GroupNFCEntryClearedOnByPathPromoteTest method setupRepos.

@Before
public void setupRepos() throws IndyClientException {
    String message = "test setup";
    a = client.stores().create(new HostedRepository(MAVEN_PKG_KEY, HOSTED_A_NAME), message, HostedRepository.class);
    b = client.stores().create(new HostedRepository(MAVEN_PKG_KEY, HOSTED_B_NAME), message, HostedRepository.class);
    g = client.stores().create(new Group(MAVEN_PKG_KEY, GROUP_G_NAME, a.getKey()), message, Group.class);
    client.content().store(b.getKey(), PATH, new ByteArrayInputStream(POM_CONTENT.getBytes()));
}
Also used : Group(org.commonjava.indy.model.core.Group) ByteArrayInputStream(java.io.ByteArrayInputStream) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Before(org.junit.Before)

Example 54 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class GroupHostedMetadataRemergedOnPromoteTest method setupRepos.

@Before
public void setupRepos() throws IndyClientException {
    String message = "test setup";
    a = client.stores().create(new HostedRepository(HOSTED_A_NAME), message, HostedRepository.class);
    b = client.stores().create(new HostedRepository(HOSTED_B_NAME), message, HostedRepository.class);
    c = client.stores().create(new HostedRepository(HOSTED_C_NAME), message, HostedRepository.class);
    g = client.stores().create(new Group(GROUP_G_NAME, a.getKey(), b.getKey()), message, Group.class);
    client.content().store(a.getKey(), PATH, new ByteArrayInputStream(REPO_CONTENT_TEMPLATE.replaceAll("%version%", A_VERSION).getBytes()));
    client.content().store(b.getKey(), PATH, new ByteArrayInputStream(REPO_CONTENT_TEMPLATE.replaceAll("%version%", B_VERSION).getBytes()));
    client.content().store(c.getKey(), PATH, new ByteArrayInputStream(REPO_CONTENT_TEMPLATE.replaceAll("%version%", C_VERSION).getBytes()));
}
Also used : Group(org.commonjava.indy.model.core.Group) ByteArrayInputStream(java.io.ByteArrayInputStream) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Before(org.junit.Before)

Example 55 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class PromotionHelper method clearStoreNFC.

/**
 * Clear NFC for the source store and affected groups.
 *
 * @param sourcePaths The set of paths that need to be cleared from the NFC.
 * @param store The store whose affected groups should have their NFC entries cleared
 */
public void clearStoreNFC(final Set<String> sourcePaths, final ArtifactStore store, final Set<Group> affectedGroups) {
    Set<String> paths = sourcePaths.stream().map(sp -> sp.startsWith("/") && sp.length() > 1 ? sp.substring(1) : sp).collect(Collectors.toSet());
    paths.forEach(path -> {
        ConcreteResource resource = new ConcreteResource(LocationUtils.toLocation(store), path);
        logger.trace("Clearing NFC path: {} from: {}\n\tResource: {}", path, store.getKey(), resource);
        nfc.clearMissing(resource);
    });
    Set<Group> groups;
    if (affectedGroups != null) {
        groups = affectedGroups;
    } else {
        try {
            groups = storeManager.query().getGroupsAffectedBy(store.getKey());
        } catch (IndyDataException e) {
            logger.warn("Failed to clear NFC for groups affected by " + store.getKey(), e);
            return;
        }
    }
    if (groups != null) {
        groups.forEach(group -> {
            KeyedLocation gl = LocationUtils.toLocation(group);
            paths.forEach(path -> {
                ConcreteResource resource = new ConcreteResource(gl, path);
                logger.trace("Clearing NFC path: {} from: {}\n\tResource: {}", path, group.getKey(), resource);
                nfc.clearMissing(resource);
            });
        });
    }
}
Also used : KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) LoggerFactory(org.slf4j.LoggerFactory) PathGauges(org.commonjava.indy.promote.metrics.PathGauges) Group(org.commonjava.indy.model.core.Group) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Transfer(org.commonjava.maven.galley.model.Transfer) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) DownloadManager(org.commonjava.indy.content.DownloadManager) StoreKey(org.commonjava.indy.model.core.StoreKey) Location(org.commonjava.maven.galley.model.Location) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) PromotionValidationException(org.commonjava.indy.promote.validate.PromotionValidationException) LocationUtils(org.commonjava.indy.util.LocationUtils) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) StoreType(org.commonjava.indy.model.core.StoreType) Set(java.util.Set) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) NotFoundCache(org.commonjava.maven.galley.spi.nfc.NotFoundCache) Collectors(java.util.stream.Collectors) ContentManager(org.commonjava.indy.content.ContentManager) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) IGNORE_READONLY(org.commonjava.indy.data.StoreDataManager.IGNORE_READONLY) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) ApplicationScoped(javax.enterprise.context.ApplicationScoped) IndyDataException(org.commonjava.indy.data.IndyDataException) StoreDataManager(org.commonjava.indy.data.StoreDataManager) IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource)

Aggregations

Group (org.commonjava.indy.model.core.Group)211 Test (org.junit.Test)111 HostedRepository (org.commonjava.indy.model.core.HostedRepository)89 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)82 StoreKey (org.commonjava.indy.model.core.StoreKey)76 ByteArrayInputStream (java.io.ByteArrayInputStream)67 InputStream (java.io.InputStream)62 IndyDataException (org.commonjava.indy.data.IndyDataException)44 AbstractContentManagementTest (org.commonjava.indy.ftest.core.AbstractContentManagementTest)42 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)42 Transfer (org.commonjava.maven.galley.model.Transfer)31 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)30 Logger (org.slf4j.Logger)30 Before (org.junit.Before)29 StoreDataManager (org.commonjava.indy.data.StoreDataManager)27 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)27 HashSet (java.util.HashSet)26 ArrayList (java.util.ArrayList)25 List (java.util.List)22 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)22