Skip to content

Improve agent starting and simultaneously restarting (common for Agent install) concurrency handling #25282

Merged
dd-mergequeue[bot] merged 8 commits intomainfrom
len.gamburg/improve-agent-restart-service
May 6, 2024
Merged

Improve agent starting and simultaneously restarting (common for Agent install) concurrency handling #25282
dd-mergequeue[bot] merged 8 commits intomainfrom
len.gamburg/improve-agent-restart-service

Conversation

@iglendd
Copy link
Copy Markdown
Contributor

@iglendd iglendd commented Apr 30, 2024

What does this PR do?

Improve agent starting and simultaneously restarting, which is common for Agent install and upgrade, concurrency handling. Specifically it handles the following edge case:

  1. [Process A] datadogagent service is starting in the end of Agent installation (no Agent services are running at this moment)
  2. [Process B] Externally and concurrently (after Agent configuration change) agent.exe restart-service CLI command is started.
  3. [Process B] agent.exe restart-service CLI command will get list of datadogagent dependent services to be stopped before stopping datadogagent service itself.
  4. [Process A] datadog-trace-agent service (or other dependent services) is started by the still starting datadogagent service (from step no.1).
  5. [Process B] agent.exe restart-service CLI command should stop just started datadog-trace-agent service before moving on to stop datadogagent service itself.

Specifically:

  • Collection of dependent services for the datadogagent service previously collected only Running dependent service. In some cases with concurrent execution stopping dependent services missed just started dependents services if they started AFTER dependent service collection,. The command would miss newly started dependent services and stopping datadogagent service would fail because a dependent service is still running. New code collects all dependent services to avoid this particular edge case. It does not solve all possible race condition but it eliminates a few of them.
  • Previously timeout for service stop was 10 seconds and now it is increased to 30 seconds.

Both changes above are also similar to the Powershell's Restart-Service cmdlet logic.

Motivation

We suspect that there is a small risk that certain small timing changes in Agent code may increase race conditions outlined above.

Additional Notes

Possible Drawbacks / Trade-offs

Describe how to test/QA your changes

No need for manual test since key logic is tested in the accompanied unit tests

iglendd added 2 commits April 29, 2024 18:34
…dition

When datadogagent service starts it will start in the end dependent services,
including datadog-process-agent and datadog-trace-agent. If at the same time
the datadogagent service is restarted by "agent.exe restart-service" command,
there are a number of edge cases with dependent services concurrent start
and stop which need to be handled explicitly. This PR makes a small change
which addresses some of the edge cases (full fix will follow). Specifically,
it addresses a case when a dependent service is started soon after restart
command starts stopping dependent services. Previously, stopping command
collects only running dependent services, this fix changes code to collect
all dependent services (because stopped service now may be running after
collection finished).
@iglendd iglendd changed the title Len.gamburg/improve agent restart service Improve agent starting and simultaneously restarting concurrency handling Apr 30, 2024
@iglendd iglendd changed the title Improve agent starting and simultaneously restarting concurrency handling Improve agent starting and simultaneously restarting (common for Agent install) concurrency handling Apr 30, 2024
@agent-platform-auto-pr
Copy link
Copy Markdown
Contributor

agent-platform-auto-pr Bot commented Apr 30, 2024

[Fast Unit Tests Report]

On pipeline 33584671 (CI Visibility). The following jobs did not run any unit tests:

Jobs:
  • tests_deb-arm64-py3
  • tests_deb-x64-py3
  • tests_flavor_dogstatsd_deb-x64
  • tests_flavor_heroku_deb-x64
  • tests_flavor_iot_deb-x64
  • tests_rpm-arm64-py3
  • tests_rpm-x64-py3

If you modified Go files and expected unit tests to run in these jobs, please double check the job logs. If you think tests should have been executed reach out to #agent-developer-experience

@pr-commenter
Copy link
Copy Markdown

pr-commenter Bot commented Apr 30, 2024

Test changes on VM

Use this command from test-infra-definitions to manually test this PR changes on a VM:

inv create-vm --pipeline-id=33584671 --os-family=ubuntu

