blob: 893eff037c8cd9772753edb6577bd77219149cb2 [file] [log] [blame]
Serge Bazanski5faa2fc2020-09-07 14:09:30 +02001// 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
17package logtree
18
19import (
20 "fmt"
21 "strings"
22 "sync"
23)
24
25// DN is the Distinguished Name, a dot-delimited path used to address loggers within a LogTree. For example, "foo.bar"
26// designates the 'bar' logger node under the 'foo' logger node under the root node of the logger. An empty string is
27// the root node of the tree.
28type DN string
29
Serge Bazanski06d65bc2020-09-24 10:51:59 +020030// Path return the parts of a DN, ie. all the elements of the dot-delimited DN path. For the root node, an empty list
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020031// will be returned. An error will be returned if the DN is invalid (contains empty parts, eg. `foo..bar`, `.foo` or
32// `foo.`.
33func (d DN) Path() ([]string, error) {
34 if d == "" {
35 return nil, nil
36 }
37 parts := strings.Split(string(d), ".")
38 for _, p := range parts {
39 if p == "" {
40 return nil, fmt.Errorf("invalid DN")
41 }
42 }
43 return parts, nil
44}
45
46// journal is the main log recording structure of logtree. It manages linked lists containing the actual log entries,
47// and implements scans across them. It does not understand the hierarchical nature of logtree, and instead sees all
48// entries as part of a global linked list and a local linked list for a given DN.
49//
50// The global linked list is represented by the head/tail pointers in journal and nextGlobal/prevGlobal pointers in
51// entries. The local linked lists are represented by heads[DN]/tails[DN] pointers in journal and nextLocal/prevLocal
52// pointers in entries:
53//
54// .------------. .------------. .------------.
55// | dn: A.B | | dn: Z | | dn: A.B |
56// | time: 1 | | time: 2 | | time: 3 |
57// |------------| |------------| |------------|
58// | nextGlobal :------->| nextGlobal :------->| nextGlobal :--> nil
59// nil <-: prevGlobal |<-------: prevGlobal |<-------| prevGlobal |
60// |------------| |------------| n |------------|
61// | nextLocal :---. n | nextLocal :->i .-->| nextLocal :--> nil
62// nil <-: prevLocal |<--: i<-: prevLocal | l :---| prevLocal |
63// '------------' | l '------------' | '------------'
64// ^ '----------------------' ^
65// | ^ |
66// | | |
67// ( head ) ( tails[Z] ) ( tail )
68// ( heads[A.B] ) ( heads[Z] ) ( tails[A.B] )
69//
70type journal struct {
71 // mu locks the rest of the structure. It must be taken during any operation on the journal.
72 mu sync.RWMutex
73
74 // tail is the side of the global linked list that contains the newest log entry, ie. the one that has been pushed
75 // the most recently. It can be nil when no log entry has yet been pushed. The global linked list contains all log
76 // entries pushed to the journal.
77 tail *entry
78 // head is the side of the global linked list that contains the oldest log entry. It can be nil when no log entry
79 // has yet been pushed.
80 head *entry
81
82 // tails are the tail sides of a local linked list for a given DN, ie. the sides that contain the newest entry. They
83 // are nil if there are no log entries for that DN.
84 tails map[DN]*entry
85 // heads are the head sides of a local linked list for a given DN, ie. the sides that contain the oldest entry. They
86 // are nil if there are no log entries for that DN.
87 heads map[DN]*entry
88
89 // quota is a map from DN to quota structure, representing the quota policy of a particular DN-designated logger.
90 quota map[DN]*quota
91
92 // subscribers are observer to logs. New log entries get emitted to channels present in the subscriber structure,
93 // after filtering them through subscriber-provided filters (eg. to limit events to subtrees that interest that
94 // particular subscriber).
95 subscribers []*subscriber
96}
97
98// newJournal creates a new empty journal. All journals are independent from eachother, and as such, all LogTrees are
99// also independent.
100func newJournal() *journal {
101 return &journal{
102 tails: make(map[DN]*entry),
103 heads: make(map[DN]*entry),
104
105 quota: make(map[DN]*quota),
106 }
107}
108
Serge Bazanskif68153c2020-10-26 13:54:37 +0100109// filter is a predicate that returns true if a log subscriber or reader is interested in a given log entry.
110type filter func(*entry) bool
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200111
Serge Bazanskif68153c2020-10-26 13:54:37 +0100112// filterAll returns a filter that accepts all log entries.
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200113func filterAll() filter {
Serge Bazanskif68153c2020-10-26 13:54:37 +0100114 return func(*entry) bool { return true }
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200115}
116
117// filterExact returns a filter that accepts only log entries at a given exact DN. This filter should not be used in
118// conjunction with journal.scanEntries - instead, journal.getEntries should be used, as it is much faster.
119func filterExact(dn DN) filter {
Serge Bazanskif68153c2020-10-26 13:54:37 +0100120 return func(e *entry) bool {
121 return e.origin == dn
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200122 }
123}
124
125// filterSubtree returns a filter that accepts all log entries at a given DN and sub-DNs. For example, filterSubtree at
126// "foo.bar" would allow entries at "foo.bar", "foo.bar.baz", but not "foo" or "foo.barr".
127func filterSubtree(root DN) filter {
128 if root == "" {
129 return filterAll()
130 }
131
132 rootParts := strings.Split(string(root), ".")
Serge Bazanskif68153c2020-10-26 13:54:37 +0100133 return func(e *entry) bool {
134 parts := strings.Split(string(e.origin), ".")
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200135 if len(parts) < len(rootParts) {
136 return false
137 }
138
139 for i, p := range rootParts {
140 if parts[i] != p {
141 return false
142 }
143 }
144
145 return true
146 }
147}
148
149// filterSeverity returns a filter that accepts log entries at a given severity level or above. See the Severity type
150// for more information about severity levels.
151func filterSeverity(atLeast Severity) filter {
Serge Bazanskif68153c2020-10-26 13:54:37 +0100152 return func(e *entry) bool {
153 return e.leveled != nil && e.leveled.severity.AtLeast(atLeast)
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200154 }
155}
156
Serge Bazanskif68153c2020-10-26 13:54:37 +0100157func filterOnlyRaw(e *entry) bool {
158 return e.raw != nil
159}
160
161func filterOnlyLeveled(e *entry) bool {
162 return e.leveled != nil
163}
164
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200165// scanEntries does a linear scan through the global entry list and returns all entries that match the given filters. If
166// retrieving entries for an exact event, getEntries should be used instead, as it will leverage DN-local linked lists
167// to retrieve them faster.
168// journal.mu must be taken at R or RW level when calling this function.
169func (j *journal) scanEntries(filters ...filter) (res []*entry) {
170 cur := j.tail
171 for {
172 if cur == nil {
173 return
174 }
175
176 passed := true
177 for _, filter := range filters {
Serge Bazanskif68153c2020-10-26 13:54:37 +0100178 if !filter(cur) {
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200179 passed = false
180 break
181 }
182 }
183 if passed {
184 res = append(res, cur)
185 }
186 cur = cur.nextGlobal
187 }
188}
189
Serge Bazanski06d65bc2020-09-24 10:51:59 +0200190// getEntries returns all entries at a given DN. This is faster than a scanEntries(filterExact), as it uses the special
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200191// local linked list pointers to traverse the journal. Additional filters can be passed to further limit the entries
192// returned, but a scan through this DN's local linked list will be performed regardless.
193// journal.mu must be taken at R or RW level when calling this function.
194func (j *journal) getEntries(exact DN, filters ...filter) (res []*entry) {
195 cur := j.tails[exact]
196 for {
197 if cur == nil {
198 return
199 }
200
201 passed := true
202 for _, filter := range filters {
Serge Bazanskif68153c2020-10-26 13:54:37 +0100203 if !filter(cur) {
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200204 passed = false
205 break
206 }
207 }
208 if passed {
209 res = append(res, cur)
210 }
211 cur = cur.nextLocal
212 }
213
214}