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

feat: migrate code from googleapis/java-batch #7364

Merged
merged 33 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6690048
feat: initial generation
Neenu1995 Jun 10, 2022
e6525ff
chore: fix dependencies
Neenu1995 Jun 10, 2022
d15bada
chore(main): release 0.1.0
release-please[bot] Jun 15, 2022
eb4c76c
chore(main): release 0.1.1-SNAPSHOT (#4)
release-please[bot] Jun 16, 2022
392b676
chore(deps): update dependency com.google.cloud:google-cloud-batch to…
renovate-bot Jun 21, 2022
96e2367
chore(main): release 0.1.1 (#8)
release-please[bot] Jun 23, 2022
abeae44
chore(main): release 0.1.2-SNAPSHOT (#9)
release-please[bot] Jun 23, 2022
4c407a6
chore(deps): update dependency com.google.cloud:google-cloud-batch to…
renovate-bot Jun 24, 2022
0a99ac4
chore(main): release 0.2.0 (#11)
release-please[bot] Jul 1, 2022
d24e1bf
chore(main): release 0.2.1-SNAPSHOT (#15)
release-please[bot] Jul 1, 2022
a9d4975
chore(main): release 0.2.1 (#19)
release-please[bot] Jul 14, 2022
f8dd33a
build(deps): update dependency org.apache.maven.plugins:maven-deploy-…
renovate-bot Jul 19, 2022
4e88014
chore(deps): update dependency com.google.cloud:google-cloud-batch to…
renovate-bot Aug 2, 2022
1501ca8
chore(main): release 0.2.2-SNAPSHOT (#20)
release-please[bot] Aug 9, 2022
838ea02
chore(main): release 0.2.2 (#29)
release-please[bot] Aug 9, 2022
7c40b8c
chore(main): release 0.2.3-SNAPSHOT (#30)
release-please[bot] Aug 9, 2022
c99e810
chore(deps): update dependency com.google.cloud:google-cloud-batch to…
renovate-bot Aug 16, 2022
a7639b2
chore(main): release 0.3.0 (#35)
release-please[bot] Sep 15, 2022
d39ff1a
chore(main): release 0.3.1-SNAPSHOT (#41)
release-please[bot] Sep 15, 2022
8269adf
chore(deps): update dependency com.google.cloud:google-cloud-batch to…
renovate-bot Sep 20, 2022
26e3418
chore(main): release 0.3.1 (#68)
release-please[bot] Sep 28, 2022
10cfa60
chore(main): release 0.3.2-SNAPSHOT (#69)
release-please[bot] Sep 28, 2022
756e851
docs(samples): added basic samples and tests (#43)
Sita04 Sep 29, 2022
ea8dbff
chore(deps): update dependency com.google.cloud:google-cloud-batch to…
renovate-bot Sep 30, 2022
b9e0351
chore(main): release 0.3.2 (#71)
release-please[bot] Oct 4, 2022
2d76b01
chore(main): release 0.3.3-SNAPSHOT (#78)
release-please[bot] Oct 4, 2022
e263407
chore(deps): update dependency com.google.cloud:google-cloud-batch to…
renovate-bot Oct 6, 2022
f0f674a
Merge remote-tracking branch 'migration/main' into java-batch-migration
Sita04 Oct 26, 2022
1cab064
updated pom to include product prefix
Sita04 Oct 26, 2022
6c8c13b
corrected the library release version
Sita04 Oct 26, 2022
6ceae3d
increased timeout for snippets
Sita04 Oct 26, 2022
364b23d
restructured and removed non-essential files
Sita04 Oct 26, 2022
9f8271b
add snippets/ directory
Sita04 Nov 1, 2022
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
49 changes: 49 additions & 0 deletions batch/snippets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>-snippets</artifactId>
<packaging>jar</packaging>
<name>Google Google Cloud Batch Snippets</name>
<url>https://github.com/googleapis/java-batch</url>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- TODO: switch to libraries-bom after this artifact is included -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-batch</artifactId>
<version>0.3.2</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
140 changes: 140 additions & 0 deletions batch/snippets/src/main/java/CreateWithContainerNoMounting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START batch_create_container_job]

import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.ComputeResource;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.LogsPolicy.Destination;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Container;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateWithContainerNoMounting {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";

// Name of the region you want to use to run the job. Regions that are
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
String region = "europe-central2";

// The name of the job that will be created.
// It needs to be unique for each project and region pair.
String jobName = "JOB_NAME";

createContainerJob(projectId, region, jobName);
}

// This method shows how to create a sample Batch Job that will run a simple command inside a
// container on Cloud Compute instances.
public static void createContainerJob(String projectId, String region, String jobName)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `batchServiceClient.close()` method on the client to safely
// clean up any remaining background resources.
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

// Define what will be done as part of the job.
Runnable runnable =
Runnable.newBuilder()
.setContainer(
Container.newBuilder()
.setImageUri("gcr.io/google-containers/busybox")
.setEntrypoint("/bin/sh")
.addCommands("-c")
.addCommands(
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
.build())
.build();

// We can specify what resources are requested by each task.
ComputeResource computeResource =
ComputeResource.newBuilder()
// In milliseconds per cpu-second. This means the task requires 2 whole CPUs.
.setCpuMilli(2000)
// In MiB.
.setMemoryMib(16)
.build();

TaskSpec task =
TaskSpec.newBuilder()
// Jobs can be divided into tasks. In this case, we have only one task.
.addRunnables(runnable)
.setComputeResource(computeResource)
.setMaxRetryCount(2)
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
.build();

// Tasks are grouped inside a job using TaskGroups.
TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "e2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
InstancePolicy instancePolicy =
InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();

AllocationPolicy allocationPolicy =
AllocationPolicy.newBuilder()
.addInstances(InstancePolicyOrTemplate.newBuilder().setPolicy(instancePolicy).build())
.build();

Job job =
Job.newBuilder()
.addTaskGroups(taskGroup)
.setAllocationPolicy(allocationPolicy)
.putLabels("env", "testing")
.putLabels("type", "container")
// We use Cloud Logging as it's an out of the box available option.
.setLogsPolicy(
LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
.build();

CreateJobRequest createJobRequest =
CreateJobRequest.newBuilder()
// The job's parent is the region in which the job will run.
.setParent(String.format("projects/%s/locations/%s", projectId, region))
.setJob(job)
.setJobId(jobName)
.build();

Job result =
batchServiceClient
.createJobCallable()
.futureCall(createJobRequest)
.get(5, TimeUnit.MINUTES);

System.out.printf("Successfully created the job: %s", result.getName());
}
}
}
// [END batch_create_container_job]
141 changes: 141 additions & 0 deletions batch/snippets/src/main/java/CreateWithScriptNoMounting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START batch_create_script_job]

import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.ComputeResource;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.LogsPolicy.Destination;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Script;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateWithScriptNoMounting {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";

// Name of the region you want to use to run the job. Regions that are
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
String region = "europe-central2";

// The name of the job that will be created.
// It needs to be unique for each project and region pair.
String jobName = "JOB_NAME";

createScriptJob(projectId, region, jobName);
}

// This method shows how to create a sample Batch Job that will run
// a simple command on Cloud Compute instances.
public static void createScriptJob(String projectId, String region, String jobName)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `batchServiceClient.close()` method on the client to safely
// clean up any remaining background resources.
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

// Define what will be done as part of the job.
Runnable runnable =
Runnable.newBuilder()
.setScript(
Script.newBuilder()
.setText(
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
// You can also run a script from a file. Just remember, that needs to be a
// script that's already on the VM that will be running the job.
// Using setText() and setPath() is mutually exclusive.
// .setPath("/tmp/test.sh")
.build())
.build();

// We can specify what resources are requested by each task.
ComputeResource computeResource =
ComputeResource.newBuilder()
// In milliseconds per cpu-second. This means the task requires 2 whole CPUs.
.setCpuMilli(2000)
// In MiB.
.setMemoryMib(16)
.build();

TaskSpec task =
TaskSpec.newBuilder()
// Jobs can be divided into tasks. In this case, we have only one task.
.addRunnables(runnable)
.setComputeResource(computeResource)
.setMaxRetryCount(2)
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
.build();

// Tasks are grouped inside a job using TaskGroups.
TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "e2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
InstancePolicy instancePolicy =
InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();

AllocationPolicy allocationPolicy =
AllocationPolicy.newBuilder()
.addInstances(InstancePolicyOrTemplate.newBuilder().setPolicy(instancePolicy).build())
.build();

Job job =
Job.newBuilder()
.addTaskGroups(taskGroup)
.setAllocationPolicy(allocationPolicy)
.putLabels("env", "testing")
.putLabels("type", "script")
// We use Cloud Logging as it's an out of the box available option.
.setLogsPolicy(
LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
.build();

CreateJobRequest createJobRequest =
CreateJobRequest.newBuilder()
// The job's parent is the region in which the job will run.
.setParent(String.format("projects/%s/locations/%s", projectId, region))
.setJob(job)
.setJobId(jobName)
.build();

Job result =
batchServiceClient
.createJobCallable()
.futureCall(createJobRequest)
.get(5, TimeUnit.MINUTES);

System.out.printf("Successfully created the job: %s", result.getName());
}
}
}
// [END batch_create_script_job]
57 changes: 57 additions & 0 deletions batch/snippets/src/main/java/DeleteJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START batch_delete_job]

import com.google.cloud.batch.v1.BatchServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeleteJob {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";

// Name of the region hosts the job.
String region = "europe-central2";

// The name of the job that you want to delete.
String jobName = "JOB_NAME";

deleteJob(projectId, region, jobName);
}

// Triggers the deletion of a Job.
public static void deleteJob(String projectId, String region, String jobName)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `batchServiceClient.close()` method on the client to safely
// clean up any remaining background resources.
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

// Construct the parent path of the job.
String name = String.format("projects/%s/locations/%s/jobs/%s", projectId, region, jobName);

batchServiceClient.deleteJobAsync(name).get(5, TimeUnit.MINUTES);
System.out.printf("Delete the job: %s", jobName);
}
}
}
// [END batch_delete_job]
Loading