| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // 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, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package logtree |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "strings" |
| 22 | "sync" |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 23 | |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame^] | 24 | "source.monogon.dev/go/logging" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 25 | "source.monogon.dev/osbase/logbuffer" |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 26 | ) |
| 27 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 28 | // LogTree is a tree-shaped logging system. For more information, see the package- |
| 29 | // level documentation. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 30 | type LogTree struct { |
| 31 | // journal is the tree's journal, storing all log data and managing subscribers. |
| 32 | journal *journal |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 33 | // root is the root node of the actual tree of the log tree. The nodes contain per- |
| 34 | // DN configuration options, notably the current verbosity level of that DN. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 35 | root *node |
| 36 | } |
| 37 | |
| 38 | func New() *LogTree { |
| 39 | lt := &LogTree{ |
| 40 | journal: newJournal(), |
| 41 | } |
| 42 | lt.root = newNode(lt, "") |
| 43 | return lt |
| 44 | } |
| 45 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 46 | // node represents a given DN as a discrete 'logger'. It implements the |
| 47 | // LeveledLogger interface for log publishing, entries from which it passes over to |
| 48 | // the logtree's journal. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 49 | type node struct { |
| 50 | // dn is the DN which this node represents (or "" if this is the root node). |
| 51 | dn DN |
| 52 | // tree is the LogTree to which this node belongs. |
| 53 | tree *LogTree |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 54 | // verbosity is the current verbosity level of this DN/node, affecting .V(n) |
| 55 | // LeveledLogger calls |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame^] | 56 | verbosity logging.VerbosityLevel |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 57 | rawLineBuffer *logbuffer.LineBuffer |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 58 | |
| 59 | // mu guards children. |
| 60 | mu sync.Mutex |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 61 | // children is a map of DN-part to a children node in the logtree. A DN-part is a |
| 62 | // string representing a part of the DN between the deliming dots, as returned by |
| 63 | // DN.Path. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 64 | children map[string]*node |
| 65 | } |
| 66 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 67 | // newNode returns a node at a given DN in the LogTree - but doesn't set up the |
| 68 | // LogTree to insert it accordingly. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 69 | func newNode(tree *LogTree, dn DN) *node { |
| 70 | n := &node{ |
| 71 | dn: dn, |
| 72 | tree: tree, |
| 73 | children: make(map[string]*node), |
| 74 | } |
| Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 75 | // TODO(q3k): make this limit configurable. If this happens, or the default (1024) gets changes, max chunk size |
| 76 | // calculations when serving the logs (eg. in NodeDebugService) must reflect this. |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 77 | n.rawLineBuffer = logbuffer.NewLineBuffer(1024, n.logRaw) |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 78 | return n |
| 79 | } |
| 80 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 81 | // nodeByDN returns the LogTree node corresponding to a given DN. If either the |
| 82 | // node or some of its parents do not exist they will be created as needed. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 83 | func (l *LogTree) nodeByDN(dn DN) (*node, error) { |
| 84 | traversal, err := newTraversal(dn) |
| 85 | if err != nil { |
| 86 | return nil, fmt.Errorf("traversal failed: %w", err) |
| 87 | } |
| 88 | return traversal.execute(l.root), nil |
| 89 | } |
| 90 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 91 | // nodeTraversal represents a request to traverse the LogTree in search of a given |
| 92 | // node by DN. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 93 | type nodeTraversal struct { |
| 94 | // want is the DN of the node's that requested to be found. |
| 95 | want DN |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 96 | // current is the path already taken to find the node, in the form of DN parts. It |
| 97 | // starts out as want.Parts() and progresses to become empty as the traversal |
| 98 | // continues. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 99 | current []string |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 100 | // left is the path that's still needed to be taken in order to find the node, in |
| 101 | // the form of DN parts. It starts out empty and progresses to become wants.Parts() |
| 102 | // as the traversal continues. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 103 | left []string |
| 104 | } |
| 105 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 106 | // next adjusts the traversal's current/left slices to the next element of the |
| 107 | // traversal, returns the part that's now being looked for (or "" if the traveral |
| 108 | // is done) and the full DN of the element that's being looked for. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 109 | // |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 110 | // For example, a traversal of foo.bar.baz will cause .next() to return the |
| 111 | // following on each invocation: |
| Tim Windelschmidt | 5e460a9 | 2024-04-11 01:33:09 +0200 | [diff] [blame] | 112 | // - part: foo, full: foo |
| 113 | // - part: bar, full: foo.bar |
| 114 | // - part: baz, full: foo.bar.baz |
| 115 | // - part: "", full: foo.bar.baz |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 116 | func (t *nodeTraversal) next() (part string, full DN) { |
| 117 | if len(t.left) == 0 { |
| 118 | return "", t.want |
| 119 | } |
| 120 | part = t.left[0] |
| 121 | t.current = append(t.current, part) |
| 122 | t.left = t.left[1:] |
| 123 | full = DN(strings.Join(t.current, ".")) |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | // newTraversal returns a nodeTraversal fora a given wanted DN. |
| 128 | func newTraversal(dn DN) (*nodeTraversal, error) { |
| 129 | parts, err := dn.Path() |
| 130 | if err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | return &nodeTraversal{ |
| 134 | want: dn, |
| 135 | left: parts, |
| 136 | }, nil |
| 137 | } |
| 138 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 139 | // execute the traversal in order to find the node. This can only be called once |
| 140 | // per traversal. Nodes will be created within the tree until the target node is |
| 141 | // reached. Existing nodes will be reused. This is effectively an idempotent way of |
| 142 | // accessing a node in the tree based on a traversal. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 143 | func (t *nodeTraversal) execute(n *node) *node { |
| 144 | cur := n |
| 145 | for { |
| 146 | part, full := t.next() |
| 147 | if part == "" { |
| 148 | return cur |
| 149 | } |
| 150 | |
| 151 | mu := &cur.mu |
| 152 | mu.Lock() |
| 153 | if _, ok := cur.children[part]; !ok { |
| Tim Windelschmidt | 5e460a9 | 2024-04-11 01:33:09 +0200 | [diff] [blame] | 154 | cur.children[part] = newNode(n.tree, full) |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 155 | } |
| 156 | cur = cur.children[part] |
| 157 | mu.Unlock() |
| 158 | } |
| 159 | } |