GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Overhaul logging background thread transport by theacodes · Pull Request #3407 · googleapis/google-cloud-python

Conversation

@theacodes

@theacodes theacodes commented May 12, 2017

Copy link
Copy Markdown
Contributor

No description provided.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Jon Wayne Parrott
Overhaul logging background thread transport
8d4a07d
@theacodes theacodes added the api: logging Issues related to the Cloud Logging API. label May 12, 2017
@theacodes
theacodes requested review from dhermes, lukesneeringer and waprin May 12, 2017 04:36
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 12, 2017
Jon Wayne Parrott added 2 commits May 11, 2017 21:38
Jon Wayne Parrott
Remove stray print
3c94f68
Jon Wayne Parrott
Remove now-unused import
3462c13
duggelz
duggelz reviewed May 12, 2017
View reviewed changes
Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated

This class reuses a single :class:`Batch` method to write successive
entries.
:type max: int

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

duggelz
duggelz reviewed May 12, 2017
View reviewed changes
Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
from google.cloud.logging.handlers.transports.base import Transport

_WORKER_THREAD_NAME = 'google.cloud.logging.handlers.transport.Worker'
_DEFAULT_GRACE_PERIOD = 5.0

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

duggelz
duggelz reviewed May 12, 2017
View reviewed changes
Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
"""
self._cloud_logger = cloud_logger
self._grace_period = grace_period
self._batch_size = batch_size

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jon Wayne Parrott
Address dgreiman's comments
40626e3
dhermes
dhermes reviewed May 12, 2017
View reviewed changes
Comment thread logging/google/cloud/logging/handlers/handlers.py
DEFAULT_LOGGER_NAME = 'python'

EXCLUDED_LOGGER_DEFAULTS = ('google.cloud', 'oauth2client')
EXCLUDED_LOGGER_DEFAULTS = (

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py
_DEFAULT_GRACE_PERIOD = 5.0 # Seconds
_DEFAULT_MAX_BATCH_SIZE = 10
_WORKER_THREAD_NAME = 'google.cloud.logging.Worker'
_WORKER_TERMINATOR = object()

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated

class _Worker(object):
"""A threaded worker that writes batches of log entries
def _get_many(q, max_items=None):

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
def _get_many(q, max_items=None):
"""Get multiple items from a Queue.

Gets at least one (blocking) and at most :param:`max_items` items

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
(non-blocking) from a given Queue. Does not mark the items as done.

Writes entries to the logger API.
:type q: ~queue.Queue

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated

transport, worker = self._make_one(client, name)

logger = worker.call_args[0][0]

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py


class TestWorker(unittest.TestCase):
NAME = 'python_logger'

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated
def _make_one_with_mock_thread(self, *args, **kw):
with mock.patch('threading.Thread', new=_Thread):
with mock.patch('atexit.register') as atexit_mock:
self.atexit_mock = atexit_mock

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated
worker = self._make_one(logger)
self.assertTrue(worker.is_alive)
self.assertIsNotNone(worker._thread)
self.assertTrue(worker._thread._daemon)

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated

self.assertFalse(worker.is_alive)

def _enqueue_record(self, worker, message):

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jon Wayne Parrott
Address dhermes review
b373f5e
theacodes
theacodes commented May 12, 2017
View reviewed changes

@theacodes theacodes left a comment

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@dhermes ready for another round.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread logging/google/cloud/logging/handlers/handlers.py
DEFAULT_LOGGER_NAME = 'python'

EXCLUDED_LOGGER_DEFAULTS = ('google.cloud', 'oauth2client')
EXCLUDED_LOGGER_DEFAULTS = (

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py
@@ -19,137 +19,224 @@

import atexit

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated

class _Worker(object):
"""A threaded worker that writes batches of log entries
def _get_many(q, max_items=None):

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
def _get_many(q, max_items=None):
"""Get multiple items from a Queue.

Gets at least one (blocking) and at most :param:`max_items` items

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
(non-blocking) from a given Queue. Does not mark the items as done.

Writes entries to the logger API.
:type q: ~queue.Queue

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated

transport, worker = self._make_one(client, name)

logger = worker.call_args[0][0]

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py


class TestWorker(unittest.TestCase):
NAME = 'python_logger'

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated
def _make_one_with_mock_thread(self, *args, **kw):
with mock.patch('threading.Thread', new=_Thread):
with mock.patch('atexit.register') as atexit_mock:
self.atexit_mock = atexit_mock

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated
worker = self._make_one(logger)
self.assertTrue(worker.is_alive)
self.assertIsNotNone(worker._thread)
self.assertTrue(worker._thread._daemon)

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated

self.assertFalse(worker.is_alive)

def _enqueue_record(self, worker, message):

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@waprin

waprin commented May 12, 2017

Copy link
Copy Markdown
Contributor

So the excluded loggers change, particularly oath2->google.auth looks very important (maybe the simple explanation for the deadlock). In fact now I'm highly highly highly suspicious that's the root cause of these deadlocks popping up so that should get merged asap.

Just as a tangent, I think the most useful change to make would be to automatically detect recursive logging traces so that type of breakage is impossible. I think Jon gave me some ideas on how that might be possible with logging filters but I haven't dug deep on it yet.

As for switching from the conditions to the queue, I am 99% indifferent but 1% 👎 , I thought about doing it that way but removing all the items from the Queue at once seemed like you might as well just use the Batch entries list, instead of having to basically copy the whole list over every time you commit which seems awkward to me. But obviously you found the existing code ugly and it's mostly a stylistic judgement call so it's up to you and other maintainers, like I said largely indifferent one way or the other since it's more or less accomplishing the same thing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@theacodes

theacodes commented May 12, 2017

Copy link
Copy Markdown
Contributor Author

As for switching from the conditions to the queue, I am 99% indifferent but 1% 👎 ... But obviously you found the existing code ugly and it's mostly a stylistic judgement call so it's up to you and other maintainers, like I said largely indifferent one way or the other since it's more or less accomplishing the same thing.

My reasoning for doing this wasn't that the code was ugly. I switched to the queue pattern because it's widely deployment, widely tested, well-understood, and easy to reason about. It gets us closer to shared-nothing which is ideal when working with concurrency. I did not like the idea of using locks/conditions/mutexes to patch around a non-thread-safe data structure.

I thought about doing it that way but removing all the items from the Queue at once seemed like you might as well just use the Batch entries list, instead of having to basically copy the whole list over every time you commit which seems awkward to me

We're not draining the entire queue each time, just a subset. And I'm less worried about copying the list because Python essentially just passes around lists of pointers in this case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dhermes
dhermes reviewed May 12, 2017
View reviewed changes
Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
max_batch_size=_DEFAULT_MAX_BATCH_SIZE):
"""
The background thread is started automatically.
:type cloud_logger: ~google.cloud.logging.logger.Logger

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py
self._grace_period = grace_period
self._max_batch_size = max_batch_size
self._queue = queue.Queue(-1)
self._queue = queue.Queue(0)

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py

