Abilita i nuovi tentativi delle funzioni basate su eventi

Questo documento descrive come abilitare i nuovi tentativi per le funzioni basate su eventi. Il nuovo tentativo automatico non è disponibile per le funzioni HTTP.

Semantica del nuovo tentativo

Cloud Functions garantisce l'esecuzione almeno una volta di una funzione basata su eventi per ogni evento emesso da un'origine evento. Per impostazione predefinita, se una chiamata a una funzione termina con un errore, la funzione non viene richiamata di nuovo e l'evento viene eliminato. Quando abiliti i nuovi tentativi su una funzione basata su eventi, Cloud Functions esegue un nuovo tentativo di chiamata di una funzione non riuscita finché non viene completata correttamente o fino alla scadenza del periodo per i nuovi tentativi.

Per le funzioni di 2ª generazione, il periodo per i nuovi tentativi scade dopo 24 ore. Per le funzioni di 1ª generazione, scade dopo 7 giorni. Cloud Functions esegue un nuovo tentativo delle funzioni basate su eventi appena create utilizzando una strategia di backoff esponenziale, con un backoff crescente compreso tra 10 e 600 secondi. Questo criterio viene applicato alle nuove funzioni la prima volta che ne esegui il deployment. Non viene applicata in modo retroattivo alle funzioni esistenti di cui è stato eseguito il deployment prima dell'applicazione delle modifiche descritte in questa nota di rilascio, anche se esegui nuovamente il deployment delle funzioni.

Quando i nuovi tentativi non sono abilitati per una funzione (impostazione predefinita), la funzione segnala sempre che è stata eseguita correttamente e nei suoi log potrebbero essere visualizzati i codici di risposta 200 OK. Ciò si verifica anche se la funzione ha riscontrato un errore. Per chiarire quando la funzione rileva un errore, assicurati di segnalare gli errori in modo appropriato.

Perché le funzioni basate su eventi non vengono completate

In rare occasioni, una funzione potrebbe uscire prematuramente a causa di un errore interno e per impostazione predefinita potrebbe essere o meno eseguito un nuovo tentativo automatico della funzione.

Più in genere, una funzione basata su eventi potrebbe non essere completata correttamente a causa di errori generati nel codice della funzione stesso. Ecco alcuni dei possibili motivi:

  • La funzione contiene un bug e il runtime genera un'eccezione.
  • La funzione non può raggiungere un endpoint di servizio o si verifica un timeout durante il tentativo.
  • La funzione genera intenzionalmente un'eccezione (ad esempio, quando un parametro non supera la convalida).
  • Una funzione Node.js restituisce una promessa rifiutata o passa un valore non null a un callback.

In uno dei casi precedenti, la funzione interrompe l'esecuzione per impostazione predefinita e l'evento viene ignorato. Per riprovare a utilizzare la funzione quando si verifica un errore, puoi modificare il criterio predefinito per nuovi tentativi impostando la proprietà "Riprova in caso di errore". In questo modo l'evento viene ripetuto più volte fino al completamento della funzione o fino alla scadenza del timeout per i nuovi tentativi.

Attivare o disattivare i nuovi tentativi

Per abilitare o disabilitare i nuovi tentativi, puoi utilizzare lo strumento a riga di comando gcloud o la console Google Cloud. I nuovi tentativi sono disattivati per impostazione predefinita.

Configurare i nuovi tentativi dallo strumento a riga di comando gcloud

Per abilitare i nuovi tentativi tramite lo strumento a riga di comando gcloud, includi il flag --retry durante il deployment della funzione:

gcloud functions deploy FUNCTION_NAME --retry FLAGS...

Per disabilitare i nuovi tentativi, esegui nuovamente il deployment della funzione senza il flag --retry:

gcloud functions deploy FUNCTION_NAME FLAGS...

Configurare i nuovi tentativi dalla console Google Cloud

Se stai creando una nuova funzione:

  1. Nella schermata Crea funzione, seleziona Aggiungi trigger e scegli il tipo di evento da attivare per la funzione.
  2. Nel riquadro Trigger Eventarc, seleziona la casella di controllo Riprova in caso di errore per abilitare i nuovi tentativi.

