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

Parallel Region Create API calls can produce top-level regions with the same tree_id #12336

Closed
DevOpsFu opened this issue Apr 24, 2023 · 7 comments · Fixed by #13942
Closed
Assignees
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@DevOpsFu
Copy link

NetBox version

v3.4.8

Python version

3.8

Steps to Reproduce

Ref: e-breuninger/terraform-provider-netbox#385

Using a tool which will produce parallel API calls to NetBox (e.g. Terraform), create two or more top-level Region objects with children, via the /dcim/regions endpoint.

In Terraform, this might look like this:

resource "netbox_region" "uk" {
  name = "United Kingdom"
}

resource "netbox_region" "ireland" {
  name = "Ireland"
}

resource "netbox_region" "london" {
  name             = "London"
  parent_region_id = netbox_region.uk.id
}

resource "netbox_region" "gatwick" {
  name             = "Gatwick"
  parent_region_id = netbox_region.uk.id
}



resource "netbox_region" "northeurope" {
  name             = "Azure North Europe"
  parent_region_id = netbox_region.ireland.id
}

resource "netbox_region" "uksouth" {
  name             = "Azure UK South"
  parent_region_id = netbox_region.london.id
}

Expected Behavior

The Regions are created with the correct tree structure in the NetBox UI

Observed Behavior

The top-level regions can sometimes be created with the same tree_id in the dcim_region table. This causes some weird UI issues whereby all the child regions will appear under one of the top-level regions, despite it not being their parent:

image

image

If you follow the child region links, the Parent IDs are set correctly.

To work around this, we can ensure that the top-level regions are created in serial so that they never receive the same tree_id value.

@DevOpsFu DevOpsFu added the type: bug A confirmed report of unexpected behavior in the application label Apr 24, 2023
@arthanson arthanson added the status: blocked Another issue or external requirement is preventing implementation label Apr 26, 2023
@arthanson
Copy link
Collaborator

This looks like it is due to django-mptt/django-mptt#677 see: django-mptt/django-mptt#555 but the PR is stagnant so marking this as blocked for either #6587 or #11421

@jeremystretch jeremystretch added the severity: medium Results in substantial degraded or broken functionality for specfic workflows label Jun 23, 2023
@arthanson arthanson self-assigned this Oct 2, 2023
@arthanson
Copy link
Collaborator

@DevOpsFu could you please test against #13942 I made the region API calls atomic which I think should fix the issue. I don't have terraform setup so don't have a good test scenario.

@DevOpsFu
Copy link
Author

DevOpsFu commented Oct 2, 2023

@DevOpsFu could you please test against #13942 I made the region API calls atomic which I think should fix the issue. I don't have terraform setup so don't have a good test scenario.

I'd be happy to test! I'll see if I can get the code at that PR built in Docker and get back to you soon 👍

@DevOpsFu
Copy link
Author

DevOpsFu commented Oct 3, 2023

Hi @arthanson , I have tested this against your branch but unfortunately I still get the same behaviour. The netbox stack was totally clean to start with, and running against a docker image built from your branch:

❯ docker ps
CONTAINER ID   IMAGE                                               COMMAND                  CREATED         STATUS                   PORTS                                       NAMES
e7dc6c711d55   netboxcommunity/netbox:12336-serialize-region-api   "/usr/bin/tini -- /o…"   5 minutes ago   Up 2 minutes (healthy)                                               netbox-docker-netbox-housekeeping-1
e2d05181e486   netboxcommunity/netbox:12336-serialize-region-api   "/usr/bin/tini -- /o…"   5 minutes ago   Up 2 minutes (healthy)                                               netbox-docker-netbox-worker-1
cf382b1dc07d   netboxcommunity/netbox:12336-serialize-region-api   "/usr/bin/tini -- /o…"   5 minutes ago   Up 5 minutes (healthy)   0.0.0.0:8000->8080/tcp, :::8000->8080/tcp   netbox-docker-netbox-1
7d36a9501ebc   postgres:15-alpine                                  "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes             5432/tcp                                    netbox-docker-postgres-1
5676b041e21b   redis:7-alpine                                      "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes             6379/tcp                                    netbox-docker-redis-cache-1
96767ca07745   redis:7-alpine                                      "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes             6379/tcp                                    netbox-docker-redis-1

When running the Terraform code I pasted originally, I see the following region structure gets produced:

image

@arthanson
Copy link
Collaborator

Thanks @DevOpsFu I guess I will have to change to use locks, will get a new version out shortly.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation and removed status: blocked Another issue or external requirement is preventing implementation labels Oct 3, 2023
@arthanson
Copy link
Collaborator

@DevOpsFu could you please try one more time, I've updated the PR to use postgres locks which I hope will fix it.

@DevOpsFu
Copy link
Author

@arthanson Looks good! I did a few create/destroy cycles with the above TF code and the region tree looked correct every time. Thanks for looking at this!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants