Skip to content

Commit 11617f3

Browse files
author
Steffen Siering
committed
Add publisher implementation for stateful inputs (elastic#19530)
Add the publisher implementation and unit tests for statefull inputs. The tests check that the publisher correctly updates the ephemeral store and prepare the update operation for the persistent store, that will be applied to the store after it has been ACKed.
1 parent 52c37b3 commit 11617f3

File tree

2 files changed

+207
-4
lines changed

2 files changed

+207
-4
lines changed

filebeat/input/v2/input-cursor/publish.go

+49-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
input "github.com/elastic/beats/v7/filebeat/input/v2"
2424
"github.com/elastic/beats/v7/libbeat/beat"
2525
"github.com/elastic/beats/v7/libbeat/common/transform/typeconv"
26+
"github.com/elastic/beats/v7/libbeat/statestore"
2627
)
2728

2829
// Publisher is used to publish an event and update the cursor in a single call to Publish.
@@ -64,12 +65,25 @@ type updateOp struct {
6465
// The ACK ordering in the publisher pipeline guarantees that update operations
6566
// will be ACKed and executed in the correct order.
6667
func (c *cursorPublisher) Publish(event beat.Event, cursorUpdate interface{}) error {
67-
panic("TODO: implement me")
68+
if cursorUpdate == nil {
69+
return c.forward(event)
70+
}
71+
72+
op, err := createUpdateOp(c.cursor.store, c.cursor.resource, cursorUpdate)
73+
if err != nil {
74+
return err
75+
}
76+
77+
event.Private = op
78+
return c.forward(event)
6879
}
6980

70-
// Execute updates the persistent store with the scheduled changes and releases the resource.
71-
func (op *updateOp) Execute(numEvents uint) {
72-
panic("TODO: implement me")
81+
func (c *cursorPublisher) forward(event beat.Event) error {
82+
c.client.Publish(event)
83+
if c.canceler == nil {
84+
return nil
85+
}
86+
return c.canceler.Err()
7387
}
7488

7589
func createUpdateOp(store *store, resource *resource, updates interface{}) (*updateOp, error) {
@@ -106,3 +120,34 @@ func (op *updateOp) done(n uint) {
106120
op.resource = nil
107121
*op = updateOp{}
108122
}
123+
124+
// Execute updates the persistent store with the scheduled changes and releases the resource.
125+
func (op *updateOp) Execute(n uint) {
126+
resource := op.resource
127+
defer op.done(n)
128+
129+
resource.stateMutex.Lock()
130+
defer resource.stateMutex.Unlock()
131+
132+
resource.activeCursorOperations -= n
133+
if resource.activeCursorOperations == 0 {
134+
resource.cursor = resource.pendingCursor
135+
resource.pendingCursor = nil
136+
} else {
137+
typeconv.Convert(&resource.cursor, op.delta)
138+
}
139+
140+
if resource.internalState.Updated.Before(op.timestamp) {
141+
resource.internalState.Updated = op.timestamp
142+
}
143+
144+
err := op.store.persistentStore.Set(resource.key, resource.inSyncStateSnapshot())
145+
if err != nil {
146+
if !statestore.IsClosed(err) {
147+
op.store.log.Errorf("Failed to update state in the registry for '%v'", resource.key)
148+
}
149+
} else {
150+
resource.internalInSync = true
151+
resource.stored = true
152+
}
153+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cursor
19+
20+
import (
21+
"context"
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
27+
"github.com/elastic/beats/v7/libbeat/beat"
28+
pubtest "github.com/elastic/beats/v7/libbeat/publisher/testing"
29+
)
30+
31+
func TestPublish(t *testing.T) {
32+
t.Run("event with cursor state creates update operation", func(t *testing.T) {
33+
store := testOpenStore(t, "test", createSampleStore(t, nil))
34+
defer store.Release()
35+
cursor := makeCursor(store, store.Get("test::key"))
36+
37+
var actual beat.Event
38+
client := &pubtest.FakeClient{
39+
PublishFunc: func(event beat.Event) { actual = event },
40+
}
41+
publisher := cursorPublisher{nil, client, &cursor}
42+
publisher.Publish(beat.Event{}, "test")
43+
44+
require.NotNil(t, actual.Private)
45+
})
46+
47+
t.Run("event without cursor creates no update operation", func(t *testing.T) {
48+
store := testOpenStore(t, "test", createSampleStore(t, nil))
49+
defer store.Release()
50+
cursor := makeCursor(store, store.Get("test::key"))
51+
52+
var actual beat.Event
53+
client := &pubtest.FakeClient{
54+
PublishFunc: func(event beat.Event) { actual = event },
55+
}
56+
publisher := cursorPublisher{nil, client, &cursor}
57+
publisher.Publish(beat.Event{}, nil)
58+
require.Nil(t, actual.Private)
59+
})
60+
61+
t.Run("publish returns error if context has been cancelled", func(t *testing.T) {
62+
ctx, cancel := context.WithCancel(context.TODO())
63+
cancel()
64+
65+
store := testOpenStore(t, "test", createSampleStore(t, nil))
66+
defer store.Release()
67+
cursor := makeCursor(store, store.Get("test::key"))
68+
69+
publisher := cursorPublisher{ctx, &pubtest.FakeClient{}, &cursor}
70+
err := publisher.Publish(beat.Event{}, nil)
71+
require.Equal(t, context.Canceled, err)
72+
})
73+
}
74+
75+
func TestOp_Execute(t *testing.T) {
76+
t.Run("applying final op marks the key as finished", func(t *testing.T) {
77+
store := testOpenStore(t, "test", createSampleStore(t, nil))
78+
defer store.Release()
79+
res := store.Get("test::key")
80+
81+
// create op and release resource. The 'resource' must still be active
82+
op := mustCreateUpdateOp(t, store, res, "test-updated-cursor-state")
83+
res.Release()
84+
require.False(t, res.Finished())
85+
86+
// this was the last op, the resource should become inactive
87+
op.Execute(1)
88+
require.True(t, res.Finished())
89+
90+
// validate state:
91+
inSyncCursor := storeInSyncSnapshot(store)["test::key"].Cursor
92+
inMemCursor := storeMemorySnapshot(store)["test::key"].Cursor
93+
want := "test-updated-cursor-state"
94+
assert.Equal(t, want, inSyncCursor)
95+
assert.Equal(t, want, inMemCursor)
96+
})
97+
98+
t.Run("acking multiple ops applies the latest update and marks key as finished", func(t *testing.T) {
99+
// when acking N events, intermediate updates are dropped in favor of the latest update operation.
100+
// This test checks that the resource is correctly marked as finished.
101+
102+
store := testOpenStore(t, "test", createSampleStore(t, nil))
103+
defer store.Release()
104+
res := store.Get("test::key")
105+
106+
// create update operations and release resource. The 'resource' must still be active
107+
mustCreateUpdateOp(t, store, res, "test-updated-cursor-state-dropped")
108+
op := mustCreateUpdateOp(t, store, res, "test-updated-cursor-state-final")
109+
res.Release()
110+
require.False(t, res.Finished())
111+
112+
// this was the last op, the resource should become inactive
113+
op.Execute(2)
114+
require.True(t, res.Finished())
115+
116+
// validate state:
117+
inSyncCursor := storeInSyncSnapshot(store)["test::key"].Cursor
118+
inMemCursor := storeMemorySnapshot(store)["test::key"].Cursor
119+
want := "test-updated-cursor-state-final"
120+
assert.Equal(t, want, inSyncCursor)
121+
assert.Equal(t, want, inMemCursor)
122+
})
123+
124+
t.Run("ACK only subset of pending ops will only update up to ACKed state", func(t *testing.T) {
125+
// when acking N events, intermediate updates are dropped in favor of the latest update operation.
126+
// This test checks that the resource is correctly marked as finished.
127+
128+
store := testOpenStore(t, "test", createSampleStore(t, nil))
129+
defer store.Release()
130+
res := store.Get("test::key")
131+
132+
// create update operations and release resource. The 'resource' must still be active
133+
op1 := mustCreateUpdateOp(t, store, res, "test-updated-cursor-state-intermediate")
134+
op2 := mustCreateUpdateOp(t, store, res, "test-updated-cursor-state-final")
135+
res.Release()
136+
require.False(t, res.Finished())
137+
138+
defer op2.done(1) // cleanup after test
139+
140+
// this was the intermediate op, the resource should still be active
141+
op1.Execute(1)
142+
require.False(t, res.Finished())
143+
144+
// validate state (in memory state is always up to data to most recent update):
145+
inSyncCursor := storeInSyncSnapshot(store)["test::key"].Cursor
146+
inMemCursor := storeMemorySnapshot(store)["test::key"].Cursor
147+
assert.Equal(t, "test-updated-cursor-state-intermediate", inSyncCursor)
148+
assert.Equal(t, "test-updated-cursor-state-final", inMemCursor)
149+
})
150+
}
151+
152+
func mustCreateUpdateOp(t *testing.T, store *store, resource *resource, updates interface{}) *updateOp {
153+
op, err := createUpdateOp(store, resource, updates)
154+
if err != nil {
155+
t.Fatalf("Failed to create update op: %v", err)
156+
}
157+
return op
158+
}

0 commit comments

Comments
 (0)