Skip to content

Commit a95a391

Browse files
committed
refactor(topics): refactor updating topic events.
1 parent 225b804 commit a95a391

File tree

15 files changed

+345
-2120
lines changed

15 files changed

+345
-2120
lines changed

alert/topics.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ func (s *Topics) RestoreTopics() {
8686
}
8787
}
8888

89-
func (s *Topics) UpdateEvent(id string, event EventState) {
89+
func (s *Topics) UpdateEvent(topicID string, event EventState) {
90+
9091
s.mu.Lock()
9192
defer s.mu.Unlock()
92-
t, ok := s.topics[id]
93+
t, ok := s.topics[topicID]
9394
if !ok {
94-
t = s.newTopic(id)
95-
s.topics[id] = t
95+
s.topics[topicID] = s.newTopic(topicID)
9696
}
9797
t.updateEvent(event)
9898
}
@@ -124,7 +124,6 @@ func (s *Topics) Collect(event Event) error {
124124
}
125125
s.mu.Unlock()
126126
}
127-
128127
return topic.collect(event)
129128
}
130129

@@ -347,16 +346,19 @@ func (t *Topic) close() {
347346
}
348347

349348
func (t *Topic) collect(event Event) error {
349+
350350
prev, ok := t.updateEvent(event.State)
351351
if ok {
352352
event.previousState = prev
353353
}
354354

355355
t.collected.Add(1)
356+
356357
return t.handleEvent(event)
357358
}
358359

