Skip to content

Commit

Permalink
fix(services/alert): Improve JSON serialization performance
Browse files Browse the repository at this point in the history
Uses mailru/easyjson to reduce the CPU and memory cost of serializing
the `TopicState` and `EventState` structs

Fixes #1971
  • Loading branch information
stuartcarnie committed Jun 20, 2018
1 parent 0e52907 commit 0afcd4e
Show file tree
Hide file tree
Showing 19 changed files with 2,844 additions and 14 deletions.
14 changes: 9 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
required = ["github.com/benbjohnson/tmpl","github.com/golang/protobuf/protoc-gen-go"]
required = [
"github.com/benbjohnson/tmpl",
"github.com/golang/protobuf/protoc-gen-go",
"github.com/mailru/easyjson/easyjson",
]

[prune]
unused-packages = true
Expand All @@ -9,6 +13,10 @@ required = ["github.com/benbjohnson/tmpl","github.com/golang/protobuf/protoc-gen
branch = "master"
name = "github.com/davecgh/go-spew"

[[constraint]]
branch = "master"
name = "github.com/mailru/easyjson"

[[constraint]]
branch = "master"
name = "github.com/evanphx/json-patch"
Expand Down Expand Up @@ -57,3 +65,11 @@ required = ["github.com/benbjohnson/tmpl","github.com/golang/protobuf/protoc-gen
name = "github.com/Azure/go-autorest"
revision = "a2fdd780c9a50455cecd249b00bdc3eb73a78e31"

[[override]]
name= "gopkg.in/fsnotify.v1"
revision = "629574ca2a5df945712d3079857300b5e4da0236"
source = "git@github.com:fsnotify/fsnotify"

[[override]]
name= "github.com/mailru/easyjson"
revision = "3fdea8d05856a0c8df22ed4bc71b3219245e4485"
8 changes: 6 additions & 2 deletions services/alert/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/influxdata/kapacitor/alert"
"github.com/influxdata/kapacitor/services/storage"
"github.com/mailru/easyjson/jlexer"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -243,11 +244,13 @@ type TopicStateDAO interface {

const topicStateVersion = 1

//easyjson:json
type TopicState struct {
Topic string `json:"topic"`
EventStates map[string]EventState `json:"event-states"`
}

//easyjson:json
type EventState struct {
Message string `json:"message"`
Details string `json:"details"`
Expand All @@ -265,8 +268,9 @@ func (t TopicState) MarshalBinary() ([]byte, error) {
}

func (t *TopicState) UnmarshalBinary(data []byte) error {
return storage.VersionJSONDecode(data, func(version int, dec *json.Decoder) error {
return dec.Decode(&t)
return storage.VersionEasyJSONDecode(data, func(version int, dec *jlexer.Lexer) error {
t.UnmarshalEasyJSON(dec)
return dec.Error()
})
}

Expand Down
262 changes: 262 additions & 0 deletions services/alert/dao_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0afcd4e

Please sign in to comment.