Search in sources :

Example 56 with Measure

use of org.commonjava.o11yphant.metrics.annotation.Measure in project indy by Commonjava.

the class MavenMetadataGenerator method generateGroupFileContent.

/**
 * Generate maven-metadata.xml and checksum files.
 * @param group
 * @param members Concrete stores in group
 * @param path
 * @param eventMetadata
 * @return
 * @throws IndyWorkflowException
 */
@Override
@Measure
public Transfer generateGroupFileContent(final Group group, final List<ArtifactStore> members, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    final Transfer rawTarget = fileManager.getTransfer(group, path);
    // First we check the metadata and all of its siblings metadata files
    if (canProcess(path) && exists(rawTarget)) {
        // Means there is no metadata change if this transfer exists, so directly return it.
        logger.trace("Raw metadata file exists for group {} of path {}, no need to regenerate.", group.getKey(), path);
        eventMetadata.set(GROUP_METADATA_EXISTS, true);
        return rawTarget;
    }
    // Then changed back to the metadata itself whatever the path is
    String toMergePath = path;
    if (!path.endsWith(MavenMetadataMerger.METADATA_NAME)) {
        toMergePath = normalize(normalize(parentPath(toMergePath)), MavenMetadataMerger.METADATA_NAME);
    }
    final Transfer target = fileManager.getTransfer(group, toMergePath);
    if (exists(target)) {
        // Means there is no metadata change if this transfer exists, so directly return it.
        logger.trace("Merged metadata file exists for group {} of path {}, no need to regenerate.", group.getKey(), toMergePath);
        eventMetadata.set(GROUP_METADATA_EXISTS, true);
        return target;
    }
    AtomicReference<IndyWorkflowException> wfEx = new AtomicReference<>();
    final String mergePath = toMergePath;
    boolean mergingDone = mergerLocks.ifUnlocked(computeKey(group, toMergePath), p -> {
        try {
            logger.debug("Start metadata generation for metadata file {} in group {}", path, group);
            List<StoreKey> contributing = new ArrayList<>();
            final Metadata md = generateGroupMetadata(group, members, contributing, path);
            if (md != null) {
                final Versioning versioning = md.getVersioning();
                logger.trace("Regenerated Metadata for group {} of path {}: latest version: {}, versions: {}", group.getKey(), mergePath, versioning != null ? versioning.getLatest() : null, versioning != null ? versioning.getVersions() : null);
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    logger.trace("Regenerate lost metadata, group: {}, path: {}", group.getKey(), path);
                    new MetadataXpp3Writer().write(baos, md);
                    final byte[] merged = baos.toByteArray();
                    try (final OutputStream fos = target.openOutputStream(TransferOperation.GENERATE, true, eventMetadata)) {
                        fos.write(merged);
                    } catch (final IOException e) {
                        throw new IndyWorkflowException("Failed to write merged metadata to: {}.\nError: {}", e, target, e.getMessage());
                    }
                    String mergeInfo = writeGroupMergeInfo(md, group, contributing, mergePath);
                    eventMetadata.set(GROUP_METADATA_GENERATED, true);
                    MetadataInfo info = new MetadataInfo(md);
                    info.setMetadataMergeInfo(mergeInfo);
                    putToMetadataCache(group.getKey(), mergePath, info);
                } catch (final IOException e) {
                    logger.error(String.format("Cannot write consolidated metadata: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
                }
            }
        } catch (IndyWorkflowException e) {
            wfEx.set(e);
            return false;
        }
        return true;
    }, (p, mergerLock) -> {
        logger.info("The metadata generation is still in process by another thread for the metadata file for this path {} in group {}, so block current thread to wait for result", path, group);
        return mergerLocks.waitForLock(THREAD_WAITING_TIME_SECONDS, mergerLock);
    });
    IndyWorkflowException ex = wfEx.get();
    if (ex != null) {
        throw ex;
    }
    if (exists(target)) {
        // if this is a checksum file, we need to return the original path (if it is metadata, original is target)
        Transfer original = fileManager.getTransfer(group, path);
        if (exists(original)) {
            if (toMergePath != path) {
                logger.debug("This is a checksum file, return the original path {}", path);
            }
            return original;
        }
    }
    if (mergingDone) {
        logger.info("Merging finished but the merging file not created correctly. See merging related log for details. Merging group: {}, path: {}", group, path);
    } else {
        logger.error("Merging not finished but thread waiting timeout, caused current thread will get a null merging result. Try to enlarge the waiting timeout. Merging group: {}, path: {}", group, path);
    }
    return null;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StoreKey(org.commonjava.indy.model.core.StoreKey) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 57 with Measure

use of org.commonjava.o11yphant.metrics.annotation.Measure in project indy by Commonjava.

the class AbstractStoreDataManager method refreshAffectedBy.

@Measure
protected void refreshAffectedBy(final ArtifactStore store, final ArtifactStore original, StoreUpdateAction action) {
    if (store == null) {
        return;
    }
    if (store instanceof Group && isExcludedGroup((Group) store)) {
        logger.info("Skip affectedBy calculation of group: {}", store.getName());
        return;
    }
    if (action == DELETE) {
        if (store instanceof Group) {
            Group grp = (Group) store;
            new HashSet<>(grp.getConstituents()).forEach((key) -> removeAffectedBy(key, store.getKey()));
            logger.info("Removed affected-by reverse mapping for: {} in {} member stores", store.getKey(), grp.getConstituents().size());
        } else {
            removeAffectedStore(store.getKey());
        }
    } else if (action == STORE) {
        // NOTE: Only group membership changes can affect our affectedBy, unless the update is a store deletion.
        if (store instanceof Group) {
            final Set<StoreKey> updatedConstituents = new HashSet<>(((Group) store).getConstituents());
            final Set<StoreKey> originalConstituents;
            if (original != null) {
                originalConstituents = new HashSet<>(((Group) original).getConstituents());
            } else {
                originalConstituents = new HashSet<>();
            }
            final Set<StoreKey> added = new HashSet<>();
            final Set<StoreKey> removed = new HashSet<>();
            for (StoreKey updKey : updatedConstituents) {
                if (!originalConstituents.contains(updKey)) {
                    added.add(updKey);
                }
            }
            for (StoreKey oriKey : originalConstituents) {
                if (!updatedConstituents.contains(oriKey)) {
                    removed.add(oriKey);
                }
            }
            removed.forEach((key) -> removeAffectedBy(key, store.getKey()));
            logger.info("Removed affected-by reverse mapping for: {} in {} member stores", store.getKey(), removed.size());
            added.forEach((key) -> addAffectedBy(key, store.getKey()));
            logger.info("Added affected-by reverse mapping for: {} in {} member stores", store.getKey(), added.size());
        }
    }
}
Also used : BiFunction(java.util.function.BiFunction) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) LoggerFactory(org.slf4j.LoggerFactory) STORE(org.commonjava.indy.db.common.StoreUpdateAction.STORE) StringUtils(org.apache.commons.lang3.StringUtils) SslValidationConfig(org.commonjava.indy.conf.SslValidationConfig) WeftManaged(org.commonjava.cdi.util.weft.WeftManaged) Map(java.util.Map) ApplicationStatus(org.commonjava.indy.util.ApplicationStatus) StoreKey(org.commonjava.indy.model.core.StoreKey) StoreEventDispatcher(org.commonjava.indy.data.StoreEventDispatcher) ArtifactStoreQuery(org.commonjava.indy.data.ArtifactStoreQuery) StoreType.group(org.commonjava.indy.model.core.StoreType.group) Collection(java.util.Collection) StoreType(org.commonjava.indy.model.core.StoreType) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) IndyConfiguration(org.commonjava.indy.conf.IndyConfiguration) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) StoreType.hosted(org.commonjava.indy.model.core.StoreType.hosted) ArtifactStoreUpdateType(org.commonjava.indy.change.event.ArtifactStoreUpdateType) IndyDataException(org.commonjava.indy.data.IndyDataException) StoreValidator(org.commonjava.indy.data.StoreValidator) Locker(org.commonjava.cdi.util.weft.Locker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Group(org.commonjava.indy.model.core.Group) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Measure(org.commonjava.o11yphant.metrics.annotation.Measure) ExecutorConfig(org.commonjava.cdi.util.weft.ExecutorConfig) DELETE(org.commonjava.indy.db.common.StoreUpdateAction.DELETE) InternalFeatureConfig(org.commonjava.indy.conf.InternalFeatureConfig) ArtifactStoreValidateData(org.commonjava.indy.data.ArtifactStoreValidateData) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Collections.emptySet(java.util.Collections.emptySet) ValuePipe(org.commonjava.indy.util.ValuePipe) HostedRepository(org.commonjava.indy.model.core.HostedRepository) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) MDC(org.slf4j.MDC) Collections(java.util.Collections) StoreDataManager(org.commonjava.indy.data.StoreDataManager) Group(org.commonjava.indy.model.core.Group) Set(java.util.Set) HashSet(java.util.HashSet) Collections.emptySet(java.util.Collections.emptySet) StoreKey(org.commonjava.indy.model.core.StoreKey) HashSet(java.util.HashSet) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 58 with Measure

