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

AIP-121: lint against cyclic references #1109

Closed
toumorokoshi opened this issue Mar 3, 2023 · 6 comments · Fixed by #1238
Closed

AIP-121: lint against cyclic references #1109

toumorokoshi opened this issue Mar 3, 2023 · 6 comments · Fixed by #1238

Comments

@toumorokoshi
Copy link
Contributor

AIP-121 states:

The relationship between resources, such as parent-child or resource references, must be representable via a directed acyclic graph.

That should be added.

@noahdietz
Copy link
Collaborator

This rule is necessary for a candidate in CS-2312, so I'm bumping the priority.

@noahdietz
Copy link
Collaborator

I'm trying to nail down the concrete definition of a cycle. There are probably a few scenarios.

  1. Parent with child reference annotated as required - impossible to create valid Parent without Child reference, but Child can't be created without a Parent to create with:
message Parent {
  // ...
  string child = 2 [
    (google.api.resource_reference).type = ".../Child",
    (google.api.field_behavior) = REQUIRED
  ];
}
  1. Parent with any child reference, regardless of field_behavior - requires the "create parent, create child, update parent" cycle
message Parent {
  // ...
  string child = 2 [
    (google.api.resource_reference).type = ".../Child"
  ];
}

Solving for 2 solves for 1 as well, obviously. Though I wonder, would an output_only reference to a child be OK? I think so. Which means only references that are accepted as input are in scope for cycle checks.

  1. Resource A that refers to Resource B, which refers back to Resource A
message A {
  // ...
  string b = 2 [
    (google.api.resource_reference).type = ".../B"
  ];
}

message B {
  // ...
  string a = 2 [
    (google.api.resource_reference).type = ".../A"
  ];
}

This is hard to tell if its even bad because just having the reference cycle does not mean that it will be a cycle at runtime i.e. Resource A instance 123 refers to Resource B instance abc and vice versa. But I think this might be an example of the issue described in the AIP The delete operation may also become more complex. Need some clarity on this - how strict are we really trying to go?

@toumorokoshi
Copy link
Contributor Author

Though I wonder, would an output_only reference to a child be OK? I think so.

yep, agreed. It's only mutable references.

This is hard to tell if its even bad because just having the reference cycle does not mean that it will be a cycle at runtime i.e. Resource A instance 123 refers to Resource B instance abc and vice versa.

I think the precise concern is that cylic references result in multi-phase operations, touching at least one of the resources twice.

I'd say scenario three (A has a mutable reference to B, b have a mutable reference to A) is also invalid.

But I think this might be an example of the issue described in the AIP The delete operation may also become more complex. Need some clarity on this - how strict are we really trying to go?

Yes - the delete operation may require unlinking the resources first (e.g. removing the resource references), then performing the delete afterward on each. This isn't strictly required as it depends on the behavior of the resource, but worst-case it'll require this multi-phase work.

I think we should ensure that no mutable references are cyclic. So among the examples above 1,2,3 are all invalid. If there was a fourth example with an OUTPUT_ONLY cyclic reference, that would be valid.

I'd also note that similar to the field_behavior rule, we'd probably have to only check the current package, avoiding some situation where a service team can't actually fix the issue by themselves.

@noahdietz
Copy link
Collaborator

Thanks that clarifies things a lot.

Is it worth following up in the AIP with a fix to make this distinction?

@toumorokoshi
Copy link
Contributor Author

there's a clause around output_only fields, but maybe it should be updated to be more like the AIP must: https://google.aip.dev/121#cyclic-references

@noahdietz
Copy link
Collaborator

Ok great, I've got the rule PR up

gcf-merge-on-green bot pushed a commit that referenced this issue Aug 29, 2023
Introduces rules to lint for mutable resource reference cycles.

Fixes #1109
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants