From a long running cluster, we found tens of thousands of stale apps still be recovered in NM restart recovery.
After investigating, there are three issues cause app state leak in NM state-store:
1. APPLICATION_LOG_HANDLING_FAILED is not handled with remove App in NMStateStore.
2. APPLICATION_LOG_HANDLING_FAILED event is missing in sent when hit aggregator's doAppLogAggregation() exception case.
3. Only Application in FINISHED status receiving APPLICATION_LOG_FINISHED has transition to remove app in NM state store. Application in other status - like APPLICATION_RESOURCES_CLEANUP will ignore the event and later forget to remove this app from NM state store even after app get finished.