-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow duplicate stream titles in route_to_stream (#154)
Cache stream ids and titles to avoid heavy database traffic during function evaluation Fixes #101
- Loading branch information
Showing
6 changed files
with
223 additions
and
25 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
126 changes: 126 additions & 0 deletions
126
...ain/java/org/graylog/plugins/pipelineprocessor/functions/messages/StreamCacheService.java
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,126 @@ | ||
/** | ||
* This file is part of Graylog Pipeline Processor. | ||
* | ||
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Graylog Pipeline Processor is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.graylog.plugins.pipelineprocessor.functions.messages; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.Maps; | ||
import com.google.common.collect.MultimapBuilder; | ||
import com.google.common.collect.Multimaps; | ||
import com.google.common.collect.SortedSetMultimap; | ||
import com.google.common.eventbus.EventBus; | ||
import com.google.common.eventbus.Subscribe; | ||
import com.google.common.util.concurrent.AbstractIdleService; | ||
|
||
import org.graylog2.database.NotFoundException; | ||
import org.graylog2.plugin.streams.Stream; | ||
import org.graylog2.streams.StreamService; | ||
import org.graylog2.streams.events.StreamsChangedEvent; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collection; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
@Singleton | ||
public class StreamCacheService extends AbstractIdleService { | ||
private static final Logger LOG = LoggerFactory.getLogger(StreamCacheService.class); | ||
|
||
private final EventBus eventBus; | ||
private final StreamService streamService; | ||
private final ScheduledExecutorService executorService; | ||
|
||
private final SortedSetMultimap<String, Stream> nameToStream = Multimaps.synchronizedSortedSetMultimap( | ||
MultimapBuilder.hashKeys() | ||
.treeSetValues(Comparator.comparing(Stream::getId)) | ||
.build()); | ||
private final Map<String, Stream> idToStream = Maps.newConcurrentMap(); | ||
|
||
@Inject | ||
public StreamCacheService(EventBus eventBus, | ||
StreamService streamService, | ||
@Named("daemonScheduler") ScheduledExecutorService executorService) { | ||
this.eventBus = eventBus; | ||
this.streamService = streamService; | ||
this.executorService = executorService; | ||
} | ||
|
||
@Override | ||
protected void startUp() throws Exception { | ||
streamService.loadAllEnabled().forEach(this::updateCache); | ||
eventBus.register(this); | ||
} | ||
|
||
@Override | ||
protected void shutDown() throws Exception { | ||
eventBus.unregister(this); | ||
} | ||
|
||
@Subscribe | ||
public void handleStreamUpdate(StreamsChangedEvent event) { | ||
executorService.schedule(() -> updateStreams(event.streamIds()), 0, TimeUnit.SECONDS); | ||
} | ||
|
||
@VisibleForTesting | ||
public void updateStreams(Collection<String> ids) { | ||
for (String id : ids) { | ||
LOG.debug("Updating stream id/title cache for id {}", id); | ||
try { | ||
final Stream stream = streamService.load(id); | ||
if (stream.getDisabled()) { | ||
purgeCache(stream.getId()); | ||
} else { | ||
updateCache(stream); | ||
} | ||
} catch (NotFoundException e) { | ||
// the stream was deleted, we only have to purge the existing entries | ||
purgeCache(id); | ||
} | ||
} | ||
} | ||
|
||
private void purgeCache(String id) { | ||
final Stream stream = idToStream.remove(id); | ||
LOG.debug("Purging stream id/title cache for id {}, stream {}", id, stream); | ||
if (stream != null) { | ||
nameToStream.remove(stream.getTitle(), stream); | ||
} | ||
} | ||
|
||
private void updateCache(Stream stream) { | ||
LOG.debug("Updating stream id/title cache for {}/'{}'", stream.getId(), stream.getTitle()); | ||
idToStream.put(stream.getId(), stream); | ||
nameToStream.put(stream.getTitle(), stream); | ||
} | ||
|
||
|
||
public Collection<Stream> getByName(String name) { | ||
return nameToStream.get(name); | ||
} | ||
|
||
@Nullable | ||
public Stream getById(String id) { | ||
return idToStream.get(id); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...java/org/graylog/plugins/pipelineprocessor/functions/messages/StreamCacheServiceTest.java
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,25 @@ | ||
package org.graylog.plugins.pipelineprocessor.functions.messages; | ||
|
||
import com.google.common.eventbus.EventBus; | ||
|
||
import org.graylog2.plugin.streams.Stream; | ||
import org.graylog2.streams.StreamService; | ||
import org.junit.Test; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.Executors; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class StreamCacheServiceTest { | ||
@Test | ||
public void getByName() throws Exception { | ||
final StreamCacheService streamCacheService = new StreamCacheService(new EventBus(), mock(StreamService.class), Executors.newSingleThreadScheduledExecutor()); | ||
|
||
// make sure getByName always returns a collection | ||
final Collection<Stream> streams = streamCacheService.getByName("nonexisting"); | ||
assertThat(streams).isNotNull().isEmpty(); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
plugin/src/test/resources/org/graylog/plugins/pipelineprocessor/functions/routeToStream.txt
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,5 @@ | ||
rule "stream routing" | ||
when true | ||
then | ||
route_to_stream(name: "some name"); | ||
end |