self._safely_commit_batch(batch)

for _ in range(len(items)):

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
"""
:type client: ~google.cloud.logging.client.Client
:param client: The Logging client.
:type client: ~google.cloud.logging.client.Client

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/google/cloud/logging/handlers/transports/background_thread.py Outdated
background thread.
def __init__(self, client, name, grace_period=_DEFAULT_GRACE_PERIOD,
batch_size=_DEFAULT_MAX_BATCH_SIZE):
"""

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread logging/tests/unit/handlers/transports/test_background_thread.py Outdated
with mock.patch('atexit.register') as atexit_mock:
self.atexit_mock = atexit_mock
return self._make_one(*args, **kw)
return worker.start()

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jon Wayne Parrott
Address dhermes review round 2
b73ecf4
@dhermes

dhermes commented May 12, 2017

Copy link
Copy Markdown
Contributor

LGTM

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@theacodes
theacodes merged commit a623929 into googleapis:master May 12, 2017
@theacodes
theacodes deleted the new-background-thread branch May 12, 2017 20:30
@tseaver tseaver mentioned this pull request Jun 26, 2017
Prep logging-1.1.0 release. #3528
Merged
@waprin waprin mentioned this pull request Jan 5, 2018
Failed to submit <n> logs #4346
Closed
parthea pushed a commit that referenced this pull request Nov 24, 2025
Jon Wayne Parrott
Overhaul logging background thread transport (#3407)
ae57a13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lukesneeringer lukesneeringer Awaiting requested review from lukesneeringer
@waprin waprin Awaiting requested review from waprin
2 more reviewers
@dhermes dhermes dhermes left review comments
@duggelz duggelz duggelz left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

api: logging Issues related to the Cloud Logging API. cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@theacodes @waprin @dhermes @duggelz @googlebot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.