gcloud pubsub subscriptions COMMAND "PURGE MESSAGES" - google-cloud-platform

In GCP console you have the possibility to purge messages in a subscription.
It looks like "gcloud pubsub subscriptions" do not support a "PURGE MESSAGES" command.
What are the best alternatives are there any other libraries or API?

With gcloud tool this functionality is provided by the seek method. It effectively resets a subscription's backlog to a point in time or to a given snapshot. So, to purge all messages from a subscription you'd pass the current time via the --time flag, for example:
gcloud pubsub subscriptions seek my_subscription__name --time=2022-02-10T11:00:00Z
Messages in the subscription that were published before this time are marked as acknowledged, and messages retained in the subscription that were published after this time are marked as unacknowledged.
The equivalent API endpoint for it is projects.subscriptions.seek.

Related

EventArc in Google Cloud Request the Cloud Run Service more than Once in One Time Trigger

Currently, I create a service in Cloud Run to retrain ML models. The service will do the retrain process when there is an event from BigQuery called google.cloud.bigquery.v2.JobService.InsertJob. I use the EventArc in GCP to trigger the Retrain Service when that event happened. But, there is a problem. The trigger request to the service multiple times in one event. So, sometimes when the retraining process is done, the trigger requests the service again, and then the retraining process is active again. Is there something that I missed? Picture bellow is my EventArc setup.
As we can see in this picture that there are other requests while the first request is in process.
Eventarc is backed on PubSub. By default, and if you don't hack the default Eventarc configuration, the delivery timeout is set to 10s. (you can update manually the pubsub subscription created by eventarc. The Eventarc engineering team is aware of that not customizable parameter)
That's why, you should have a retry every 10s.
You have 2 solutions to that:
Either create an async process. I mean receive the PubSub message (tbe eventarc event), ack it immediately, and, in background, run your retrain
Or (not my preferred way), update the eventarc pubsub subscription and set the message retention duration to 5 seconds.

PubSub Lite: Acknowledge all messages currently in backlog

How can I acknowledge all messages that are currently in the backlog for Google PubSub Lite subscription. I tried using
gcloud pubsub lite-subscriptions ack-up-to SUBSCRIPTION \
--location LOCATION --partition PARTITION --offset OFFSET --project PROJECT
but I don't know how to set the offset accordingly.
To receive messages from a Lite subscription, request messages from the Lite subscription. The client library automatically connects to the partitions in the Lite topic attached to the Lite subscription. If more than one subscriber client is instantiated, messages will be distributed across all clients. The number of partitions in the topic determines the maximum number of subscriber clients that can simultaneously connect to a subscription. You can see more documentation and examples.
You can see these example to receive messages from Lite subscriptions:
gcloud pubsub lite-subscriptions subscribe SUBSCRIPTION_ID \
--location=LITE_LOCATION \
--auto-ack
But if you want to subscribe to a Pub/Sub Lite subscription and automatically acknowledge messages, you can see this example:
gcloud pubsub lite-subscriptions subscribe mysubscription --location=us-central1-a --auto-ack
To subscribe to specific partitions in a subscription, this example:
gcloud pubsub lite-subscriptions subscribe mysubscription --location=us-central1-a --partitions=0,1,2
You can see more documentation about these examples.

How to deploy pubsub-triggered cloud function with message ordering?

I want to deploy a Pubsub-triggered Cloud Function with message ordering:
https://cloud.google.com/pubsub/docs/ordering
gcloud functions deploy doesn't have an option to set an --enable-message-ordering option:
https://cloud.google.com/sdk/gcloud/reference/functions/deploy
Should I pre-create the subscription before deploying the function? If so, does Cloud Functions have a well-known format for how it matches to a subscription name? It seems maybe the format is: gcf-{function-name}-{region}-{topic-name}, but it also looks like the name format has changed over time, e.g. older deployed functions don't have the region name in the subscription. Is there a stable way to do this?
You must create message ordering pub/sub and Cloud function manually.
Fisrt, Create a pub/sub topic, and then create a subscription that subscribes pub/sub topic with --enable-message-ordering
Second, Create a Cloud function that will serve ordered pub/sub messages.
Last, back to the pub/sub subscription, Edit delivery type to push and specify your cloud function endpoint.
So final diagram is like below.
Publisher -> Pub/sub topic -> Pub/sub subscriber -> Cloud function
You tried to make a connection Pub/sub topic with Cloud function directly.
But for message ordering, Pub/sub needs topic -> subscriber connection.
So only pub/sub topic -> pub/sub subscriber -> Cloud function connection can delivers ordered messages to your function.
When declaring a Pub/Sub topic with a Cloud Function alike:
exports.pubsub = functions.pubsub.topic('some-topic').onPublish((message, context) => {}
The problem is that message ordering is only available for the subscription, but not for the topic.On deploy a push subscription is automatically being created at: cloudpubsub/subscription/list (the one which starts with gcf-*). This only appears to work when manually subscribing to a topic: Enabling message ordering. I haven't yet tried if it would pick up a subscription with the same name; if everything fails, one still could record the messages and then order by timestamp or a "sequence token": https://www.youtube.com/watch?v=nQ9_Xur2aM4.

GCP - how to add alert on number of messages sent to a pubsub dead letter queue?

I have my application which processes messages from a pubsub topic and if it fails the message is send to a separate dlq topic. I want to be able to set an alarm in monitoring that when during a day there were 30k messages sent to the dlq it notifies me and I can check why my service is not wokring.
I tried to set up some polices in gcp but I don't know and couldn't find anywhere in the docs how to setup a metric of daily processed messages on a topic.
Can anyone help me ?
You can create a new alert policy like this
PubSub subscription/unacked messages.
You can add a filter on your subscription name if you have several subscriptions in your project.
Add the notification channel that you want, an email in my case. After few minutes, you can see the first alert
And the email
EDIT
For the acked messages, you can do this
I never tried an aggregation over 1 day, but it should be OK.
Please check the following GCP community tutorials which outline how to create an alert-based event archiver with Stackdriver and Cloud Pub/Sub
https://cloud.google.com/community/tutorials/cloud-pubsub-drainer

gcloud alpha pubsub subscriptions seek method not found exception

I have a large backlog of undelivered messages for one Google Cloud Pub/Sub subscription. I would rather not have to process every message to get caught up, and I cannot delete the subscription manually because is was created using Cloud Deployments.
The gcloud seek command appears to be what I need (https://cloud.google.com/sdk/gcloud/reference/alpha/pubsub/subscriptions/seek). However, upon running this command in the Google Cloud Shell, I receive a "method not found" exception:
gcloud alpha pubsub subscriptions seek my_subscription__name --time=2016-11-11T06:20:57
ERROR: (gcloud.alpha.pubsub.subscriptions.seek) Subscription [my_subscription__name:seek] not found: Method not found.
The subscription type is "Pull".
The API for this method is white-list only at the moment -- but stay tuned. We'll find a way to clarify this in the CLI documentation or output.