Skip to content

Commit

Permalink
allow customizing disabled journal events + tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Sep 14, 2020
1 parent 6d29d75 commit 05aa5f2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
49 changes: 49 additions & 0 deletions journal/registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package journal

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestDisabledEvents(t *testing.T) {
req := require.New(t)

test := func(dis DisabledEvents) func(*testing.T) {
return func(t *testing.T) {
registry := NewEventTypeRegistry(dis)

reg1 := registry.RegisterEventType("system1", "disabled1")
reg2 := registry.RegisterEventType("system2", "disabled2")

req.False(reg1.Enabled())
req.False(reg2.Enabled())
req.True(reg1.safe)
req.True(reg2.safe)

reg3 := registry.RegisterEventType("system3", "enabled3")
req.True(reg3.Enabled())
req.True(reg3.safe)
}
}

t.Run("direct", test(DisabledEvents{
EventType{System: "system1", Event: "disabled1"},
EventType{System: "system2", Event: "disabled2"},
}))

dis, err := ParseDisabledEvents("system1:disabled1,system2:disabled2")
req.NoError(err)

t.Run("parsed", test(dis))

dis, err = ParseDisabledEvents(" system1:disabled1 , system2:disabled2 ")
req.NoError(err)

t.Run("parsed_spaces", test(dis))
}

func TestParseDisableEvents(t *testing.T) {
_, err := ParseDisabledEvents("system1:disabled1:failed,system2:disabled2")
require.Error(t, err)
}
21 changes: 21 additions & 0 deletions journal/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package journal

import (
"fmt"
"strings"
"time"

logging "github.com/ipfs/go-log"
Expand All @@ -20,6 +22,25 @@ var (
// DisabledEvents is the set of event types whose journaling is suppressed.
type DisabledEvents []EventType

// ParseDisabledEvents parses a string of the form: "system1:event1,system2:event2[,...]"
// into a DisabledEvents object, returning an error if the string failed to parse.
//
// It sanitizes strings via strings.TrimSpace.
func ParseDisabledEvents(s string) (DisabledEvents, error) {
s = strings.TrimSpace(s) // sanitize
evts := strings.Split(s, ",")
ret := make(DisabledEvents, 0, len(evts))
for _, evt := range evts {
evt = strings.TrimSpace(evt) // sanitize
s := strings.Split(evt, ":")
if len(s) != 2 {
return nil, fmt.Errorf("invalid event type: %s", s)
}
ret = append(ret, EventType{System: s[0], Event: s[1]})
}
return ret, nil
}

// EventType represents the signature of an event.
type EventType struct {
System string
Expand Down
15 changes: 14 additions & 1 deletion node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package node
import (
"context"
"errors"
"os"
"time"

logging "github.com/ipfs/go-log"
Expand Down Expand Up @@ -68,6 +69,10 @@ import (
"github.com/filecoin-project/lotus/storage/sectorblocks"
)

// EnvJournalDisabledEvents is the environment variable through which disabled
// journal events can be customized.
const EnvJournalDisabledEvents = "LOTUS_JOURNAL_DISABLED_EVENTS"

//nolint:deadcode,varcheck
var log = logging.Logger("builder")

Expand Down Expand Up @@ -157,7 +162,15 @@ type Settings struct {
func defaults() []Option {
return []Option{
// global system journal.
Override(new(journal.DisabledEvents), journal.DefaultDisabledEvents),
Override(new(journal.DisabledEvents), func() journal.DisabledEvents {
if env, ok := os.LookupEnv(EnvJournalDisabledEvents); ok {
if ret, err := journal.ParseDisabledEvents(env); err == nil {
return ret
}
}
// fallback if env variable is not set, or if it failed to parse.
return journal.DefaultDisabledEvents
}),
Override(new(journal.Journal), modules.OpenFilesystemJournal),
Override(InitJournalKey, func(j journal.Journal) {
journal.J = j // eagerly sets the global journal through fx.Invoke.
Expand Down

0 comments on commit 05aa5f2

Please sign in to comment.