Skip to content

Revamp/855 make pomCache configurable through properties #907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/build-sbm-support-rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ name: Build SBM Revamp
on:
push:
branches:
- "revamp/**"
- "version/revamp"
branches-ignore:
- "main"
- "**"
paths:
- "sbm-support-rewrite/**"
jobs:
Expand Down
38 changes: 38 additions & 0 deletions sbm-support-rewrite/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,44 @@ The `OpenRewriteProjectParser` aims to provide the exact same parsing capabiliti

This allows parsing a given project to it's OpenRewrite AST representation and then provide the AST to OR recipes and visitors to run OpenRewrite recipes without requiring a build plugin.

## Configuration

### Maven Pom Cache
OpenRewrite downloads Maven Pom files to resolve dependencies.
The pom files get cached and the cache depends on the system.

- 32-Bit systems use the `InMemoryPomCache`.
- 64-Bit systems use the `RocksdbMavenPomCache`.
####

#### Pom Cache Properties

|===
|Name |Description |Default Value

|`parser.isPomCacheEnabled`
|If the flag is set to false, only the default, in-memory cache is used.
|`false`

|`parser.pomCacheDirectory`
|Defines the cache dir for RocksdbMavenPomCache when `parser.isPomCacheEnabled` is `true`
|`~/.rewrite-cache`
|===

#### Custom Pom Cache
Users could provide a custom `MavenPomCache` implementation by overwriting the bean definition in `RewriteMavenPomCacheConfig.mavenPomCache()`.

[source,java]
....
@Bean
@Primary
public MavenPomCache mavenPomCache() {
return new CustomMavenPomCache();
}
....


## Running OpenRewrite recipes

### Scan the codebase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2021 - 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.PropertiesPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;

/**
* Add default values from 'sbm-support-rewrite.properties' to environment.
*/
@Component
public class ParserPropertiesPostProcessor implements EnvironmentPostProcessor {

private final PropertiesPropertySourceLoader loader = new PropertiesPropertySourceLoader();

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Resource path = new ClassPathResource("/META-INF/sbm-support-rewrite.properties");
PropertySource<?> propertySource = loadProperties(path);
environment.getPropertySources().addLast(propertySource);
}

