Search in sources :

Example 1 with PluginFactory

use of com.amazon.dataprepper.model.plugin.PluginFactory in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse415.

@Test
public void testHTTPJsonResponse415() {
    // Prepare
    final int testMaxPendingRequests = 1;
    final int testThreadCount = 1;
    final int serverTimeoutInMillis = 500;
    when(sourceConfig.getRequestTimeoutInMillis()).thenReturn(serverTimeoutInMillis);
    when(sourceConfig.getMaxPendingRequests()).thenReturn(testMaxPendingRequests);
    when(sourceConfig.getThreadCount()).thenReturn(testThreadCount);
    HTTPSourceUnderTest = new HTTPSource(sourceConfig, pluginMetrics, pluginFactory);
    // Start the source
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    final RequestHeaders testRequestHeaders = RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build();
    final HttpData testHttpData = HttpData.ofUtf8("[{\"log\": \"somelog\"}]");
    // Fill in the buffer
    WebClient.of().execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.OK)).join();
    // Disable client timeout
    WebClient testWebClient = WebClient.builder().responseTimeoutMillis(0).build();
    // When/Then
    testWebClient.execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.REQUEST_TIMEOUT)).join();
    // verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(2.0, requestReceivedCount.getValue());
    final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, successRequestsCount.getValue());
    final Measurement requestTimeoutsCount = MetricsTestUtil.getMeasurementFromList(requestTimeoutsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestTimeoutsCount.getValue());
    final Measurement requestProcessDurationMax = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.MAX);
    final double maxDurationInMillis = 1000 * requestProcessDurationMax.getValue();
    Assertions.assertTrue(maxDurationInMillis > serverTimeoutInMillis);
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) Measurement(io.micrometer.core.instrument.Measurement) HttpData(com.linecorp.armeria.common.HttpData) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) WebClient(com.linecorp.armeria.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 2 with PluginFactory

use of com.amazon.dataprepper.model.plugin.PluginFactory in project data-prepper by opensearch-project.

the class DataPrepperServerConfigurationTest method testGivenPluginFactoryAndPluginSettingsThenCreateAuthenticationProvider.

@Test
public void testGivenPluginFactoryAndPluginSettingsThenCreateAuthenticationProvider() {
    final PluginFactory pluginFactory = mock(PluginFactory.class);
    final PluginSetting pluginSetting = mock(PluginSetting.class);
    final DataPrepperCoreAuthenticationProvider expected = mock(DataPrepperCoreAuthenticationProvider.class);
    when(pluginFactory.loadPlugin(eq(DataPrepperCoreAuthenticationProvider.class), eq(pluginSetting))).thenReturn(expected);
    final DataPrepperCoreAuthenticationProvider authenticationProvider = serverConfiguration.authenticationProvider(pluginFactory, pluginSetting);
    assertThat(authenticationProvider, is(expected));
}
Also used : DataPrepperCoreAuthenticationProvider(com.amazon.dataprepper.pipeline.server.DataPrepperCoreAuthenticationProvider) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Test(org.junit.jupiter.api.Test)

Example 3 with PluginFactory

use of com.amazon.dataprepper.model.plugin.PluginFactory in project data-prepper by opensearch-project.

the class PluginArgumentsContextTest method createArguments_with_pluginFactory_should_return_the_instance_from_the_builder.

@Test
void createArguments_with_pluginFactory_should_return_the_instance_from_the_builder() {
    final PluginFactory pluginFactory = mock(PluginFactory.class);
    final PluginArgumentsContext objectUnderTest = new PluginArgumentsContext.Builder().withPluginSetting(pluginSetting).withPluginFactory(pluginFactory).build();
    assertThat(objectUnderTest.createArguments(new Class[] { PluginFactory.class }), equalTo(new Object[] { pluginFactory }));
}
Also used : PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Test(org.junit.jupiter.api.Test)

Example 4 with PluginFactory

use of com.amazon.dataprepper.model.plugin.PluginFactory in project data-prepper by opensearch-project.

the class HTTPSourceTest method setUp.

@BeforeEach
public void setUp() {
    lenient().when(serverBuilder.annotatedService(any())).thenReturn(serverBuilder);
    lenient().when(serverBuilder.http(anyInt())).thenReturn(serverBuilder);
    lenient().when(serverBuilder.https(anyInt())).thenReturn(serverBuilder);
    lenient().when(serverBuilder.build()).thenReturn(server);
    lenient().when(server.start()).thenReturn(completableFuture);
    sourceConfig = mock(HTTPSourceConfig.class);
    lenient().when(sourceConfig.getPort()).thenReturn(2021);
    lenient().when(sourceConfig.getRequestTimeoutInMillis()).thenReturn(10_000);
    lenient().when(sourceConfig.getThreadCount()).thenReturn(200);
    lenient().when(sourceConfig.getMaxConnectionCount()).thenReturn(500);
    lenient().when(sourceConfig.getMaxPendingRequests()).thenReturn(1024);
    lenient().when(sourceConfig.hasHealthCheckService()).thenReturn(true);
    MetricsTestUtil.initMetrics();
    pluginMetrics = PluginMetrics.fromNames(PLUGIN_NAME, TEST_PIPELINE_NAME);
    pluginFactory = mock(PluginFactory.class);
    final ArmeriaHttpAuthenticationProvider authenticationProvider = mock(ArmeriaHttpAuthenticationProvider.class);
    when(pluginFactory.loadPlugin(eq(ArmeriaHttpAuthenticationProvider.class), any(PluginSetting.class))).thenReturn(authenticationProvider);
    testBuffer = getBuffer();
    HTTPSourceUnderTest = new HTTPSource(sourceConfig, pluginMetrics, pluginFactory);
}
Also used : ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with PluginFactory

use of com.amazon.dataprepper.model.plugin.PluginFactory in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse429.

@Test
public void testHTTPJsonResponse429() throws InterruptedException {
    // Prepare
    final Map<String, Object> settings = new HashMap<>();
    final int testMaxPendingRequests = 1;
    final int testThreadCount = 1;
    final int clientTimeoutInMillis = 100;
    final int serverTimeoutInMillis = (testMaxPendingRequests + testThreadCount + 1) * clientTimeoutInMillis;
    when(sourceConfig.getRequestTimeoutInMillis()).thenReturn(serverTimeoutInMillis);
    when(sourceConfig.getMaxPendingRequests()).thenReturn(testMaxPendingRequests);
    when(sourceConfig.getThreadCount()).thenReturn(testThreadCount);
    HTTPSourceUnderTest = new HTTPSource(sourceConfig, pluginMetrics, pluginFactory);
    // Start the source
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    final RequestHeaders testRequestHeaders = RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build();
    final HttpData testHttpData = HttpData.ofUtf8("[{\"log\": \"somelog\"}]");
    // Fill in the buffer
    WebClient.of().execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.OK)).join();
    // Send requests to throttle the server when buffer is full
    // Set the client timeout to be less than source serverTimeoutInMillis / (testMaxPendingRequests + testThreadCount)
    WebClient testWebClient = WebClient.builder().responseTimeoutMillis(clientTimeoutInMillis).build();
    for (int i = 0; i < testMaxPendingRequests + testThreadCount; i++) {
        CompletionException actualException = Assertions.assertThrows(CompletionException.class, () -> testWebClient.execute(testRequestHeaders, testHttpData).aggregate().join());
        assertThat(actualException.getCause(), instanceOf(ResponseTimeoutException.class));
    }
    // When/Then
    testWebClient.execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.TOO_MANY_REQUESTS)).join();
    // Wait until source server timeout a request processing thread
    Thread.sleep(serverTimeoutInMillis);
    // New request should timeout instead of being rejected
    CompletionException actualException = Assertions.assertThrows(CompletionException.class, () -> testWebClient.execute(testRequestHeaders, testHttpData).aggregate().join());
    assertThat(actualException.getCause(), instanceOf(ResponseTimeoutException.class));
    // verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(testMaxPendingRequests + testThreadCount + 2, requestReceivedCount.getValue());
    final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, successRequestsCount.getValue());
    final Measurement rejectedRequestsCount = MetricsTestUtil.getMeasurementFromList(rejectedRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, rejectedRequestsCount.getValue());
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Measurement(io.micrometer.core.instrument.Measurement) HashMap(java.util.HashMap) AsciiString(io.netty.util.AsciiString) WebClient(com.linecorp.armeria.client.WebClient) HttpData(com.linecorp.armeria.common.HttpData) CompletionException(java.util.concurrent.CompletionException) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) Test(org.junit.jupiter.api.Test)

