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;
}
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());
}
}
}
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;
});
}
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;
}
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];
}
Aggregations