private PropertySource<?> loadProperties(Resource path) {
Assert.isTrue(path.exists(), () -> "Resource " + path + " does not exist");
try {
return this.loader.load("custom-resource", path).get(0);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to load properties configuration from " + path, ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,54 @@
*/
package org.springframework.sbm;

import org.openrewrite.Recipe;
import org.openrewrite.RecipeRun;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.InMemoryLargeSourceSet;
import org.openrewrite.xml.tree.Xml;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.openrewrite.maven.cache.CompositeMavenPomCache;
import org.openrewrite.maven.cache.InMemoryMavenPomCache;
import org.openrewrite.maven.cache.MavenPomCache;
import org.openrewrite.maven.cache.RocksdbMavenPomCache;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.sbm.parsers.*;
import org.springframework.sbm.recipes.RewriteRecipeDiscovery;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.sbm.parsers.ParserProperties;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Path;
import java.util.List;


/**
* Module configuration.
*
* @author Fabian Krüger
*/
@Slf4j
@SpringBootApplication
public class RewriteParserConfig {

@Bean
@ConditionalOnMissingBean(MavenPomCache.class)
MavenPomCache mavenPomCache(ParserProperties parserProperties) {
MavenPomCache mavenPomCache = new InMemoryMavenPomCache();
if (parserProperties.isPomCacheEnabled()) {
if (!"64".equals(System.getProperty("sun.arch.data.model", "64"))) {
log.warn("parser.isPomCacheEnabled was set to true but RocksdbMavenPomCache is not supported on 32-bit JVM. falling back to InMemoryMavenPomCache");
} else {
try {
mavenPomCache = new CompositeMavenPomCache(
new InMemoryMavenPomCache(),
new RocksdbMavenPomCache(Path.of(parserProperties.getPomCacheDirectory()))
);
} catch (Exception e) {
log.warn("Unable to initialize RocksdbMavenPomCache, falling back to InMemoryMavenPomCache");
if (log.isDebugEnabled()) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
log.debug(exceptionAsString);
}
}
}
}
return mavenPomCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
@RequiredArgsConstructor
class BuildFileParser {

private final ParserSettings parserSettings;

/**
* Parse a list of Maven Pom files to a Map of {@code Path} and their parsed {@link Xml.Document}s.
* The {@link Xml.Document}s are marked with {@link org.openrewrite.maven.tree.MavenResolutionResult} and the provided provenance markers.
Expand Down Expand Up @@ -81,6 +79,7 @@ public Map<Path, Xml.Document> parseBuildFiles(
Assert.isTrue(resourcesWithoutProvenanceMarker.isEmpty(), "No provenance marker provided for these pom files %s".formatted(resourcesWithoutProvenanceMarker.stream().map(r -> ResourceUtil.getPath(r).toAbsolutePath()).toList()));

if(skipMavenParsing) {
log.info("Maven parsing skipped [parser.skipMavenParsing=true].");
return Map.of();
}

Expand All @@ -95,14 +94,6 @@ public Map<Path, Xml.Document> parseBuildFiles(
// 385 : 387
initializeMavenSettings(executionContext);

// 389 : 393
if (parserSettings.isPomCacheEnabled()) {
//The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
//If the flag is set to false, only the default, in-memory cache is used.
MavenPomCache pomCache = getPomCache();
MavenExecutionContextView.view(executionContext).setPomCache(pomCache);
}

// 395 : 398

mavenParserBuilder.activeProfiles(activeProfiles.toArray(new String[]{}));
Expand Down Expand Up @@ -168,39 +159,6 @@ private List<SourceFile> parsePoms(Path baseDir, List<Resource> pomFiles, MavenP
return mavenParserBuilder.build().parseInputs(pomFileInputs, baseDir, executionContext).toList();
}

/**
* {@link MavenMojoProjectParser##getPomCache()}
*/
private static MavenPomCache getPomCache() {
// if (pomCache == null) {
// if (isJvm64Bit()) {
// try {
// if (pomCacheDirectory == null) {
// //Default directory in the RocksdbMavenPomCache is ".rewrite-cache"
// pomCache = new CompositeMavenPomCache(
// new InMemoryMavenPomCache(),
// new RocksdbMavenPomCache(Paths.get(System.getProperty("user.home")))
// );
// } else {
// pomCache = new CompositeMavenPomCache(
// new InMemoryMavenPomCache(),
// new RocksdbMavenPomCache(Paths.get(pomCacheDirectory))
// );
// }
// } catch (Exception e) {
// logger.warn("Unable to initialize RocksdbMavenPomCache, falling back to InMemoryMavenPomCache");
// logger.debug(e);
// }
// } else {
// logger.warn("RocksdbMavenPomCache is not supported on 32-bit JVM. falling back to InMemoryMavenPomCache");
// }
// }
// if (pomCache == null) {
MavenPomCache pomCache = new InMemoryMavenPomCache();
// }
return pomCache;
}

private void initializeMavenSettings(ExecutionContext executionContext) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@
@RequiredArgsConstructor
class MavenMojoProjectParserFactory {

private final ParserSettings parserSettings;
private final ParserProperties parserProperties;

public MavenMojoProjectParser create(Path baseDir, List<MavenProject> mavenProjects, PlexusContainer plexusContainer, MavenSession session) {
return buildMavenMojoProjectParser(
baseDir,
mavenProjects,
parserSettings.isPomCacheEnabled(),
parserSettings.getPomCacheDirectory(),
parserSettings.isSkipMavenParsing(),
parserSettings.getIgnoredPathPatterns(),
parserSettings.getPlainTextMasks(),
parserSettings.getSizeThresholdMb(),
parserSettings.isRunPerSubmodule(),
parserProperties.isPomCacheEnabled(),
parserProperties.getPomCacheDirectory(),
parserProperties.isSkipMavenParsing(),
parserProperties.getIgnoredPathPatterns(),
parserProperties.getPlainTextMasks(),
parserProperties.getSizeThresholdMb(),
parserProperties.isRunPerSubmodule(),
plexusContainer,
session);
}
Expand Down Expand Up @@ -98,16 +98,16 @@ public MavenMojoProjectParser create(Path baseDir, RuntimeInformation runtimeInf
return new MavenMojoProjectParser(
new Slf4jToMavenLoggerAdapter(log),
baseDir,
parserSettings.isPomCacheEnabled(),
parserSettings.getPomCacheDirectory(),
parserProperties.isPomCacheEnabled(),
parserProperties.getPomCacheDirectory(),
runtimeInformation,
parserSettings.isSkipMavenParsing(),
parserSettings.getIgnoredPathPatterns(),
parserSettings.getPlainTextMasks(),
parserSettings.getSizeThresholdMb(),
parserProperties.isSkipMavenParsing(),
parserProperties.getIgnoredPathPatterns(),
parserProperties.getPlainTextMasks(),
parserProperties.getSizeThresholdMb(),
null,
settingsDecrypter,
parserSettings.isRunPerSubmodule()
parserProperties.isRunPerSubmodule()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@
package org.springframework.sbm.parsers;

import lombok.*;
import org.apache.commons.logging.Log;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.stereotype.Component;

import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* ConfigurationProperties with prefix {@code parser}.
* Defaults coming from {@code META-INF/sbm-support-rewrite.properties}
*
* @author Fabian Krüger
*/
Expand All @@ -37,13 +35,11 @@
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "parser")
public class ParserSettings {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public class ParserProperties {

private String loggerClass;
/**
* Defines if pom files get cached or if they are always downloaded.
*/
private boolean pomCacheEnabled = true;
private boolean pomCacheEnabled = false;
private String pomCacheDirectory;
private boolean skipMavenParsing = false;
private Set<String> plainTextMasks = new HashSet<>();
Expand All @@ -53,13 +49,6 @@ public class ParserSettings {
private List<String> activeProfiles = List.of("default");
private Set<String> ignoredPathPatterns = new HashSet<>();

/**
* @return fully qualified classname of the logger to use.
*/
public String getLoggerClass() {
return loggerClass;
}

public void setLoggerClass(String loggerClass) {
this.loggerClass = loggerClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.nio.file.PathMatcher;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

/**
Expand All @@ -40,7 +39,7 @@
@RequiredArgsConstructor
class ProjectScanner {
private final ResourceLoader resourceLoader;
private final ParserSettings parserSettings;
private final ParserProperties parserProperties;

public List<Resource> scan(Path baseDir) {
if(!baseDir.isAbsolute()) {
Expand All @@ -55,7 +54,7 @@ public List<Resource> scan(Path baseDir) {
try {
Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern);

List<PathMatcher> pathMatchers = parserSettings.getIgnoredPathPatterns().stream()
List<PathMatcher> pathMatchers = parserProperties.getIgnoredPathPatterns().stream()
.map(p -> p.startsWith("glob:") ? p : "glob:" + p)
.map(baseDir.getFileSystem()::getPathMatcher)
.toList();
Expand Down
Loading