359360
func (t *Topic) handleEvent(event Event) error {
361+
360362
t.mu.RLock()
361363
defer t.mu.RUnlock()
362364

@@ -406,6 +408,7 @@ func (t *Topic) updateEvent(state EventState) (EventState, bool) {
406408

407409
type sortedStates []*EventState
408410

411+
// TODO(docmerlin): replaced sortedStates with a heap or something similar
409412
func (e sortedStates) Len() int { return len(e) }
410413
func (e sortedStates) Swap(i int, j int) { e[i], e[j] = e[j], e[i] }
411414
func (e sortedStates) Less(i int, j int) bool {

build.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,8 @@ def run_tests(race, parallel, timeout, verbose):
211211
test_command += " -timeout {}".format(timeout)
212212
test_command += " ./..."
213213
logging.info("Running tests...")
214-
215-
packages = run("go list ./...").split("\n")
216-
print(packages)
217214
logging.info("Test command: " + test_command)
218-
for package in packages:
219-
run(test_command + " " + package, printOutput=logging.getLogger().getEffectiveLevel() == logging.DEBUG)
220-
215+
output = run(test_command, printOutput=logging.getLogger().getEffectiveLevel() == logging.DEBUG)
221216
return True
222217

223218
def package_udfs(version, dist_dir):

server/server.go

-1
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,6 @@ func (s *Server) startServices() error {
11351135
return fmt.Errorf("open service %T: %s", service, err)
11361136
}
11371137
s.Diag.Debug("opened service", keyvalue.KV("service", fmt.Sprintf("%T", service)))
1138-
11391138
// Apply config overrides after the config override service has been opened and before any dynamic services.
11401139
if service == s.ConfigOverrideService && !s.config.SkipConfigOverrides && s.config.ConfigOverride.Enabled {
11411140
// Apply initial config updates

server/server_test.go

+59-58
Original file line numberDiff line numberDiff line change
@@ -2857,7 +2857,6 @@ test value=1 0000000011
28572857
func TestServer_UpdateTaskID(t *testing.T) {
28582858
s, cli := OpenDefaultServer(t)
28592859
defer s.Close()
2860-
println("here")
28612860
id := "testTaskID"
28622861
ttype := client.StreamTask
28632862
dbrps := []client.DBRP{
@@ -9990,70 +9989,70 @@ func TestServer_AlertHandlers_CRUD(t *testing.T) {
99909989
},
99919990
}
99929991
for _, tc := range testCases {
9993-
// Create default config
9994-
c := NewConfig(t)
9995-
s := OpenServer(c)
9996-
cli := Client(s)
9997-
defer s.Close()
9998-
9999-
h, err := cli.CreateTopicHandler(cli.TopicHandlersLink(tc.topic), tc.create)
10000-
if err != nil {
10001-
t.Fatal(err)
10002-
}
10003-
10004-
if !reflect.DeepEqual(h, tc.expCreate) {
10005-
t.Errorf("unexpected handler created:\ngot\n%#v\nexp\n%#v\n", h, tc.expCreate)
10006-
}
10007-
10008-
h, err = cli.PatchTopicHandler(h.Link, tc.patch)
10009-
if err != nil {
10010-
t.Fatal(err)
10011-
}
9992+
t.Run(tc.topic, func(t *testing.T) {
9993+
// Create default config
9994+
c := NewConfig(t)
9995+
s := OpenServer(c)
9996+
cli := Client(s)
9997+
defer s.Close()
9998+
h, err := cli.CreateTopicHandler(cli.TopicHandlersLink(tc.topic), tc.create)
9999+
if err != nil {
10000+
t.Fatal(err)
10001+
}
1001210002

10013-
if !reflect.DeepEqual(h, tc.expPatch) {
10014-
t.Errorf("unexpected handler patched:\ngot\n%#v\nexp\n%#v\n", h, tc.expPatch)
10015-
}
10003+
if !reflect.DeepEqual(h, tc.expCreate) {
10004+
t.Errorf("unexpected handler created:\ngot\n%#v\nexp\n%#v\n", h, tc.expCreate)
10005+
}
1001610006

10017-
h, err = cli.ReplaceTopicHandler(h.Link, tc.put)
10018-
if err != nil {
10019-
t.Fatal(err)
10020-
}
10007+
h, err = cli.PatchTopicHandler(h.Link, tc.patch)
10008+
if err != nil {
10009+
t.Fatal(err)
10010+
}
1002110011

10022-
if !reflect.DeepEqual(h, tc.expPut) {
10023-
t.Errorf("unexpected handler put:\ngot\n%#v\nexp\n%#v\n", h, tc.expPut)
10024-
}
10012+
if !reflect.DeepEqual(h, tc.expPatch) {
10013+
t.Errorf("unexpected handler patched:\ngot\n%#v\nexp\n%#v\n", h, tc.expPatch)
10014+
}
1002510015

10026-
// Restart server
10027-
s.Restart()
10016+
h, err = cli.ReplaceTopicHandler(h.Link, tc.put)
10017+
if err != nil {
10018+
t.Fatal(err)
10019+
}
10020+
if !reflect.DeepEqual(h, tc.expPut) {
10021+
t.Errorf("unexpected handler put:\ngot\n%#v\nexp\n%#v\n", h, tc.expPut)
10022+
}
1002810023

10029-
rh, err := cli.TopicHandler(h.Link)
10030-
if err != nil {
10031-
t.Fatalf("could not find handler after restart: %v", err)
10032-
}
10033-
if got, exp := rh, h; !reflect.DeepEqual(got, exp) {
10034-
t.Errorf("unexpected handler after restart:\ngot\n%#v\nexp\n%#v\n", got, exp)
10035-
}
10024+
// Restart server
10025+
s.Restart()
1003610026

10037-
err = cli.DeleteTopicHandler(h.Link)
10038-
if err != nil {
10039-
t.Fatal(err)
10040-
}
10027+
rh, err := cli.TopicHandler(h.Link)
10028+
if err != nil {
10029+
t.Fatalf("could not find handler after restart: %v", err)
10030+
}
10031+
if got, exp := rh, h; !reflect.DeepEqual(got, exp) {
10032+
t.Errorf("unexpected handler after restart:\ngot\n%#v\nexp\n%#v\n", got, exp)
10033+
}
1004110034

10042-
_, err = cli.TopicHandler(h.Link)
10043-
if err == nil {
10044-
t.Errorf("expected handler to be deleted")
10045-
}
10035+
err = cli.DeleteTopicHandler(h.Link)
10036+
if err != nil {
10037+
t.Fatal(err)
10038+
}
1004610039

10047-
handlers, err := cli.ListTopicHandlers(cli.TopicHandlersLink(tc.topic), nil)
10048-
if err != nil {
10049-
t.Fatal(err)
10050-
}
10051-
for _, h := range handlers.Handlers {
10052-
if h.ID == tc.expPut.ID {
10040+
_, err = cli.TopicHandler(h.Link)
10041+
if err == nil {
1005310042
t.Errorf("expected handler to be deleted")
10054-
break
1005510043
}
10056-
}
10044+
10045+
handlers, err := cli.ListTopicHandlers(cli.TopicHandlersLink(tc.topic), nil)
10046+
if err != nil {
10047+
t.Fatal(err)
10048+
}
10049+
for _, h := range handlers.Handlers {
10050+
if h.ID == tc.expPut.ID {
10051+
t.Errorf("expected handler to be deleted")
10052+
break
10053+
}
10054+
}
10055+
})
1005710056
}
1005810057
}
1005910058

@@ -12001,7 +12000,10 @@ stream
1200112000
v := url.Values{}
1200212001
v.Add("precision", "s")
1200312002
s.MustWrite("mydb", "myrp", point, v)
12003+
time.Sleep(15 * time.Second)
1200412004

12005+
q, _ := s.AlertService.EventStates("tcp", -1)
12006+
_ = q
1200512007
s.Restart()
1200612008

1200712009
// Check TCP handler got event
@@ -12029,9 +12031,8 @@ stream
1202912031
exp := []alert.Data{alertData}
1203012032
got := ts.Data()
1203112033
if !reflect.DeepEqual(exp, got) {
12032-
t.Errorf("unexpected tcp request:\nexp\n%+v\ngot\n%+v\n", exp, got)
12034+
t.Fatalf("unexpected tcp request:\nexp\n%+v\ngot\n%+v\n", exp, got)
1203312035
}
12034-
1203512036
// Check event on topic
1203612037
l := cli.TopicEventsLink(tcpTopic)
1203712038
expTopicEvents := client.TopicEvents{
@@ -12055,7 +12056,7 @@ stream
1205512056
t.Fatal(err)
1205612057
}
1205712058
if !reflect.DeepEqual(te, expTopicEvents) {
12058-
t.Errorf("unexpected topic events for publish topic:\ngot\n%+v\nexp\n%+v\n", te, expTopicEvents)
12059+
t.Fatalf("unexpected topic events for publish topic:\ngot\n%+v\nexp\n%+v\n", te, expTopicEvents)
1205912060
}
1206012061
}
1206112062

services/alert/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ func (s *apiServer) handleListEvents(topic string, w http.ResponseWriter, r *htt
298298
httpd.HttpError(w, fmt.Sprintf("failed to get topic events: %s", err.Error()), true, http.StatusInternalServerError)
299299
return
300300
}
301+
301302
res := client.TopicEvents{
302303
Link: s.topicEventsLink(topic, client.Self),
303304
Topic: topic,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// +build ignore
2+
3+
// TEMPORARY AUTOGENERATED FILE: easyjson bootstapping code to launch
4+
// the actual generator.
5+
6+
package main
7+
8+
import (
9+
"fmt"
10+
"os"
11+
12+
"github.com/mailru/easyjson/gen"
13+
14+
pkg "github.com/influxdata/kapacitor/services/alert"
15+
)
16+
17+
func main() {
18+
g := gen.NewGenerator("dao_easyjson.go")
19+
g.SetPkg("alert", "github.com/influxdata/kapacitor/services/alert")
20+
g.Add(pkg.EasyJSON_exporter_EventState(nil))
21+
g.Add(pkg.EasyJSON_exporter_TopicState(nil))
22+
if err := g.Run(os.Stdout); err != nil {
23+
fmt.Fprintln(os.Stderr, err)
24+
os.Exit(1)
25+
}
26+
}

0 commit comments

Comments
 (0)