@iglendd iglendd marked this pull request as ready for review April 30, 2024 23:55
@iglendd iglendd requested review from a team as code owners April 30, 2024 23:55
Comment thread releasenotes/notes/improve-agent-start-restart-concurrency-642420923b9ea275.yaml Outdated
Copy link
Copy Markdown
Contributor

@drichards-87 drichards-87 left a comment

Choose a reason for hiding this comment

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

Left you a minor suggestion from Docs and approved the PR.

Comment thread releasenotes/notes/improve-agent-start-restart-concurrency-642420923b9ea275.yaml Outdated
iglendd and others added 2 commits May 3, 2024 15:45
…420923b9ea275.yaml

Co-authored-by: DeForest Richards <56796055+drichards-87@users.noreply.github.com>
@iglendd
Copy link
Copy Markdown
Contributor Author

iglendd commented May 3, 2024

/merge

@dd-devflow
Copy link
Copy Markdown

dd-devflow Bot commented May 3, 2024

🚂 MergeQueue

This merge request is not mergeable yet, because of pending checks/missing approvals. It will be added to the queue as soon as checks pass and/or get approvals.
Note: if you pushed new commits since the last approval, you may need additional approval.
You can remove it from the waiting list with /remove command.

Use /merge -c to cancel this operation!

@pr-commenter
Copy link
Copy Markdown

pr-commenter Bot commented May 3, 2024

Regression Detector

Regression Detector Results

Run ID: 380f03ad-b66c-40f3-82eb-466b82bbf438
Baseline: cba18c7
Comparison: e510fe7

Performance changes are noted in the perf column of each table:

  • ✅ = significantly better comparison variant performance
  • ❌ = significantly worse comparison variant performance
  • ➖ = no significant change in performance

No significant changes in experiment optimization goals

Confidence level: 90.00%
Effect size tolerance: |Δ mean %| ≥ 5.00%

There were no significant changes in experiment optimization goals at this confidence level and effect size tolerance.

Experiments ignored for regressions

Regressions in experiments with settings containing erratic: true are ignored.

perf experiment goal Δ mean % Δ mean % CI
file_to_blackhole % cpu utilization +30.35 [+25.44, +35.26]

Fine details of change detection per experiment

perf experiment goal Δ mean % Δ mean % CI
file_to_blackhole % cpu utilization +30.35 [+25.44, +35.26]
tcp_syslog_to_blackhole ingress throughput +4.80 [-16.97, +26.57]
uds_dogstatsd_to_api_cpu % cpu utilization +1.78 [-1.07, +4.63]
process_agent_standard_check_with_stats memory utilization +0.39 [+0.34, +0.44]
process_agent_standard_check memory utilization +0.37 [+0.32, +0.43]
pycheck_1000_100byte_tags % cpu utilization +0.18 [-4.43, +4.78]
otel_to_otel_logs ingress throughput +0.10 [-0.26, +0.45]
idle memory utilization +0.05 [+0.01, +0.09]
tcp_dd_logs_filter_exclude ingress throughput +0.04 [+0.01, +0.07]
trace_agent_json ingress throughput +0.00 [-0.01, +0.01]
trace_agent_msgpack ingress throughput -0.00 [-0.01, +0.01]
uds_dogstatsd_to_api ingress throughput -0.01 [-0.22, +0.19]
process_agent_real_time_mode memory utilization -0.27 [-0.32, -0.22]
file_tree memory utilization -0.37 [-0.46, -0.28]
basic_py_check % cpu utilization -0.53 [-2.93, +1.88]

Explanation

A regression test is an A/B test of target performance in a repeatable rig, where "performance" is measured as "comparison variant minus baseline variant" for an optimization goal (e.g., ingress throughput). Due to intrinsic variability in measuring that goal, we can only estimate its mean value for each experiment; we report uncertainty in that value as a 90.00% confidence interval denoted "Δ mean % CI".

For each experiment, we decide whether a change in performance is a "regression" -- a change worth investigating further -- if all of the following criteria are true:

  1. Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.

  2. Its 90.00% confidence interval "Δ mean % CI" does not contain zero, indicating that if our statistical model is accurate, there is at least a 90.00% chance there is a difference in performance between baseline and comparison variants.

  3. Its configuration does not mark it "erratic".

@dd-devflow
Copy link
Copy Markdown

dd-devflow Bot commented May 3, 2024

⚠️ MergeQueue

This merge request was unqueued

If you need support, contact us on Slack #devflow!

@iglendd
Copy link
Copy Markdown
Contributor Author

iglendd commented May 6, 2024

/merge

@dd-devflow
Copy link
Copy Markdown

dd-devflow Bot commented May 6, 2024

🚂 MergeQueue

Pull request added to the queue.

There are 5 builds ahead! (estimated merge in less than 3h)

Use /merge -c to cancel this operation!

@dd-mergequeue dd-mergequeue Bot merged commit 57020d1 into main May 6, 2024
@dd-mergequeue dd-mergequeue Bot deleted the len.gamburg/improve-agent-restart-service branch May 6, 2024 14:39
iglendd added a commit that referenced this pull request May 8, 2024
It is the second and final part of the solution based on the first part
partial solution (#25282).
This PR is based on the same testing framework and its major
contribution is detecting various parts of the race condition mostly
based on the fact that ControlService with SERVICE_CONTROL_STOP code
would fail if any dependent service is running. Accordingly, second
or more iterations to stop dependent services will be required, unless
total timeout is exceeded.

This will make Agent restart-service robust enough to handle race
conditions when the Agent is just starting, but starting concurrently
to the Agent restarting via Agent CLI command. This will not help
Powershell restart-service cmdlet which would have non insignificant
chances to fail if by the time core agent service is about to stop
one of its dependent services is concurrently started just before
attempting to stop main service.

In addition, a modest refactoring had been done to remove duplicate
calls and reuse more recent Go runtime svc.ListDependentServices()
function instead of handcrafted version.
iglendd added a commit that referenced this pull request May 8, 2024
…25453)

* WINA-747 Complete solution to solve startup/shutdown race con…dition

It is the second and final part of the solution based on the first part
partial solution (#25282).
This PR is based on the same testing framework and its major
contribution is detecting various parts of the race condition mostly
based on the fact that ControlService with SERVICE_CONTROL_STOP code
would fail if any dependent service is running. Accordingly, second
or more iterations to stop dependent services will be required, unless
total timeout is exceeded.

This will make Agent restart-service robust enough to handle race
conditions when the Agent is just starting, but starting concurrently
to the Agent restarting via Agent CLI command. This will not help
Powershell restart-service cmdlet which would have non insignificant
chances to fail if by the time core agent service is about to stop
one of its dependent services is concurrently started just before
attempting to stop main service.

In addition, a modest refactoring had been done to remove duplicate
calls and reuse more recent Go runtime svc.ListDependentServices()
function instead of handcrafted version.
alexgallotta pushed a commit that referenced this pull request May 9, 2024
…t install) concurrency handling (#25282)

* WINA-747 Partially addressing agent service startup/shutdown race condition

When datadogagent service starts it will start in the end dependent services,
including datadog-process-agent and datadog-trace-agent. If at the same time
the datadogagent service is restarted by "agent.exe restart-service" command,
there are a number of edge cases with dependent services concurrent start
and stop which need to be handled explicitly. This PR makes a small change
which addresses some of the edge cases (full fix will follow). Specifically,
it addresses a case when a dependent service is started soon after restart
command starts stopping dependent services. Previously, stopping command
collects only running dependent services, this fix changes code to collect
all dependent services (because stopped service now may be running after
collection finished).

* Added scaffolding to deterministically test handling of starting and restarting concurrency

* Add a release note

* Fix lint problems

* Fix lint problem #2

* Update releasenotes/notes/improve-agent-start-restart-concurrency-642420923b9ea275.yaml

Co-authored-by: DeForest Richards <56796055+drichards-87@users.noreply.github.com>

---------

Co-authored-by: DeForest Richards <56796055+drichards-87@users.noreply.github.com>

var status svc.Status
status, err = s.mainSvc.Query()
assert.Nil(s.T(), err, "Main service should restar successfully: %v", err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

💬 suggestion
Using the same error message you use at line 307 can end up in harder time knowing what did not work in case of failure, suggest changing the message here to should query main service with no error

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do, too bad I did not wait for you to review :), thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants