forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Separate async and sync publisher into own files * Add Publish interface method which can be used to manually trigger publish (see elastic#2456) * Add getDataEvents function * Rename publish to publisher package
- Loading branch information
Showing
5 changed files
with
154 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package publisher | ||
|
||
import ( | ||
"expvar" | ||
|
||
"github.com/elastic/beats/filebeat/input" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/publisher" | ||
) | ||
|
||
var ( | ||
eventsSent = expvar.NewInt("publish.events") | ||
) | ||
|
||
type LogPublisher interface { | ||
Start() | ||
Stop() | ||
Publish() error | ||
} | ||
|
||
func New( | ||
async bool, | ||
in, out chan []*input.Event, | ||
pub publisher.Publisher, | ||
) LogPublisher { | ||
if async { | ||
return newAsyncLogPublisher(in, out, pub) | ||
} | ||
return newSyncLogPublisher(in, out, pub) | ||
} | ||
|
||
// getDataEvents returns all events which contain data (not only state updates) | ||
func getDataEvents(events []*input.Event) []common.MapStr { | ||
dataEvents := make([]common.MapStr, 0, len(events)) | ||
for _, event := range events { | ||
if event.HasData() { | ||
dataEvents = append(dataEvents, event.ToMapStr()) | ||
} | ||
} | ||
return dataEvents | ||
} |
2 changes: 1 addition & 1 deletion
2
filebeat/publish/publish_test.go → filebeat/publisher/publisher_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// +build !integration | ||
|
||
package publish | ||
package publisher | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package publisher | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/elastic/beats/filebeat/input" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/beats/libbeat/publisher" | ||
) | ||
|
||
type syncLogPublisher struct { | ||
pub publisher.Publisher | ||
client publisher.Client | ||
in, out chan []*input.Event | ||
|
||
done chan struct{} | ||
wg sync.WaitGroup | ||
} | ||
|
||
func newSyncLogPublisher( | ||
in, out chan []*input.Event, | ||
pub publisher.Publisher, | ||
) *syncLogPublisher { | ||
return &syncLogPublisher{ | ||
in: in, | ||
out: out, | ||
pub: pub, | ||
done: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (p *syncLogPublisher) Start() { | ||
p.client = p.pub.Connect() | ||
|
||
p.wg.Add(1) | ||
go func() { | ||
defer p.wg.Done() | ||
|
||
logp.Info("Start sending events to output") | ||
|
||
for { | ||
err := p.Publish() | ||
if err != nil { | ||
logp.Debug("publisher", "Shutting down sync publisher") | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (p *syncLogPublisher) Publish() error { | ||
var events []*input.Event | ||
select { | ||
case <-p.done: | ||
return errors.New("publishing was stopped") | ||
case events = <-p.in: | ||
} | ||
|
||
ok := p.client.PublishEvents(getDataEvents(events), publisher.Sync, publisher.Guaranteed) | ||
if !ok { | ||
// PublishEvents will only returns false, if p.client has been closed. | ||
return errors.New("publisher didn't published events") | ||
} | ||
|
||
logp.Debug("publish", "Events sent: %d", len(events)) | ||
eventsSent.Add(int64(len(events))) | ||
|
||
// Tell the registrar that we've successfully sent these events | ||
select { | ||
case <-p.done: | ||
return errors.New("publishing was stopped") | ||
case p.out <- events: | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *syncLogPublisher) Stop() { | ||
p.client.Close() | ||
close(p.done) | ||
p.wg.Wait() | ||
} |