Skip to content
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

Rewrote the PropertiesProvider API to support initialization, close and better multithreading #339

Merged
merged 1 commit into from
Mar 29, 2022

Conversation

mathieucarbou
Copy link
Owner

@mathieucarbou mathieucarbou commented Mar 28, 2022

  • Correctly initialize and close the resources used in properties providers.
  • Better support multi-threading
  • Remove any contention created by synchronized

Ref: #335 | #327

CC: @philippn

@mathieucarbou mathieucarbou added is:bug Bugs to fix in:git MLP Git integration labels Mar 28, 2022
@mathieucarbou mathieucarbou added this to the 4.2 milestone Mar 28, 2022
@mathieucarbou mathieucarbou self-assigned this Mar 28, 2022
@mathieucarbou mathieucarbou force-pushed the closing-providers branch 2 times, most recently from 1d7e972 to ef7866d Compare March 28, 2022 12:52
@mathieucarbou mathieucarbou marked this pull request as draft March 28, 2022 13:23
@mathieucarbou
Copy link
Owner Author

Note: still need to be improved because it will slow down...

@mathieucarbou mathieucarbou changed the title Correctly close the repository Rewrote the PropertiesProvider API to support initialization, close and multithreading better Mar 28, 2022
@mathieucarbou mathieucarbou marked this pull request as ready for review March 28, 2022 15:14
@mathieucarbou
Copy link
Owner Author

@hazendaz @dbwiddis : ready for review!

* @deprecated Use instead {@link #adjustProperties(AbstractLicenseMojo, Map, Document)}
*/
@Deprecated
default Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Properties currentProperties, Document document) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kept for backward compat' just in case...

}

@Override
default void close() {}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addition

Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Properties currentProperties, Document document);
public interface PropertiesProvider extends Closeable {

default void init(AbstractLicenseMojo mojo, Map<String, String> currentProperties) {}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addition

Comment on lines 128 to 134
// One-time warning for shallow repo
if (mojo.warnIfShallow && !warnedIfShallow.get()) {
SVNInfo info = svnClientManager.getWCClient().doInfo(documentFile, SVNRevision.HEAD);
if (info.getDepth() != SVNDepth.INFINITY) {
if (warnedIfShallow.compareAndSet(false, true)) {
mojo.warn(
"Sparse svn repository detected. Year and author property values may not be accurate.");
Copy link
Owner Author

@mathieucarbou mathieucarbou Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbwiddis : FYI warnedIfShallow code updated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I first put this in I didn't realize the SVN provider didn't have the same set of properties. There are no authors to consider, and only the "last change" year is relevant (so a sparse repo may miss the latest commit of a file and default to current year). So this can probably just say "year" values like the adjusted git one above.

Comment on lines +58 to +69
private final AtomicBoolean warnedIfShallow = new AtomicBoolean();
private final Queue<SVNClientManager> clients = new ConcurrentLinkedQueue<>();
private final ThreadLocal<SimpleDateFormat> sdfTimestampThreadLocal = ThreadLocal.withInitial(
() -> new SimpleDateFormat("yyyyMMdd-HH:mm:ss"));
private final ThreadLocal<SVNClientManager> svnClientThreadLocal = ThreadLocal.withInitial(() -> {
SVNClientManager svnClientManager = svnCredentials == null ?
SVNClientManager.newInstance(new DefaultSVNOptions()) :
SVNClientManager.newInstance(new DefaultSVNOptions(), svnCredentials.getLogin(),
svnCredentials.getPassword());
clients.offer(svnClientManager);
return svnClientManager;
});
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously incorrect resource cleanup: thread-local SVN clients were not closed. Kee ing track of all created clients per thread to clean them at the end.

Comment on lines +594 to +600
Map<String, String> globalProperties = getDefaultProperties();

for (final Map.Entry<String, String> entry : mergeProperties(licenseSet, document).entrySet()) {
if (entry.getValue() != null) {
props.setProperty(entry.getKey(), entry.getValue());
} else {
props.remove(entry.getKey());
}
}
for (final PropertiesProvider provider : propertiesProviders) {
try {
final Map<String, String> providerProperties = provider.getAdditionalProperties(AbstractLicenseMojo.this, props, document);
if (getLog().isDebugEnabled()) {
getLog().debug("provider: " + provider.getClass() + " brought new properties\n" + providerProperties);
}
for (Map.Entry<String, String> entry : providerProperties.entrySet()) {
if (entry.getValue() != null) {
props.setProperty(entry.getKey(), entry.getValue());
} else {
props.remove(entry.getKey());
}
}
} catch (Exception e) {
getLog().warn("failure occurred while calling " + provider.getClass(), e);
}
// we override by properties in the licenseSet
if (licenseSet.properties != null) {
for (Map.Entry<String, String> entry : licenseSet.properties.entrySet()) {
if (!System.getProperties().contains(entry.getKey())) {
globalProperties.put(entry.getKey(), entry.getValue());
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid executing this code for each file.


for (final PropertiesProvider provider : ServiceLoader.load(PropertiesProvider.class,
Thread.currentThread().getContextClassLoader())) {
provider.init(this, globalProperties);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init called here

@@ -685,6 +720,7 @@ public Properties load(final Document document) {

} finally {
executorService.shutdownNow();
propertiesProviders.forEach(PropertiesProvider::close);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close called here

@mathieucarbou mathieucarbou changed the title Rewrote the PropertiesProvider API to support initialization, close and multithreading better Rewrote the PropertiesProvider API to support initialization, close and better multithreading Mar 28, 2022
Copy link
Contributor

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One suggested text change.

Comment on lines 128 to 134
// One-time warning for shallow repo
if (mojo.warnIfShallow && !warnedIfShallow.get()) {
SVNInfo info = svnClientManager.getWCClient().doInfo(documentFile, SVNRevision.HEAD);
if (info.getDepth() != SVNDepth.INFINITY) {
if (warnedIfShallow.compareAndSet(false, true)) {
mojo.warn(
"Sparse svn repository detected. Year and author property values may not be accurate.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I first put this in I didn't realize the SVN provider didn't have the same set of properties. There are no authors to consider, and only the "last change" year is relevant (so a sparse repo may miss the latest commit of a file and default to current year). So this can probably just say "year" values like the adjusted git one above.

@mathieucarbou mathieucarbou merged commit aec479a into master Mar 29, 2022
@mathieucarbou mathieucarbou deleted the closing-providers branch April 4, 2022 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in:git MLP Git integration is:bug Bugs to fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants