Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Post k8s-events properly from multiple threads (thread-safe) #148

Merged
merged 6 commits into from
Jul 16, 2019

Conversation

nolar
Copy link
Contributor

@nolar nolar commented Jul 14, 2019

Part of an effort to make Kopf-based operators more stable and resilient.

Issue : #142 #136
Related: #128

Description

When k8s-event-posting was moved to a background task (#125), some asyncio-related tasks were made way to complicated (multiple dynamically created loggers) and implemented improperly (non-thread-safe put_nowait() when used across threads).

The non-thread-safeness made some k8s-events not posted as soon as it was possible, but waiting for few seconds until something happens on an asyncio event-loop (because put_nowait() does not wake up the event-loop). And it was failing of debug mode was used.

This has become important since #128 — as not only Kopf's internal routines, but also the sync-handlers (which run in a thread-pool) could log INFO+ messages, which should be posted to k8s.

This PR simplifies the solution to one and only one logger (and therefore one and only one handler), and by posting all k8s-events synchronously but instantly via one and only one function (enqueue()).

In that function (enqueue()), one of the thread-safe methods is used to put an event to a queue: either the same-threaded method (thread-safe by definition), or cross-threaded method (explicitly thread-safe).

Types of Changes

  • Refactor/improvements

@nolar nolar requested a review from samurang87 as a code owner July 14, 2019 17:55
@zincr
Copy link

zincr bot commented Jul 14, 2019

🤖 zincr found 0 problems , 1 warning

ℹ️ Large Commits
✅ Approvals
✅ Specification
✅ Dependency Licensing

Details on how to resolve are provided below


Large Commits

Checks all commits for large additions to a single file. Large commits should be reviewed more carefully for potential copyright and licensing issues

This file contains a substantial change, please review to determine if the change comes from an external source and if there are any copyright or licensing issues to be aware of

@codecov-io
Copy link

codecov-io commented Jul 14, 2019

Codecov Report

Merging #148 into master will decrease coverage by 0.57%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #148      +/-   ##
==========================================
- Coverage   83.55%   82.98%   -0.58%     
==========================================
  Files          30       30              
  Lines        1545     1587      +42     
  Branches      227      232       +5     
==========================================
+ Hits         1291     1317      +26     
- Misses        209      227      +18     
+ Partials       45       43       -2
Impacted Files Coverage Δ
kopf/reactor/handling.py 85.64% <100%> (+0.07%) ⬆️
kopf/engines/posting.py 86.27% <100%> (+3.77%) ⬆️
kopf/engines/logging.py 94.73% <100%> (+2.73%) ⬆️
kopf/reactor/queueing.py 56% <0%> (-8.29%) ⬇️
kopf/testing/runner.py 86.81% <0%> (+5.23%) ⬆️

@nolar nolar added the bug Something isn't working label Jul 15, 2019
else:
# No event-loop or another event-loop - assume another thread.
# Use the cross-thread thread-safe methods. Block until enqueued there.
fut = asyncio.run_coroutine_threadsafe(queue.put(event), loop=loop)

Choose a reason for hiding this comment

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

fut?

Copy link
Contributor Author

@nolar nolar Jul 16, 2019

Choose a reason for hiding this comment

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

Some official docs use that too. E.g. asyncio.

But right, it's better to have normal names. Fixed.

samurang87
samurang87 previously approved these changes Jul 16, 2019
Copy link

@samurang87 samurang87 left a comment

Choose a reason for hiding this comment

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

Much appreciated!

@nolar nolar merged commit e12c27b into zalando-incubator:master Jul 16, 2019
@nolar nolar deleted the log-queues-sync branch July 16, 2019 10:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants