Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b951997
Changed package, added information to CODEOWNERS
TetyanaYahodska Oct 15, 2024
9ee20d7
Added information to CODEOWNERS
TetyanaYahodska Oct 15, 2024
f0b8314
Added timeout
TetyanaYahodska Oct 16, 2024
055d61e
Merge branch 'main' into tpu-vm-crud-operations
TetyanaYahodska Oct 16, 2024
d3e1dee
Fixed parameters for test
TetyanaYahodska Oct 16, 2024
2253b54
Fixed DeleteTpuVm and naming
TetyanaYahodska Oct 17, 2024
d29a6b5
Added comment, created Util class
TetyanaYahodska Oct 18, 2024
d832b31
Merge branch 'main' into tpu-vm-crud-operations
TetyanaYahodska Oct 23, 2024
6956852
Fixed naming
TetyanaYahodska Oct 23, 2024
478beaa
Fixed whitespace
TetyanaYahodska Oct 23, 2024
f6b76cc
Merge branch 'main' into tpu-vm-crud-operations
TetyanaYahodska Oct 29, 2024
ec13f4d
Split PR into smaller, deleted redundant code
TetyanaYahodska Oct 29, 2024
9de8f3a
Implemented tpu_vm_stop and tpu_vm_start samples, created tests
TetyanaYahodska Oct 29, 2024
dcac83e
Merged changes from main
TetyanaYahodska Oct 30, 2024
579e661
Changed zone
TetyanaYahodska Oct 30, 2024
736c1fd
Merge branch 'main' into tpu_vm_stop-start
TetyanaYahodska Oct 31, 2024
ccfd268
Fixed empty lines and tests, deleted cleanup method
TetyanaYahodska Oct 31, 2024
a09e0ce
Merged changes from main
TetyanaYahodska Nov 7, 2024
579de8c
Merged changes from main
TetyanaYahodska Nov 18, 2024
f0fb0aa
Fixed tests
TetyanaYahodska Nov 18, 2024
9bbe9a0
Merged changes from main
TetyanaYahodska Nov 20, 2024
17de5cd
Merge branch 'main' into tpu_vm_stop-start
TetyanaYahodska Nov 26, 2024
5d0baf7
Merge branch 'main' into tpu_vm_stop-start
TetyanaYahodska Nov 27, 2024
1ca7638
Fixed comment
TetyanaYahodska Nov 27, 2024
c8c33c8
Resolved conflict
TetyanaYahodska Dec 9, 2024
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
2 changes: 1 addition & 1 deletion tpu/src/main/java/tpu/GetTpuVm.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class GetTpuVm {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Google Cloud project you want to create a node.
// Project ID or project number of the Google Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";
// The zone in which to create the TPU.
// For more information about supported TPU types for specific zones,
Expand Down
58 changes: 58 additions & 0 deletions tpu/src/main/java/tpu/StartTpuVm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024 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.
*/

package tpu;

//[START tpu_vm_start]
import com.google.cloud.tpu.v2.Node;
import com.google.cloud.tpu.v2.NodeName;
import com.google.cloud.tpu.v2.StartNodeRequest;
import com.google.cloud.tpu.v2.TpuClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class StartTpuVm {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Google Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";
// The zone where the TPU is located.
// For more information about supported TPU types for specific zones,
// see https://cloud.google.com/tpu/docs/regions-zones
String zone = "us-central1-f";
// The name for your TPU.
String nodeName = "YOUR_TPU_NAME";

startTpuVm(projectId, zone, nodeName);
}

// Starts a TPU VM with the specified name in the given project and zone.
public static Node startTpuVm(String projectId, String zone, String nodeName)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (TpuClient tpuClient = TpuClient.create()) {
String name = NodeName.of(projectId, zone, nodeName).toString();

StartNodeRequest request = StartNodeRequest.newBuilder().setName(name).build();

return tpuClient.startNodeAsync(request).get();
}
}
}
//[END tpu_vm_start]
59 changes: 59 additions & 0 deletions tpu/src/main/java/tpu/StopTpuVm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 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.
*/

package tpu;

//[START tpu_vm_stop]
import com.google.cloud.tpu.v2.Node;
import com.google.cloud.tpu.v2.NodeName;
import com.google.cloud.tpu.v2.StopNodeRequest;
import com.google.cloud.tpu.v2.TpuClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class StopTpuVm {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Google Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";
// The zone where the TPU is located.
// For more information about supported TPU types for specific zones,
// see https://cloud.google.com/tpu/docs/regions-zones
String zone = "us-central1-f";
// The name for your TPU.
String nodeName = "YOUR_TPU_NAME";

stopTpuVm(projectId, zone, nodeName);
}

// Stops a TPU VM with the specified name in the given project and zone.
public static Node stopTpuVm(String projectId, String zone, String nodeName)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (TpuClient tpuClient = TpuClient.create()) {
String name = NodeName.of(projectId, zone, nodeName).toString();

StopNodeRequest request = StopNodeRequest.newBuilder().setName(name).build();

return tpuClient.stopNodeAsync(request).get();
}
}
}
//[END tpu_vm_stop]

44 changes: 44 additions & 0 deletions tpu/src/test/java/tpu/TpuVmIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.google.cloud.tpu.v2.GetNodeRequest;
import com.google.cloud.tpu.v2.ListNodesRequest;
import com.google.cloud.tpu.v2.Node;
import com.google.cloud.tpu.v2.StartNodeRequest;
import com.google.cloud.tpu.v2.StopNodeRequest;
import com.google.cloud.tpu.v2.TpuClient;
import com.google.cloud.tpu.v2.TpuSettings;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -166,4 +168,46 @@ public void testListTpuVm() throws IOException {
verify(mockTpuClient, times(1)).listNodes(any(ListNodesRequest.class));
}
}

@Test
public void testStartTpuVm() throws IOException, ExecutionException, InterruptedException {
try (MockedStatic<TpuClient> mockedTpuClient = mockStatic(TpuClient.class)) {
TpuClient mockClient = mock(TpuClient.class);
Node mockNode = mock(Node.class);
OperationFuture mockFuture = mock(OperationFuture.class);

mockedTpuClient.when(TpuClient::create).thenReturn(mockClient);
when(mockClient.startNodeAsync(any(StartNodeRequest.class)))
.thenReturn(mockFuture);
when(mockFuture.get()).thenReturn(mockNode);

Node returnedNode = StartTpuVm.startTpuVm(PROJECT_ID, ZONE, NODE_NAME);

verify(mockClient, times(1))
.startNodeAsync(any(StartNodeRequest.class));
verify(mockFuture, times(1)).get();
assertEquals(returnedNode, mockNode);
}
}

@Test
public void testStopTpuVm() throws IOException, ExecutionException, InterruptedException {
try (MockedStatic<TpuClient> mockedTpuClient = mockStatic(TpuClient.class)) {
TpuClient mockClient = mock(TpuClient.class);
Node mockNode = mock(Node.class);
OperationFuture mockFuture = mock(OperationFuture.class);

mockedTpuClient.when(TpuClient::create).thenReturn(mockClient);
when(mockClient.stopNodeAsync(any(StopNodeRequest.class)))
.thenReturn(mockFuture);
when(mockFuture.get()).thenReturn(mockNode);

Node returnedNode = StopTpuVm.stopTpuVm(PROJECT_ID, ZONE, NODE_NAME);

verify(mockClient, times(1))
.stopNodeAsync(any(StopNodeRequest.class));
verify(mockFuture, times(1)).get();
assertEquals(returnedNode, mockNode);
}
}
}