-
Notifications
You must be signed in to change notification settings - Fork 0
/
logtail.go
300 lines (267 loc) · 7.81 KB
/
logtail.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
package veracity
import (
"context"
"fmt"
"strconv"
"time"
"github.com/datatrails/go-datatrails-common/azblob"
"github.com/datatrails/go-datatrails-common/cose"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"github.com/datatrails/go-datatrails-merklelog/massifs/snowflakeid"
"github.com/datatrails/go-datatrails-merklelog/massifs/watcher"
"github.com/urfave/cli/v2"
)
type TailConfig struct {
// Interval defines the wait period between repeated tail checks if many
// checks have been asked for.
Interval time.Duration
// TenantIdentity identifies the log of interest
TenantIdentity string
}
// LogTailActivity can represent either the seal or the massif that has most recently
// been updated for the log.
type LogTailActivity struct {
watcher.LogTail
LogSize uint64
LastIDEpoch uint8
LastIDTimestamp uint64
LogActivity time.Time
TagActivity time.Time
}
// MassifTail contains the massif specific tail information
type MassifTail struct {
LogTailActivity
FirstIndex uint64
}
// SealTail contains the seal specific tail information
type SealTail struct {
LogTailActivity
Count uint64
Signed cose.CoseSign1Message
State massifs.MMRState
}
// String returns a printable. loggable pretty rendering of the tail
func (st SealTail) String() string {
s := fmt.Sprintf(
"seal: %d, mmrSize: %d, lastid: %s, seal time: %v, log activity: %v",
st.Number, st.State.MMRSize, st.LastID,
time.UnixMilli(st.State.Timestamp).UTC().Format(time.RFC3339),
st.LogActivity.UTC().Format(time.RFC3339),
)
if st.LastID != "" {
return fmt.Sprintf(
"%s, tag activity: %v",
s, st.TagActivity.UTC().Format(time.RFC3339),
)
}
return fmt.Sprintf("%s, tag activity: ** tag not set **", s)
}
// NewTailConfig derives a configuration from the supplied comand line options context
func NewTailConfig(cCtx *cli.Context, cmd *CmdCtx) (TailConfig, error) {
cfg := TailConfig{}
// note: the cli defaults to 1 second interval and count = 1. so by default
// the interval is ignored. If count is 0 or > 1, we get a single second
// sleep by default.
cfg.Interval = cCtx.Duration("interval")
cfg.TenantIdentity = cCtx.String("tenant")
if cfg.TenantIdentity == "" {
return TailConfig{}, fmt.Errorf("tenant identity is required")
}
return cfg, nil
}
// String returns a printable. loggable pretty rendering of the tail
func (lt MassifTail) String() string {
s := fmt.Sprintf(
"massif: %d, mmrSize: %d, lastid: %s, log activity: %v",
lt.Number, lt.LogSize, lt.LastID,
lt.LogActivity.UTC().Format(time.RFC3339),
)
if lt.LastID != "" {
return fmt.Sprintf(
"%s, tag activity: %v",
s, lt.TagActivity.UTC().Format(time.RFC3339),
)
}
return fmt.Sprintf("%s, tag activity: ** tag not set **", s)
}
// TailSeal returns the most recently added seal for the log
func TailSeal(
ctx context.Context,
rootReader massifs.SignedRootReader,
tenantIdentity string,
) (SealTail, error) {
var err error
var tailSeal massifs.LogBlobContext
st := SealTail{
LogTailActivity: LogTailActivity{
LogTail: watcher.LogTail{
Tenant: tenantIdentity,
},
},
}
tailSeal, st.Count, err = rootReader.GetLazyContext(
ctx, tenantIdentity, massifs.LastBlob, azblob.WithListTags())
if err != nil {
return SealTail{}, err
}
tags := tailSeal.Tags
msg, state, err := rootReader.ReadLogicalContext(ctx, tailSeal, azblob.WithGetTags())
if err != nil {
return SealTail{}, err
}
st.Signed = *msg
st.State = state
st.Path = tailSeal.BlobPath
st.Number, st.Ext, err = massifs.ParseMassifPathNumberExt(st.Path)
if err != nil {
return SealTail{}, err
}
// The log activity as it stood when the seal was made is on the state
lastMS, err := snowflakeid.IDUnixMilli(st.State.IDTimestamp, uint8(st.State.CommitmentEpoch))
if err != nil {
return SealTail{}, err
}
st.LogActivity = time.UnixMilli(lastMS)
// And the seal blob also has a tag so this can be indexed
//lastIDTag := tailSeal.Tags[massifs.TagKeyLastID]
st.LastID = tags[massifs.TagKeyLastID]
id, epoch, err := massifs.SplitIDTimestampHex(st.LastID)
if err != nil {
return SealTail{}, err
}
lastMS, err = snowflakeid.IDUnixMilli(id, epoch)
if err != nil {
return SealTail{}, err
}
st.TagActivity = time.UnixMilli(lastMS)
return st, err
}
// TailMassif returns the active massif for the tenant
func TailMassif(
ctx context.Context,
massifReader MassifReader,
tenantIdentity string,
) (MassifTail, error) {
var err error
lt := MassifTail{
LogTailActivity: LogTailActivity{
LogTail: watcher.LogTail{
Tenant: tenantIdentity,
},
},
}
tailMassif, err := massifReader.GetHeadMassif(ctx, tenantIdentity, massifs.WithListBlobOption(azblob.WithGetTags()))
if err != nil {
return MassifTail{}, fmt.Errorf(
"error reading head massif for tenant %s: %w",
tenantIdentity, err)
}
lt.Path = tailMassif.BlobPath
lt.Number = tailMassif.Start.MassifIndex
number, ext, err := massifs.ParseMassifPathNumberExt(lt.Path)
if err != nil {
return MassifTail{}, err
}
if number != lt.Number {
return MassifTail{}, fmt.Errorf("path base file doesn't match massif index in log start record")
}
lt.Ext = ext
logActivityMS, err := tailMassif.LastCommitUnixMS(uint8(tailMassif.Start.CommitmentEpoch))
if err != nil {
return MassifTail{}, fmt.Errorf(
"error reading last activity time from head massif for tenant %s: %w",
tenantIdentity, err)
}
lt.LogActivity = time.UnixMilli(logActivityMS)
firstIndexTag := tailMassif.Tags[massifs.TagKeyFirstIndex]
lt.FirstIndex, err = strconv.ParseUint(firstIndexTag, 16, 64)
if err != nil {
return MassifTail{}, err
}
lt.LogSize = tailMassif.RangeCount()
lt.LastID = tailMassif.Tags[massifs.TagKeyLastID]
lt.LastIDTimestamp, lt.LastIDEpoch, err = massifs.SplitIDTimestampHex(lt.LastID)
if err != nil {
return MassifTail{}, err
}
lastMS, err := snowflakeid.IDUnixMilli(lt.LastIDTimestamp, lt.LastIDEpoch)
if err != nil {
return MassifTail{}, err
}
lt.TagActivity = time.UnixMilli(lastMS)
return lt, nil
}
func NewLogTailCmd() *cli.Command {
return &cli.Command{Name: "tail",
Usage: `report the current tail (most recent end) of the log
if --count is > 1, re-check every interval seconds until the count is exhasted
if --count is explicitly zero, check forever
`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mode",
Usage: "Any of [massif, seal, both], defaults to both",
Value: "massif",
},
&cli.StringFlag{
Name: "tenant", Aliases: []string{"t"},
Usage: "tenant identity",
Required: true,
},
&cli.DurationFlag{
Name: "interval", Aliases: []string{"d"},
Value: time.Second,
Usage: "The default polling interval is one second, setting the interval to zero disables polling",
},
&cli.IntFlag{
Name: "count", Usage: "Number of intervals. Zero means forever. Defaults to single poll",
Value: 1,
},
},
Action: func(cCtx *cli.Context) error {
var err error
cmd := &CmdCtx{}
ctx := context.Background()
if err = cfgMassifReader(cmd, cCtx); err != nil {
return err
}
if err = cfgRootReader(cmd, cCtx); err != nil {
return err
}
cfg, err := NewTailConfig(cCtx, cmd)
if err != nil {
return err
}
count := cCtx.Int("count")
mode := cCtx.String("mode")
for {
var lt MassifTail
var st SealTail
if mode == "both" || mode == "massif" {
lt, err = TailMassif(ctx, cmd.massifReader, cfg.TenantIdentity)
if err != nil {
return err
}
fmt.Printf("%s\n", lt.String())
}
if mode == "both" || mode == "seal" {
st, err = TailSeal(ctx, cmd.rootReader, cfg.TenantIdentity)
if err != nil {
return err
}
fmt.Printf("%s\n", st.String())
}
// Note we don't allow a zero interval
if count == 1 || cfg.Interval == 0 {
break
}
// count == 0 is infinite
if count > 1 {
count--
}
time.Sleep(cfg.Interval)
}
return nil
},
}
}