Lite の予約を作成、管理する

このドキュメントでは、Pub/Sub Lite の予約の使用と管理の方法について説明します。このドキュメントでは、容量ユニットのコンセプトと、アプリケーションのスループットに対応するために必要な容量を決定する方法についても説明します。

Lite 予約の概要

Lite 予約は、リージョン内の 1 つ以上の Lite トピック間でスループット容量をプロビジョニングして動的に共有します。これらのトピックは、リージョンまたはゾーンの Lite トピックです。Lite 予約は、個別の Lite トピックのスループット容量を設定するよりも優れたオプションです。Lite 予約では、管理するリソースとモニタリングするリソースの数が少なくなります。また、個々の Lite トピックのスループット容量を選択する必要はありません。

Lite 予約を使用するには、同じリージョンにある Lite トピックを特定の Lite 予約にアタッチします。1 つの Lite 予約は、ゾーンとリージョンの両方の Lite トピックにアタッチできます。Lite 予約にアタッチされた Lite トピック内のすべてのパーティションは、Lite 予約でプロビジョニングされたスループット容量を動的に消費します。Lite トピックに Lite 予約を指定した場合は、レート制限を設定して、Lite トピックの各パーティションのスループット容量を制限できます。

容量ユニットの概要

Lite 予約のスループット容量は、容量ユニットでプロビジョニングされます。

1 つの容量ユニットが次のスループットに対応します。

Lite トピックタイプ パブリッシュのスループット(MiBps) サブスクライブのスループット(MiBps)
ゾーン 1 2
リージョン 0.25 0.5

予約の名前に関するガイドライン

Pub/Sub Lite リソース名は、トピック、サブスクリプション、予約などの Pub/Sub Lite リソースを一意に識別します。リソース名は次の形式になっている必要があります。

projects/project-identifier/collection/ID

  • project-identifier: Google Cloud コンソールから取得可能なプロジェクト ID またはプロジェクト番号を指定する必要があります。たとえば、my-cool-project はプロジェクト ID です。123456789123 はプロジェクト番号です。

  • collection: topicssubscriptionsreservations のいずれかにする必要があります。

  • ID: 次のガイドラインに従う必要があります。

    • 文字列 goog で始めないこと。
    • 文字から始まる
    • 3~255 文字の長さであること。
    • 次の文字だけが含まれている: 文字 [A-Za-z]、数字 [0-9]、ダッシュ -、アンダースコア _、ピリオド .、チルダ ~、プラス記号 +、パーセント記号 %

    URL エンコードのないリソース名で、上記のリストの特殊文字を使用できます。ただし、URL で使用されている場合、その他すべての特殊文字が適切にエンコードまたはデコードされることを確認する必要があります。たとえば、mi-tópico は無効な ID です。mi-t%C3%B3pico は有効です。この形式は、REST 呼び出しを行うときに重要です。

Lite 予約を使用するためのガイドライン

Lite 予約によるスループットのプロビジョニングは、リージョン Lite トピックでは必須ですが、ゾーン Lite トピックでは省略できます。Lite 予約はリージョン リソースです。

Lite 予約を構成する場合は、次の点に留意してください。

  • Lite 予約に必要な容量ユニットの数は、その Lite 予約のすべてのトピックとサブスクリプションで予想される 1 分間のピーク スループットと同じである必要があります。

  • Lite 予約のすべての Lite トピックの合計スループットが容量に達すると、パブリッシュと配信が抑制されます。Pub/Sub Lite サーバーはクライアントへのメッセージ送信を停止し、メッセージはパブリッシャー クライアントにバッファリングされます。reservation/throughput_capacity/utilization のアラートを設定すると、予約がスロットルされる可能性がある場合に通知され、予約サイズを増やすことができます。

  • 予約のスループットは必要に応じて更新できます。変更は 1 分以内に反映されます。ただし、予約のスループットを減少させた場合は、24 時間、引き続き高い料金が課金されます。Pub/Sub Lite は本質的には容量課金制であり、スケールアップは即座に行われスケールダウンも可能です。

  • Lite 予約にアタッチされているすべての Lite トピックは、同じスループット容量のプールから消費します。したがって、Lite トピックで大量のスループット容量が突然使用されて、他の Lite トピックが使用できる容量が枯渇し、メッセージのパブリッシュや受信ができなくなる可能性があります。Lite トピックを完全に分離する必要がある場合は、独立した予約にアタッチできます。

