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

Refactored the PipelineParser/PipelineConfiguration to use the PipelineDataFlowModel #1680

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 @@ -7,6 +7,7 @@

import com.amazon.dataprepper.model.annotations.SingleThread;
import com.amazon.dataprepper.model.buffer.Buffer;
import com.amazon.dataprepper.model.configuration.PipelinesDataFlowModel;
import com.amazon.dataprepper.model.configuration.PluginSetting;
import com.amazon.dataprepper.model.peerforwarder.RequiresPeerForwarding;
import com.amazon.dataprepper.model.plugin.NoPluginFoundException;
Expand All @@ -15,14 +16,13 @@
import com.amazon.dataprepper.model.processor.Processor;
import com.amazon.dataprepper.model.sink.Sink;
import com.amazon.dataprepper.model.source.Source;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.opensearch.dataprepper.parser.model.PipelineConfiguration;
import org.opensearch.dataprepper.peerforwarder.PeerForwardingProcessorDecorator;
import org.opensearch.dataprepper.pipeline.Pipeline;
import org.opensearch.dataprepper.pipeline.PipelineConnector;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -59,10 +59,15 @@ public PipelineParser(final String pipelineConfigurationFileLocation, final Plug
*/
public Map<String, Pipeline> parseConfiguration() {
try {
final Map<String, PipelineConfiguration> pipelineConfigurationMap = OBJECT_MAPPER.readValue(
new File(pipelineConfigurationFileLocation),
new TypeReference<Map<String, PipelineConfiguration>>() {
});
final PipelinesDataFlowModel pipelinesDataFlowModel = OBJECT_MAPPER.readValue(new File(pipelineConfigurationFileLocation),
PipelinesDataFlowModel.class);

final Map<String, PipelineConfiguration> pipelineConfigurationMap = pipelinesDataFlowModel.getPipelines().entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> new PipelineConfiguration(entry.getValue())
));
final List<String> allPipelineNames = PipelineConfigurationValidator.validateAndGetPipelineNames(pipelineConfigurationMap);

// LinkedHashMap to preserve insertion order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,18 @@

package org.opensearch.dataprepper.parser.model;

import com.amazon.dataprepper.model.configuration.PipelineModel;
import com.amazon.dataprepper.model.configuration.PluginModel;
import com.amazon.dataprepper.model.configuration.PluginSetting;
import com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.lang.String.format;

public class PipelineConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(PipelineConfiguration.class);

/**
* @throws IllegalArgumentException If a non-null value is provided to both parameters.
* Guarantees only a prepper, processor, or neither is provided, not both.
* @param preppers Deserialized preppers plugin configuration, cannot be used in combination with the processors parameter, nullable
* @param processors Deserialized processors plugin configuration, cannot be used in combination with the preppers parameter, nullable
* @return the non-null parameter passed or null if both parameters are null.
*/
private static List<Map.Entry<String, Map<String, Object>>> validateProcessor(
final List<Map.Entry<String, Map<String, Object>>> preppers,
final List<Map.Entry<String, Map<String, Object>>> processors) {
if (preppers != null) {
LOG.warn("Prepper configurations are deprecated, processor configurations will be required in Data Prepper 2.0");
}

if (preppers != null && processors != null) {
final String message = "Pipeline configuration cannot specify a prepper and processor configuration. " +
"It is recommended to move prepper configurations to the processor section to maintain " +
"compatibility with DataPrepper version 1.2 and above.";
throw new IllegalArgumentException(message);
}
else if (preppers != null) {
return preppers;
}
else {
return processors;
}
}

private static final String WORKERS_COMPONENT = "workers";
private static final String DELAY_COMPONENT = "delay";
private static final int DEFAULT_READ_BATCH_DELAY = 3_000;
Expand All @@ -63,43 +29,13 @@ else if (preppers != null) {
private final Integer workers;
private final Integer readBatchDelay;

public PipelineConfiguration(
final Map.Entry<String, Map<String, Object>> source,
final Map.Entry<String, Map<String, Object>> buffer,
final List<Map.Entry<String, Map<String, Object>>> processors,
final List<Map.Entry<String, Map<String, Object>>> sinks,
final Integer workers,
final Integer delay) {
this.sourcePluginSetting = getSourceFromConfiguration(source);
this.bufferPluginSetting = getBufferFromConfigurationOrDefault(buffer);
this.processorPluginSettings = getProcessorsFromConfiguration(processors);
this.sinkPluginSettings = getSinksFromConfiguration(sinks);
this.workers = getWorkersFromConfiguration(workers);
this.readBatchDelay = getReadBatchDelayFromConfiguration(delay);
}

/**
* @since 1.2
* Constructor for deserialized Json data.
* @param source Deserialized source plugin configuration
* @param buffer Deserialized buffer plugin configuration, nullable
* @param preppers Deserialized preppers plugin configuration, cannot be used in combination with the processors parameter, nullable
* @param processors Deserialized processors plugin configuration, cannot be used in combination with the preppers parameter, nullable
* @param sinks Deserialized sinks plugin configuration
* @param workers Deserialized workers plugin configuration, nullable
* @param delay Deserialized delay plugin configuration, nullable
*/
@JsonCreator
@Deprecated
public PipelineConfiguration(
@JsonProperty("source") final Map.Entry<String, Map<String, Object>> source,
@JsonProperty("buffer") final Map.Entry<String, Map<String, Object>> buffer,
@Deprecated @JsonProperty("prepper") final List<Map.Entry<String, Map<String, Object>>> preppers,
@JsonProperty("processor") final List<Map.Entry<String, Map<String, Object>>> processors,
@JsonProperty("sink") final List<Map.Entry<String, Map<String, Object>>> sinks,
@JsonProperty("workers") final Integer workers,
@JsonProperty("delay") final Integer delay) {
this(source, buffer, validateProcessor(preppers, processors), sinks, workers, delay);
public PipelineConfiguration(final PipelineModel pipelineModel) {
this.sourcePluginSetting = getSourceFromPluginModel(pipelineModel.getSource());
this.bufferPluginSetting = getBufferFromPluginModelOrDefault(pipelineModel.getBuffer());
this.processorPluginSettings = getProcessorsFromPluginModel(pipelineModel.getProcessors());
this.sinkPluginSettings = getSinksFromPluginModel(pipelineModel.getSinks());
this.workers = getWorkersFromPipelineModel(pipelineModel);
this.readBatchDelay = getReadBatchDelayFromPipelineModel(pipelineModel);
}

public PluginSetting getSourcePluginSetting() {
Expand Down Expand Up @@ -141,62 +77,61 @@ private void updatePluginSetting(
pluginSetting.setProcessWorkers(this.workers);
}

private PluginSetting getSourceFromConfiguration(final Map.Entry<String, Map<String, Object>> sourceConfiguration) {
if (sourceConfiguration == null) {
private PluginSetting getSourceFromPluginModel(final PluginModel pluginModel) {
if (pluginModel == null) {
throw new IllegalArgumentException("Invalid configuration, source is a required component");
}
return getPluginSettingFromConfiguration(sourceConfiguration);
return getPluginSettingFromPluginModel(pluginModel);
}

private PluginSetting getBufferFromConfigurationOrDefault(
final Map.Entry<String, Map<String, Object>> bufferConfiguration) {
if (bufferConfiguration == null) {
private PluginSetting getBufferFromPluginModelOrDefault(
final PluginModel pluginModel) {
if (pluginModel == null) {
return BlockingBuffer.getDefaultPluginSettings();
}
return getPluginSettingFromConfiguration(bufferConfiguration);
return getPluginSettingFromPluginModel(pluginModel);
}

private List<PluginSetting> getSinksFromConfiguration(
final List<Map.Entry<String, Map<String, Object>>> sinkConfigurations) {
private List<PluginSetting> getSinksFromPluginModel(
final List<PluginModel> sinkConfigurations) {
if (sinkConfigurations == null || sinkConfigurations.isEmpty()) {
throw new IllegalArgumentException("Invalid configuration, at least one sink is required");
}
return sinkConfigurations.stream().map(PipelineConfiguration::getPluginSettingFromConfiguration)
return sinkConfigurations.stream().map(PipelineConfiguration::getPluginSettingFromPluginModel)
.collect(Collectors.toList());
}

private List<PluginSetting> getProcessorsFromConfiguration(
final List<Map.Entry<String, Map<String, Object>>> processorConfigurations) {
private List<PluginSetting> getProcessorsFromPluginModel(
final List<PluginModel> processorConfigurations) {
if (processorConfigurations == null || processorConfigurations.isEmpty()) {
return Collections.emptyList();
}
return processorConfigurations.stream().map(PipelineConfiguration::getPluginSettingFromConfiguration)
return processorConfigurations.stream().map(PipelineConfiguration::getPluginSettingFromPluginModel)
.collect(Collectors.toList());
}


private static PluginSetting getPluginSettingFromConfiguration(
final Map.Entry<String, Map<String, Object>> configuration) {
final Map<String, Object> settingsMap = configuration.getValue();
//PluginSettings is required to update pipeline name
return new PluginSetting(configuration.getKey(), settingsMap == null ? new HashMap<>() : settingsMap);
private static PluginSetting getPluginSettingFromPluginModel(final PluginModel pluginModel) {
final Map<String, Object> settingsMap = pluginModel.getPluginSettings();
return new PluginSetting(pluginModel.getPluginName(), settingsMap == null ? new HashMap<>() : settingsMap);
}

private Integer getWorkersFromConfiguration(final Integer workersConfiguration) {
final Integer configuredWorkers = getValueFromConfiguration(workersConfiguration, WORKERS_COMPONENT);
private Integer getWorkersFromPipelineModel(final PipelineModel pipelineModel) {
final Integer configuredWorkers = pipelineModel.getWorkers();
validateConfiguration(configuredWorkers, WORKERS_COMPONENT);
return configuredWorkers == null ? DEFAULT_WORKERS : configuredWorkers;
}

private Integer getReadBatchDelayFromConfiguration(final Integer delayConfiguration) {
final Integer configuredDelay = getValueFromConfiguration(delayConfiguration, DELAY_COMPONENT);
private Integer getReadBatchDelayFromPipelineModel(final PipelineModel pipelineModel) {
final Integer configuredDelay = pipelineModel.getReadBatchDelay();
validateConfiguration(configuredDelay, DELAY_COMPONENT);
return configuredDelay == null ? DEFAULT_READ_BATCH_DELAY : configuredDelay;
}

private Integer getValueFromConfiguration(final Integer configuration, final String component) {
private void validateConfiguration(final Integer configuration, final String component) {
if (configuration != null && configuration <= 0) {
throw new IllegalArgumentException(format("Invalid configuration, %s cannot be %s",
throw new IllegalArgumentException(String.format("Invalid configuration, %s cannot be %s",
component, configuration));
}
return configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.opensearch.dataprepper;

import com.amazon.dataprepper.model.configuration.PluginModel;
import com.amazon.dataprepper.model.configuration.PluginSetting;
import org.opensearch.dataprepper.parser.model.PipelineConfiguration;
import com.fasterxml.jackson.core.type.TypeReference;
Expand All @@ -22,6 +23,9 @@
import java.util.Map;
import java.util.Set;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TestDataProvider {
public static final String TEST_PIPELINE_NAME = "test-pipeline-1";
public static final String TEST_PLUGIN_NAME_1 = "test-plugin-1";
Expand Down Expand Up @@ -84,16 +88,23 @@ private static Map<String, Object> validSettingsForPlugin() {
return settingsMap;
}

public static Map.Entry<String, Map<String, Object>> validSingleConfiguration() {
return Map.entry(TEST_PLUGIN_NAME_1, validSettingsForPlugin());
public static PluginModel validSingleConfiguration() {
return validPluginModel(TEST_PLUGIN_NAME_1);
}

private static PluginModel validPluginModel(final String pluginName) {
final PluginModel pluginModel = mock(PluginModel.class);
when(pluginModel.getPluginName()).thenReturn(pluginName);
when(pluginModel.getPluginSettings()).thenReturn(validSettingsForPlugin());
return pluginModel;
}

public static List<Map.Entry<String, Map<String, Object>>> validMultipleConfiguration() {
return Arrays.asList(Map.entry(TEST_PLUGIN_NAME_1, validSettingsForPlugin()), Map.entry(TEST_PLUGIN_NAME_2, validSettingsForPlugin()));
public static List<PluginModel> validMultipleConfiguration() {
return Arrays.asList(validPluginModel(TEST_PLUGIN_NAME_1), validPluginModel(TEST_PLUGIN_NAME_2));
}

public static List<Map.Entry<String, Map<String, Object>>> validMultipleConfigurationOfSizeOne() {
return Collections.singletonList(Map.entry(TEST_PLUGIN_NAME_1, validSettingsForPlugin()));
public static List<PluginModel> validMultipleConfigurationOfSizeOne() {
return Collections.singletonList(validSingleConfiguration());
}

public static Map<String, PipelineConfiguration> readConfigFile(final String configFilePath) throws IOException {
Expand Down
Loading