Search in sources :

Example 1 with DataflowPipelineJob

use of org.apache.beam.runners.dataflow.DataflowPipelineJob in project java-docs-samples by GoogleCloudPlatform.

the class WorkloadGeneratorTest method testPipeline.

@Test
public void testPipeline() throws IOException, InterruptedException {
    String workloadJobName = "bigtable-workload-generator-test-" + new Date().getTime();
    final int WORKLOAD_DURATION = 10;
    final int WAIT_DURATION = WORKLOAD_DURATION * 60 * 1000;
    int rate = 1000;
    BigtableWorkloadOptions options = PipelineOptionsFactory.create().as(BigtableWorkloadOptions.class);
    options.setBigtableInstanceId(instanceId);
    options.setBigtableTableId(TABLE_ID);
    options.setWorkloadRate(rate);
    options.setRegion(REGION_ID);
    options.setWorkloadDurationMinutes(WORKLOAD_DURATION);
    options.setRunner(DataflowRunner.class);
    options.setJobName(workloadJobName);
    final PipelineResult pipelineResult = WorkloadGenerator.generateWorkload(options);
    MetricServiceClient metricServiceClient = MetricServiceClient.create();
    ProjectName name = ProjectName.of(projectId);
    // Wait X minutes and then get metrics for the X minute period.
    Thread.sleep(WAIT_DURATION);
    long startMillis = System.currentTimeMillis() - WAIT_DURATION;
    TimeInterval interval = TimeInterval.newBuilder().setStartTime(Timestamps.fromMillis(startMillis)).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
    ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder().setName(name.toString()).setFilter("metric.type=\"bigtable.googleapis.com/server/request_count\"").setInterval(interval).build();
    ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
    long startRequestCount = 0;
    long endRequestCount = 0;
    for (TimeSeries ts : response.iterateAll()) {
        startRequestCount = ts.getPoints(0).getValue().getInt64Value();
        endRequestCount = ts.getPoints(ts.getPointsCount() - 1).getValue().getInt64Value();
    }
    assertThat(endRequestCount - startRequestCount > rate);
    // Stop the running job.
    String jobId = ((DataflowPipelineJob) pipelineResult).getJobId();
    DataflowClient client = DataflowClient.create(options);
    Job job = client.getJob(jobId);
    assertThat(job.getCurrentState().equals("JOB_STATE_CANCELLED"));
}
Also used : TimeSeries(com.google.bigtable.repackaged.com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval) ProjectName(com.google.bigtable.repackaged.com.google.monitoring.v3.ProjectName) PipelineResult(org.apache.beam.sdk.PipelineResult) ListTimeSeriesPagedResponse(com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) Date(java.util.Date) ListTimeSeriesRequest(com.google.bigtable.repackaged.com.google.monitoring.v3.ListTimeSeriesRequest) BigtableWorkloadOptions(bigtable.WorkloadGenerator.BigtableWorkloadOptions) DataflowPipelineJob(org.apache.beam.runners.dataflow.DataflowPipelineJob) DataflowClient(org.apache.beam.runners.dataflow.DataflowClient) Job(com.google.api.services.dataflow.model.Job) DataflowPipelineJob(org.apache.beam.runners.dataflow.DataflowPipelineJob) Test(org.junit.Test)

Example 2 with DataflowPipelineJob

use of org.apache.beam.runners.dataflow.DataflowPipelineJob in project java-docs-samples by GoogleCloudPlatform.

the class WorkloadGenerator method cancelJob.

private static void cancelJob(BigtableWorkloadOptions options, DataflowPipelineJob pipelineResult) throws IOException {
    String jobId = pipelineResult.getJobId();
    DataflowClient client = DataflowClient.create(options);
    Job job = client.getJob(jobId);
    job.setRequestedState("JOB_STATE_CANCELLED");
    client.updateJob(jobId, job);
}
Also used : DataflowClient(org.apache.beam.runners.dataflow.DataflowClient) DataflowPipelineJob(org.apache.beam.runners.dataflow.DataflowPipelineJob) Job(com.google.api.services.dataflow.model.Job)

Aggregations

Job (com.google.api.services.dataflow.model.Job)2 DataflowClient (org.apache.beam.runners.dataflow.DataflowClient)2 DataflowPipelineJob (org.apache.beam.runners.dataflow.DataflowPipelineJob)2 BigtableWorkloadOptions (bigtable.WorkloadGenerator.BigtableWorkloadOptions)1 MetricServiceClient (com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient)1 ListTimeSeriesPagedResponse (com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)1 ListTimeSeriesRequest (com.google.bigtable.repackaged.com.google.monitoring.v3.ListTimeSeriesRequest)1 ProjectName (com.google.bigtable.repackaged.com.google.monitoring.v3.ProjectName)1 TimeInterval (com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval)1 TimeSeries (com.google.bigtable.repackaged.com.google.monitoring.v3.TimeSeries)1 Date (java.util.Date)1 PipelineResult (org.apache.beam.sdk.PipelineResult)1 Test (org.junit.Test)1