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

vfkit: More robust state management #20506

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

nirs
Copy link
Contributor

@nirs nirs commented Mar 9, 2025

We get and set vfkit state using the HTTP API. This is not very robust since the API is provided by the process we are terminating:

  • Setting the state to HardStop typically fails when vfkit is terminated, leading to unneeded retries and delays.
  • Setting the state to Stop may fail if vfkit is already stopped or shutting down, or does not listen to the socket.
  • Getting the state is racy, since vfkit may be shutting down or terminate right after checking the pid (time of check, time of use).
  • The pidfile was not removed in all cases, or removed too late.

This change fixes the issues by using vfkit API only for setting state, and falling back to process package pid management functions if the API fails.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 9, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: nirs
Once this PR has been reviewed and has the lgtm label, please assign spowelljr for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 9, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @nirs. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 9, 2025
@minikube-bot
Copy link
Collaborator

Can one of the admins verify this patch?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 10, 2025
@nirs nirs force-pushed the vfkit-state-fixes branch from a23597f to 91bd6f2 Compare March 10, 2025 23:21
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 10, 2025
@nirs nirs mentioned this pull request Mar 14, 2025
Current code contains multiple implementations for managing a process
using pids, with various issues:

- Some are unsafe, terminating a process by pid without validating that
  the pid belongs to the right process. Some use unclear
- Using unclear terms like checkPid() (what does it mean?)
- Some are missing tests

Let's clean up the mess by introducing a process package. The package
provides:

- process.WritePidfile(): write a pid to file
- process.ReadPidfile(): read pid from file
- process.Exists(): tells if process matching pid and name exists
- process.Terminate() terminates a process matching pid and name
- process.Kil() kill a process matching pid and name

The library is tested on linux, darwin, and windows. On windows we don't
have a standard way to terminate a process gracefully, so
process.Terminate() is the same as process.Kill().

I want to use this package in vfkit and the new vment package, and later
we can use it for qemu, hyperkit, and other code using managing
processes with pids.
@nirs nirs force-pushed the vfkit-state-fixes branch from 91bd6f2 to 7106520 Compare March 15, 2025 23:44
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 15, 2025
nirs added 2 commits March 16, 2025 02:08
- Simplify GetState() using process.ReadPidfile()
- Simplify Start() using process.WritePidfile()
GetState() had several issues:

- When accessing vfkit HTTP API, we handled only "running",
  "VirtualMachineStateRunning", "stopped", and
  "VirtualMachineStateStopped", but there are other 10 possible states,
  which we handled as state.None, when vfkit is running and need to be
  stopped. This can lead to wrong handling in the caller.

- When handling "stopped" and "VirtualMachineStateStopped" we returned
  state.Stopped, but did not remove the pidfile. This can cause
  termination of unrelated process or reporting wrong status when the
  pid is reused.

- Accessing the HTTP API will fail after we stop or kill it. This
  cause GetState() to fail when the process is actually stopped, which
  can lead to unnecessary retries and long delays (kubernetes#20503).

- When retuning state.None during Remove(), we use tried to do graceful
  shutdown which does not make sense in minikube delete flow, and is not
  consistent with state.Running handling.

Accessing vfkit API to check for state does not add much value for our
used case, checking if the vfkit process is running, and it is not
reliable.

Fix all the issues by not using the HTTP API in GetState(), and use only
the process state. We still use the API for stopping and killing vfkit
to do graceful shutdown. This also simplifies Remove(), since we need to
handle only the state.Running state.

With this change we consider vfkit as stopped only when the process does
not exist, which takes about 3 seconds after the state is reported as
"stopped".

Example stop flow:

    I0309 18:15:40.260249   18857 main.go:141] libmachine: Stopping "minikube"...
    I0309 18:15:40.263225   18857 main.go:141] libmachine: set state: {State:Stop}
    I0309 18:15:46.266902   18857 main.go:141] libmachine: Machine "minikube" was stopped.
    I0309 18:15:46.267122   18857 stop.go:75] duration metric: took 6.127761459s to stop

Example delete flow:

    I0309 17:00:49.483078   18127 out.go:177] * Deleting "minikube" in vfkit ...
    I0309 17:00:49.499252   18127 main.go:141] libmachine: set state: {State:HardStop}
    I0309 17:00:49.569938   18127 lock.go:35] WriteFile acquiring /Users/nir/.kube/config: ...
    I0309 17:00:49.573977   18127 out.go:177] * Removed all traces of the "minikube" cluster.
@nirs nirs force-pushed the vfkit-state-fixes branch from 7106520 to 57dfb92 Compare March 16, 2025 00:15
@nirs nirs marked this pull request as ready for review March 20, 2025 21:19
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 20, 2025
@k8s-ci-robot k8s-ci-robot requested review from medyagh and prezha March 20, 2025 21:19
@nirs nirs requested a review from cfergeau March 20, 2025 21:21
@nirs
Copy link
Contributor Author

nirs commented Mar 20, 2025

@medyagh I think this version addresses all the comments. It includes the new process package added in #20528.

@medyagh
Copy link
Member

medyagh commented Mar 21, 2025

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 21, 2025
@minikube-pr-bot

This comment has been minimized.

@minikube-pr-bot

This comment has been minimized.

nirs added 3 commits March 22, 2025 15:13
Previously we did not check the process name when checking a pid from a
pidfile. If the pidfile became state we would assume that vfkit is
running and try to stop it via the HTTP API, which would never succeed.
Now we detect stale pidfile and remove it.

If removing the stale pidfile fails, we don't want to fail the operation
since we know that vfkit is not running. We log the failure to aid
debugging of stale pidfile.
If setting vfkit state to "Stop" fails, we used to return an error.
Retrying the operation may never succeed.

Fix by falling back to terminating vfkit using a signal. This terminates
vfkit immediately similar to HardStop[1].

We can still fail if the pidfile is corrupted but this is unlikely and
requires manual cleanup.

In the case when we are sure the vfkit process does not exist, we remove
the pidfile immediately, avoiding leftover pidfile if the caller does
not call GetState() after Stop().

[1] crc-org/vfkit#284
We know that setting the state to `HardStop` typically fails:

    I0309 19:19:42.378591   21795 out.go:177] 🔥  Deleting "minikube" in vfkit ...
    W0309 19:19:42.397472   21795 delete.go:106] remove failed, will retry: kill: Post "http://_/vm/state": EOF

This may lead to unnecessary retries and delays. Fix by falling back to
sending a SIGKILL signal.

Example delete flow when setting vfkit state fails:

    I0309 20:07:41.688259   25540 out.go:177] 🔥  Deleting "minikube" in vfkit ...
    I0309 20:07:41.712017   25540 main.go:141] libmachine: Failed to set vfkit state to 'HardStop': Post "http://_/vm/state": EOF
@nirs nirs force-pushed the vfkit-state-fixes branch from 57dfb92 to 6319ed1 Compare March 22, 2025 13:15
@nirs
Copy link
Contributor Author

nirs commented Mar 22, 2025

@medyagh current version addresses all the comments and should be ready for merge.

@nirs nirs requested a review from medyagh March 22, 2025 13:18
@minikube-pr-bot
Copy link

kvm2 driver with docker runtime

+----------------+----------+---------------------+
|    COMMAND     | MINIKUBE | MINIKUBE (PR 20506) |
+----------------+----------+---------------------+
| minikube start | 51.1s    | 50.8s               |
| enable ingress | 16.6s    | 15.3s               |
+----------------+----------+---------------------+

Times for minikube start: 50.9s 51.4s 50.8s 49.6s 53.0s
Times for minikube (PR 20506) start: 51.8s 51.0s 48.3s 52.7s 50.0s

Times for minikube ingress: 14.5s 19.0s 16.0s 18.5s 14.9s
Times for minikube (PR 20506) ingress: 15.0s 15.0s 15.0s 16.9s 14.5s

docker driver with docker runtime

