Skip to content

Override default Distribution Download URL with customTestDistributionUrl for OpenSearch, which is passed as a parameter from a plugin.#1737

Closed
Rishikesh1159 wants to merge 9 commits intoopensearch-project:mainfrom
Rishikesh1159:main
Closed

Override default Distribution Download URL with customTestDistributionUrl for OpenSearch, which is passed as a parameter from a plugin.#1737
Rishikesh1159 wants to merge 9 commits intoopensearch-project:mainfrom
Rishikesh1159:main

Conversation

@Rishikesh1159
Copy link
Copy Markdown
Member

Override default Distribution Download URL with custom URL

Signed-off-by: Rishikesh1159 rishireddy1159@gmail.com

Description

It Overrides Distribution Download URL with customTestDistributionUrl which is passed as a parameter from plugin. Along with changes made in this PR, plugins should make subsequent change to allow user to pass custom url from CLI.

Issues Resolved

#1240

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

…nUrl for OpenSearch, which is passed as a parameter from a plugin.

Signed-off-by: Rishikesh1159 <rishireddy1159@gmail.com>
@Rishikesh1159 Rishikesh1159 requested a review from a team as a code owner December 15, 2021 18:53
@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@Rishikesh1159
Copy link
Copy Markdown
Member Author

start gradle check

Copy link
Copy Markdown
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

I expect the behavior of this feature to be "if I set the distribution URL, that's what's used and nothing else". So this is close! Let's document this as well as part of this PR.

private NamedDomainObjectContainer<OpenSearchDistribution> distributionsContainer;
private NamedDomainObjectContainer<DistributionResolution> distributionsResolutionStrategiesContainer;

private static String customUrl;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Name consistently, I would just do distributionUrl and setDistributionUrl since it's downloading ... a distribution.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, I will rename them accordingly

if (customUrl != null && !customUrl.isEmpty()) {
addIvyRepo(project, DOWNLOAD_REPO_NAME, customUrl, FAKE_IVY_GROUP, "");
addIvyRepo(project, SNAPSHOT_REPO_NAME, customUrl, FAKE_SNAPSHOT_IVY_GROUP, "");
customUrl = null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why does this URL get reset?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

When I was testing in local, if I don't reset the customUrl to null it is getting cached by gradle and when someother plugin/method which don't want to use customUrl calls this method setupDownloadServiceRepo(), the customurl value is still being set to old value and condition in if block will be true and executed. So, to avoid this I had to reset the customUrl value. I observed when a plugin individually is tested against Opensearch it executes this method setupDownloadServiceRepo() only once.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You have added a static property, so when you set it, it gets set globally, and forever. You need to figure out a way of how to set it only for the scope/instance that you are interested in instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes I thought about it, but there is certain way the parameter is being passed to DistributionDownloadPlugin.java which makes to use static variable.

--> First when plugin in ran against Opensearch it calls "TestClustersPlugin.java", here a call to DistributionDownoadPlugin.java is happening only once with project as parameter.

--> But Project values are still unset when a call to distributionDownloadPlugin.java is happening. So, I currently don't have custom URL passed by user from plugin. This file uses afterEvaluate() method which is listener interface and make some methods of file execute after project is evaluated.

--> Custom URL is passed to OpenSearchCluster.java using a setter method. This file is called by TestClustersPlugin.java after calling DistributionDownloadPlugin.java. So by the time I have custom URL from plugin, call to DistributionDownloadPlugin.java is already done.

--> If I try to create another instance of DistributionDownloadPlugin.java after getting custom URL from plugin with same project as parameter, it throws an error that "this project is already present".

--> So, the only option I had to send custom URL to DistributionDownloadPlugin.java was to use a static setter and call it by it's class name.

--> I have tried different ways to avoid setting a static variable, but they were failing. I will try asking other developers for suggestions to avoid it.

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

✅   Gradle Check success 816a593
Log 1511

Reports 1511

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

✅   Gradle Check success 816a593
Log 1512

Reports 1512

Signed-off-by: Rishikesh1159 <rishireddy1159@gmail.com>
@dblock
Copy link
Copy Markdown
Member

dblock commented Dec 15, 2021

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

✅   Gradle Check success 2f68702
Log 1514

Reports 1514

Copy link
Copy Markdown
Contributor

@nknize nknize left a comment

Choose a reason for hiding this comment

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

/cc @saratvemulapalli can you review? Make sure this is the approach that was discussed on the issue.

Let's also make sure this isn't opening something nefarious by exposing unfriendly URLs to the build process. Before approval be sure to have good documentation for this (e.g., javadocs) public methods that enable changing the download URL are sneaky so this isn't clear whether this is a benign change.

Copy link
Copy Markdown
Member

@saratvemulapalli saratvemulapalli left a comment

Choose a reason for hiding this comment

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

Dropped a couple of comments.
+1 to unit tests, documentation.

Comment on lines +149 to +153
// This method is called by plugins to override default Url
public static void setDistributionUrl(String distributionUrl) {
DistributionDownloadPlugin.setDistributionUrl(distributionUrl);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. Instead of exposing a public static method, let the interface define this method TestClusterConfiguration, OpenSearchCluster.java and OpenSearchNode.java can override it.

  2. And since its really a custom url, I would rename it to setCustomDistributionUrl

  3. Instead of directly exposing OpenSearchCluster to DistributionDownloadPlugin, pass this information to OpenSearchNode.java which should set the right distribution. OpenSearchNode.java is designed to setup up the node for tests.

Suggested change
// This method is called by plugins to override default Url
public static void setDistributionUrl(String distributionUrl) {
DistributionDownloadPlugin.setDistributionUrl(distributionUrl);
}
// This method sets a custom distribution url overriding Ivy repository configured.
@Override
public void setCustomDistributionUrl(String customDistributionUrl) {
...
}

https://github.com/opensearch-project/OpenSearch/blob/main/buildSrc/src/main/java/org/opensearch/gradle/testclusters/TestClusterConfiguration.java#L53

Comment on lines +142 to +146
public static void setDistributionUrl(String distributionUrl) {
DistributionDownloadPlugin.distributionUrl = distributionUrl;
}

// pkg private for tests and set up Download Repository
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there a way to avoid distributionUrl to be static?

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

❌   Gradle Check failure e101370
Log 1572

Reports 1572

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

❌   Gradle Check failure 5ca37b2
Log 1574

Reports 1574

Signed-off-by: Rishikesh1159 <rishireddy1159@gmail.com>
@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

❌   Gradle Check failure 4acf50d
Log 1579

Reports 1579

@Rishikesh1159
Copy link
Copy Markdown
Member Author

start gradle check

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

❌   Gradle Check failure 4acf50d
Log 1580

Reports 1580

@Rishikesh1159
Copy link
Copy Markdown
Member Author

start gradle check

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

✅   Gradle Check success 4acf50d
Log 1582

Reports 1582

@Rishikesh1159
Copy link
Copy Markdown
Member Author

I figured out a new approach for solving this issue which is much cleaner and simple. Here we will directly set customDistributionUrl as an external property (in ext {}section) in build.gradle file of plugin (ex:anomaly detection plugin).

We can access this external property by method "project.findProperty(“customDistributionUrl”)." This is possible because we are moving setupDownloadServiceRepo() to setupDistributions() in DistributionDownloadPlugin.java. We are moving this into setupDistribution() method because this method is executed after project is evaluated and by then project is configured and we have all properties set.

I think this approach will solve all of requested changes for this PR. Please comment on this approach, if this is any better than previous one and any changes needed

Copy link
Copy Markdown
Member

@saratvemulapalli saratvemulapalli left a comment

Choose a reason for hiding this comment

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

Very nice work @Rishikesh1159 !!
Love this new approach.

Overall it looks good to me, dropped a comment on tests.

@saratvemulapalli
Copy link
Copy Markdown
Member

Requested reviews from @dblock and @nknize as they have requested changes, github doesnt allow changes to be merged.

assertEquals(distro.getType(), Type.ARCHIVE);
}

public void testCustomDistributionUrl() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you add another test which sets the customDistributionUrl and make sure DistributionDownloadPlugin downloads the artifact?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure @saratvemulapalli I will add another test for this scenario.

addIvyRepo(project, SNAPSHOT_REPO_NAME, "https://artifacts.opensearch.org", FAKE_SNAPSHOT_IVY_GROUP, SNAPSHOT_PATTERN_LAYOUT);
// checks if custom Distribution Url has been passed by user from plugins
if (project.findProperty("customDistributionUrl") != null) {
String customDistributionUrl = project.findProperty("customDistributionUrl").toString();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: avoid doing 2 lookups.

... customDistributionUrl = project.findProperty("customDistributionUrl")
if (customDistributionUrl != null) {
    ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dblock sure, I will make this change.

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

❌   Gradle Check failure a1d4d87
Log 1803

Reports 1803

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

✅   Gradle Check success 5fcb072
Log 1830

Reports 1830

…at changes.

Signed-off-by: Rishikesh1159 <rishireddy1159@gmail.com>
@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

✅   Gradle Check success e5957f1
Log 1832

Reports 1832

Copy link
Copy Markdown
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Needs tests that show which ivy repos were added and which were not based on this input.

Call setupDownloadServiceRepo and test the results.

@opensearch-ci-bot
Copy link
Copy Markdown
Collaborator

✅   Gradle Check success a24c895
Log 2056

Reports 2056

@Rishikesh1159
Copy link
Copy Markdown
Member Author

Needs tests that show which ivy repos were added and which were not based on this input.

Call setupDownloadServiceRepo and test the results.

As both addIvyRepo() snd setupDownloadServiceRepo() are private methods, so I find only way to mock them is using PowerMocks. If we don't want use PowerMocks we need to change access modifiers of both these methods. @saratvemulapalli can you please help me how proceed further with this?

@dblock
Copy link
Copy Markdown
Member

dblock commented Feb 2, 2022

@Rishikesh1159 Any good reason not make these methods public?

@Rishikesh1159
Copy link
Copy Markdown
Member Author

Accidently deleted the fork repo in which I opened this PR. Closing this PR now. New PR is opened to solve this, PR #2086 . Changes suggested and new discussion can found in new PR.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants