Skip to content

Fix performance issues when caching maven repository #204

Closed

Description

@jactor-rises

There are huge performance issues with the caching mechanism of maven repositories

In the middle of the day, when building a maven artefact, to fetch the cache when my actions run can take from 2-3 minutes (!)

Then my maven build is done in 20-30 seconds...

After my build, then It takes approx one and a half minute before the cache is saved...

When the performance of the caching of the repository is so bad, it is better to run without caching (!). This is a serious setback in performance when developing and it is difficult to deliver code to our production environments in a quick manner.

I am sorry not to provide any examples, but my repositories are private belonging to https://github.com/navikt. A typical cache configuration looks like this:

      - uses: actions/[email protected]
        with:
          path: ~/.m2
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-

Activity

  1. changed the title [-]Performance issues when caching maven repository[/-] [+]Fix performance issues when caching maven repository[/+] on Mar 4, 2020
  2. dhadka commented on Mar 4, 2020

    @dhadka
    Contributor

    Hi @jactor-rises, thanks for reporting this. Looking at our server-side telemetry for your repos, I'm not seeing any indication that calls to the cache server are slow. 95% of calls are completing in < 0.2 seconds. It's likely either (1) slowness downloading the cache content from storage, or (2) slowness processing the cache action on the runner.

    There are a couple of things you can try:

    1. Enable debug logging and see if you can determine where the time is spent. You can also post a sample log here, or e-mail the log to dhadka (at) github (dot) com, and we can take a look.

    2. There is a known issue where the hashFiles function can take a long time if it's scanning a bunch of files. See Post actions/cache@v1 takes 4+ hours to complete #203, for example. Hopefully getting the debug logs will help determine if that's the cause.

    3. Check the size of the cache. If the cache is large, the time spent downloading and extracting the contents can add up. The debug logs should show the file size + the time to untar. If the issue is the time spent extracting the file, there is work underway to help improve that (Use zstd instead of gzip #184).

    4. Is the slowness you see occurring in all runs, or does it only occur periodically?

    5. Make sure you're using the latest version of the cache action. Using uses: actions/cache@v1 will always point to the latest version. I'm seeing a (very) small number of calls using an older (slower) version.

  3. jactor-rises commented on Mar 4, 2020

    @jactor-rises
    Author

    95% of calls are completing in < 0.2 seconds.

    if you can see our repos, then check all repos where the name starts with bidrag-
    I will debug tomorrow and check out #203 (even though this issue is talking about "post action"-step)

  4. jactor-rises commented on Mar 6, 2020

    @jactor-rises
    Author

    Some prints from a recent workflow:

      Set up job 2s
      Run actions/checkout@v2 1s
      Run actions/[email protected] 4m 20s
      Run mvn release:update-versions 8s
      Run mvn help:evaluate -Dexpression=project.version -q -DforceStdout > .new-version 2s
      Run navikt/bidrag-actions/[email protected] 3s
      Post actions/[email protected] 0s
      Post actions/checkout@v2 1s
      Complete job
    

    from the log archive for the action

    2020-03-06T10:55:08.6870075Z ##[group]Run actions/[email protected]
    2020-03-06T10:55:08.6870372Z with:
    2020-03-06T10:55:08.6870573Z   path: ~/.m2
    2020-03-06T10:55:08.6870808Z   key: Linux-maven-562b36f27489e4171733c926ac517b7d65434b447915d461c1a1752faa573e6b
    2020-03-06T10:55:08.6871034Z   restore-keys: Linux-maven-
    
    2020-03-06T10:55:08.6871225Z env:
    2020-03-06T10:55:08.6871441Z   IMAGE: docker.pkg.github.com/navikt/bidrag-dokument/bidrag-dokument:5c4c8d56322f0833e21fdb6e9c4244863acf4e86
    2020-03-06T10:55:08.6871706Z ##[endgroup]
    2020-03-06T10:59:12.8177569Z Cache Size: ~1299 MB (1362588221 B)
    2020-03-06T10:59:12.8218641Z [command]/bin/tar -xz -f /home/runner/work/_temp/154a3855-a675-465c-9025-66fd10bffc6a/cache.tgz -C /home/runner/.m2
    2020-03-06T10:59:28.4565146Z Cache restored from key: Linux-maven-2703b3ee614506d371e8a090bab0c055c4c0f20b60d2647c4b645b8f0b2f04c1
    

    Is this due to the cache size? The performance are really unstable. Sometimes it takes about 30 sec... How can I reduce my cache?

  5. jactor-rises commented on Mar 10, 2020

    @jactor-rises
    Author

    my delay is caused by the cache size:

    Cache Size: ~1299 MB (1362588221 B)

    How to I remove stuff from the cache which is no longer needed? I constantly update versions, so the cache grows bigger and bigger...

  6. dhadka commented on Mar 10, 2020

    @dhadka
    Contributor

    I constantly update versions, so the cache grows bigger and bigger

    Interesting, I'm guessing Maven doesn't have a mechanism to clean up unused dependencies? I would recommend either:

    1. Finding a way to clean up unused dependencies in ~/.m2/repositories.
    2. Remove the restore-keys. Essentially, when you change a version, restore-keys lets you download an older copy of the cache, Maven updates with the new version(s), and a new cache is created with the old+new version. This may cause the cache to slow down when you change versions, since it needs to re-download all dependencies, but might be worth it to reduce the cache size.
  7. jactor-rises commented on Mar 12, 2020

    @jactor-rises
    Author

    testing if the following plugin will reduce the cache size:

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>${build-helper-maven.version}</version>
            <executions>
              <execution>
                <id>remove-old-artifacts</id>
                <phase>package</phase>
                <goals>
                  <goal>remove-project-artifact</goal>
                </goals>
                <configuration>
                  <removeAll>true</removeAll>
                </configuration>
              </execution>
            </executions>
          </plugin>
    
  8. jactor-rises commented on Mar 12, 2020

    @jactor-rises
    Author

    great success!!!

    Cache Size: ~135 MB (141814240 B)

  9. Marcono1234 commented on Jun 1, 2020

    @Marcono1234
    Contributor

    It appears remove-project-artifact removes artifacts of the project which is currently built, so I assume your GitHub workflow builds with mvn install (or mvn deploy).
    Out of curiosity, if that is indeed the case, is this really needed in your use-case, or would mvn verify already suffice?

  10. jactor-rises commented on Jun 2, 2020

    @jactor-rises
    Author

    the build-helper.-maven-plugin removes artefacts no longer used (older version than these used when building)

    from apache.org:

    verify - run any checks on results of integration tests to ensure quality criteria are met

    the purpose of man verify is to "verify" that your dependencies is doing what they are supposed to...

  11. Marcono1234 commented on Jun 2, 2020

    @Marcono1234
    Contributor

    If I understood remove-project-artifact correctly, it only removes older artifacts of the project which is currently built, see also the source code of it.

    So if using that plugin fixed your performance issue, I assume your workflow is running either mvn install or mvn deploy (which implies mvn install) and therefore keeps installing new versions of the artifacts to the local Maven repository.
    Is that not the case?

  12. jactor-rises commented on Jun 3, 2020

    @jactor-rises
    Author

    yes it only discards old versions no longer stated in the pom. it only cashes the dependencies used in your pom so that the cache do not grow more than what is actually needed

  13. Marcono1234 commented on Jun 4, 2020

    @Marcono1234
    Contributor

    it only cashes the dependencies used in your pom so that the cache do not grow more than what is actually needed

    I am rather certain that this is not the case. It only removes older versions of the currently built project. This would match what the documentation says, how the code of the plugin looks and what it did when I ran it locally (it did not remove older versions of dependencies). You should be able to verify this by looking at the workflow log (the plugin describes there which packages it deleted).

    Sorry for driving this issue off-topic, my intention was to point out that the Maven command (possibly mvn install?) used here might not be ideal and that using that plugin is not necessarily required. However, that turned into this rather long discussion.

  14. jactor-rises commented on Jun 5, 2020

    @jactor-rises
    Author

    ...hmm... I have misread the documentation in that case... but it works for my purpose... it reduced the .m2-cached files from over 900 mb to amount 100 mb...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions