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

Fix broken links section in docker-compose recipe #2850

Merged
merged 12 commits into from
Oct 31, 2016
Merged

Fix broken links section in docker-compose recipe #2850

merged 12 commits into from
Oct 31, 2016

Conversation

mmorhun
Copy link
Contributor

@mmorhun mmorhun commented Oct 20, 2016

What does this PR do?

Fix broken links section in docker-compose recipe.

What issues does this PR fix or reference?

#2687

Previous behavior

If links section is present in recipe than workspace will fail to start.

New behavior

Links and aliases works now.

@mmorhun
Copy link
Contributor Author

mmorhun commented Oct 20, 2016

@garagatyi @benoitf @l0rd @AndrienkoAleksandr review please

@mmorhun
Copy link
Contributor Author

mmorhun commented Oct 20, 2016

I'll add tests a bit later, maybe in M7

@AndrienkoAleksandr
Copy link
Contributor

+1

@codenvy-ci
Copy link

Build # 755 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/755/ to view the results.

@gazarenkov
Copy link
Contributor

What is the reason of names like "...CorrectSource"?
It is supposed to have some "...IncorrectSources" and things as well?
Things like this makes the code less readable

@mmorhun
Copy link
Contributor Author

mmorhun commented Oct 21, 2016

Agree, I'll change the name.

Copy link
Contributor

@benoitf benoitf left a comment

Choose a reason for hiding this comment

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

For me, the tests should be added before merging as I don't see how we can be sure without testing.

@@ -17,7 +17,7 @@