Se stai aggiornando una funzione esistente:

  1. Nella pagina Panoramica di Cloud Functions, fai clic sul nome della funzione che stai aggiornando per aprire la schermata Dettagli funzione, quindi scegli Modifica dalla barra dei menu per visualizzare i riquadri dei trigger HTTPS ed Eventarc.
  2. Nel riquadro Trigger Eventarc, fai clic sull'icona di modifica per modificare le impostazioni del trigger.
  3. Nel riquadro Trigger Eventarc, seleziona o deseleziona la casella di controllo Riprova in caso di errore per abilitare o disabilitare i nuovi tentativi.

best practice

In questa sezione vengono descritte le best practice per l'utilizzo dei nuovi tentativi.

Usa Riprova per gestire gli errori temporanei

Poiché la funzione viene ripetuta continuamente fino all'esecuzione corretta, gli errori permanenti come i bug devono essere eliminati dal codice tramite test prima di abilitare i nuovi tentativi. I nuovi tentativi sono ideali per gestire gli errori intermittenti/temporanei con un'alta probabilità di risoluzione al momento dei nuovi tentativi, ad esempio un endpoint di servizio instabile o un timeout.

Imposta una condizione di fine per evitare cicli infiniti di nuovi tentativi

Una best practice consiste nel proteggere la funzione dal loop continuo quando utilizzi i nuovi tentativi. Puoi farlo includendo una condizione di fine ben definita, prima che la funzione inizi l'elaborazione. Tieni presente che questa tecnica funziona solo se la funzione viene avviata correttamente ed è in grado di valutare la condizione finale.

Un approccio semplice ma efficace consiste nell'eliminare gli eventi con timestamp che risalgono a più di una certa data. Ciò consente di evitare esecuzioni eccessive quando gli errori sono permanenti o di durata maggiore del previsto.

Ad esempio, questo snippet di codice ignora tutti gli eventi più vecchi di 10 secondi:

Node.js

/**
 * Background Cloud Function that only executes within
 * a certain time period after the triggering event
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.avoidInfiniteRetries = (event, callback) => {
  const eventAge = Date.now() - Date.parse(event.timestamp);
  const eventMaxAge = 10000;

  // Ignore events that are too old
  if (eventAge > eventMaxAge) {
    console.log(`Dropping event ${event} with age ${eventAge} ms.`);
    callback();
    return;
  }

  // Do what the function is supposed to do
  console.log(`Processing event ${event} with age ${eventAge} ms.`);

  // Retry failed function executions
  const failed = false;
  if (failed) {
    callback('some error');
  } else {
    callback();
  }
};

Python

from datetime import datetime, timezone

# The 'python-dateutil' package must be included in requirements.txt.
from dateutil import parser

def avoid_infinite_retries(data, context):
    """Background Cloud Function that only executes within a certain
    time period after the triggering event.

    Args:
        data (dict): The event payload.
        context (google.cloud.functions.Context): The event metadata.
    Returns:
        None; output is written to Stackdriver Logging
    """

    timestamp = context.timestamp

    event_time = parser.parse(timestamp)
    event_age = (datetime.now(timezone.utc) - event_time).total_seconds()
    event_age_ms = event_age * 1000

    # Ignore events that are too old
    max_age_ms = 10000
    if event_age_ms > max_age_ms:
        print(f"Dropped {context.event_id} (age {event_age_ms}ms)")
        return "Timeout"

    # Do what the function is supposed to do
    print(f"Processed {context.event_id} (age {event_age_ms}ms)")
    return  # To retry the execution, raise an exception here

Go


// Package tips contains tips for writing Cloud Functions in Go.
package tips

import (
	"context"
	"fmt"
	"log"
	"time"

	"cloud.google.com/go/functions/metadata"
)

// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
type PubSubMessage struct {
	Data []byte `json:"data"`
}

// FiniteRetryPubSub demonstrates how to avoid inifinite retries.
func FiniteRetryPubSub(ctx context.Context, m PubSubMessage) error {
	meta, err := metadata.FromContext(ctx)
	if err != nil {
		// Assume an error on the function invoker and try again.
		return fmt.Errorf("metadata.FromContext: %w", err)
	}

	// Ignore events that are too old.
	expiration := meta.Timestamp.Add(10 * time.Second)
	if time.Now().After(expiration) {
		log.Printf("event timeout: halting retries for expired event '%q'", meta.EventID)
		return nil
	}

	// Add your message processing logic.
	return processTheMessage(m)
}

Java


import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import com.google.gson.Gson;
import functions.eventpojos.PubsubMessage;
import java.time.Duration;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.logging.Logger;

public class RetryTimeout implements BackgroundFunction<PubsubMessage> {
  private static final Logger logger = Logger.getLogger(RetryTimeout.class.getName());
  private static final long MAX_EVENT_AGE = 10_000;

  // Use Gson (https://github.com/google/gson) to parse JSON content.
  private static final Gson gson = new Gson();

  /**
   * Background Cloud Function that only executes within
   * a certain time period after the triggering event
   */
  @Override
  public void accept(PubsubMessage message, Context context) {
    ZonedDateTime utcNow = ZonedDateTime.now(ZoneOffset.UTC);
    ZonedDateTime timestamp = ZonedDateTime.parse(context.timestamp());

    long eventAge = Duration.between(timestamp, utcNow).toMillis();

    // Ignore events that are too old
    if (eventAge > MAX_EVENT_AGE) {
      logger.info(String.format("Dropping event with timestamp %s.", timestamp));
      return;
    }

    // Process events that are recent enough
    // To retry this invocation, throw an exception here
    logger.info(String.format("Processing event with timestamp %s.", timestamp));
  }
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.PubSub.V1;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace TimeBoundedRetries;

