-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.go
445 lines (405 loc) · 11.7 KB
/
syslog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package log
import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/index/scorch"
"github.com/blevesearch/bleve/index/store/boltdb"
"github.com/blevesearch/bleve/mapping"
"github.com/pborman/uuid"
"github.com/rs/xid"
"go.uber.org/zap"
"github.com/pydio/cells/common/proto/log"
)
const (
BufferedChanSize = 10000
MinRotationSize = 68 * 1024
DefaultRotationSize = int64(200 * 1024 * 1024)
)
var (
blockingInserts = false
)
// SyslogServer is the syslog specific implementation of the Log server
type SyslogServer struct {
SearchIndex bleve.IndexAlias
rotationSize int64
indexes []bleve.Index
cursor int
indexPath string
mappingName string
opened bool
inserts chan interface{}
insertsDone chan bool
crtBatch *bleve.Batch
flushLock *sync.Mutex
}
// NewSyslogServer creates and configures a default Bleve instance to store technical logs
// Setting rotationSize to -1 fully disables rotation
func NewSyslogServer(indexPath string, mappingName string, rotationSize int64) (*SyslogServer, error) {
if rotationSize > -1 && rotationSize < MinRotationSize {
return nil, fmt.Errorf("use a rotation size bigger than %d", MinRotationSize)
}
server := &SyslogServer{
rotationSize: rotationSize,
}
if blockingInserts {
server.inserts = make(chan interface{})
} else {
server.inserts = make(chan interface{}, BufferedChanSize)
}
er := server.Open(indexPath, mappingName)
return server, er
}
// Open lists all existing indexes and creates a writeable index on the active one
// and a composed index for searching. It calls watchInserts() to start watching for
// new logs
func (s *SyslogServer) Open(indexPath string, mappingName string) error {
s.indexPath = indexPath
s.mappingName = mappingName
s.SearchIndex = bleve.NewIndexAlias()
s.indexes = []bleve.Index{}
s.flushLock = &sync.Mutex{}
existing := s.listIndexes(true)
if len(existing) == 0 {
index, err := openOneIndex(indexPath, mappingName)
if err != nil {
return err
}
s.SearchIndex.Add(index)
s.indexes = append(s.indexes, index)
s.cursor = 0
} else {
for _, iName := range existing {
iPath := filepath.Join(filepath.Dir(indexPath), iName)
if index, err := openOneIndex(iPath, mappingName); err == nil {
s.indexes = append(s.indexes, index)
} else {
fmt.Println("[pydio.grpc.log] Cannot open bleve index", iPath, err)
}
}
s.SearchIndex.Add(s.indexes...)
s.cursor = len(s.indexes) - 1
}
s.insertsDone = make(chan bool)
s.opened = true
if indexPath != "" && s.rotationSize > -1 {
s.rotateIfNeeded()
}
go s.watchInserts()
return nil
}
func (s *SyslogServer) getWriteIndex() bleve.Index {
return s.indexes[s.cursor]
}
func (s *SyslogServer) Close() {
if !s.opened {
return
}
s.opened = false
close(s.insertsDone)
}
func (s *SyslogServer) listIndexes(renameIfNeeded ...bool) (paths []string) {
dirPath, base := filepath.Split(s.indexPath)
dir, err := os.Open(dirPath)
if err != nil {
return
}
defer dir.Close()
files, err := dir.Readdir(-1)
if err != nil {
return
}
for _, file := range files {
if !file.IsDir() {
continue
}
curBase := filepath.Base(file.Name())
if curBase == base {
paths = append(paths, curBase)
} else if strings.HasPrefix(curBase, base) {
// Ensure suffix is a number ".0001", ".0002", etc.
test := strings.TrimLeft(strings.TrimPrefix(curBase, base+"."), "0")
if _, e := strconv.ParseInt(test, 10, 32); e == nil {
paths = append(paths, curBase)
}
}
}
sort.Strings(paths)
if len(renameIfNeeded) > 0 && renameIfNeeded[0] && len(paths) > 0 && paths[0] != base {
// Old files were removed, renumber files
for _, p := range paths {
src := filepath.Join(dirPath, p)
t1 := filepath.Join(dirPath, fmt.Sprintf("%s-rename", p))
os.Rename(src, t1)
}
for i, p := range paths {
src := filepath.Join(dirPath, fmt.Sprintf("%s-rename", p))
t2 := filepath.Join(dirPath, fmt.Sprintf("%s.%04d", base, i))
if i == 0 {
t2 = s.indexPath
}
os.Rename(src, t2)
}
return s.listIndexes()
}
return
}
func (s *SyslogServer) watchInserts() {
for {
select {
case in := <-s.inserts:
var msg *IndexableLog
if m, ok := in.(*IndexableLog); ok {
msg = m
} else if line, ok := in.(*log.Log); ok {
var err error
msg, err = MarshallLogMsg(line)
if err != nil {
break
}
} else {
// Unsupported type
break
}
s.flushLock.Lock()
if s.crtBatch == nil {
s.crtBatch = s.getWriteIndex().NewBatch()
}
s.crtBatch.Index(xid.New().String(), msg)
if s.crtBatch.Size() > 5000 {
s.flush()
}
s.flushLock.Unlock()
case <-time.After(3 * time.Second):
s.flushLock.Lock()
s.flush()
s.flushLock.Unlock()
case <-s.insertsDone:
s.flushLock.Lock()
s.flush()
s.flushLock.Unlock()
s.SearchIndex.Close()
for _, i := range s.indexes {
i.Close()
}
return
}
}
}
func (s *SyslogServer) rotateIfNeeded() {
if s.indexPath == "" || s.rotationSize == -1 {
return
}
checkPath := s.indexPath
if s.cursor > 0 {
checkPath = fmt.Sprintf("%s.%04d", s.indexPath, s.cursor)
}
du, e := indexDiskUsage(checkPath)
if e != nil {
fmt.Println("[pydio.grpc.log] Cannot compute disk usage for bleve index", e.Error())
return
}
if du > s.rotationSize {
fmt.Println("Rotating "+s.indexPath+" for size ", du)
// Open a new index
newPath := fmt.Sprintf("%s.%04d", s.indexPath, len(s.indexes))
newIndex, er := openOneIndex(newPath, s.mappingName)
if er != nil {
fmt.Println("[pydio.grpc.log] Cannot create new bleve index", er.Error())
return
}
s.indexes = append(s.indexes, newIndex)
s.SearchIndex.Add(newIndex)
s.cursor = len(s.indexes) - 1
}
}
func (s *SyslogServer) flush() {
if s.crtBatch != nil {
s.getWriteIndex().Batch(s.crtBatch)
s.rotateIfNeeded()
s.crtBatch = nil
}
}
// PutLog adds a new LogMessage in the syslog index.
func (s *SyslogServer) PutLog(line *log.Log) error {
if blockingInserts {
s.inserts <- line
} else {
select {
case s.inserts <- line:
default:
}
}
return nil
}
// ListLogs performs a query in the bleve index, based on the passed query string.
// It returns results as a stream of log.ListLogResponse for each corresponding hit.
// Results are ordered by descending timestamp rather than by score.
func (s *SyslogServer) ListLogs(str string, page, size int32) (chan log.ListLogResponse, error) {
return BleveListLogs(s.SearchIndex, str, page, size)
}
// DeleteLogs truncate logs based on a search query
func (s *SyslogServer) DeleteLogs(query string) (int64, error) {
return BleveDeleteLogs(s.getWriteIndex(), query)
}
// AggregatedLogs performs a faceted query in the syslog repository. UNIMPLEMENTED.
func (s *SyslogServer) AggregatedLogs(_ string, _ string, _ int32) (chan log.TimeRangeResponse, error) {
return nil, fmt.Errorf("unimplemented method")
}
// Resync creates a copy of current index. It has been originally used for switching analyze format from bleve to scorch.
func (s *SyslogServer) Resync(logger *zap.Logger) error {
copyDir := filepath.Join(filepath.Dir(s.indexPath), uuid.New())
e := os.Mkdir(copyDir, 0777)
if e != nil {
return e
}
defer func() {
os.RemoveAll(copyDir)
}()
copyPath := filepath.Join(copyDir, filepath.Base(s.indexPath))
dup, er := NewSyslogServer(copyPath, s.mappingName, s.rotationSize)
if er != nil {
return er
}
logTaskInfo(logger, "Listing Index inside new one", "info")
if err := BleveDuplicateIndex(s.SearchIndex, dup.inserts, func(s string) {
logTaskInfo(logger, s, "info")
}); err != nil {
return err
}
s.Close()
dup.Close()
<-time.After(5 * time.Second) // Make sure original is closed
logTaskInfo(logger, "Removing old indexes", "info")
for _, ip := range s.listIndexes() {
if err := os.RemoveAll(filepath.Join(filepath.Dir(s.indexPath), ip)); err != nil {
return err
}
}
logTaskInfo(logger, "Moving new indexes", "info")
for _, ip := range dup.listIndexes() {
src := filepath.Join(copyDir, ip)
target := filepath.Join(filepath.Join(filepath.Dir(s.indexPath), ip))
if err := os.Rename(src, target); err != nil {
return err
}
}
logTaskInfo(logger, "Restarting new server", "info")
if err := s.Open(s.indexPath, s.mappingName); err != nil {
return err
}
logTaskInfo(logger, "Resync operation done", "info")
return nil
}
// Truncate gathers size of existing indexes, starting from last. When max is reached
// it starts deleting all previous indexes.
func (s *SyslogServer) Truncate(max int64, logger *zap.Logger) error {
logTaskInfo(logger, "Closing log server, waiting for five seconds", "info")
dir := filepath.Dir(s.indexPath)
s.Close()
<-time.After(5 * time.Second)
logTaskInfo(logger, "Start purging old files", "info")
indexes := s.listIndexes()
var i int
var total int64
var remove bool
for i = len(indexes) - 1; i >= 0; i-- {
if remove {
e := os.RemoveAll(filepath.Join(dir, indexes[i]))
if e != nil {
logTaskInfo(logger, fmt.Sprintf("cannot remove index %s", indexes[i]), "error")
}
} else if u, e := indexDiskUsage(filepath.Join(dir, indexes[i])); e == nil {
total += u
remove = total > max
}
}
// Now restart - it will renumber files
logTaskInfo(logger, "Re-opening log server", "info")
s.Open(s.indexPath, s.mappingName)
logTaskInfo(logger, "Truncate operation done", "info")
return nil
}
func logTaskInfo(l *zap.Logger, msg string, level string) {
if l == nil {
fmt.Println("[pydio.grpc.log] " + msg)
} else if level == "info" {
l.Info(msg)
} else if level == "error" {
l.Error(msg)
} else {
l.Debug(msg)
}
}
// openOneIndex tries to open an existing index at a given path, or creates a new one
func openOneIndex(bleveIndexPath string, mappingName string) (bleve.Index, error) {
index, err := bleve.Open(bleveIndexPath)
if err != nil {
indexMapping := bleve.NewIndexMapping()
// Create, configure and add a specific document mapping
logMapping := bleve.NewDocumentMapping()
// Exclude JSONZaps from indexing
logMapping.AddFieldMapping(&mapping.FieldMapping{Type: "text", Name: "JsonZaps", Index: false, Store: true})
indexMapping.AddDocumentMapping(mappingName, logMapping)
// Creates the new index and initializes the server
if bleveIndexPath == "" {
index, err = bleve.NewMemOnly(indexMapping)
} else {
index, err = bleve.NewUsing(bleveIndexPath, indexMapping, scorch.Name, boltdb.Name, nil)
}
if err != nil {
return nil, err
}
}
return index, nil
}
// indexDiskUsage is a simple implementation for computing directory size
func indexDiskUsage(currPath string) (int64, error) {
var size int64
dir, err := os.Open(currPath)
if err != nil {
return 0, err
}
defer dir.Close()
files, err := dir.Readdir(-1)
if err != nil {
return 0, err
}
for _, file := range files {
if file.IsDir() {
s, e := indexDiskUsage(filepath.Join(currPath, file.Name()))
if e != nil {
return 0, e
}
size += s
} else {
size += file.Size()
}
}
return size, nil
}