Skip to content

Commit 2ce80d0

Browse files
feat(batch): add Batch labels job sample (GoogleCloudPlatform#9414)
* Implemented batch_labels_job, created test * Fixed test naming * Fixed test * Added tags * Added tags * Deleted redundant code * Fixed testcase
1 parent d9f6c15 commit 2ce80d0

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.example.batch;
16+
17+
// [START batch_labels_job]
18+
19+
import com.google.cloud.batch.v1.BatchServiceClient;
20+
import com.google.cloud.batch.v1.ComputeResource;
21+
import com.google.cloud.batch.v1.CreateJobRequest;
22+
import com.google.cloud.batch.v1.Job;
23+
import com.google.cloud.batch.v1.LogsPolicy;
24+
import com.google.cloud.batch.v1.Runnable;
25+
import com.google.cloud.batch.v1.TaskGroup;
26+
import com.google.cloud.batch.v1.TaskSpec;
27+
import com.google.protobuf.Duration;
28+
import java.io.IOException;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
33+
34+
public class CreateBatchLabelJob {
35+
36+
public static void main(String[] args)
37+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
// Project ID or project number of the Google Cloud project you want to use.
40+
String projectId = "YOUR_PROJECT_ID";
41+
// Name of the region you want to use to run the job. Regions that are
42+
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
43+
String region = "us-central1";
44+
// The name of the job that will be created.
45+
// It needs to be unique for each project and region pair.
46+
String jobName = "example-job";
47+
// Name of the label1 to be applied for your Job.
48+
String labelName1 = "JOB_LABEL_NAME1";
49+
// Value for the label1 to be applied for your Job.
50+
String labelValue1 = "JOB_LABEL_VALUE1";
51+
// Name of the label2 to be applied for your Job.
52+
String labelName2 = "JOB_LABEL_NAME2";
53+
// Value for the label2 to be applied for your Job.
54+
String labelValue2 = "JOB_LABEL_VALUE2";
55+
56+
createBatchLabelJob(projectId, region, jobName, labelName1,
57+
labelValue1, labelName2, labelValue2);
58+
}
59+
60+
// Creates a job with labels defined in the labels field.
61+
public static Job createBatchLabelJob(String projectId, String region, String jobName,
62+
String labelName1, String labelValue1, String labelName2, String labelValue2)
63+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64+
// Initialize client that will be used to send requests. This client only needs to be created
65+
// once, and can be reused for multiple requests.
66+
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
67+
68+
// Define what will be done as part of the job.
69+
Runnable runnable =
70+
Runnable.newBuilder()
71+
.setContainer(
72+
Runnable.Container.newBuilder()
73+
.setImageUri("gcr.io/google-containers/busybox")
74+
.setEntrypoint("/bin/sh")
75+
.addCommands("-c")
76+
.addCommands(
77+
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
78+
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
79+
.build())
80+
.build();
81+
82+
// We can specify what resources are requested by each task.
83+
ComputeResource computeResource =
84+
ComputeResource.newBuilder()
85+
// In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
86+
.setCpuMilli(2000)
87+
// In MiB.
88+
.setMemoryMib(2000)
89+
.build();
90+
91+
TaskSpec task =
92+
TaskSpec.newBuilder()
93+
// Jobs can be divided into tasks. In this case, we have only one task.
94+
.addRunnables(runnable)
95+
.setComputeResource(computeResource)
96+
.setMaxRetryCount(2)
97+
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
98+
.build();
99+
100+
// Tasks are grouped inside a job using TaskGroups.
101+
// Currently, it's possible to have only one task group.
102+
TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(1).setTaskSpec(task).build();
103+
104+
Job job =
105+
Job.newBuilder()
106+
.addTaskGroups(taskGroup)
107+
// We use Cloud Logging as it's an out of the box available option.
108+
.setLogsPolicy(LogsPolicy.newBuilder()
109+
.setDestination(LogsPolicy.Destination.CLOUD_LOGGING).build())
110+
// Labels and their value to be applied to the job.
111+
.putLabels(labelName1, labelValue1)
112+
.putLabels(labelName2, labelValue2)
113+
.build();
114+
115+
CreateJobRequest createJobRequest =
116+
CreateJobRequest.newBuilder()
117+
// The job's parent is the region in which the job will run.
118+
.setParent(String.format("projects/%s/locations/%s", projectId, region))
119+
.setJob(job)
120+
.setJobId(jobName)
121+
.build();
122+
123+
Job result =
124+
batchServiceClient
125+
.createJobCallable()
126+
.futureCall(createJobRequest)
127+
.get(5, TimeUnit.MINUTES);
128+
129+
System.out.printf("Successfully created the job: %s", result.getName());
130+
131+
return result;
132+
}
133+
}
134+
135+
}
136+
// [END batch_labels_job]

batch/snippets/src/test/java/com/example/batch/CreateResourcesIT.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public class CreateResourcesIT {
5858
+ UUID.randomUUID().toString().substring(0, 7);
5959
private static final String CUSTOM_EVENT_NAME = "test-job"
6060
+ UUID.randomUUID().toString().substring(0, 7);
61+
private static final String BATCH_LABEL_JOB = "test-job-label"
62+
+ UUID.randomUUID().toString().substring(0, 7);
6163
private static final String LOCAL_SSD_NAME = "test-disk"
6264
+ UUID.randomUUID().toString().substring(0, 7);
6365
private static final String PERSISTENT_DISK_NAME = "test-disk"
@@ -107,6 +109,7 @@ public static void cleanUp() {
107109
safeDeleteJob(PERSISTENT_DISK_JOB);
108110
safeDeleteJob(NOTIFICATION_NAME);
109111
safeDeleteJob(NFS_JOB_NAME);
112+
safeDeleteJob(BATCH_LABEL_JOB);
110113
}
111114

112115
private static void safeDeleteJob(String jobName) {
@@ -268,6 +271,28 @@ public void createScriptJobWithNfsTest()
268271

269272
}
270273

274+
275+
@Test
276+
public void createBatchLabelJobTest()
277+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
278+
String labelName1 = "env";
279+
String labelValue1 = "env_value";
280+
String labelName2 = "test";
281+
String labelValue2 = "test_value";
282+
283+
Job job = CreateBatchLabelJob.createBatchLabelJob(PROJECT_ID, REGION,
284+
BATCH_LABEL_JOB, labelName1, labelValue1, labelName2, labelValue2);
285+
286+
Assert.assertNotNull(job);
287+
ACTIVE_JOBS.add(job);
288+
289+
Assert.assertTrue(job.getName().contains(BATCH_LABEL_JOB));
290+
Assert.assertTrue(job.containsLabels(labelName1));
291+
Assert.assertTrue(job.containsLabels(labelName2));
292+
Assert.assertTrue(job.getLabelsMap().containsValue(labelValue1));
293+
Assert.assertTrue(job.getLabelsMap().containsValue(labelValue2));
294+
}
295+
271296
private void createEmptyDisk(String projectId, String zone, String diskName,
272297
String diskType, long diskSizeGb)
273298
throws IOException, ExecutionException, InterruptedException, TimeoutException {

0 commit comments

Comments
 (0)