Search in sources :

Example 16 with State

use of org.apache.gobblin.configuration.State in project incubator-gobblin by apache.

the class ThrottleWriterTest method testGetFinalState.

public void testGetFinalState() throws IOException {
    PartitionedDataWriter writer = mock(PartitionedDataWriter.class);
    when(writer.getFinalState()).thenReturn(new State());
    int parallelism = 2;
    int qps = 4;
    DataWriter<Void> throttleWriter = setup(writer, parallelism, qps, ThrottleType.QPS);
    State state = ((FinalState) throttleWriter).getFinalState();
    verify(writer, times(1)).getFinalState();
    Assert.assertTrue(state.contains(ThrottleWriter.THROTTLED_TIME_KEY));
}
Also used : State(org.apache.gobblin.configuration.State) FinalState(org.apache.gobblin.util.FinalState) FinalState(org.apache.gobblin.util.FinalState)

Example 17 with State

use of org.apache.gobblin.configuration.State in project incubator-gobblin by apache.

the class ThrottleWriterTest method setup.

private DataWriter<Void> setup(DataWriter<Void> writer, int parallelism, int rate, ThrottleType type) throws IOException {
    State state = new State();
    state.appendToSetProp(ThrottleWriter.WRITER_LIMIT_RATE_LIMIT_KEY, Integer.toString(rate * parallelism));
    state.appendToSetProp(ThrottleWriter.WRITER_THROTTLE_TYPE_KEY, type.name());
    state.appendToSetProp(ConfigurationKeys.TASK_EXECUTOR_THREADPOOL_SIZE_KEY, Integer.toString(parallelism));
    state.appendToSetProp(ConfigurationKeys.SOURCE_MAX_NUMBER_OF_PARTITIONS, Integer.toString(parallelism));
    DataWriterWrapperBuilder<Void> builder = new DataWriterWrapperBuilder<>(writer, state);
    return builder.build();
}
Also used : State(org.apache.gobblin.configuration.State) FinalState(org.apache.gobblin.util.FinalState)

Example 18 with State

use of org.apache.gobblin.configuration.State in project incubator-gobblin by apache.

the class SalesforceRestWriterTest method testBatchInsertDuplicate.

public void testBatchInsertDuplicate() throws IOException, URISyntaxException {
    final int recordSize = 25;
    final int batchSize = recordSize;
    State state = new State();
    state.appendToSetProp(CONF_PREFIX + BATCH_SIZE, Integer.toString(batchSize));
    state.appendToSetProp(CONF_PREFIX + BATCH_RESOURCE_PATH, "test");
    setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST, state);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(client.execute(any(HttpUriRequest.class))).thenReturn(response);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(200);
    HttpEntity entity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(entity);
    JsonObject jsonResponse = new JsonObject();
    jsonResponse.addProperty("hasErrors", true);
    JsonArray resultJsonArr = new JsonArray();
    jsonResponse.add("results", resultJsonArr);
    JsonObject subResult1 = new JsonObject();
    subResult1.addProperty("statusCode", 400);
    JsonArray subResultArr = new JsonArray();
    JsonObject errJson = new JsonObject();
    errJson.addProperty("errorCode", SalesforceRestWriter.DUPLICATE_VALUE_ERR_CODE);
    subResultArr.add(errJson);
    subResult1.add("result", subResultArr);
    JsonObject subResult2 = new JsonObject();
    subResult2.addProperty("statusCode", 400);
    subResult2.add("result", subResultArr);
    resultJsonArr.add(subResult1);
    resultJsonArr.add(subResult2);
    when(entity.getContent()).thenReturn(new ByteArrayInputStream(jsonResponse.toString().getBytes()));
    RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject());
    writer = spy(writer);
    for (int i = 0; i < recordSize; i++) {
        writer.write(restEntry);
    }
    writer.commit();
    Assert.assertEquals(writer.recordsWritten(), recordSize);
    verify(writer, times(recordSize)).writeImpl(restEntry);
    verify(writer, times(recordSize)).onNewRecord(restEntry);
    double sendCount = ((double) recordSize) / ((double) batchSize);
    sendCount = Math.ceil(sendCount);
    verify(writer, times((int) sendCount)).sendRequest(any(HttpUriRequest.class));
    verify(client, times((int) sendCount)).execute(any(HttpUriRequest.class));
    verify(writer, times((int) sendCount)).waitForResponse(any(ListenableFuture.class));
    verify(writer, times((int) sendCount)).processResponse(any(CloseableHttpResponse.class));
    verify(writer, times(1)).flush();
    verify(writer, never()).onConnect(any(URI.class));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) JsonObject(com.google.gson.JsonObject) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) JsonArray(com.google.gson.JsonArray) ByteArrayInputStream(java.io.ByteArrayInputStream) State(org.apache.gobblin.configuration.State) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) RestEntry(org.apache.gobblin.converter.http.RestEntry)

Example 19 with State

use of org.apache.gobblin.configuration.State in project incubator-gobblin by apache.

the class SalesforceRestWriterTest method testBatchInsertSuccess.

public void testBatchInsertSuccess() throws IOException, URISyntaxException {
    final int recordSize = 113;
    final int batchSize = 25;
    State state = new State();
    state.appendToSetProp(CONF_PREFIX + BATCH_SIZE, Integer.toString(batchSize));
    state.appendToSetProp(CONF_PREFIX + BATCH_RESOURCE_PATH, "test");
    setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST, state);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(client.execute(any(HttpUriRequest.class))).thenReturn(response);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(200);
    HttpEntity entity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(entity);
    JsonObject jsonResponse = new JsonObject();
    jsonResponse.addProperty("hasErrors", false);
    ByteArrayInputStream[] streams = new ByteArrayInputStream[recordSize];
    for (int i = 0; i < recordSize - 1; i++) {
        streams[i] = new ByteArrayInputStream(jsonResponse.toString().getBytes());
    }
    when(entity.getContent()).thenReturn(new ByteArrayInputStream(jsonResponse.toString().getBytes()), streams);
    RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject());
    writer = spy(writer);
    for (int i = 0; i < recordSize; i++) {
        writer.write(restEntry);
    }
    writer.commit();
    Assert.assertEquals(writer.recordsWritten(), recordSize);
    verify(writer, times(recordSize)).writeImpl(restEntry);
    verify(writer, times(recordSize)).onNewRecord(restEntry);
    double sendCount = ((double) recordSize) / ((double) batchSize);
    sendCount = Math.ceil(sendCount);
    verify(writer, times((int) sendCount)).sendRequest(any(HttpUriRequest.class));
    verify(client, times((int) sendCount)).execute(any(HttpUriRequest.class));
    verify(writer, times((int) sendCount)).waitForResponse(any(ListenableFuture.class));
    verify(writer, times((int) sendCount)).processResponse(any(CloseableHttpResponse.class));
    verify(writer, times(1)).flush();
    verify(writer, never()).onConnect(any(URI.class));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) JsonObject(com.google.gson.JsonObject) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) State(org.apache.gobblin.configuration.State) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) RestEntry(org.apache.gobblin.converter.http.RestEntry)

Example 20 with State

use of org.apache.gobblin.configuration.State in project incubator-gobblin by apache.

the class TimeBasedAvroWriterPartitionerTest method setUp.

@BeforeClass
public void setUp() throws IOException {
    File stagingDir = new File(STAGING_DIR);
    File outputDir = new File(OUTPUT_DIR);
    if (!stagingDir.exists()) {
        stagingDir.mkdirs();
    } else {
        FileUtils.deleteDirectory(stagingDir);
    }
    if (!outputDir.exists()) {
        outputDir.mkdirs();
    } else {
        FileUtils.deleteDirectory(outputDir);
    }
    this.schema = new Schema.Parser().parse(AVRO_SCHEMA);
    State properties = new State();
    properties.setProp(TimeBasedAvroWriterPartitioner.WRITER_PARTITION_COLUMNS, PARTITION_COLUMN_NAME);
    properties.setProp(ConfigurationKeys.WRITER_BUFFER_SIZE, ConfigurationKeys.DEFAULT_BUFFER_SIZE);
    properties.setProp(ConfigurationKeys.WRITER_FILE_SYSTEM_URI, ConfigurationKeys.LOCAL_FS_URI);
    properties.setProp(ConfigurationKeys.WRITER_STAGING_DIR, STAGING_DIR);
    properties.setProp(ConfigurationKeys.WRITER_OUTPUT_DIR, OUTPUT_DIR);
    properties.setProp(ConfigurationKeys.WRITER_FILE_PATH, BASE_FILE_PATH);
    properties.setProp(ConfigurationKeys.WRITER_FILE_NAME, FILE_NAME);
    properties.setProp(TimeBasedWriterPartitioner.WRITER_PARTITION_PATTERN, "yyyy/MM/dd");
    properties.setProp(ConfigurationKeys.WRITER_PARTITIONER_CLASS, TimeBasedAvroWriterPartitioner.class.getName());
    // Build a writer to write test records
    DataWriterBuilder<Schema, GenericRecord> builder = new AvroDataWriterBuilder().writeTo(Destination.of(Destination.DestinationType.HDFS, properties)).writeInFormat(WriterOutputFormat.AVRO).withWriterId(WRITER_ID).withSchema(this.schema).withBranches(1).forBranch(0);
    this.writer = new PartitionedDataWriter<Schema, GenericRecord>(builder, properties);
}
Also used : State(org.apache.gobblin.configuration.State) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) File(java.io.File) AvroDataWriterBuilder(org.apache.gobblin.writer.AvroDataWriterBuilder) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

State (org.apache.gobblin.configuration.State)195 Test (org.testng.annotations.Test)103 WorkUnitState (org.apache.gobblin.configuration.WorkUnitState)74 SourceState (org.apache.gobblin.configuration.SourceState)38 Path (org.apache.hadoop.fs.Path)30 File (java.io.File)20 IOException (java.io.IOException)16 Map (java.util.Map)14 WorkingState (org.apache.gobblin.configuration.WorkUnitState.WorkingState)14 WorkUnit (org.apache.gobblin.source.workunit.WorkUnit)14 TaskState (org.apache.hadoop.mapreduce.v2.api.records.TaskState)13 Properties (java.util.Properties)12 FinalState (org.apache.gobblin.util.FinalState)12 Configuration (org.apache.hadoop.conf.Configuration)12 TaskLevelPolicyCheckResults (org.apache.gobblin.qualitychecker.task.TaskLevelPolicyCheckResults)9 Config (com.typesafe.config.Config)8 ArrayList (java.util.ArrayList)8 GenericRecord (org.apache.avro.generic.GenericRecord)8 LongWatermark (org.apache.gobblin.source.extractor.extract.LongWatermark)7 FileInputStream (java.io.FileInputStream)6