Lite 予約に対するパーティションの影響

Lite 予約のすべての Lite トピック内のパーティションの合計数は、予約済みの容量ユニット数以下でなければなりません。たとえば、容量ユニットが 10 個の Lite 予約を、Lite トピックまたはパーティションの合計数が 10 を超える Lite トピックのセットに接続することはできません。

Lite 予約の容量ユニットを計算する

Lite 予約のサイズは、容量ユニットの数によって決定されます。次の例は、個別の Lite トピックに必要な容量を見積もる方法を示しています。

  1. パブリッシュのピーク スループットを特定します。

    Lite 予約にアタッチされているすべての Lite トピックのスループットのピークを見積もります。予約のサイズはいつでも更新できるため、スループットの概算で十分です。

  2. ピーク時のサブスクライブ スループットを特定します。

    ピーク時のサブスクライブ スループットは、ピーク時のパブリッシュ スループットと同様に計算されます。ただし、サブスクライバーが必ずしもパブリッシャーのピークレートでメッセージを受信する必要はありません。処理の遅延が許容される場合、Pub/Sub Lite では、メッセージを一定のレートでバッファリングして処理できます。同時に、時折のサブスクライバーのダウンタイムに対応するために、パブリッシュの容量よりも多くのサブスクライブ容量が必要になることもあります。

  3. 必要な容量ユニットの合計数を決定します。

    上記のセクション内の表に、容量ユニットがさまざまなオペレーションに対応可能な MiBps の数を指定します。Lite 予約のサンプルに必要な容量ユニットを計算する式は次のとおりです。

    • ゾーン Lite トピックのピーク パブリッシュ スループット = Pz MiBps
    • リージョン Lite トピックのピーク パブリッシュ スループット = Pr MiBps
    • ゾーン Lite トピックのピーク サブスクライブ スループット = Sz MiBps
    • リージョン Lite トピックのピーク サブスクライブ スループット = Sr MiBps

      必要な容量ユニットの数 = (Pz / 1 MiBps) + (Pr/0.25 MiBps) + (Sz/2 MiBps) + (Sr/0.5 MiBps)

      この数値が整数でない場合は、値を切り上げる必要があります。

Lite 予約なしでスループット容量を構成する

  • トピック公開スループット容量: 4 ~ 16 MiBps のスループット容量を指定できます。この値は整数にする必要があります。

  • トピック サブスクライブのスループット容量: 4~32 MiBps のスループット容量を指定できます。この値は整数にする必要があります。

サブスクライブのスループットについては、スループット容量は、予想されるパブリッシュのスループットのとトピックのサブスクリプション数の積に等しくなるように準備することをおすすめします。

個々のトピックにプロビジョニングされたパブリッシャーとサブスクライバーの容量に互換性はありません。このため、常に予約を使用することをおすすめします。

予約を使用する代わりにトピックの容量を直接プロビジョニングする場合、請求が異なります。詳細については、Pub/Sub Lite の料金ページをご覧ください。

Lite 予約の作成

Lite 予約は、接続されている Lite トピックと同じプロジェクトとリージョンに存在する必要があります。使用可能なゾーンとリージョンのリストについては、Pub/Sub Lite のロケーションをご覧ください。

Lite 予約は、Google Cloud コンソール、Google Cloud CLI、または Pub/Sub Lite API を使用して作成できます。

Console

  1. [Lite 予約] ページに移動します。

    [Lite Reservations] に移動

  2. [LITE 予約を作成] をクリックします。

  3. リージョンを選択します。

  4. Lite 予約 ID を入力します。

  5. スループット容量ユニットの量をプロビジョニングします。

  6. [作成] をクリックします。

gcloud

Lite 予約を作成するには、gcloud pubsub lite-reservations create コマンドを使用します。

gcloud pubsub lite-reservations create myRes \
  --location=us-central1 \
  --throughput-capacity=INTEGER_NUMBER_OF_UNITS
gcloud pubsub lite-topics create myTopic \
  --throughput-reservation=myRez \
  --zone=ZONE \
  --location=us-central1a \
  --partitions \
  --storage

Go

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Go の設定手順を実施してください。

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsublite"
)