Aggregations

PluginFactory (com.amazon.dataprepper.model.plugin.PluginFactory)5 PluginSetting (com.amazon.dataprepper.model.configuration.PluginSetting)4 ArmeriaHttpAuthenticationProvider (com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider)3 Test (org.junit.jupiter.api.Test)3 MetricNames (com.amazon.dataprepper.metrics.MetricNames)2 MetricsTestUtil (com.amazon.dataprepper.metrics.MetricsTestUtil)2 PluginMetrics (com.amazon.dataprepper.metrics.PluginMetrics)2 CheckpointState (com.amazon.dataprepper.model.CheckpointState)2 Log (com.amazon.dataprepper.model.log.Log)2 Record (com.amazon.dataprepper.model.record.Record)2 BlockingBuffer (com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer)2 ClientFactory (com.linecorp.armeria.client.ClientFactory)2 ResponseTimeoutException (com.linecorp.armeria.client.ResponseTimeoutException)2 WebClient (com.linecorp.armeria.client.WebClient)2 AggregatedHttpResponse (com.linecorp.armeria.common.AggregatedHttpResponse)2 HttpData (com.linecorp.armeria.common.HttpData)2 HttpMethod (com.linecorp.armeria.common.HttpMethod)2 HttpStatus (com.linecorp.armeria.common.HttpStatus)2 MediaType (com.linecorp.armeria.common.MediaType)2 RequestHeaders (com.linecorp.armeria.common.RequestHeaders)2