Search in sources :

Example 11 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.

the class ProfilerTest method setUp.

@Before
public void setUp() throws Exception {
    ticker = new FakeTicker().setAutoIncrementStep(1, TimeUnit.NANOSECONDS);
    profiler = new Profiler(ticker);
    recordingCallback = new RecordingListener();
    // We don't record anything  before start
    try (ProfilerTask ignore = profiler.start("bar")) {
        profiler.simpleTask("foo", 10, 20);
    }
    assertThat(recordingCallback.events).isEmpty();
    profiler.init(ImmutableList.of(recordingCallback));
}
Also used : ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) RecordingListener(com.google.copybara.testing.profiler.RecordingListener) FakeTicker(com.google.common.testing.FakeTicker) Before(org.junit.Before)

Example 12 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.

the class ProfilerTest method testNoCallback.

@Test
public void testNoCallback() {
    Profiler profiler = new Profiler(ticker);
    profiler.init(ImmutableList.of());
    try (ProfilerTask ignore = profiler.start("bar")) {
        profiler.simpleTask("foo", 10, 20);
    }
    profiler.stop();
    long time = ticker.advance(42).read();
    // Nothing was created in the stack. We cannot get the queue without forcing initialization
    // so we change the ticker and see that the value is a detached with start=42. IOW: Created
    // when we do tasQueue.get() here:
    assertThat(profiler.taskQueue.get()).containsExactly(new Task("//detached_thread", time + 1, -1));
}
Also used : ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Test(org.junit.Test)

Example 13 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.

the class GithubPROrigin method getRevisionForPR.

private GitRevision getRevisionForPR(String project, int prNumber) throws RepoException, ValidationException {
    if (!requiredLabels.isEmpty()) {
        int retryCount = 0;
        Set<String> requiredButNotPresent;
        do {
            Issue issue;
            try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_issue")) {
                issue = githubOptions.getApi(project).getIssue(project, prNumber);
            }
            requiredButNotPresent = Sets.newHashSet(requiredLabels);
            requiredButNotPresent.removeAll(Collections2.transform(issue.getLabels(), Label::getName));
            // If we got all the labels we want or none of the ones we didn't get are retryable, return.
            if (requiredButNotPresent.isEmpty() || Collections.disjoint(requiredButNotPresent, retryableLabels)) {
                break;
            }
            Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
            retryCount++;
        } while (retryCount < RETRY_COUNT);
        if (!requiredButNotPresent.isEmpty()) {
            throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because it is missing the following" + " labels: %s", project, prNumber, requiredButNotPresent));
        }
    }
    PullRequest prData;
    try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_pr")) {
        prData = githubOptions.getApi(project).getPullRequest(project, prNumber);
    }
    if (requiredState == StateFilter.OPEN && !prData.isOpen()) {
        throw new EmptyChangeException(String.format("Pull Request %d is not open", prNumber));
    }
    if (requiredState == StateFilter.CLOSED && prData.isOpen()) {
        throw new EmptyChangeException(String.format("Pull Request %d is open", prNumber));
    }
    String stableRef = useMerge ? GithubUtil.asMergeRef(prNumber) : GithubUtil.asHeadRef(prNumber);
    // Fetch also the baseline branch. It is almost free and doing a roundtrip later would hurt
    // latency.
    console.progressFmt("Fetching Pull Request %d and branch '%s'", prNumber, prData.getBase().getRef());
    try {
        getRepository().fetch(asGithubUrl(project), /*prune=*/
        false, /*force=*/
        true, ImmutableList.of(stableRef + ":refs/PR_HEAD", // GitRepository need the whole reference name.
        "refs/heads/" + prData.getBase().getRef() + ":refs/PR_BASE_BRANCH"));
    } catch (CannotResolveRevisionException e) {
        if (useMerge) {
            throw new CannotResolveRevisionException(String.format("Cannot find a merge reference for Pull Request %d." + " It might have a conflict with head.", prNumber), e);
        } else {
            throw new CannotResolveRevisionException(String.format("Cannot find Pull Request %d.", prNumber), e);
        }
    }
    GitRevision gitRevision = getRepository().resolveReference("PR_HEAD");
    String integrateLabel = new GithubPRIntegrateLabel(getRepository(), generalOptions, project, prNumber, prData.getHead().getLabel(), gitRevision.getSha1()).toString();
    ImmutableMap.Builder<String, String> labels = ImmutableMap.builder();
    labels.put(GITHUB_PR_NUMBER_LABEL, Integer.toString(prNumber));
    labels.put(GitModule.DEFAULT_INTEGRATE_LABEL, integrateLabel);
    labels.put(GITHUB_BASE_BRANCH, prData.getBase().getRef());
    String mergeBase = getRepository().mergeBase("refs/PR_HEAD", "refs/PR_BASE_BRANCH");
    labels.put(GITHUB_BASE_BRANCH_SHA1, mergeBase);
    labels.put(GITHUB_PR_TITLE, prData.getTitle());
    labels.put(GITHUB_PR_BODY, prData.getBody());
    return new GitRevision(getRepository(), gitRevision.getSha1(), /*reviewReference=*/
    null, stableRef, labels.build(), url);
}
Also used : Issue(com.google.copybara.git.github.api.Issue) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) PullRequest(com.google.copybara.git.github.api.PullRequest) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Endpoint(com.google.copybara.Endpoint) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 14 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.

the class Main method cleanupOutputDir.