func createReservation(w io.Writer, projectID, region, reservationID string, throughputCapacity int) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// reservationID := "my-reservation"
	// throughputCapacity := 4
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
	res, err := client.CreateReservation(ctx, pubsublite.ReservationConfig{
		Name:               reservationPath,
		ThroughputCapacity: throughputCapacity,
	})
	if err != nil {
		return fmt.Errorf("client.CreateReservation got err: %w", err)
	}
	fmt.Fprintf(w, "Created reservation: %s\n", res.Name)
	return nil
}

Java

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Java の設定手順を実施してください。

import com.google.api.gax.rpc.AlreadyExistsException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import com.google.cloud.pubsublite.proto.Reservation;
import java.util.concurrent.ExecutionException;

public class CreateReservationExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";
    String reservationId = "your-reservation-id";
    // Each unit of throughput capacity supports up to 1 MiB/s of published messages or
    // 2 MiB/s of subscribed messages.
    int throughputCapacity = 4;

    createReservationExample(projectNumber, cloudRegion, reservationId, throughputCapacity);
  }

  public static void createReservationExample(
      long projectNumber, String cloudRegion, String reservationId, int throughputCapacity)
      throws Exception {

    ReservationPath reservationPath =
        ReservationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .setName(ReservationName.of(reservationId))
            .build();

    Reservation reservation =
        Reservation.newBuilder()
            .setName(reservationPath.toString())
            .setThroughputCapacity(throughputCapacity)
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Reservation response = adminClient.createReservation(reservation).get();
      System.out.println(response.getAllFields() + " created successfully.");
    } catch (ExecutionException e) {
      try {
        throw e.getCause();
      } catch (AlreadyExistsException alreadyExists) {
        System.out.println("This reservation already exists.");
      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
    }
  }
}

Python

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Python の設定手順を実施してください。

from google.api_core.exceptions import AlreadyExists
from google.cloud.pubsublite import AdminClient, Reservation
from google.cloud.pubsublite.types import CloudRegion, ReservationPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"
# Each unit of throughput capacity supports up to 1 MiB/s of published messages
# or 2 MiB/s of subscribed messages. Must be a positive integer.
# throughput_capacity = 4

cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)

reservation = Reservation(
    name=str(reservation_path),
    throughput_capacity=throughput_capacity,
)

client = AdminClient(cloud_region)
try:
    response = client.create_reservation(reservation)
    print(f"{response.name} created successfully.")
except AlreadyExists:
    print(f"{reservation_path} already exists.")

Lite 予約を更新する

Lite 予約に構成されている容量ユニットの数は更新できます。

Lite 予約は、Google Cloud コンソール、Google Cloud CLI、または Pub/Sub Lite API を使用して更新できます。

Console

  1. [Lite 予約] ページに移動します。

    [Lite Reservations] に移動

  2. Lite 予約 ID をクリックします。

  3. [Lite 予約の詳細] ページで [編集] をクリックします。

gcloud

Lite 予約を更新するには、gcloud pubsub lite-reservations update コマンドを使用します。

gcloud pubsub lite-reservations update RESERVATION_ID \
  --location=REGION \
  --throughput-capacity=THROUGHPUT_CAPACITY

次のように置き換えます。

  • RESERVATION_ID: Lite 予約の ID
  • REGION: 予約のリージョン
  • THROUGHPUT_CAPACITY: 予約のスループット容量

Go

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Go の設定手順を実施してください。

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsublite"
)

func updateReservation(w io.Writer, projectID, region, reservationID string, throughputCapacity int) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// reservationID := "my-reservation-id"
	// throughputCapacity := 8
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
	config := pubsublite.ReservationConfigToUpdate{
		Name:               reservationPath,
		ThroughputCapacity: throughputCapacity,
	}
	updatedCfg, err := client.UpdateReservation(ctx, config)
	if err != nil {
		return fmt.Errorf("client.UpdateReservation got err: %w", err)
	}
	fmt.Fprintf(w, "Updated reservation: %#v\n", updatedCfg)
	return nil
}

Java

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Java の設定手順を実施してください。

import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import com.google.cloud.pubsublite.proto.Reservation;
import java.util.concurrent.ExecutionException;