public class Function : ICloudEventFunction<MessagePublishedData>
{
    private static readonly TimeSpan MaxEventAge = TimeSpan.FromSeconds(10);
    private readonly ILogger _logger;

    // Note: for additional testability, use an injectable clock abstraction.
    public Function(ILogger<Function> logger) =>
        _logger = logger;

    public Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
    {
        string textData = data.Message.TextData;

        DateTimeOffset utcNow = DateTimeOffset.UtcNow;

        // Every PubSub CloudEvent will contain a timestamp.
        DateTimeOffset timestamp = cloudEvent.Time.Value;
        DateTimeOffset expiry = timestamp + MaxEventAge;

        // Ignore events that are too old.
        if (utcNow > expiry)
        {
            _logger.LogInformation("Dropping PubSub message '{text}'", textData);
            return Task.CompletedTask;
        }

        // Process events that are recent enough.
        // If this processing throws an exception, the message will be retried until either
        // processing succeeds or the event becomes too old and is dropped by the code above.
        _logger.LogInformation("Processing PubSub message '{text}'", textData);
        return Task.CompletedTask;
    }
}

Ruby

require "functions_framework"

FunctionsFramework.cloud_event "avoid_infinite_retries" do |event|
  # Use the event timestamp to determine the event age.
  event_age_secs = Time.now - event.time.to_time
  event_age_ms = (event_age_secs * 1000).to_i

  max_age_ms = 10_000
  if event_age_ms > max_age_ms
    # Ignore events that are too old.
    logger.info "Dropped #{event.id} (age #{event_age_ms}ms)"

  else
    # Do what the function is supposed to do.
    logger.info "Handling #{event.id} (age #{event_age_ms}ms)..."
    failed = true

    # Raise an exception to signal failure and trigger a retry.
    raise "I failed!" if failed
  end
end

PHP


/**
 * This function shows an example method for avoiding infinite retries in
 * Google Cloud Functions. By default, functions configured to automatically
 * retry execution on failure will be retried indefinitely - causing an
 * infinite loop. To avoid this, we stop retrying executions (by not throwing
 * exceptions) for any events that are older than a predefined threshold.
 */

use Google\CloudFunctions\CloudEvent;

function avoidInfiniteRetries(CloudEvent $event): void
{
    $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');

    $eventId = $event->getId();

    // The maximum age of events to process.
    $maxAge = 10; // 10 seconds

    // The age of the event being processed.
    $eventAge = time() - strtotime($event->getTime());

    // Ignore events that are too old
    if ($eventAge > $maxAge) {
        fwrite($log, 'Dropping event ' . $eventId . ' with age ' . $eventAge . ' seconds' . PHP_EOL);
        return;
    }

    // Do what the function is supposed to do
    fwrite($log, 'Processing event: ' . $eventId . ' with age ' . $eventAge . ' seconds' . PHP_EOL);

    // infinite_retries failed function executions
    $failed = true;
    if ($failed) {
        throw new Exception('Event ' . $eventId . ' failed; retrying...');
    }
}

Distinguere tra errori irreversibili ed errori irreversibili

Se per la funzione sono stati attivati nuovi tentativi, qualsiasi errore non gestito attiverà un nuovo tentativo. Assicurati che il codice acquisisca tutti gli errori che non dovrebbero comportare un nuovo tentativo.

Node.js

/**
 * Background Cloud Function that demonstrates
 * how to toggle retries using a promise
 *
 * @param {object} event The Cloud Functions event.
 * @param {object} event.data Data included with the event.
 * @param {object} event.data.retry User-supplied parameter that tells the function whether to retry.
 */
exports.retryPromise = event => {
  const tryAgain = !!event.data.retry;

  if (tryAgain) {
    throw new Error('Retrying...');
  } else {
    console.error('Not retrying...');
    return Promise.resolve();
  }
};

/**
 * Background Cloud Function that demonstrates
 * how to toggle retries using a callback
 *
 * @param {object} event The Cloud Functions event.
 * @param {object} event.data Data included with the event.
 * @param {object} event.data.retry User-supplied parameter that tells the function whether to retry.
 * @param {function} callback The callback function.
 */
exports.retryCallback = (event, callback) => {
  const tryAgain = !!event.data.retry;
  const err = new Error('Error!');

  if (tryAgain) {
    console.error('Retrying:', err);
    callback(err);
  } else {
    console.error('Not retrying:', err);
    callback();
  }
};

Python

from google.cloud import error_reporting

error_client = error_reporting.Client()

def retry_or_not(data, context):
    """Background Cloud Function that demonstrates how to toggle retries.

    Args:
        data (dict): The event payload.
        context (google.cloud.functions.Context): The event metadata.
    Returns:
        None; output is written to Stackdriver Logging
    """

    # Retry based on a user-defined parameter
    try_again = data.data.get("retry") is not None

    try:
        raise RuntimeError("I failed you")
    except RuntimeError:
        error_client.report_exception()
        if try_again:
            raise  # Raise the exception and try again
        else:
            pass  # Swallow the exception and don't retry

Go


// Package tips contains tips for writing Cloud Functions in Go.
package tips

import (
	"context"
	"errors"
	"log"
)

// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
type PubSubMessage struct {
	Data []byte `json:"data"`
}

// RetryPubSub demonstrates how to toggle using retries.
func RetryPubSub(ctx context.Context, m PubSubMessage) error {
	name := string(m.Data)
	if name == "" {
		name = "World"
	}

	// A misconfigured client will stay broken until the function is redeployed.
	client, err := MisconfiguredDataClient()
	if err != nil {
		log.Printf("MisconfiguredDataClient (retry denied):  %v", err)
		// A nil return indicates that the function does not need a retry.
		return nil
	}

	// Runtime error might be resolved with a new attempt.
	if err = FailedWriteOperation(client, name); err != nil {
		log.Printf("FailedWriteOperation (retry expected): %v", err)
		// A non-nil return indicates that a retry is needed.
		return err
	}

	return nil
}

Java


import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import functions.eventpojos.PubsubMessage;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.logging.Logger;

public class RetryPubSub implements BackgroundFunction<PubsubMessage> {
  private static final Logger logger = Logger.getLogger(RetryPubSub.class.getName());

  // Use Gson (https://github.com/google/gson) to parse JSON content.
  private static final Gson gson = new Gson();

  @Override
  public void accept(PubsubMessage message, Context context) {
    String bodyJson = new String(
        Base64.getDecoder().decode(message.getData()), StandardCharsets.UTF_8);
    JsonElement bodyElement = gson.fromJson(bodyJson, JsonElement.class);

    // Get the value of the "retry" JSON parameter, if one exists
    boolean retry = false;
    if (bodyElement != null && bodyElement.isJsonObject()) {
      JsonObject body = bodyElement.getAsJsonObject();

      if (body.has("retry") && body.get("retry").getAsBoolean()) {
        retry = true;
      }
    }

    // Retry if appropriate
    if (retry) {
      // Throwing an exception causes the execution to be retried
      throw new RuntimeException("Retrying...");
    } else {
      logger.info("Not retrying...");
    }
  }
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.PubSub.V1;
using Microsoft.Extensions.Logging;
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Retry;

public class Function : ICloudEventFunction<MessagePublishedData>
{
    private readonly ILogger _logger;

    public Function(ILogger<Function> logger) =>
        _logger = logger;