+----------------+----------+---------------------+
|    COMMAND     | MINIKUBE | MINIKUBE (PR 20506) |
+----------------+----------+---------------------+
| minikube start | 21.2s    | 22.7s               |
| enable ingress | 12.4s    | 12.6s               |
+----------------+----------+---------------------+

Times for minikube start: 20.9s 21.2s 21.2s 21.0s 21.6s
Times for minikube (PR 20506) start: 21.9s 23.3s 24.4s 22.9s 21.2s

Times for minikube ingress: 12.8s 12.7s 12.7s 12.3s 11.3s
Times for minikube (PR 20506) ingress: 12.2s 12.2s 13.3s 12.2s 12.8s

docker driver with containerd runtime

+----------------+----------+---------------------+
|    COMMAND     | MINIKUBE | MINIKUBE (PR 20506) |
+----------------+----------+---------------------+
| minikube start | 20.9s    | 21.0s               |
| enable ingress | 30.9s    | 32.7s               |
+----------------+----------+---------------------+

Times for minikube start: 19.5s 22.2s 22.5s 19.9s 20.5s
Times for minikube (PR 20506) start: 19.8s 20.5s 20.1s 21.7s 22.9s

Times for minikube ingress: 39.2s 22.7s 30.2s 23.3s 39.2s
Times for minikube (PR 20506) ingress: 38.7s 39.3s 22.7s 39.8s 22.7s

@minikube-pr-bot
Copy link

Here are the number of top 10 failed tests in each environments with lowest flake rate.

Environment Test Name Flake Rate
Docker_Windows (2 failed) TestErrorSpam/setup(gopogh) Unknown
Docker_Windows (2 failed) TestPause/serial/VerifyDeletedResources(gopogh) Unknown
Docker_Linux (8 failed) TestFunctional/parallel/MySQL(gopogh) 0.00% (chart)
Docker_Linux (8 failed) TestFunctional/parallel/TunnelCmd/serial/WaitService/Setup(gopogh) 0.00% (chart)
Docker_Linux (8 failed) TestFunctional/parallel/TunnelCmd/serial/AccessDirect(gopogh) 0.00% (chart)
Docker_Linux (8 failed) TestMultiControlPlane/serial/DeployApp(gopogh) 0.00% (chart)
Docker_Linux (8 failed) TestMultiControlPlane/serial/PingHostFromPods(gopogh) 0.00% (chart)
Docker_Linux (8 failed) TestMultiNode/serial/DeployApp2Nodes(gopogh) 0.00% (chart)
Docker_Linux (8 failed) TestMultiNode/serial/PingHostFrom2Pods(gopogh) 0.00% (chart)
Docker_Linux (8 failed) TestSkaffold(gopogh) 0.00% (chart)
Docker_Linux_docker_arm64 (6 failed) TestAddons/parallel/Ingress(gopogh) 0.00% (chart)
Docker_Linux_docker_arm64 (6 failed) TestFunctional/parallel/PersistentVolumeClaim(gopogh) 0.00% (chart)
Docker_Linux_docker_arm64 (6 failed) TestMultiControlPlane/serial/StartCluster(gopogh) 0.00% (chart)
Docker_Linux_docker_arm64 (6 failed) TestMultiControlPlane/serial/AddWorkerNode(gopogh) 0.00% (chart)
Docker_Linux_docker_arm64 (6 failed) TestMultiControlPlane/serial/DeployApp(gopogh) 2.04% (chart)
Docker_Linux_docker_arm64 (6 failed) TestMultiControlPlane/serial/PingHostFromPods(gopogh) 2.04% (chart)

Besides the following environments also have failed tests:

  • Docker_Linux_containerd_arm64: 1 failed (gopogh)

  • Docker_Cloud_Shell: 3 failed (gopogh)

  • Docker_Linux_crio: 1 failed (gopogh)

  • Docker_Linux_crio_arm64: 28 failed (gopogh)

  • KVM_Linux_crio: 10 failed (gopogh)

To see the flake rates of all tests by environment, click here.

@nirs
Copy link
Contributor Author

nirs commented Mar 28, 2025

@medyagh what is the next step here? are the failed test real failure or test flakes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants