Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waiting for data adapter/cache creation in bundle importer. #4197

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.graylog2.inputs.InputService;
import org.graylog2.inputs.converters.ConverterFactory;
import org.graylog2.inputs.extractors.ExtractorFactory;
import org.graylog2.lookup.LookupTableService;
import org.graylog2.lookup.db.DBCacheService;
import org.graylog2.lookup.db.DBDataAdapterService;
import org.graylog2.lookup.db.DBLookupTableService;
Expand All @@ -39,7 +40,9 @@
import org.graylog2.timeranges.TimeRangeFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import java.util.concurrent.ScheduledExecutorService;

public class BundleImporterProvider implements Provider<BundleImporter> {

Expand All @@ -60,9 +63,11 @@ public class BundleImporterProvider implements Provider<BundleImporter> {
private final DBLookupTableService dbLookupTableService;
private final DBCacheService dbCacheService;
private final DBDataAdapterService dbDataAdapterService;
private final LookupTableService lookupTableService;
private final TimeRangeFactory timeRangeFactory;
private final ClusterEventBus clusterBus;
private final ObjectMapper objectMapper;
private final ScheduledExecutorService scheduler;

@Inject
public BundleImporterProvider(final InputService inputService,
Expand All @@ -82,9 +87,11 @@ public BundleImporterProvider(final InputService inputService,
final DBLookupTableService dbLookupTableService,
final DBCacheService dbCacheService,
final DBDataAdapterService dbDataAdapterService,
final LookupTableService lookupTableService,
final TimeRangeFactory timeRangeFactory,
final ClusterEventBus clusterBus,
final ObjectMapper objectMapper) {
final ObjectMapper objectMapper,
@Named("daemonScheduler") ScheduledExecutorService scheduler) {
this.inputService = inputService;
this.inputRegistry = inputRegistry;
this.extractorFactory = extractorFactory;
Expand All @@ -102,9 +109,11 @@ public BundleImporterProvider(final InputService inputService,
this.dbLookupTableService = dbLookupTableService;
this.dbCacheService = dbCacheService;
this.dbDataAdapterService = dbDataAdapterService;
this.lookupTableService = lookupTableService;
this.timeRangeFactory = timeRangeFactory;
this.clusterBus = clusterBus;
this.objectMapper = objectMapper;
this.scheduler = scheduler;
}

@Override
Expand All @@ -113,7 +122,7 @@ public BundleImporter get() {
streamService, streamRuleService, indexSetRegistry, outputService, dashboardService,
dashboardWidgetCreator, serverStatus, messageInputFactory,
inputLauncher, grokPatternService,
dbLookupTableService, dbCacheService, dbDataAdapterService,
timeRangeFactory, clusterBus, objectMapper);
dbLookupTableService, dbCacheService, dbDataAdapterService, lookupTableService,
timeRangeFactory, clusterBus, objectMapper, scheduler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.graylog2.bundles;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand All @@ -35,12 +34,15 @@
import org.graylog2.inputs.InputService;
import org.graylog2.inputs.converters.ConverterFactory;
import org.graylog2.inputs.extractors.ExtractorFactory;
import org.graylog2.lookup.LookupTableService;
import org.graylog2.lookup.db.DBCacheService;
import org.graylog2.lookup.db.DBDataAdapterService;
import org.graylog2.lookup.db.DBLookupTableService;
import org.graylog2.lookup.dto.CacheDto;
import org.graylog2.lookup.dto.DataAdapterDto;
import org.graylog2.lookup.dto.LookupTableDto;
import org.graylog2.lookup.events.CachesUpdated;
import org.graylog2.lookup.events.DataAdaptersUpdated;
import org.graylog2.lookup.events.LookupTablesDeleted;
import org.graylog2.lookup.events.LookupTablesUpdated;
import org.graylog2.plugin.Message;
Expand All @@ -52,7 +54,9 @@
import org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException;
import org.graylog2.plugin.indexer.searches.timeranges.TimeRange;
import org.graylog2.plugin.inputs.MessageInput;
import org.graylog2.plugin.lookup.LookupCache;
import org.graylog2.plugin.lookup.LookupCacheConfiguration;
import org.graylog2.plugin.lookup.LookupDataAdapter;
import org.graylog2.plugin.lookup.LookupDataAdapterConfiguration;
import org.graylog2.rest.models.dashboards.requests.WidgetPositionsRequest;
import org.graylog2.shared.inputs.InputLauncher;
Expand All @@ -67,18 +71,24 @@
import org.graylog2.streams.StreamService;
import org.graylog2.streams.events.StreamsChangedEvent;
import org.graylog2.timeranges.TimeRangeFactory;
import org.graylog2.utilities.LatchUpdaterListener;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.graylog2.plugin.inputs.Extractor.Type.GROK;
Expand All @@ -104,9 +114,11 @@ public class BundleImporter {
private final DBLookupTableService dbLookupTableService;
private final DBCacheService dbCacheService;
private final DBDataAdapterService dbDataAdapterService;
private final LookupTableService lookupTableService;
private final TimeRangeFactory timeRangeFactory;
private final ClusterEventBus clusterBus;
private final ObjectMapper objectMapper;
private final ScheduledExecutorService scheduler;

private final Map<String, org.graylog2.grok.GrokPattern> createdGrokPatterns = new HashMap<>();
private final Map<String, MessageInput> createdInputs = new HashMap<>();
Expand All @@ -119,6 +131,7 @@ public class BundleImporter {
private final Map<String, org.graylog2.plugin.streams.Output> outputsByReferenceId = new HashMap<>();
private final Map<String, org.graylog2.plugin.streams.Stream> streamsByReferenceId = new HashMap<>();

// TODO: This class has become HUGE, it should be split into smaller classes.
@Inject
public BundleImporter(final InputService inputService,
final InputRegistry inputRegistry,
Expand All @@ -137,9 +150,11 @@ public BundleImporter(final InputService inputService,
final DBLookupTableService dbLookupTableService,
final DBCacheService dbCacheService,
final DBDataAdapterService dbDataAdapterService,
final LookupTableService lookupTableService,
final TimeRangeFactory timeRangeFactory,
final ClusterEventBus clusterBus,
final ObjectMapper objectMapper) {
final ObjectMapper objectMapper,
@Named("daemonScheduler") ScheduledExecutorService scheduler) {
this.inputService = inputService;
this.inputRegistry = inputRegistry;
this.extractorFactory = extractorFactory;
Expand All @@ -157,9 +172,11 @@ public BundleImporter(final InputService inputService,
this.dbLookupTableService = dbLookupTableService;
this.dbCacheService = dbCacheService;
this.dbDataAdapterService = dbDataAdapterService;
this.lookupTableService = lookupTableService;
this.timeRangeFactory = timeRangeFactory;
this.clusterBus = clusterBus;
this.objectMapper = objectMapper;
this.scheduler = scheduler;
}

public void runImport(final ConfigurationBundle bundle, final String userName) {
Expand All @@ -180,7 +197,7 @@ public void runImport(final ConfigurationBundle bundle, final String userName) {
if (!rollback()) {
LOG.error("Rollback unsuccessful.");
}
Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -695,7 +712,9 @@ private void createLookupTables(String bundleId, Set<LookupTableBundle> lookupTa
createdLookupTables.put(dto.id(), dto);
}

clusterBus.post(LookupTablesUpdated.create(createdLookupTables.values()));
if (!createdLookupDataAdapters.isEmpty()) {
clusterBus.post(LookupTablesUpdated.create(createdLookupTables.values()));
}
}

private void createLookupCaches(String bundleId, Set<LookupCacheBundle> lookupCaches) {
Expand All @@ -709,6 +728,22 @@ private void createLookupCaches(String bundleId, Set<LookupCacheBundle> lookupCa
.build());
createdLookupCaches.put(dto.id(), dto);
}

if (!createdLookupCaches.isEmpty()) {
clusterBus.post(CachesUpdated.create(createdLookupCaches.keySet()));
}

final Collection<LookupCache> caches = lookupTableService.getCaches(createdLookupCaches.keySet());
final CountDownLatch latch = new CountDownLatch(caches.size());
caches.forEach(c -> c.addListener(new LatchUpdaterListener(latch), scheduler));

try {
if (!latch.await(30, TimeUnit.SECONDS)) {
LOG.warn("Starting imported Lookup Table Caches did not finish within 30 seconds. A server restart might be required for imported Lookup Tables to function.");
}
} catch (InterruptedException e) {
LOG.warn("Starting imported Lookup Table Caches did not finish properly. A server restart might be required for imported Lookup Tables to function: ", e);
}
}

private void createLookupDataAdapters(String bundleId, Set<LookupDataAdapterBundle> lookupDataAdapters) {
Expand All @@ -722,5 +757,21 @@ private void createLookupDataAdapters(String bundleId, Set<LookupDataAdapterBund
.build());
createdLookupDataAdapters.put(dto.id(), dto);
}

if (!createdLookupDataAdapters.isEmpty()) {
clusterBus.post(DataAdaptersUpdated.create(createdLookupDataAdapters.keySet()));
}

final Collection<LookupDataAdapter> dataAdapters = lookupTableService.getDataAdapters(createdLookupDataAdapters.keySet());
final CountDownLatch latch = new CountDownLatch(dataAdapters.size());
dataAdapters.forEach(da -> da.addListener(new LatchUpdaterListener(latch), scheduler));

try {
if (!latch.await(30, TimeUnit.SECONDS)) {
LOG.warn("Starting imported Lookup Table Data Adapters did not finish within 30 seconds. A server restart might be required for imported Lookup Tables to function.");
}
} catch (InterruptedException e) {
LOG.warn("Starting imported Lookup Table Data Adapters did not finish properly. A server restart might be required for imported Lookup Tables to function: ", e);
}
}
}