Skip to content

fix(datastore): do not override DLB replica DSNs in for loop

João Pereira requested to merge dlb-fix-for-dsn into master

What does this MR do?

Variables in for loops are reused for each iteration, and in this specific case, we were passing its address (&dsn) to the lb.connector.Open function. Since dsn is a single variable reused across iterations, the pointer &dsn will always point to the same memory location, which contains the last value assigned to dsn by the loop. Note that the actual connection logic is correct, as each connection is open using the correct DSN. The problem is in the logging. See below.

Before

INFO[0000] enabling database load balancing with service discovery  go_version=go1.21.5 instance_id=c1a11593-c88c-468c-9f83-9a7b9c24d52c nameserver=localhost port=8600 record=replica.pgbouncer.service.consul replica_check_interval_s=15 version=v4.8.0-gitlab-2-g85a3e9896
INFO[0000] resolving replicas with service discovery     component=registry.datastore.DBLoadBalancer go_version=go1.21.5 instance_id=c1a11593-c88c-468c-9f83-9a7b9c24d52c version=v4.8.0-gitlab-2-g85a3e9896
INFO[0000] opening replica connection                    address="127.0.0.1:6432" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 index=0 instance_id=c1a11593-c88c-468c-9f83-9a7b9c24d52c total=4 version=v4.8.0-gitlab-2-g85a3e9896
INFO[0000] opening replica connection                    address="127.0.0.1:6435" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 index=1 instance_id=c1a11593-c88c-468c-9f83-9a7b9c24d52c total=4 version=v4.8.0-gitlab-2-g85a3e9896
INFO[0000] opening replica connection                    address="127.0.0.1:6433" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 index=2 instance_id=c1a11593-c88c-468c-9f83-9a7b9c24d52c total=4 version=v4.8.0-gitlab-2-g85a3e9896
INFO[0000] opening replica connection                    address="127.0.0.1:6434" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 index=3 instance_id=c1a11593-c88c-468c-9f83-9a7b9c24d52c total=4 version=v4.8.0-gitlab-2-g85a3e9896
INFO[0000] updating replicas list                        added="[127.0.0.1:6434 127.0.0.1:6434 127.0.0.1:6434 127.0.0.1:6434]" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 instance_id=c1a11593-c88c-468c-9f83-9a7b9c24d52c removed="[]" version=v4.8.0-gitlab-2-g85a3e9896

Note that we open connections against each host as expected, but then we log 4 times the last host as added.

After

INFO[0000] enabling database load balancing with service discovery  go_version=go1.21.5 instance_id=f318daaf-212c-4075-ac9b-e6d2e58705c1 nameserver=localhost port=8600 record=replica.pgbouncer.service.consul replica_check_interval_s=15 version=v4.8.0-gitlab-2-g85a3e9896.m
INFO[0000] resolving replicas with service discovery     component=registry.datastore.DBLoadBalancer go_version=go1.21.5 instance_id=f318daaf-212c-4075-ac9b-e6d2e58705c1 version=v4.8.0-gitlab-2-g85a3e9896.m
INFO[0000] opening replica connection                    address="127.0.0.1:6435" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 index=0 instance_id=f318daaf-212c-4075-ac9b-e6d2e58705c1 total=4 version=v4.8.0-gitlab-2-g85a3e9896.m
INFO[0000] opening replica connection                    address="127.0.0.1:6433" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 index=1 instance_id=f318daaf-212c-4075-ac9b-e6d2e58705c1 total=4 version=v4.8.0-gitlab-2-g85a3e9896.m
INFO[0000] opening replica connection                    address="127.0.0.1:6434" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 index=2 instance_id=f318daaf-212c-4075-ac9b-e6d2e58705c1 total=4 version=v4.8.0-gitlab-2-g85a3e9896.m
INFO[0000] opening replica connection                    address="127.0.0.1:6432" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 index=3 instance_id=f318daaf-212c-4075-ac9b-e6d2e58705c1 total=4 version=v4.8.0-gitlab-2-g85a3e9896.m
INFO[0000] updating replicas list                        added="[127.0.0.1:6435 127.0.0.1:6433 127.0.0.1:6434 127.0.0.1:6432]" component=registry.datastore.DBLoadBalancer go_version=go1.21.5 instance_id=f318daaf-212c-4075-ac9b-e6d2e58705c1 removed="[]" version=v4.8.0-gitlab-2-g85a3e9896.m

Author checklist

  • Assign one of conventiona-commit prefixes to the MR.
    • fix: Indicates a bug fix, triggers a patch release.
    • feat: Signals the introduction of a new feature, triggers a minor release.
    • perf: Focuses on performance improvements that don't introduce new features or fix bugs, triggers a patch release.
    • docs: Updates or changes to documentation. Does not trigger a release.
    • style: Changes that do not affect the code's functionality. Does not trigger a release.
    • refactor: Modifications to the code that do not fix bugs or add features but improve code structure or readability. Does not trigger a release.
    • test: Changes related to adding or modifying tests. Does not trigger a release.
    • chore: Routine tasks that don't affect the application, such as updating build processes, package manager configs, etc. Does not trigger a release.
    • build: Changes that affect the build system or external dependencies. May trigger a release.
    • ci: Modifications to continuous integration configuration files and scripts. Does not trigger a release.
    • revert: Reverts a previous commit. It could result in a patch, minor, or major release.
  • Feature flags
    • This change does not require a feature flag
    • Added feature flag: ( Add the Feature flag tracking issue link here )
  • Unit-tests
    • Unit-tests are not required
    • I added unit tests
  • Documentation:
  • database changes including schema migrations:
    • Change does not introduce database changes
    • MR includes DB chagnes
      • Do not include code that depends on the schema migrations in the same commit. Split the MR into two or more.
      • Manually run up and down migrations in a postgres.ai production database clone and post a screenshot of the result here.
      • If adding new queries, extract a query plan from postgres.ai and post the link here. If changing existing queries, also extract a query plan for the current version for comparison.
        • I do not have access to postgres.ai and have made a comment on this MR asking for these to be run on my behalf.
  • Ensured this change is safe to deploy to individual stages in the same environment (cny -> prod). State-related changes can be troublesome due to having parts of the fleet processing (possibly related) requests in different ways.
  • If the change contains a breaking change, apply the breaking change label.
  • If the change is considered high risk, apply the label high-risk-change
  • Changes cannot be rolled back
    • Change can be safelly rolled back
    • Change can't be safelly rolled back
      • Apply the label cannot-rollback.
      • Add a section to the MR description that includes the following details:
        • The reasoning behind why a release containing the presented MR can not be rolled back (e.g. schema migrations or changes to the FS structure)
        • Detailed steps to revert/disable a feature introduced by the same change where a migration cannot be rolled back. (note: ideally MRs containing schema migrations should not contain feature changes.)
        • Ensure this MR does not add code that depends on these changes that cannot be rolled back.
Documentation/resources

Code review guidelines

Go Style guidelines

Reviewer checklist

  • Ensure the commit and MR tittle are still accurate.
  • If the change contains a breaking change, verify the breaking change label.
  • If the change is considered high risk, verify the label high-risk-change
  • Identify if the change can be rolled back safely. (note: all other reasons for not being able to rollback will be sufficiently captured by major version changes).
Edited by Suleimi Ahmed

Merge request reports

Loading