AWS console way out of sync with instance state
The AWS console "Task" tab shows ~48 tasks, but instances have only 3. My naive understanding is that the ecs-agent is what the AWS console uses to know what is happening on the instances, hence the query here.
A "docker ps -a" on all the instances shows nothing related to a task mentioned on the console.
I am doing an hourly purge of meta-data ( i.e.: "docker ps -a -q | xargs docker rm" on each instance )to help keep the disk footprint low, prevent docker from locking up completely. I suspect this may be the causing the issue, but how to do that without the console (and hence AWS's notion of memory reservation etc. getting all messed up?
And/Or how to re-sync things?
@aaithal - perhaps you have some comment, relates a bit to the space issue you helped me with in issue #309
You could use the stop-task API from the aws cli or the SDK to stop tasks.
The AWS console "Task" tab shows ~48 tasks
Could you please share the status(Last status and Desired status) of tasks that are not running on the instance?
Both status columns in the console showed up as "Running" . I upgraded the ecs-agent to version 1.8.0 and things look better so far. Thank you for the help.
@aaithal Sorry, forgot to respond to first part of your coment above: Pretty sure I tried something like "ecs stop-task --task ffffffff-uuuu-nnnn-kkkk-yyyyggggoooo" with no success. Choosing "Stop All" from the "Tasks" tab on the "Cluster" page did seem to clear them out though. (task-id changed for fun, and in case it leaks anything of import)
I spoke to soon. @aaithal or anyone else have ideas? Seems when I finally upgraded all 4 instances to ecs-agent 1.8.0 everything broke. This morning I find the console (and aws cli) thinks some 40 tasks are associated with the cluster, 27 running, 13 in pending state. Looking on the actual cluster instances - nothing running at all except the ecs-agent according to docker and ps, and the cpu usage has been flat all night confirming it is not doing much if anything. ;-)
A sample of describe-tasks output:
aws ecs describe-tasks --cluster macc --tasks 010ebe32-99a6-42f3-ac85-61440d6af71f
{
"failures": [],
"tasks": [
{
"taskArn": "arn:aws:ecs:us-east-1:860733554218:task/010ebe32-99a6-42f3-ac85-61440d6af71f",
"overrides": {
"containerOverrides": [
{
"name": "django_mentad"
}
]
},
"lastStatus": "RUNNING",
"containerInstanceArn": "arn:aws:ecs:us-east-1:860733554218:container-instance/54659fea-2db6-43a3-9044-540f1712b13f",
"createdAt": 1455762905.788,
"clusterArn": "arn:aws:ecs:us-east-1:860733554218:cluster/macc",
"startedAt": 1455762907.83,
"desiredStatus": "RUNNING",
"taskDefinitionArn": "arn:aws:ecs:us-east-1:860733554218:task-definition/prod_new_collect_ads_-u_262317968:4",
"containers": [
{
"containerArn": "arn:aws:ecs:us-east-1:860733554218:container/f33092df-3534-4ca2-b2d3-13a53ddbcffd",
"taskArn": "arn:aws:ecs:us-east-1:860733554218:task/010ebe32-99a6-42f3-ac85-61440d6af71f",
"lastStatus": "RUNNING",
"name": "django_mentad",
"networkBindings": []
}
]
}
]
}
and for a PENDING task:
aws ecs describe-tasks --cluster macc --tasks 21629223-5b44-4d2d-bcb3-34fe2a955974
{
"failures": [],
"tasks": [
{
"taskArn": "arn:aws:ecs:us-east-1:860733554218:task/21629223-5b44-4d2d-bcb3-34fe2a955974",
"overrides": {
"containerOverrides": [
{
"name": "django_mentad"
}
]
},
"lastStatus": "PENDING",
"containerInstanceArn": "arn:aws:ecs:us-east-1:860733554218:container-instance/7a6bbe7b-ec41-4f34-ab5c-6e6ad7c2f714",
"createdAt": 1455761670.714,
"clusterArn": "arn:aws:ecs:us-east-1:860733554218:cluster/macc",
"desiredStatus": "RUNNING",
"taskDefinitionArn": "arn:aws:ecs:us-east-1:860733554218:task-definition/prod_new_collect_ads_-u_115973711880080:6",
"containers": [
{
"containerArn": "arn:aws:ecs:us-east-1:860733554218:container/1cfb409d-d38b-4c87-b80b-4f8203d9f393",
"taskArn": "arn:aws:ecs:us-east-1:860733554218:task/21629223-5b44-4d2d-bcb3-34fe2a955974",
"lastStatus": "PENDING",
"name": "django_mentad"
}
]
}
]
}
OK, so the ecs-agent, though still running had lost connectivity with whatever in AWS keeps track of things (ECS Cluster page showed "Agent Connected" "false"). Running "sudo stop ecs; sleep 10; sudo start ecs" on each instance seemed to get things flowing again. Logs I should look at? (was not running agent in DEBUG mode... ECS_LOGFILE is not set) In each instance I found lots of "Error" lines like:
[...]
[email protected]: /var/log/ecs/ecs-agent.log.2016-02-18-05:2016-02-18T05:16:27Z [INFO] Error from tcs; backing off module="tcs handler" err="unexpected EOF"
[email protected]: /var/log/ecs/ecs-agent.log.2016-02-18-11:2016-02-18T11:25:30Z [ERROR] Error getting message from ws backend module="ws client" err="unexpected EOF"
[email protected]: /var/log/ecs/ecs-agent.log.2016-02-18-11:2016-02-18T11:25:30Z [INFO] Error from tcs; backing off module="tcs handler" err="unexpected EOF"
[email protected]: /var/log/ecs/ecs-agent.log.2016-02-18-16:2016-02-18T16:56:33Z [ERROR] Error getting message from ws backend module="ws client" err="unexpected EOF"
[email protected]: /var/log/ecs/ecs-agent.log.2016-02-18-16:2016-02-18T16:56:33Z [INFO] Error from tcs; backing off module="tcs handler" err="unexpected EOF"
[email protected]: /var/log/ecs/ecs-agent.log.2016-02-18-18:2016-02-18T18:42:53Z [CRITICAL] Error saving state before final shutdown module="TerminationHandler" err="Multiple error:
[email protected]: /var/log/ecs/ecs-agent.log.2016-02-18-18:2016-02-18T18:51:25Z [WARN] Error getting cpu stats module="stats" err="No data in the queue" container="&{DockerID:47f2fcd76a16af1974a904fea1c0717813c17c1489a7193557f0477d174c0026}"
Not sure how relevant these are.
I just experienced similar behavior after upgrading my instances to v1.8.0, I set a number of my clusters desiredCount to 0, and the console shows there are no more running tasks, but after logging into the EC2 instances I can clearly see the tasks running, and the only relevant logs are the ones @workybee posted (I also was not running in DEBUG mode). It seems there is a bug in the 1.8.0 version of the agent that is causing a disconnect between what the AWS API has and what is actually running on the instances. I'll try and reproduce this issue in a testing environment with DEBUG on and see if I can't get back to you with additional logs, but this is very troubling...
@workybee Do you have more complete logs? The lines you've posted are not relevant; nothing that mentions tcs relates to task delivery.
@ChrisRut If you're able to reproduce with debug logs, that would be very helpful.
I have managed to duplicate this on my cluster. What I thought was an issue I referenced in https://github.com/aws/amazon-ecs-agent/issues/281, actually looks to be more related to this issue. When a service deploy is initiated, it appears the agent does not stop the running container, however UI shows the new task is in a pending state. If I do nothing more than manually stop and start the ecs agent, the deploy continues as normal. Note I just updated to the latest AMI (2015.09.f) yesterday from 2015.09.e which did not have the issue.
The time line is as follows: 21:17:22 Updated the service (via a cloudformation stack update) 21:27:58 Got tired of wating and manual restarted the ecs agent Deploy finished.
I've attached a debug log to the issue, and have scrubbed most identifying info. If someone on the ECS team wants the raw log so that they can cross reference to my running ECS cluster let me know. ecs-agent.log.2016-02-18-21.txt
@tschutte This looks like the most interesting part to me:
2016-02-18T21:27:58Z [CRITICAL] Error saving state before final shutdown module="TerminationHandler" err="Multiple error:
0: Timed out waiting for TaskEngine to settle"
If this happens again, can you run docker kill -s USR1 ecs-agent before restarting it? It'd also be good to get the Docker daemon logs and anything suspicious in the output of dmesg. It's unclear if what you're seeing is the same as this issue or not.
So after about 1 hour things locked up again with the AWS console thinking many tasks still running, but CPU metrics, docker and raw ps all saying nothing of interest still running. Am now trying to get DEBUG logs. Have added ECS_LOGFILE=/ecs-agent.log to the end of /etc/ecs/ecs.config. When I restarted the ecs-agent ("sudo stop ecs ; sleep 5 ; sudo start ecs" on each instance) I don't seem to get anything into the log file, but all of a sudden things start flowing again. Happy to try other efforts, recommendations? Should I upgrade from 2015.09.e to 2015.09.f?
Can you unset ECS_LOGFILE and just change ECS_LOGLEVEL=debug? Logs should be mounted by default into /var/log/ecs so you should see them there without messing with ECS_LOGFILE.
2015.09.f fixes a bug with the Agent that might help (and also includes the fix for the glibc vulnerability), so I would recommend updating.
After something less than 53 minutes of uptime, things have frozen up in an inconsistent state again. The console thinks there are 8 PENDING tasks, and 48 RUNNING, docker ps, and ps directly on the instances shows nothing except the ecs-agent running. Will upgrade to 2015.09.f and try ECS_LOGLEVEL=debug, post what that produces in an hour or so.
So am getting logs now, haven't seen things lock up yet ;-) Took a while to get new instances properly configured, so still time... will be happy if things keep going for another hour (or at least start to feel better).
This new combo of 2015.09.f and ecs-agent version 1.8.0 running with debug enabled seems to be staying in sync between the AWS console and what is actually happening on the cluster instances. Can hope it keeps going through the night - but I'll not be looking hard till tomorrow. ;-) Thanks for the suggestions today. @samuelkarp
@samuelkarp Thanks. I'll see if I can view one last service update failure and obtain the output of docker kill. Note there was nothing of interest in dmesg or the docker logs other than the standard "GET" messages.
My team is getting frustrated with the frequent failing service updates, so I will also be rolling back to a version of the AMI which is more stable for me. I'll attempt to replicate the issue again in a personal test environment. If you think this issue is not related, I'll move out of this issue and create my own.
@samuelkarp Here is a agent log with the output of the kill command. Note the service update fired at around 14:17:51 according to the ECS UI, but it reported an AGENT error:
service MyService-1OXQ7AOIF1ZZT was unable to place a task because no container instance met all of its requirements. The closest matching container-instance 4ac6193c-8165-409f-9ab3-b769637b2749 encountered error "AGENT". For more information, see the Troubleshooting section.
I finally ran the docker kill at 14:37. Let me know if you want me to continue in this issue or move to a different one. ecs-agent.log.2016-02-19-14.txt
@tschutte Thank you, that output is super helpful. Can you let me know how many containers you had running on this host? (You should be able to see this in the data file located at /var/lib/ecs/data/ecs_agent_data.json.) Here's what I think is happening:
- The
enginepackage inside the Agent maintains state and attempts to prevent concurrency problems by locking the state whenever it's read or modified (this lock). In the output you provided, I see tons of goroutines waiting to acquire a read lock (which indicates that the write lock is held) and residual effects elsewhere (channels waiting to send/receive that are blocked because the engine is waiting on the lock already). - While normally we expect to receive state changes from the Docker daemon via the event stream, we also have a reconciliation mechanism to periodically inspect each of the containers under management and ensure that we have the state correctly (this method). That method acquires the state lock before iterating through all known containers.
- Each time we inspect a container, we expect Docker to return a result within 30 seconds.
- I'm guessing (and I'll keep investigating to confirm) that Docker is responding extremely slowly (potentially taking the full 30 seconds) and thus that method is holding the lock for 30 seconds multiplied by the total number of known containers (this includes new containers, running containers, and exited containers that have not yet been removed).
TL;DR: I think Docker might be responding very slowly and causing the Agent to hang. I will keep investigating to either confirm or deny.
@samuelkarp There are a total of 3 containers on each host, 1 for the ecs agent, and 2 as part of my task definition with a shared volume between them. I think I have also reproduced this issue using a couple of small sample containers that just loop in a while loop. I'm working now to button up the task definition and will upload a test container to docker hub so you can hopefully observe this behavior for yourself. Stay tuned.
I did go digging through some of the older logs in the hour or two proceeding the error and notice something that I think correlates to the stuck lock. I found a case each time where the log entry "Writing docker event to the associated task" was missing the corresponding "Wrote docker event to the associated task" (from docker_task_engine.go line 382).
@tschutte That's 2 in the task definition, but how many times has that task been run on the host? Each time a task is run, it creates new containers. I'm interested in the total number of known containers, which you'll find in that data file. Something like this should show it:
sudo yum -y install jq
sudo cat /var/lib/ecs/data/ecs_agent_data.json | jq '.Data.TaskEngine.Tasks[].Containers[].Name' | wc -l
What you're noting about the log lines is indeed relevant; that's one of the residual effects (channel blocked because there's no active reader) of the lock being held a long time.