-
Notifications
You must be signed in to change notification settings - Fork 42
/
log.go
171 lines (134 loc) · 4.59 KB
/
log.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
// Copyright (c) 2019 Perlin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package wavelet
import (
"encoding/hex"
"sync"
"time"
"github.com/perlin-network/wavelet/conf"
"github.com/perlin-network/wavelet/log"
"github.com/valyala/fastjson"
)
// CollapseResultsLogger is used to write CollapseResults to the logger's writers.
//
// It writes directly into the writers without going through zerolog.
// The reason is that, zerolog will write into all the writers, even the writer's
// module does not match with the message's module.
//
// It also has a buffer to prevent blocking, as writing all the transactions in
// a collapse result may take sometime. A collapse result may contain
// tens of thousands of transactions.
type CollapseResultsLogger struct {
arena *fastjson.Arena
timeLayout string // "2006-01-02T15:04:05Z07:00"
bufTime []byte
// Buffer for a batch of messages (many transactions)
bufBatch []logBuffer
flushCh chan []logBuffer
stopWg sync.WaitGroup
stop chan struct{}
closed bool
}
type logBuffer struct {
module []byte
message []byte
}
func NewCollapseResultsLogger() *CollapseResultsLogger {
c := &CollapseResultsLogger{
arena: &fastjson.Arena{},
timeLayout: "2006-01-02T15:04:05Z07:00",
bufTime: make([]byte, 0, 64),
bufBatch: make([]logBuffer, 0, conf.GetBlockTXLimit()/4),
flushCh: make(chan []logBuffer, 1024),
stop: make(chan struct{}),
}
c.stopWg.Add(1)
go func() {
defer c.stopWg.Done()
for {
// Make stop higher priority.
// To prevent the runtime from repeatedly selecting flush channel when the stop channel has been closed.
select {
case <-c.stop:
return
default:
}
select {
case b := <-c.flushCh:
for i := range b {
_ = log.Write(string(b[i].module), b[i].message)
}
case <-c.stop:
return
}
}
}()
return c
}
func (c *CollapseResultsLogger) Log(results *collapseResults) {
timestamp := time.Now()
modTx := []byte(log.ModuleTX)
eventApplied := []byte("applied")
bufTxID := make([]byte, hex.EncodedLen(SizeTransactionID))
bufAccount := make([]byte, hex.EncodedLen(SizeAccountID))
for _, tx := range results.applied {
_ = hex.Encode(bufTxID, tx.ID[:])
_ = hex.Encode(bufAccount, tx.Sender[:])
c.addTx(modTx, eventApplied, timestamp, int(tx.Tag), bufTxID, bufAccount, nil)
}
eventRejected := []byte("rejected")
for i, tx := range results.rejected {
_ = hex.Encode(bufTxID, tx.ID[:])
_ = hex.Encode(bufAccount, tx.Sender[:])
c.addTx(modTx, eventRejected, timestamp, int(tx.Tag), bufTxID, bufAccount, results.rejectedErrors[i])
}
c.flush()
}
func (c *CollapseResultsLogger) addTx(mod, event []byte,
timestamp time.Time, tag int,
txID []byte, sender []byte, logError error) {
o := c.arena.NewObject()
o.Set("mod", c.arena.NewStringBytes(mod))
o.Set("event", c.arena.NewStringBytes(event))
o.Set("time", c.arena.NewStringBytes(timestamp.AppendFormat(c.bufTime, c.timeLayout)))
o.Set("tag", c.arena.NewNumberInt(tag))
o.Set("tx_id", c.arena.NewStringBytes(txID))
o.Set("sender_id", c.arena.NewStringBytes(sender))
if logError != nil {
o.Set("error", c.arena.NewString(logError.Error()))
}
// The length of the JSON is 227, not including the error field.
buf := make([]byte, 0, 256)
c.bufBatch = append(c.bufBatch, logBuffer{module: mod, message: o.MarshalTo(buf)})
c.bufTime = c.bufTime[:0]
c.arena.Reset()
}
func (c *CollapseResultsLogger) flush() {
c.flushCh <- c.bufBatch
c.bufBatch = make([]logBuffer, 0, cap(c.bufBatch))
}
func (c *CollapseResultsLogger) Stop() {
if c.closed {
return
}
close(c.stop)
c.stopWg.Wait()
close(c.flushCh)
c.closed = true
}