Update the retention period, the scaling factor (throughput capacity), and per partition bytes (storage capacity) of a Lite topic.
Documentation pages that include this code sample
To view the code sample used in context, see the following documentation:
Code sample
Java
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.CloudZone;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.TopicName;
import com.google.cloud.pubsublite.TopicPath;
import com.google.cloud.pubsublite.proto.Topic;
import com.google.cloud.pubsublite.proto.Topic.PartitionConfig;
import com.google.cloud.pubsublite.proto.Topic.RetentionConfig;
import com.google.protobuf.FieldMask;
import com.google.protobuf.util.Durations;
import java.util.Arrays;
public class UpdateTopicExample {
public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String cloudRegion = "your-cloud-region";
char zoneId = 'b';
String topicId = "your-topic-id";
long projectNumber = Long.parseLong("123456789");
updateTopicExample(cloudRegion, zoneId, projectNumber, topicId);
}
public static void updateTopicExample(
String cloudRegion, char zoneId, long projectNumber, String topicId) throws Exception {
TopicPath topicPath =
TopicPath.newBuilder()
.setProject(ProjectNumber.of(projectNumber))
.setLocation(CloudZone.of(CloudRegion.of(cloudRegion), zoneId))
.setName(TopicName.of(topicId))
.build();
Iterable<String> iterablePaths =
Arrays.asList(
"partition_config.scale",
"retention_config.per_partition_bytes",
"retention_config.period");
FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(iterablePaths).build();
Topic topic =
Topic.newBuilder()
.setPartitionConfig(
PartitionConfig.newBuilder()
// Set publishing throughput to 4 times the standard partition
// throughput of 4 MiB per sec. This must be in the range [1,4]. A
// topic with `scale` of 2 and count of 10 is charged for 20 partitions.
.setScale(4)
.build())
.setRetentionConfig(
RetentionConfig.newBuilder()
// Set storage per partition to 200 GiB. This must be 30 GiB-10 TiB.
// If the number of bytes stored in any of the topic's partitions grows
// beyond this value, older messages will be dropped to make room for
// newer ones, regardless of the value of `period`.
// Be careful when decreasing storage per partition as it may cause
// lost messages.
.setPerPartitionBytes(200 * 1024 * 1024 * 1024L)
.setPeriod(Durations.fromDays(7)))
.setName(topicPath.toString())
.build();
AdminClientSettings adminClientSettings =
AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();
try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
Topic topicBeforeUpdate = adminClient.getTopic(topicPath).get();
System.out.println("Before update: " + topicBeforeUpdate.getAllFields());
Topic topicAfterUpdate = adminClient.updateTopic(topic, fieldMask).get();
System.out.println("After update: " + topicAfterUpdate.getAllFields());
}
}
}
Python
from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient, Topic
from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath
from google.protobuf.duration_pb2 import Duration
from google.protobuf.field_mask_pb2 import FieldMask
# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# zone_id = "a"
# topic_id = "your-topic-id"
cloud_region = CloudRegion(cloud_region)
location = CloudZone(cloud_region, zone_id)
topic_path = TopicPath(project_number, location, topic_id)
# Defines which topic fields to update.
field_mask = FieldMask(
paths=[
"partition_config.scale",
"retention_config.per_partition_bytes",
"retention_config.period",
]
)
# Defines how to update the topic fields.
topic = Topic(
name=str(topic_path),
partition_config=Topic.PartitionConfig(
# Set publishing throughput to 2x standard partition throughput of 4 MiB
# per second. This must in the range [1,4]. A topic with `scale` of 2 and
# `count` of 10 is charged for 20 partitions.
scale=2,
),
retention_config=Topic.RetentionConfig(
# Set storage per partition to 100 GiB. This must be in the range 30 GiB-10TiB.
# If the number of byptes stored in any of the topic's partitions grows beyond
# this value, older messages will be dropped to make room for newer ones,
# regardless of the value of `period`.
# Be careful when decreasing storage per partition as it may cuase lost messages.
per_partition_bytes=100 * 1024 * 1024 * 1024,
# Allow messages to be stored for 14 days.
period=Duration(seconds=60 * 60 * 24 * 14),
),
)
client = AdminClient(cloud_region)
try:
response = client.update_topic(topic, field_mask)
print(f"{response.name} updated successfully.")
except NotFound:
print(f"{topic_path} not found.")