/** @author andrew00x */
public class NetworkSettings {
private String ipAddress;
private String iPAddress;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would keep ipAddress as it was as method name is getIpAddress() not getIPAddress()

Copy link
Contributor Author

@mmorhun mmorhun Oct 21, 2016

Choose a reason for hiding this comment

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

Yes, ipAddress is conventional name , but iPAddress is needed for parsing value from json. Exactly like iPPrefixLen, iPv6Gateway in the same class.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @benoitf that iPAddress is weird. Wouldn't it possible to use @SerializedName annotation instead?

*/
@VisibleForTesting
void replaceServiceToContainerNameInLinks(CheServiceImpl serviceToStart, List<CheServiceImpl> launchedServices) {
final List<String> containerLinks = serviceToStart.getLinks();
Copy link
Contributor

Choose a reason for hiding this comment

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

we couldn't use Java8 stream/filter/collect for the whole method ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can, but I think that it is redundant here.
If we used stream here it would made code more complicated and we would had additional expenses for stream creation/collection here (note, that in many cases list with links contains 0 or 1 element, i.e. how often and how many links you use in docker compose?)

@codenvy-ci
Copy link

Build # 762 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/762/ to view the results.

@codenvy-ci
Copy link

Build # 777 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/777/ to view the results.

/**
* Replaces linked to this service's name with container name which represents the service in links section.
* The problem is that a user writes names of other services in links section in compose file.
* But actually links are constraints and they values should be names of containers (not services) to be linked.
Copy link
Contributor

Choose a reason for hiding this comment

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

and they values => and their values

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thx

void replaceServiceToContainerNameInLinks(CheServiceImpl serviceToStart, Map<String, CheServiceImpl> services) {
final List<String> containerLinks = serviceToStart.getLinks();
for (int i = 0; i < containerLinks.size(); i++) {
String link = containerLinks.get(i);
Copy link
Contributor

Choose a reason for hiding this comment

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

Extracting the body of the for loop in a new method would make it more readable

Copy link
Contributor Author

@mmorhun mmorhun Oct 26, 2016

Choose a reason for hiding this comment

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

Taking into account, that we have only one loop in the method and this loop actually is method's body and body of this loop cannot be reused somewhere and if so, we should pass iterator and links list into new method as parameters, IMO, there is no point to extract loop body into another method. @garagatyi WDYT?

Choose a reason for hiding this comment

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

Since you are going to refactor this code lets figure out what can be cleaned up after that refactoring

@VisibleForTesting
void replaceServiceToContainerNameInLinks(CheServiceImpl serviceToStart, Map<String, CheServiceImpl> services) {
final List<String> containerLinks = serviceToStart.getLinks();
for (int i = 0; i < containerLinks.size(); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could probably use for each here (for (String link: containersLinks) {)

Copy link
Contributor Author

@mmorhun mmorhun Oct 26, 2016

Choose a reason for hiding this comment

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

No, I cannot. I need to change the list. I can use Iterator here, but I think that current version is more readable.

int colonIndex = link.indexOf(':');
if (colonIndex != -1) {
serviceName = link.substring(0, colonIndex);
serviceAlias = link.substring(colonIndex + 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

What happen if user provide a (bad) link with an empty alias like: serviceDb:

Copy link
Contributor

Choose a reason for hiding this comment

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

split() method is probably more appropriate here instead of manually parsing the links string.

Copy link
Contributor Author

@mmorhun mmorhun Oct 26, 2016

Choose a reason for hiding this comment

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

If user provide link with empty alias, like serviceDb:, than CheEnvironmentValidator will throw an exception and a user will be notified about syntax error.


CheServiceImpl serviceLinkedTo = services.get(serviceName);
if (serviceLinkedTo != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

An error should be returned if the user is trying to link a service that does not exist (i.e. serviceLinkedTo == null)

Copy link
Contributor Author

@mmorhun mmorhun Oct 26, 2016

Choose a reason for hiding this comment

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

I think, that error should be reported. Please note, that things like that are validated by CheEnvironmentValidator.

CheServiceImpl serviceLinkedTo = services.get(serviceName);
if (serviceLinkedTo != null) {
String containerNameLinkedTo = serviceLinkedTo.getContainerName();
containerLinks.set(i, (serviceAlias == null) ?
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of modifying current containersLinks list you could make this method return a new list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But in case of no links we need additional logic which made code a bit complicated or return new empty list when it can be untouched, which is a bit weird to me.

@@ -895,6 +895,43 @@ public void shouldBeAbleToRemoveSnapshot() throws Exception {
verify(instanceProvider).removeInstanceSnapshot(machineSource);
}

@Test
public void shouldReplaceServiceNameWithContainerNameAndAliasInLinks() {
Copy link
Contributor

Choose a reason for hiding this comment

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

You are testing 2 things here (links with and without alias). You could probably split this test in 2 separate tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

void normalizeLinks(CheServiceImpl serviceToNormalizeLinks, Map<String, CheServiceImpl> services) {
final List<String> containerLinks = serviceToNormalizeLinks.getLinks();
for (int i = 0; i < containerLinks.size(); i++) {
String serviceNameAndAliasToLink[] = containerLinks.get(i).split(":"); // a link has format: 'name:alias' or 'name'

Choose a reason for hiding this comment

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

Pass second argument to split

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

containerNameLinkTo + ':' + serviceAlias);
} else {
// should never happens. Errors like this should be filtered by CheEnvironmentValidator
LOG.error("Attempt to link non existing service {} to {} service.", serviceName, serviceToNormalizeLinks);

Choose a reason for hiding this comment

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

It is better to throw IllegalArgumentExcption

if (serviceLinkTo != null) {
String containerNameLinkTo = serviceLinkTo.getContainerName();
containerLinks.set(i, (serviceAlias == null) ?
containerNameLinkTo + ':' + serviceName :

Choose a reason for hiding this comment

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

Is it needed? I suppose it already works in such a way?

@VisibleForTesting
void normalizeLinks(CheServiceImpl serviceToNormalizeLinks, Map<String, CheServiceImpl> services) {
final List<String> containerLinks = serviceToNormalizeLinks.getLinks();
for (int i = 0; i < containerLinks.size(); i++) {

Choose a reason for hiding this comment

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

I would suggest to simplify code with stream API - it looks overcomplicated

@@ -667,6 +711,7 @@ private void startEnvironmentQueue(String namespace,
try {
machineProvider.createNetwork(networkId);

EnvironmentHolder environmentHolder;

Choose a reason for hiding this comment

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

Is it still used?

@@ -736,7 +781,7 @@ private void startEnvironmentQueue(String namespace,
boolean queuePolled = false;
try (StripedLocks.WriteLock lock = stripedLocks.acquireWriteLock(workspaceId)) {
ensurePreDestroyIsNotExecuted();
EnvironmentHolder environmentHolder = environments.get(workspaceId);
environmentHolder = environments.get(workspaceId);

Choose a reason for hiding this comment

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

Is it still used?

@codenvy-ci
Copy link

Build # 802 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/802/ to view the results.

Signed-off-by: Mykola Morhun <[email protected]>
@@ -895,6 +895,65 @@ public void shouldBeAbleToRemoveSnapshot() throws Exception {
verify(instanceProvider).removeInstanceSnapshot(machineSource);
}

@Test
public void shouldReplaceServiceNameWithContainerNameInLinks() {

Choose a reason for hiding this comment

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

Please ensure that code of MachineProviderImpl is also covered

@codenvy-ci
Copy link

Build # 812 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/812/ to view the results.

Mykola Morhun added 2 commits October 28, 2016 09:17
@mmorhun
Copy link
Contributor Author

mmorhun commented Oct 28, 2016

@garagatyi @benoitf @l0rd take a look again please

@codenvy-ci
Copy link

Build # 829 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/829/ to view the results.

Copy link

@garagatyi garagatyi left a comment

Choose a reason for hiding this comment

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

Good job!

@l0rd
Copy link
Contributor

l0rd commented Oct 30, 2016

Nice @mmorhun! LGTM

Mykola Morhun added 2 commits October 31, 2016 16:01
@mmorhun mmorhun merged commit d74b24f into master Oct 31, 2016
@mmorhun mmorhun deleted the CHE-2687 branch October 31, 2016 14:22
@codenvy-ci
Copy link

Build # 861 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/861/ to view the results.

JPinkney pushed a commit to JPinkney/che that referenced this pull request Aug 17, 2017
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.

7 participants