Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Deploy Environment Weights #13069

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

Open
wants to merge 7 commits into
base: jetty-12.1.x
Choose a base branch
from

Conversation

joakime
Copy link
Contributor

@joakime joakime commented Apr 30, 2025

Enhancement to handle Default Environment sorting via weights assigned to the Environment when the environment is configured with the DeploymentScanner.

@joakime joakime requested review from gregw and sbordet April 30, 2025 22:08
@joakime joakime self-assigned this Apr 30, 2025
gregw

This comment was marked as outdated.

Copy link
Contributor

@gregw gregw left a comment

Choose a reason for hiding this comment

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

I don't really like the approach of setting weights everywhere.
I'd prefer a centralized approach. Just have a setter on the scanner that takes a list of environment names and the weight is the index of the env name in that list. We can have a default list with our known environments and it will be an error if an unknown env name needs to be sorted

@joakime joakime requested a review from gregw May 2, 2025 04:04
@joakime
Copy link
Contributor Author

joakime commented May 2, 2025

I don't really like the approach of setting weights everywhere.

I reverted the XML and Test case changes.

I'd prefer a centralized approach. Just have a setter on the scanner that takes a list of environment names and the weight is the index of the env name in that list. We can have a default list with our known environments and it will be an error if an unknown env name needs to be sorted

I'm good up to the "and it will be an error if an unknown env name needs to be sorted" bit.
I left the new configureEnvironment(String name, int weight) alone.
But restored the original configureEnvironment(String name).
Also introduced a properties file that has "our" weights.

This allows for new environments, which your proposal doesn't allow.
It also allows groups to insert their own implementations of ee# environments anywhere they want (even overriding the environments we ship with a weight higher than ours if they want to)

We are down from 24 files changed, to 4 now in this PR.

private final Server server;
private final FilenameFilter filenameFilter;
private final List<Path> monitoredDirs = new CopyOnWriteArrayList<>();
private final ContextHandlerFactory contextHandlerFactory;
private final Map<String, PathsApp> trackedApps = new HashMap<>();
private final Map<String, Attributes> environmentAttributesMap = new HashMap<>();
private Set<String> trackedEnvironments = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
private List<TrackedEnv> trackedEnvironments = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Make it final.

@@ -301,20 +272,56 @@ void addScannerListener(Scanner.Listener listener)
scanner.addListener(listener);
}

protected int getDefaultWeight(String name)
{
URL url = getClass().getResource("environment-weights.properties");
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this will work if DeploymentScanner is subclassed.

Copy link
Contributor

Choose a reason for hiding this comment

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

A properties file is too hard to change and it still makes weights in the "API". We don't need weights, we just need an order. If somebody defines a new environment, then setting a new order is easier than inventing a weight that is robust to future changes.

@@ -301,20 +272,56 @@ void addScannerListener(Scanner.Listener listener)
scanner.addListener(listener);
}

protected int getDefaultWeight(String name)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like this method.
We should just have configureEnvironment(String, int).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Read Greg's review, he didn't like this approach

Copy link
Contributor

@sbordet sbordet May 4, 2025

Choose a reason for hiding this comment

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

Well, I don't like the current one.

We decided to shy away from "default" environments, so the core environment was not created upfront, etc. so why now we should have upfront weights?

The weights are not everywhere, they are just in 1 method of DeploymentScanner.
Only Jetty XML code should call that method, so a custom corp-deploy module should just be aware of Jetty's xyz-deploy modules, if they are used simultaneously with corp-deploy.

Otherwise, there are no Jetty xyz-deploy modules in use, we don't load our environment-weights.properties that we don't use, and there only will be corp-deploy and spring-corp-deploy, with the weights they decide to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A prior commit did this
See the revert at 00f95a7 for how it looked.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't like this approach either. I don't like the concept of weights being seen externally at all, as it is just an abstraction that we don't need to both the user with. I think we should just have:

    void setEnvironmentOrder(List<String> environmentNames);
    List<String> getEnvironmentOrder();

With the default value being ["core", "static", "ee8", "ee9", "ee10", "ee11"].

If internally we turn that into weights, then fine, but no weights should be in any public API.

