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 An extension of FileHandler, advises the Kernel to not cache the file in PageCache when it is written.
40
41 While there is nothing wrong with such cache (it will be cleaned when memory is needed), it
42 causes ever-growing memory usage when scheduler is running as it keeps on writing new log
43 files and the files are not rotated later on. This might lead to confusion for our users,
44 who are monitoring memory usage of Scheduler - without realising that it is harmless and
45 expected in this case.
46
47 See https://github.com/apache/airflow/issues/14924
48
49 Adding the advice to Kernel might help with not generating the cache memory growth in the first place.
50 """
51
52 def _open(self):
53 return make_file_io_non_caching(super()._open())
54
55
56class NonCachingRotatingFileHandler(RotatingFileHandler):
57 """
58 An extension of RotatingFileHandler, advises the Kernel to not cache the file in PageCache when written.
59
60 While there is nothing wrong with such cache (it will be cleaned when memory is needed), it
61 causes ever-growing memory usage when scheduler is running as it keeps on writing new log
62 files and the files are not rotated later on. This might lead to confusion for our users,
63 who are monitoring memory usage of Scheduler - without realising that it is harmless and
64 expected in this case.
65
66 See https://github.com/apache/airflow/issues/27065
67
68 Adding the advice to Kernel might help with not generating the cache memory growth in the first place.
69 """
70
71 def _open(self):
72 return make_file_io_non_caching(super()._open())