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 | |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 19 | import ( |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 20 | "errors" |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 21 | "sync/atomic" |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 22 | ) |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 23 | |
| 24 | // LogReadOption describes options for the LogTree.Read call. |
| 25 | type LogReadOption struct { |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 26 | withChildren bool |
| 27 | withStream bool |
| 28 | withBacklog int |
| 29 | onlyLeveled bool |
| 30 | onlyRaw bool |
| 31 | leveledWithMinimumSeverity Severity |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 34 | // WithChildren makes Read return/stream data for both a given DN and all its |
| 35 | // children. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 36 | func WithChildren() LogReadOption { return LogReadOption{withChildren: true} } |
| 37 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 38 | // WithStream makes Read return a stream of data. This works alongside WithBacklog |
| 39 | // to create a read-and-stream construct. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 40 | func WithStream() LogReadOption { return LogReadOption{withStream: true} } |
| 41 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 42 | // WithBacklog makes Read return already recorded log entries, up to count |
| 43 | // elements. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 44 | func WithBacklog(count int) LogReadOption { return LogReadOption{withBacklog: count} } |
| 45 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 46 | // BacklogAllAvailable makes WithBacklog return all backlogged log data that |
| 47 | // logtree possesses. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 48 | const BacklogAllAvailable int = -1 |
| 49 | |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 50 | func OnlyRaw() LogReadOption { return LogReadOption{onlyRaw: true} } |
| 51 | |
| 52 | func OnlyLeveled() LogReadOption { return LogReadOption{onlyLeveled: true} } |
| 53 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 54 | // LeveledWithMinimumSeverity makes Read return only log entries that are at least |
| 55 | // at a given Severity. If only leveled entries are needed, OnlyLeveled must be |
| 56 | // used. This is a no-op when OnlyRaw is used. |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 57 | func LeveledWithMinimumSeverity(s Severity) LogReadOption { |
| 58 | return LogReadOption{leveledWithMinimumSeverity: s} |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 61 | // LogReader permits reading an already existing backlog of log entries and to |
| 62 | // stream further ones. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 63 | type LogReader struct { |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 64 | // Backlog are the log entries already logged by LogTree. This will only be set if |
| 65 | // WithBacklog has been passed to Read. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 66 | Backlog []*LogEntry |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 67 | // Stream is a channel of new entries as received live by LogTree. This will only |
| 68 | // be set if WithStream has been passed to Read. In this case, entries from this |
| 69 | // channel must be read as fast as possible by the consumer in order to prevent |
| 70 | // missing entries. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 71 | Stream <-chan *LogEntry |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 72 | // done is channel used to signal (by closing) that the log consumer is not |
| 73 | // interested in more Stream data. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 74 | done chan<- struct{} |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 75 | // missed is an atomic integer pointer that tells the subscriber how many messages |
| 76 | // in Stream they missed. This pointer is nil if no streaming has been requested. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 77 | missed *uint64 |
| 78 | } |
| 79 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 80 | // Missed returns the amount of entries that were missed from Stream (as the |
| 81 | // channel was not drained fast enough). |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 82 | func (l *LogReader) Missed() uint64 { |
| 83 | // No Stream. |
| 84 | if l.missed == nil { |
| 85 | return 0 |
| 86 | } |
| 87 | return atomic.LoadUint64(l.missed) |
| 88 | } |
| 89 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 90 | // Close closes the LogReader's Stream. This must be called once the Reader does |
| 91 | // not wish to receive streaming messages anymore. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 92 | func (l *LogReader) Close() { |
| 93 | if l.done != nil { |
| 94 | close(l.done) |
| 95 | } |
| 96 | } |
| 97 | |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 98 | var ( |
| 99 | ErrRawAndLeveled = errors.New("cannot return logs that are simultaneously OnlyRaw and OnlyLeveled") |
| 100 | ) |
| 101 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 102 | // Read and/or stream entries from a LogTree. The returned LogReader is influenced |
| 103 | // by the LogReadOptions passed, which influence whether the Read will return |
| 104 | // existing entries, a stream, or both. In addition the options also dictate |
| 105 | // whether only entries for that particular DN are returned, or for all sub-DNs as |
| 106 | // well. |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 107 | func (l *LogTree) Read(dn DN, opts ...LogReadOption) (*LogReader, error) { |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 108 | l.journal.mu.RLock() |
| 109 | defer l.journal.mu.RUnlock() |
| 110 | |
| 111 | var backlog int |
| 112 | var stream bool |
| 113 | var recursive bool |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 114 | var leveledSeverity Severity |
| 115 | var onlyRaw, onlyLeveled bool |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 116 | |
| 117 | for _, opt := range opts { |
| 118 | if opt.withBacklog > 0 || opt.withBacklog == BacklogAllAvailable { |
| 119 | backlog = opt.withBacklog |
| 120 | } |
| 121 | if opt.withStream { |
| 122 | stream = true |
| 123 | } |
| 124 | if opt.withChildren { |
| 125 | recursive = true |
| 126 | } |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 127 | if opt.leveledWithMinimumSeverity != "" { |
| 128 | leveledSeverity = opt.leveledWithMinimumSeverity |
| 129 | } |
| 130 | if opt.onlyLeveled { |
| 131 | onlyLeveled = true |
| 132 | } |
| 133 | if opt.onlyRaw { |
| 134 | onlyRaw = true |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 138 | if onlyLeveled && onlyRaw { |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 139 | return nil, ErrRawAndLeveled |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 142 | var filters []filter |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 143 | if onlyLeveled { |
| 144 | filters = append(filters, filterOnlyLeveled) |
| 145 | } |
| 146 | if onlyRaw { |
| 147 | filters = append(filters, filterOnlyRaw) |
| 148 | } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 149 | if recursive { |
| 150 | filters = append(filters, filterSubtree(dn)) |
| 151 | } else { |
| 152 | filters = append(filters, filterExact(dn)) |
| 153 | } |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 154 | if leveledSeverity != "" { |
| 155 | filters = append(filters, filterSeverity(leveledSeverity)) |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | var entries []*entry |
| 159 | if backlog > 0 || backlog == BacklogAllAvailable { |
| 160 | // TODO(q3k): pass over the backlog count to scanEntries/getEntries, instead of discarding them here. |
| 161 | if recursive { |
| 162 | entries = l.journal.scanEntries(filters...) |
| 163 | } else { |
| 164 | entries = l.journal.getEntries(dn, filters...) |
| 165 | } |
| 166 | if backlog != BacklogAllAvailable && len(entries) > backlog { |
| 167 | entries = entries[:backlog] |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | var sub *subscriber |
| 172 | if stream { |
| 173 | sub = &subscriber{ |
| 174 | // TODO(q3k): make buffer size configurable |
| 175 | dataC: make(chan *LogEntry, 128), |
| 176 | doneC: make(chan struct{}), |
| 177 | filters: filters, |
| 178 | } |
| 179 | l.journal.subscribe(sub) |
| 180 | } |
| 181 | |
| 182 | lr := &LogReader{} |
| 183 | lr.Backlog = make([]*LogEntry, len(entries)) |
| 184 | for i, entry := range entries { |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 185 | lr.Backlog[i] = entry.external() |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 186 | } |
| 187 | if stream { |
| 188 | lr.Stream = sub.dataC |
| 189 | lr.done = sub.doneC |
| 190 | lr.missed = &sub.missed |
| 191 | } |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 192 | return lr, nil |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 193 | } |