    public Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
    {
        bool retry = false;
        string text = data.Message?.TextData;

        // Get the value of the "retry" JSON parameter, if one exists.
        if (!string.IsNullOrEmpty(text))
        {
            JsonElement element = JsonSerializer.Deserialize<JsonElement>(data.Message.TextData);

            retry = element.TryGetProperty("retry", out var property) &&
                property.ValueKind == JsonValueKind.True;
        }

        // Throwing an exception causes the execution to be retried.
        if (retry)
        {
            throw new InvalidOperationException("Retrying...");
        }
        else
        {
            _logger.LogInformation("Not retrying...");
        }
        return Task.CompletedTask;
    }
}

Ruby

require "functions_framework"

FunctionsFramework.cloud_event "retry_or_not" do |event|
  try_again = event.data["retry"]

  begin
    # Simulate a failure
    raise "I failed!"
  rescue RuntimeError => e
    logger.warn "Caught an error: #{e}"
    if try_again
      # Raise an exception to return a 500 and trigger a retry.
      logger.info "Trying again..."
      raise ex
    else
      # Return normally to end processing of this event.
      logger.info "Giving up."
    end
  end
end

PHP


use Google\CloudFunctions\CloudEvent;

function tipsRetry(CloudEvent $event): void
{
    $cloudEventData = $event->getData();
    $pubSubData = $cloudEventData['message']['data'];

    $json = json_decode(base64_decode($pubSubData), true);

    // Determine whether to retry the invocation based on a parameter
    $tryAgain = $json['some_parameter'];

    if ($tryAgain) {
        /**
         * Functions with automatic retries enabled should throw exceptions to
         * indicate intermittent failures that a retry might fix. In this
         * case, a thrown exception will cause the original function
         * invocation to be re-sent.
         */
        throw new Exception('Intermittent failure occurred; retrying...');
    }

    /**
     * If a function with retries enabled encounters a non-retriable
     * failure, it should return *without* throwing an exception.
     */
    $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');
    fwrite($log, 'Not retrying' . PHP_EOL);
}

Rendi le funzioni basate su eventi non ripetibili idempotenti

Le funzioni basate su eventi che possono essere riprovate devono essere idempotenti. Di seguito sono riportate alcune linee guida generali per rendere questa funzione idempotente:

  • Molte API esterne (come Stripe) consentono di fornire una chiave di idempotenza come parametro. Se utilizzi un'API di questo tipo, devi utilizzare l'ID evento come chiave di idempotenza.
  • L'idempotency funziona bene con la consegna "at-least-once", perché consente di riprovare in sicurezza. Una best practice generale per scrivere codice affidabile è combinare l'idempotenza con i nuovi tentativi.
  • Assicurati che il codice sia internamente idempotente. Ad esempio:
    • Assicurati che le mutazioni possano verificarsi più di una volta senza cambiare i risultati.
    • Esegui una query sullo stato del database in una transazione prima di modificarne lo stato.
    • Assicurati che tutti gli effetti collaterali siano idempotenti.
  • Imporre un controllo transazionale all'esterno della funzione, indipendentemente dal codice. Ad esempio, lo stato permanente da qualche parte registra che un determinato ID evento è già stato elaborato.
  • Gestisci le chiamate di funzione duplicate fuori banda. Ad esempio, avere un processo separato per la pulizia dopo chiamate di funzioni duplicate.

Configura il criterio per i nuovi tentativi

A seconda delle esigenze della Cloud Function, puoi configurare direttamente il criterio di nuovo tentativo. Questo ti consente di configurare una qualsiasi combinazione di quanto segue:

  • Ridurre il periodo di prova da 7 giorni a un minimo di 10 minuti.
  • Modifica il tempo minimo e massimo di backoff per la strategia di nuovo tentativo di backoff esponenziale.
  • Modifica la strategia per i nuovi tentativi per riprovare immediatamente.
  • Configura un argomento messaggi non recapitabili.
  • Imposta un numero massimo e minimo di tentativi di consegna.

Per configurare il criterio per i nuovi tentativi:

  1. Scrivi una funzione HTTP.
  2. Utilizza l'API Pub/Sub per creare una sottoscrizione Pub/Sub, specificando l'URL della funzione come destinazione.

Per ulteriori informazioni sulla configurazione diretta di Pub/Sub, consulta la documentazione di Pub/Sub sulla gestione degli errori.

Passaggi successivi