use of org.commonjava.indy.promote.model.PathsPromoteRequest in project indy by Commonjava.
the class EventAuditListener method transformFileGroupingEvent.
private void transformFileGroupingEvent(PromoteCompleteEvent event, FileGroupingEvent fileGroupingEvent) {
PathsPromoteCompleteEvent pathsPromoteCompleteEvent = (PathsPromoteCompleteEvent) event;
PathsPromoteResult promoteResult = pathsPromoteCompleteEvent.getPromoteResult();
String error = promoteResult.getError();
if (error != null) {
logger.trace("Error in promoteResult, skip audit.");
return;
}
Set<String> paths = promoteResult.getCompletedPaths();
if (paths.isEmpty()) {
logger.trace("No completedPaths, skip audit.");
return;
}
PathsPromoteRequest req = promoteResult.getRequest();
StoreKey source = req.getSource();
StoreKey target = req.getTarget();
// TODO How about generating the key if it does not exist.
TrackingKey trackingKey = getTrackingKey(source);
if (trackingKey == null) {
logger.trace("No tracking key found to: {}", source);
return;
}
Map<String, String> extra = new HashMap<>();
extra.put(EventConstants.SOURCE, source.toString());
extra.put(EventConstants.TARGET, target.toString());
fileGroupingEvent.setExtra(extra);
fileGroupingEvent.setTimestamp(new Date());
fileGroupingEvent.setSessionId(trackingKey.getId());
}
use of org.commonjava.indy.promote.model.PathsPromoteRequest in project indy by Commonjava.
the class IndyPromoteClientModule method promoteByPath.
public PathsPromoteResult promoteByPath(final StoreKey src, final StoreKey target, final boolean purgeSource, final String... paths) throws IndyClientException {
final PathsPromoteRequest req = new PathsPromoteRequest(src, target, new HashSet<String>(Arrays.asList(paths))).setPurgeSource(purgeSource);
final PathsPromoteResult result = http.postWithResponse(PATHS_PROMOTE_PATH, req, PathsPromoteResult.class, HttpStatus.SC_OK);
return result;
}
use of org.commonjava.indy.promote.model.PathsPromoteRequest in project indy by Commonjava.
the class PromotionManager method doPathsPromotion.
private PathsPromoteResult doPathsPromotion(PathsPromoteRequest request, boolean skipValidation, String baseUrl) throws IndyWorkflowException, PromotionValidationException {
final Set<String> paths = request.getPaths();
final StoreKey source = request.getSource();
logger.info("Do paths promotion, promotionId: {}, source: {}, target: {}, size: {}", request.getPromotionId(), source, request.getTarget(), paths != null ? paths.size() : -1);
List<Transfer> contents;
if (paths == null || paths.isEmpty()) {
// This is used to let galley ignore the NPMPathStorageCalculator handling,
// which will append package.json to a directory transfer and make listing not applicable.
ThreadContext context = ThreadContext.getContext(true);
context.put(RequestContextHelper.IS_RAW_VIEW, Boolean.TRUE);
contents = downloadManager.listRecursively(source, DownloadManager.ROOT_PATH);
context.put(RequestContextHelper.IS_RAW_VIEW, Boolean.FALSE);
} else {
contents = promotionHelper.getTransfersForPaths(source, paths);
}
final Set<String> pending = contents.stream().map(Transfer::getPath).collect(Collectors.toSet());
if (pending.isEmpty()) {
return new PathsPromoteResult(request, pending, emptySet(), emptySet(), null);
}
AtomicReference<Exception> ex = new AtomicReference<>();
StoreKeyPaths plk = new StoreKeyPaths(request.getTargetKey(), pending);
PathsPromoteResult promoteResult;
if (request.isFailWhenExists()) {
promoteResult = conflictManager.checkAnd(plk, pathsLockKey -> runValidationAndPathPromotions(skipValidation, request, baseUrl, ex, pending, contents), pathsLockKey -> {
String msg = String.format("Conflict detected, store: %s, paths: %s", pathsLockKey.getTarget(), pending);
logger.warn(msg);
return new PathsPromoteResult(request, pending, emptySet(), emptySet(), msg, null);
});
} else {
promoteResult = runValidationAndPathPromotions(skipValidation, request, baseUrl, ex, pending, contents);
}
if (ex.get() != null) {
throwProperException(ex.get());
}
// purge only if all paths were promoted successfully
if (promoteResult != null && promoteResult.succeeded() && request.isPurgeSource()) {
promotionHelper.purgeSourceQuietly(request.getSource(), pending);
}
return promoteResult;
}
use of org.commonjava.indy.promote.model.PathsPromoteRequest in project indy by Commonjava.
the class PromotionManagerTest method promoteAllByPath_RaceToPromote_FirstLocksTargetStore.
@Test
@Ignore("volatile, owing to galley fs locks")
public void promoteAllByPath_RaceToPromote_FirstLocksTargetStore() throws Exception {
Random rand = new Random();
final HostedRepository[] sources = { new HostedRepository(MAVEN_PKG_KEY, "source1"), new HostedRepository(MAVEN_PKG_KEY, "source2") };
final String[] paths = { "/path/path1", "/path/path2", "/path3", "/path/path/4" };
Stream.of(sources).forEach((source) -> {
try {
storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
Stream.of(paths).forEach((path) -> {
byte[] buf = new byte[1024 * 1024 * 2];
rand.nextBytes(buf);
try {
contentManager.store(source, path, new ByteArrayInputStream(buf), TransferOperation.UPLOAD, new EventMetadata());
} catch (IndyWorkflowException e) {
e.printStackTrace();
Assert.fail("failed to store generated file to: " + source + path);
}
});
} catch (IndyDataException e) {
e.printStackTrace();
Assert.fail("failed to store hosted repository: " + source);
}
});
final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
PathsPromoteResult[] results = new PathsPromoteResult[2];
CountDownLatch cdl = new CountDownLatch(2);
AtomicInteger counter = new AtomicInteger(0);
Stream.of(sources).forEach((source) -> {
int idx = counter.getAndIncrement();
executor.execute(() -> {
try {
results[idx] = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey(), paths), FAKE_BASE_URL);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Promotion from source: " + source + " failed.");
} finally {
cdl.countDown();
}
});
try {
Thread.sleep(25);
} catch (InterruptedException e) {
Assert.fail("Test interrupted");
}
});
assertThat("Promotions failed to finish.", cdl.await(30, TimeUnit.SECONDS), equalTo(true));
// first one should succeed.
PathsPromoteResult result = results[0];
assertThat(result.getRequest().getSource(), equalTo(sources[0].getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
Set<String> pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
Set<String> skipped = result.getSkippedPaths();
assertThat(skipped == null || skipped.isEmpty(), equalTo(true));
Set<String> completed = result.getCompletedPaths();
assertThat(completed, notNullValue());
assertThat(completed.size(), equalTo(paths.length));
assertThat(result.getError(), nullValue());
Stream.of(paths).forEach((path) -> {
HostedRepository src = sources[0];
Transfer sourceRef = downloadManager.getStorageReference(src, path);
Transfer targetRef = downloadManager.getStorageReference(target, path);
assertThat(targetRef.exists(), equalTo(true));
try (InputStream sourceIn = sourceRef.openInputStream();
InputStream targetIn = targetRef.openInputStream()) {
int s, t;
while ((s = sourceIn.read()) == targetIn.read()) {
if (s == -1) {
break;
}
}
if (s != -1) {
Assert.fail(path + " doesn't match between source: " + src + " and target: " + target);
}
} catch (IOException e) {
e.printStackTrace();
Assert.fail("Failed to compare contents of: " + path + " between source: " + src + " and target: " + target);
}
});
// second one should be completely skipped.
result = results[1];
assertThat(result.getRequest().getSource(), equalTo(sources[1].getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
skipped = result.getSkippedPaths();
assertThat(skipped, notNullValue());
assertThat(skipped.size(), equalTo(paths.length));
completed = result.getCompletedPaths();
assertThat(completed == null || completed.isEmpty(), equalTo(true));
assertThat(result.getError(), nullValue());
}
use of org.commonjava.indy.promote.model.PathsPromoteRequest in project indy by Commonjava.
the class PromotionManagerTest method promoteAllByPath_PurgeSource_PushTwoArtifactsToHostedRepo_VerifyCopiedToOtherHostedRepo.
@Test
public void promoteAllByPath_PurgeSource_PushTwoArtifactsToHostedRepo_VerifyCopiedToOtherHostedRepo() throws Exception {
prepareHostedReposAndTwoPaths();
final PathsPromoteResult result = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey()).setPurgeSource(true), FAKE_BASE_URL);
assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
final Set<String> pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
final Set<String> completed = result.getCompletedPaths();
assertThat(completed, notNullValue());
assertThat(completed.size(), equalTo(2));
assertThat(result.getError(), nullValue());
verifyExistence(true, true, false, false);
}
Aggregations