* (Example usage: {@code static} is 100, {@code core} is 200, {@code ee8} is 1008, {@code ee11} is 1011)
* @return the deployment configuration for the {@link Environment}.
*/
public EnvironmentConfig configureEnvironment(String name, int weight)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just have this method and delete configureEnvironment(String name).
Rename this to trackEnvironment() maybe?
The XML files will pass in the weight and there will be no need for the environment-weights.properties file.
This method is only called by tests or XML files.


trackedEnvironments.add(new TrackedEnv(name, weight));
}

public void setDefaultEnvironmentName(String name)
Copy link
Contributor

Choose a reason for hiding this comment

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

This method should be removed there is no concept of default, if not determined by the environments that are enabled.

.toList();
}

protected void addTrackedEnvironment(String name, int weight)
Copy link
Contributor

Choose a reason for hiding this comment

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

Make it private or package.

@@ -346,12 +353,29 @@ public String getDefaultEnvironmentName()
if (defaultEnvironmentName == null)
{
return trackedEnvironments.stream()
.min(ENVIRONMENT_COMPARATOR)
.max(Comparator.comparing(TrackedEnv::weight))
Copy link
Contributor

Choose a reason for hiding this comment

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

Method getDefaultEnvironmentName() should be private.

DeploymentScanner deploymentScanner = new DeploymentScanner(new Server());
for (Map.Entry<String, Integer> entry : input.entrySet())
{
deploymentScanner.addTrackedEnvironment(entry.getKey(), entry.getValue());
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be:

Suggested change
deploymentScanner.addTrackedEnvironment(entry.getKey(), entry.getValue());
deploymentScanner.configureEnvironment(entry.getKey(), entry.getValue());

@@ -1684,4 +1721,8 @@ else if (ret != PathsApp.State.REMOVED)
return ret != null ? ret : PathsApp.State.UNCHANGED;
}
}

record TrackedEnv(String name, int weight)
Copy link
Contributor

Choose a reason for hiding this comment

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

TrackedEnv -> TrackedEnvironment

public void testDefaultWeights()
{
DeploymentScanner deploymentScanner = new DeploymentScanner(new Server());
assertThat(deploymentScanner.getDefaultWeight("core"), is(200));
Copy link
Contributor

Choose a reason for hiding this comment

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

There is too much magic here.
Where the 200 come from?

I would expect this test to call configureEnvironment("core", 200) before the asserts, so it's clear where the 200 comes from.

Copy link
Contributor

@gregw gregw left a comment

Choose a reason for hiding this comment

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

I don't like weights being in the API at all. Just have an order list.

@@ -301,20 +272,56 @@ void addScannerListener(Scanner.Listener listener)
scanner.addListener(listener);
}

protected int getDefaultWeight(String name)
Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't like this approach either. I don't like the concept of weights being seen externally at all, as it is just an abstraction that we don't need to both the user with. I think we should just have:

    void setEnvironmentOrder(List<String> environmentNames);
    List<String> getEnvironmentOrder();

With the default value being ["core", "static", "ee8", "ee9", "ee10", "ee11"].

If internally we turn that into weights, then fine, but no weights should be in any public API.

@@ -301,20 +272,56 @@ void addScannerListener(Scanner.Listener listener)
scanner.addListener(listener);
}

protected int getDefaultWeight(String name)
{
URL url = getClass().getResource("environment-weights.properties");
Copy link
Contributor

Choose a reason for hiding this comment

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

A properties file is too hard to change and it still makes weights in the "API". We don't need weights, we just need an order. If somebody defines a new environment, then setting a new order is easier than inventing a weight that is robust to future changes.

@sbordet
Copy link
Contributor

sbordet commented May 5, 2025

@gregw I don't see how the "order list" would work in case of custom environments.

With weights in the APIs, users will be exposed to the fact that there is an order, while calling setEnvironmentOrder() would be easily missed.

Furthermore, we would have to remember to update that list for every new EE version, while with weights, we just need to copy jetty-ee11-deploy.xml into jetty-ee12-deploy.xml, and change the new XML file (which we have to anyway, as we likely need to change package names to refer to ee12).

We don't bother users with weights, just implementers of new deployment environments, which should be rare.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

3 participants