protected void cleanupOutputDir(GeneralOptions generalOptions) throws RepoException, IOException, ValidationException {
    try (ProfilerTask ignore = generalOptions.profiler().start("clean_outputdir")) {
        generalOptions.console().progress("Cleaning output directory");
        generalOptions.ioRepoTask("clean_outputdir", () -> {
            if (generalOptions.isNoCleanup()) {
                return null;
            }
            generalOptions.getDirFactory().cleanupTempDirs();
            return null;
        });
    }
}
Also used : ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask)

Example 15 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.

the class WorkflowRunHelper method doMigrate.

private ImmutableList<DestinationEffect> doMigrate(O rev, @Nullable O lastRev, Console processConsole, Metadata metadata, Changes changes, @Nullable String destinationBaseline, @Nullable O changeIdentityRevision) throws IOException, RepoException, ValidationException {
    Path checkoutDir = workdir.resolve("checkout");
    try (ProfilerTask ignored = profiler().start("prepare_workdir")) {
        processConsole.progress("Cleaning working directory");
        if (Files.exists(workdir)) {
            FileUtil.deleteRecursively(workdir);
        }
        Files.createDirectories(checkoutDir);
    }
    processConsole.progress("Checking out the change");
    try (ProfilerTask ignored = profiler().start("origin.checkout", profiler().taskType(workflow.getOrigin().getType()))) {
        originReader.checkout(rev, checkoutDir);
    }
    // Remove excluded origin files.
    PathMatcher originFiles = workflow.getOriginFiles().relativeTo(checkoutDir);
    processConsole.progress("Removing excluded origin files");
    int deleted = FileUtil.deleteFilesRecursively(checkoutDir, FileUtil.notPathMatcher(originFiles));
    if (deleted != 0) {
        processConsole.info(String.format("Removed %d files from workdir that do not match origin_files", deleted));
    }
    Path originCopy = null;
    if (workflow.getReverseTransformForCheck() != null) {
        try (ProfilerTask ignored = profiler().start("reverse_copy")) {
            workflow.getConsole().progress("Making a copy or the workdir for reverse checking");
            originCopy = Files.createDirectories(workdir.resolve("origin"));
            FileUtil.copyFilesRecursively(checkoutDir, originCopy, FAIL_OUTSIDE_SYMLINKS);
        }
    }
    TransformWork transformWork = new TransformWork(checkoutDir, metadata, changes, workflow.getConsole(), new MigrationInfo(getOriginLabelName(), writer), resolvedRef).withLastRev(lastRev).withCurrentRev(rev);
    try (ProfilerTask ignored = profiler().start("transforms")) {
        workflow.getTransformation().transform(transformWork);
    }
    if (workflow.getReverseTransformForCheck() != null) {
        workflow.getConsole().progress("Checking that the transformations can be reverted");
        Path reverse;
        try (ProfilerTask ignored = profiler().start("reverse_copy")) {
            reverse = Files.createDirectories(workdir.resolve("reverse"));
            FileUtil.copyFilesRecursively(checkoutDir, reverse, FAIL_OUTSIDE_SYMLINKS);
        }
        try (ProfilerTask ignored = profiler().start("reverse_transform")) {
            workflow.getReverseTransformForCheck().transform(new TransformWork(reverse, new Metadata(transformWork.getMessage(), transformWork.getAuthor()), changes, workflow.getConsole(), new MigrationInfo(/*originLabel=*/
            null, null), resolvedRef));
        }
        String diff;
        try {
            diff = new String(DiffUtil.diff(originCopy, reverse, workflow.isVerbose(), workflow.getGeneralOptions().getEnvironment()), StandardCharsets.UTF_8);
        } catch (InsideGitDirException e) {
            throw new ValidationException("Cannot use 'reversible_check = True' because Copybara temporary directory (%s) is" + " inside a git directory (%s). Please remove the git repository or use %s flag.", e.getPath(), e.getGitDirPath(), OUTPUT_ROOT_FLAG);
        }
        if (!diff.trim().isEmpty()) {
            workflow.getConsole().error("Non reversible transformations:\n" + DiffUtil.colorize(workflow.getConsole(), diff));
            throw new ValidationException("Workflow '%s' is not reversible", workflow.getName());
        }
    }
    workflow.getConsole().progress("Checking that destination_files covers all files in transform result");
    new ValidateDestinationFilesVisitor(workflow.getDestinationFiles(), checkoutDir).verifyFilesToWrite();
    // TODO(malcon): Pass metadata object instead
    TransformResult transformResult = new TransformResult(checkoutDir, rev, transformWork.getAuthor(), transformWork.getMessage(), resolvedRef, workflow.getName(), changes, rawSourceRef);
    if (destinationBaseline != null) {
        transformResult = transformResult.withBaseline(destinationBaseline);
    }
    transformResult = transformResult.withAskForConfirmation(workflow.isAskForConfirmation()).withIdentity(workflow.getMigrationIdentity(changeIdentityRevision, transformWork));
    ImmutableList<DestinationEffect> result;
    try (ProfilerTask ignored = profiler().start("destination.write", profiler().taskType(workflow.getDestination().getType()))) {
        result = writer.write(transformResult, processConsole);
    }
    Verify.verifyNotNull(result, "Destination returned a null result.");
    Verify.verify(!result.isEmpty(), "Destination " + writer + " returned an empty set of effects");
    return result;
}
Also used : Path(java.nio.file.Path) ValidationException(com.google.copybara.exception.ValidationException) InsideGitDirException(com.google.copybara.util.InsideGitDirException) PathMatcher(java.nio.file.PathMatcher) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask)

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