use of org.commonjava.o11yphant.metrics.annotation.Measure in project indy by Commonjava.

the class IspnNotFoundCache method clearMissing.

@Override
@Measure
public void clearMissing(final Location location) {
    nfcCache.execute((cache) -> {
        Set<String> paths = getMissing(location);
        paths.forEach(path -> cache.remove(getResourceKey(new ConcreteResource(location, path))));
        return null;
    });
}
Also used : ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) StoreKey.fromString(org.commonjava.indy.model.core.StoreKey.fromString) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 59 with Measure

use of org.commonjava.o11yphant.metrics.annotation.Measure in project indy by Commonjava.

the class IspnNotFoundCache method isMissing.

@Override
@Measure
public boolean isMissing(final ConcreteResource resource) {
    String key = getResourceKey(resource);
    NfcConcreteResourceWrapper obj = nfcCache.get(key);
    boolean timeout = (obj != null && obj.getTimeout() < System.currentTimeMillis());
    boolean missing = (obj != null && !timeout);
    if (timeout) {
        nfcCache.remove(key);
    }
    logger.trace("NFC check: {}, obj: {}, timeout: {}, missing: {}", resource, obj, timeout, missing);
    return missing;
}
Also used : StoreKey.fromString(org.commonjava.indy.model.core.StoreKey.fromString) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 60 with Measure

use of org.commonjava.o11yphant.metrics.annotation.Measure in project indy by Commonjava.

the class IspnNotFoundCache method getSize.

@Override
@Measure
public long getSize(StoreKey storeKey) {
    Query query = queryFactory.from(NfcConcreteResourceWrapper.class).select(Expression.count("path")).having("location").eq(storeKey.toString()).toBuilder().build();
    List<Object> result = query.list();
    Object[] count = (Object[]) result.get(0);
    return (Long) count[0];
}
Also used : Query(org.infinispan.query.dsl.Query) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Aggregations

Measure (org.commonjava.o11yphant.metrics.annotation.Measure)65 StoreKey (org.commonjava.indy.model.core.StoreKey)23 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)19 Transfer (org.commonjava.maven.galley.model.Transfer)19 Logger (org.slf4j.Logger)19 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)16 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)14 IndyDataException (org.commonjava.indy.data.IndyDataException)13 ArrayList (java.util.ArrayList)12 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)11 HashSet (java.util.HashSet)9 Group (org.commonjava.indy.model.core.Group)8 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)8 BoundStatement (com.datastax.driver.core.BoundStatement)6 StoreDataManager (org.commonjava.indy.data.StoreDataManager)6 StoreKey.fromString (org.commonjava.indy.model.core.StoreKey.fromString)6 Query (org.infinispan.query.dsl.Query)6 IOException (java.io.IOException)5 Set (java.util.Set)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5