public class UpdateReservationExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";
    String reservationId = "your-reservation-id";
    // Each unit of throughput capacity supports up to 1 MiB/s of published messages or
    // 2 MiB/s of subscribed messages.
    int throughputCapacity = 8;

    updateReservationExample(projectNumber, cloudRegion, reservationId, throughputCapacity);
  }

  public static void updateReservationExample(
      long projectNumber, String cloudRegion, String reservationId, int throughputCapacity)
      throws Exception {

    ReservationPath reservationPath =
        ReservationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .setName(ReservationName.of(reservationId))
            .build();

    com.google.protobuf.FieldMask fieldMask =
        com.google.protobuf.FieldMask.newBuilder().addPaths("throughput_capacity").build();

    Reservation reservation =
        Reservation.newBuilder()
            .setName(reservationPath.toString())
            .setThroughputCapacity(throughputCapacity)
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Reservation reservationBeforeUpdate = adminClient.getReservation(reservationPath).get();
      System.out.println("Before update: " + reservationBeforeUpdate.getAllFields());

      Reservation reservationAfterUpdate =
          adminClient.updateReservation(reservation, fieldMask).get();
      System.out.println("After update: " + reservationAfterUpdate.getAllFields());
    } catch (ExecutionException e) {
      try {
        throw e.getCause();
      } catch (NotFoundException notFound) {
        System.out.println("This reservation is not found.");
      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
    }
  }
}

Python

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Python の設定手順を実施してください。

from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient, Reservation
from google.cloud.pubsublite.types import CloudRegion, ReservationPath
from google.protobuf.field_mask_pb2 import FieldMask

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"
# Each unit of throughput capacity supports up to 1 MiB/s of published messages
# or 2 MiB/s of subscribed messages. Must be a positive integer.
# throughput_capacity = 8

cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)

# Defines which fields to update in the reservation.
field_mask = FieldMask(paths=["throughput_capacity"])

reservation = Reservation(
    name=str(reservation_path), throughput_capacity=throughput_capacity
)

client = AdminClient(cloud_region)
try:
    response = client.get_reservation(reservation_path)
    print(f"Before update: {response}")

    response = client.update_reservation(reservation, field_mask)
    print(f"After update: {response}")
except NotFound:
    print(f"{reservation_path} is not found.")

Lite 予約の詳細を取得する

Lite 予約の詳細は、Google Cloud コンソール、Google Cloud CLI、または Pub/Sub Lite API を使用して取得できます。

Console

  1. [Lite 予約] ページに移動します。

    [Lite Reservations] に移動

  2. Lite 予約 ID をクリックします。

gcloud

Lite 予約の詳細を取得するには、gcloud pubsub lite-reservations describe コマンドを使用します。

gcloud pubsub lite-reservations describe RESERVATION_ID \
  --location=REGION

次のように置き換えます。

  • RESERVATION_ID: Lite 予約の ID
  • REGION: 予約のリージョン

Go

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Go の設定手順を実施してください。

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsublite"
)

func getReservation(w io.Writer, projectID, region, reservationID string) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// reservationID := "my-reservation"
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
	res, err := client.Reservation(ctx, reservationPath)
	if err != nil {
		return fmt.Errorf("client.Reservation got err: %w", err)
	}
	fmt.Fprintf(w, "Got reservation: %#v\n", res)
	return nil
}

Java

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Java の設定手順を実施してください。

import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import com.google.cloud.pubsublite.proto.Reservation;
import java.util.concurrent.ExecutionException;

public class GetReservationExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";
    String reservationId = "your-reservation-id";

    getReservationExample(projectNumber, cloudRegion, reservationId);
  }

  public static void getReservationExample(
      long projectNumber, String cloudRegion, String reservationId) throws Exception {
    ReservationPath reservationPath =
        ReservationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .setName(ReservationName.of(reservationId))
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Reservation response = adminClient.getReservation(reservationPath).get();
      long throughputCapacity = response.getThroughputCapacity();
      System.out.println(
          response.getAllFields()
              + "\nhas "
              + throughputCapacity
              + " units of throughput capacity.");
    } catch (ExecutionException e) {
      try {
        throw e.getCause();
      } catch (NotFoundException notFound) {
        System.out.println("This reservation is not found.");
      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
    }
  }
}

Python

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Python の設定手順を実施してください。

from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import CloudRegion, ReservationPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"

cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)

client = AdminClient(cloud_region)
try:
    response = client.get_reservation(reservation_path)
    print(
        f"{response.name} has {response.throughput_capacity} units of throughput capacity."
    )
