use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class Workflow method run.
@Override
public void run(Path workdir, @Nullable String sourceRef) throws RepoException, IOException, ValidationException {
validateFlags();
try (ProfilerTask ignore = profiler().start("run/" + name)) {
console.progress("Getting last revision: " + "Resolving " + ((sourceRef == null) ? "origin reference" : sourceRef));
O resolvedRef = generalOptions.repoTask("origin.resolve_source_ref", () -> origin.resolve(sourceRef));
logger.log(Level.INFO, String.format("Running Copybara for workflow '%s' and ref '%s': %s", name, resolvedRef.asString(), this.toString()));
logger.log(Level.INFO, String.format("Using working directory : %s", workdir));
WorkflowRunHelper<O, D> helper = newRunHelper(workdir, resolvedRef, sourceRef);
try (ProfilerTask ignored = profiler().start(mode.toString().toLowerCase())) {
mode.run(helper);
}
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class WorkflowRunHelper method migrate.
/**
* Performs a full migration, including checking out files from the origin, deleting excluded
* files, transforming the code, and writing to the destination. This writes to the destination
* exactly once.
*
* @param rev revision to the version which will be written to the destination
* @param lastRev last revision that was migrated
* @param processConsole console to use to print progress messages
* @param metadata metadata of the change to be migrated
* @param changes changes included in this migration
* @param destinationBaseline it not null, use this baseline in the destination
* @param changeIdentityRevision the revision to be used for computing the change identity
*/
ImmutableList<DestinationEffect> migrate(O rev, @Nullable O lastRev, Console processConsole, Metadata metadata, Changes changes, @Nullable String destinationBaseline, @Nullable O changeIdentityRevision) throws IOException, RepoException, ValidationException {
ImmutableList<DestinationEffect> effects = ImmutableList.of();
boolean callPerMigrationHook = true;
try {
eventMonitor().onChangeMigrationStarted(new ChangeMigrationStartedEvent());
effects = doMigrate(rev, lastRev, processConsole, metadata, changes, destinationBaseline, changeIdentityRevision);
return effects;
} catch (EmptyChangeException empty) {
effects = ImmutableList.of(new DestinationEffect(Type.NOOP, empty.getMessage(), changes.getCurrent(), /*destinationRef=*/
null, ImmutableList.of()));
throw empty;
} catch (ValidationException | IOException | RepoException | RuntimeException e) {
effects = ImmutableList.of(new DestinationEffect(Type.ERROR, "Errors happened during the migration", changes.getCurrent(), /*destinationRef=*/
null, ImmutableList.of(e.getMessage() != null ? e.getMessage() : e.toString())));
callPerMigrationHook = e instanceof ValidationException;
throw e;
} finally {
eventMonitor().onChangeMigrationFinished(new ChangeMigrationFinishedEvent(effects));
if (callPerMigrationHook) {
FinishHookContext finishHookContext = new FinishHookContext(getOriginReader().getFeedbackEndPoint(), getDestinationWriter().getFeedbackEndPoint(), effects, resolvedRef, new SkylarkConsole(getConsole()));
try (ProfilerTask ignored = profiler().start("finish_hooks")) {
for (Action action : workflow.getAfterMigrationActions()) {
try (ProfilerTask ignored2 = profiler().start(action.getName())) {
logger.log(Level.INFO, "Running after migration hook: " + action.getName());
action.run(finishHookContext);
}
}
}
}
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class ProfilerTest method reeentrantTest.
@Test
public void reeentrantTest() {
try (ProfilerTask p1 = profiler.start("task1")) {
try (ProfilerTask p2 = profiler.start("task2")) {
profiler.simpleTask("task3", ticker.read(), ticker.read());
profiler.simpleTask("task4", ticker.read(), ticker.read());
}
try (ProfilerTask p2 = profiler.start("task5")) {
profiler.simpleTask("task6", ticker.read(), ticker.read());
profiler.simpleTask("task7", ticker.read(), ticker.read());
}
}
profiler.stop();
assertThat(recordingCallback.events).isEqualTo(ImmutableList.of(new TaskWithType(EventType.START, new Task("//copybara", 0, -1)), new TaskWithType(EventType.START, new Task("//copybara/task1", 1, -1)), new TaskWithType(EventType.START, new Task("//copybara/task1/task2", 2, -1)), new TaskWithType(EventType.START, new Task("//copybara/task1/task2/task3", 3, -1)), new TaskWithType(EventType.END, new Task("//copybara/task1/task2/task3", 3, 4)), new TaskWithType(EventType.START, new Task("//copybara/task1/task2/task4", 5, -1)), new TaskWithType(EventType.END, new Task("//copybara/task1/task2/task4", 5, 6)), new TaskWithType(EventType.END, new Task("//copybara/task1/task2", 2, 7)), new TaskWithType(EventType.START, new Task("//copybara/task1/task5", 8, -1)), new TaskWithType(EventType.START, new Task("//copybara/task1/task5/task6", 9, -1)), new TaskWithType(EventType.END, new Task("//copybara/task1/task5/task6", 9, 10)), new TaskWithType(EventType.START, new Task("//copybara/task1/task5/task7", 11, -1)), new TaskWithType(EventType.END, new Task("//copybara/task1/task5/task7", 11, 12)), new TaskWithType(EventType.END, new Task("//copybara/task1/task5", 8, 13)), new TaskWithType(EventType.END, new Task("//copybara/task1", 1, 14)), new TaskWithType(EventType.END, new Task("//copybara", 0, 15))));
// We don't record events once stopped.
recordingCallback.events.clear();
try (ProfilerTask ignore = profiler.start("bar")) {
profiler.simpleTask("foo", 10, 20);
}
assertThat(recordingCallback.events).isEmpty();
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class Workflow method run.
@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, IOException, ValidationException {
if (sourceRefs.size() > 1) {
throw new CommandLineException(String.format("Workflow does not support multiple source_ref arguments yet: %s", ImmutableList.copyOf(sourceRefs)));
}
@Nullable String sourceRef = sourceRefs.size() == 1 ? sourceRefs.get(0) : null;
validateFlags();
try (ProfilerTask ignore = profiler().start("run/" + name)) {
console.progress("Getting last revision: " + "Resolving " + ((sourceRef == null) ? "origin reference" : sourceRef));
O resolvedRef = generalOptions.repoTask("origin.resolve_source_ref", () -> origin.resolve(sourceRef));
logger.log(Level.INFO, String.format("Running Copybara for workflow '%s' and ref '%s': %s", name, resolvedRef.asString(), this.toString()));
logger.log(Level.INFO, String.format("Using working directory : %s", workdir));
ImmutableList.Builder<DestinationEffect> allEffects = ImmutableList.builder();
WorkflowRunHelper<O, D> helper = newRunHelper(workdir, resolvedRef, sourceRef, event -> {
allEffects.addAll(event.getDestinationEffects());
eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(event));
});
try (ProfilerTask ignored = profiler().start(mode.toString().toLowerCase())) {
mode.run(helper);
} finally {
if (!getGeneralOptions().dryRunMode) {
try (ProfilerTask ignored = profiler().start("after_all_migration")) {
ImmutableList<DestinationEffect> effects = allEffects.build();
ImmutableList<DestinationEffect> resultEffects = runHooks(effects, getAfterAllMigrationActions(), // Only do this once for all the actions
memoized(c -> helper.getOriginReader().getFeedbackEndPoint(c)), // Only do this once for all the actions
memoized(c -> helper.getDestinationWriter().getFeedbackEndPoint(c)), resolvedRef);
if (effects.size() != resultEffects.size()) {
console.warn("Effects where created in after_all_migrations, but they are ignored.");
}
}
}
}
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class Workflow method runHooks.
ImmutableList<DestinationEffect> runHooks(ImmutableList<DestinationEffect> effects, ImmutableList<Action> actions, LazyResourceLoader<Endpoint> originEndpoint, LazyResourceLoader<Endpoint> destinationEndpoint, Revision resolvedRef) throws ValidationException, RepoException {
SkylarkConsole console = new SkylarkConsole(getConsole());
List<DestinationEffect> hookDestinationEffects = new ArrayList<>();
for (Action action : actions) {
try (ProfilerTask ignored2 = profiler().start(action.getName())) {
logger.log(Level.INFO, "Running after migration hook: " + action.getName());
FinishHookContext context = new FinishHookContext(action, originEndpoint, destinationEndpoint, ImmutableList.copyOf(effects), generalOptions.labels, resolvedRef, console);
action.run(context);
hookDestinationEffects.addAll(context.getNewDestinationEffects());
}
}
return ImmutableList.<DestinationEffect>builder().addAll(effects).addAll(hookDestinationEffects).build();
}
Aggregations