Search in sources :

Example 16 with ProfilerTask

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);
        }
    }
}
Also used : ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask)

Example 17 with ProfilerTask

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);
                    }
                }
            }
        }
    }
}
Also used : Action(com.google.copybara.feedback.Action) ValidationException(com.google.copybara.exception.ValidationException) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) FinishHookContext(com.google.copybara.feedback.FinishHookContext) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 18 with ProfilerTask

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();
}
Also used : ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Test(org.junit.Test)

Example 19 with ProfilerTask

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.");
                    }
                }
            }
        }
    }
}
Also used : Writer(com.google.copybara.Destination.Writer) ConfigFile(com.google.copybara.config.ConfigFile) MigrationReference(com.google.copybara.Info.MigrationReference) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) FinishHookContext(com.google.copybara.feedback.FinishHookContext) Callable(java.util.concurrent.Callable) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ChangesResponse(com.google.copybara.Origin.Reader.ChangesResponse) TokenType(com.google.copybara.templatetoken.Token.TokenType) ImmutableList(com.google.common.collect.ImmutableList) Path(java.nio.file.Path) Profiler(com.google.copybara.profiler.Profiler) Nullable(javax.annotation.Nullable) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) CHANGE_REQUEST_FROM_SOT(com.google.copybara.WorkflowMode.CHANGE_REQUEST_FROM_SOT) Reader(com.google.copybara.Origin.Reader) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Identity(com.google.copybara.util.Identity) ImmutableMap(com.google.common.collect.ImmutableMap) Migration(com.google.copybara.config.Migration) Token(com.google.copybara.templatetoken.Token) Action(com.google.copybara.action.Action) MoreObjects(com.google.common.base.MoreObjects) ValidationException(com.google.copybara.exception.ValidationException) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) Set(java.util.Set) LazyResourceLoader.memoized(com.google.copybara.LazyResourceLoader.memoized) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) EventMonitors(com.google.copybara.monitor.EventMonitor.EventMonitors) DestinationStatus(com.google.copybara.Destination.DestinationStatus) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) CHANGE_REQUEST(com.google.copybara.WorkflowMode.CHANGE_REQUEST) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) List(java.util.List) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) Paths(java.nio.file.Paths) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CommandLineException(com.google.copybara.exception.CommandLineException) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableList(com.google.common.collect.ImmutableList) CommandLineException(com.google.copybara.exception.CommandLineException) Nullable(javax.annotation.Nullable)

Example 20 with ProfilerTask

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();
}
Also used : SkylarkConsole(com.google.copybara.transform.SkylarkConsole) Action(com.google.copybara.action.Action) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ArrayList(java.util.ArrayList) FinishHookContext(com.google.copybara.feedback.FinishHookContext)

Aggregations

ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)29 ValidationException (com.google.copybara.exception.ValidationException)10 ImmutableList (com.google.common.collect.ImmutableList)8 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)8 RepoException (com.google.copybara.exception.RepoException)7 Test (org.junit.Test)6 Change (com.google.copybara.Change)5 Endpoint (com.google.copybara.Endpoint)5 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)5 SkylarkConsole (com.google.copybara.transform.SkylarkConsole)5 IOException (java.io.IOException)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Preconditions (com.google.common.base.Preconditions)4 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)4 Action (com.google.copybara.action.Action)4 ChangeMigrationFinishedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent)4 Path (java.nio.file.Path)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)3 ImmutableListMultimap.toImmutableListMultimap (com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap)3