except NotFound:
    print(f"{reservation_path} is not found.")

Lite 予約を一覧表示する

Console

gcloud

プロジェクト内の Lite 予約のリストを表示するには、gcloud pubsub lite-reservations list コマンドを使用します。

gcloud pubsub lite-reservations list \
  --location=REGION

次のように置き換えます。

  • REGION: 予約のリージョン

Go

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Go の設定手順を実施してください。

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsublite"
	"google.golang.org/api/iterator"
)

func listReservations(w io.Writer, projectID, region string) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, region)
	resIter := client.Reservations(ctx, parent)
	for {
		res, err := resIter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("resIter.Next() got err: %w", err)
		}
		fmt.Fprintf(w, "Got reservation config: %v", res)
	}
	return nil
}

Java

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Java の設定手順を実施してください。

import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.LocationPath;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.proto.Reservation;
import java.util.List;

public class ListReservationsExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";

    listReservationsExample(projectNumber, cloudRegion);
  }

  public static void listReservationsExample(long projectNumber, String cloudRegion)
      throws Exception {

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    LocationPath locationPath =
        LocationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      List<Reservation> reservations = adminClient.listReservations(locationPath).get();
      for (Reservation reservation : reservations) {
        System.out.println(reservation.getAllFields());
      }
      System.out.println(reservations.size() + " reservation(s) listed in " + locationPath + ".");
    }
  }
}

Python

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Python の設定手順を実施してください。

from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import LocationPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"

location_path = LocationPath(project_number, cloud_region)

client = AdminClient(cloud_region)
response = client.list_reservations(location_path)

for reservation in response:
    print(reservation)

print(f"{len(response)} reservation(s) listed in your project and location.")

Lite 予約を削除する

Lite 予約は、Google Cloud コンソール、Google Cloud CLI、または Pub/Sub Lite API を使用して削除できます。予約を削除する際に、予約にトピックが含まれません。既存のトピックを別の予約にアタッチすると、予約を削除できます。

Console

  1. [Lite 予約] ページに移動します。

    [Lite Reservations] に移動

  2. Lite 予約 ID をクリックします。

  3. [Lite 予約の詳細] ページで [削除] をクリックします。

  4. 表示されたダイアログで [削除] をクリックして、削除する Lite 予約の削除を確定します。

gcloud

Lite サブスクリプションを削除するには、gcloud pubsub lite-reservations delete コマンドを使用します。

gcloud pubsub lite-reservations delete RESERVATION_ID \
  --location=REGION

次のように置き換えます。

  • RESERVATION_ID: Lite 予約の ID
  • REGION: 予約のリージョン

Go

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Go の設定手順を実施してください。

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsublite"
)

func deleteReservation(w io.Writer, projectID, region, reservationID string) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// reservationID := "my-reservation"
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
	err = client.DeleteReservation(ctx, reservationPath)
	if err != nil {
		return fmt.Errorf("client.DeleteReservation got err: %w", err)
	}
	fmt.Fprint(w, "Deleted reservation")
	return nil
}

Java

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Java の設定手順を実施してください。

import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import java.util.concurrent.ExecutionException;

public class DeleteReservationExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";
    String reservationId = "your-reservation-id";

    deleteReservationExample(projectNumber, cloudRegion, reservationId);
  }

  public static void deleteReservationExample(
      long projectNumber, String cloudRegion, String reservationId) throws Exception {

    ReservationPath reservationPath =
        ReservationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .setName(ReservationName.of(reservationId))
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    // If a reservation has topics attached, you must delete the topics before deleting
    // the reservation.
    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      adminClient.deleteReservation(reservationPath).get();
      System.out.println(reservationPath + " deleted successfully.");
    } catch (ExecutionException e) {
      try {
        throw e.getCause();
      } catch (NotFoundException notFound) {
        System.out.println("This reservation is not found.");
      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
    }
  }
}

Python

このサンプルを実行する前に、Pub/Sub Lite クライアント ライブラリの Python の設定手順を実施してください。

from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import CloudRegion, ReservationPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"

cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)

client = AdminClient(cloud_region)
try:
    client.delete_reservation(reservation_path)
    print(f"{reservation_path} deleted successfully.")
except NotFound:
    print(f"{reservation_path} not found.")

次のステップ