Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/airflow/utils/log/non_caching_file_handler.py: 56%

18 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

1# Licensed to the Apache Software Foundation (ASF) under one 

2# or more contributor license agreements. See the NOTICE file 

3# distributed with this work for additional information 

4# regarding copyright ownership. The ASF licenses this file 

5# to you under the Apache License, Version 2.0 (the 

6# "License"); you may not use this file except in compliance 

7# with the License. You may obtain a copy of the License at 

8# 

9# http://www.apache.org/licenses/LICENSE-2.0 

10# 

11# Unless required by applicable law or agreed to in writing, 

12# software distributed under the License is distributed on an 

13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 

14# KIND, either express or implied. See the License for the 

15# specific language governing permissions and limitations 

16# under the License. 

17from __future__ import annotations 

18 

19import os 

20from logging import FileHandler 

21from logging.handlers import RotatingFileHandler 

22from typing import IO 

23 

24 

25def make_file_io_non_caching(io: IO[str]) -> IO[str]: 

26 try: 

27 fd = io.fileno() 

28 os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED) 

29 except Exception: 

30 # in case either file descriptor cannot be retrieved or fadvise is not available 

31 # we should simply return the wrapper retrieved by FileHandler's open method 

32 # the advice to the kernel is just an advice and if we cannot give it, we won't 

33 pass 

34 return io 

35 

36 

37class NonCachingFileHandler(FileHandler): 

38 """ 

39 This is an extension of the python FileHandler that advises the Kernel to not cache the file 

40 in PageCache when it is written. While there is nothing wrong with such cache (it will be cleaned 

41 when memory is needed), it causes ever-growing memory usage when scheduler is running as it keeps 

42 on writing new log files and the files are not rotated later on. This might lead to confusion 

43 for our users, who are monitoring memory usage of Scheduler - without realising that it is 

44 harmless and expected in this case. 

45 

46 See https://github.com/apache/airflow/issues/14924 

47 

48 Adding the advice to Kernel might help with not generating the cache memory growth in the first place. 

49 """ 

50 

51 def _open(self): 

52 return make_file_io_non_caching(super()._open()) 

53 

54 

55class NonCachingRotatingFileHandler(RotatingFileHandler): 

56 """ 

57 This is an extension of the python RotatingFileHandler that advises the Kernel to not cache the file 

58 in PageCache when it is written. While there is nothing wrong with such cache (it will be cleaned 

59 when memory is needed), it causes ever-growing memory usage when scheduler is running as it keeps 

60 on writing new log files and the files are not rotated later on. This might lead to confusion 

61 for our users, who are monitoring memory usage of Scheduler - without realising that it is 

62 harmless and expected in this case. 

63 

64 See https://github.com/apache/airflow/issues/27065 

65 

66 Adding the advice to Kernel might help with not generating the cache memory growth in the first place. 

67 """ 

68 

69 def _open(self): 

70 return make_file_io_non_caching(super()._open())