Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fcm): Implement sendEach, sendEachAsync, sendEachForMulticast and sendEachForMulticastAsync #785

Merged
merged 3 commits into from
Jun 2, 2023

Conversation

Doris-Ge
Copy link
Contributor

@Doris-Ge Doris-Ge commented Mar 17, 2023

  1. Add sendEach, sendEachAsync, sendEachForMulticast and sendEachForMulticastAsync
  2. Deprecate sendAll, sendAllAsync, sendMulticast and sendMulticastAsync

sendEach vs sendAll

  1. sendEach sends one HTTP request to V1 Send endpoint for each
    message in the array.
    sendAll sends only one HTTP request to V1 Batch Send endpoint
    to send all messages in the array.
  2. sendEach calls messagingClient.send to send each message
    and constructs a SendResponse with the returned messageId.
    If messagingClient.send throws out an exception, sendEach
    will catch the exception and also turn it into a SendResponse
    with the exception in it.
    sendEach calls ApiFutures.allAsList().get() to execute all
    messagingClient.send calls asynchronously and wait for all of
    them to complete and construct a BatchResponse with all
    SendResponses.
    Therefore, unlike sendAll, sendEach does not always throw
    an error for a total failure. It can also return a BatchResponse
    with only errors in it.

sendEachForMulticast calls sendEach under the hood.
sendEachAsync is the async version of sendEach.
sendEachForMulticastAsync is the async version of sendEachForMulticast.

Will send another PR for integration tests

@Doris-Ge Doris-Ge changed the base branch from master to fcm-batch-send March 17, 2023 23:21
List<SendResponse> responses = ApiFutures.allAsList(list).get();
return new BatchResponseImpl(responses);
} catch (InterruptedException | ExecutionException e) {
throw new FirebaseMessagingException(ErrorCode.CANCELLED, SERVICE_ID);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can discuss what error code we want to throw here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we can discuss whether we should make an effort to add a unit test for this case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one message fails to go through would this throw immediately cancelling the rest of the messages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one message fails because of FirebaseMesssagingException, then it won't cancel the rest of the messages because the exception will be caught in sendOpForSendResponse.

Based on my read of https://dzone.com/articles/google-guava-%E2%80%93-futures, allAsList is fail-fast.
So if one message fails because the execute() throws an exception other than FirebaseMesssagingException,

      protected SendResponse execute() {
        try {
          String messageId = messagingClient.send(message, dryRun);
          return SendResponse.fromMessageId(messageId);
        } catch (FirebaseMessagingException e) {
          return SendResponse.fromException(e);
        }
      }

then the rest of the messages will be cancelled. I'm not sure in what cases this could happen, but I think it should be a corner case.

Copy link
Contributor Author

@Doris-Ge Doris-Ge Jun 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this PR. Let me know if you have any suggested changes for this. I'll fix it in another PR.

…orMulticastAsync

`sendEach` vs `sendAll`
1. `sendEach` sends one HTTP request to V1 Send endpoint for each
    message in the array.
   `sendAll` sends only one HTTP request to V1 Batch Send endpoint
    to send all messages in the array.
2. `sendEach` calls `messagingClient.send` to send each message
    and constructs a `SendResponse` with the returned `messageId`.
    If `messagingClient.send` throws out an exception, `sendEach`
    will catch the exception and also turn it into a `SendResponse`
    with the exception in it.
    `sendEach` calls `ApiFutures.allAsList().get()` to execute all
    `messagingClient.send` calls asynchronously and wait for all of
    them to complete and construct a `BatchResponse` with all
    `SendResponse`s.
    Therefore, unlike `sendAll`, `sendEach` does not always throw
    an error for a total failure. It can also return a `BatchResponse`
    with only errors in it.

`sendEachForMulticast` calls `sendEach` under the hood.
`sendEachAsync` is the async version of `sendEach`.
`sendEachForMulticastAsync` is the async version of `sendEachForMulticast`.
@Doris-Ge Doris-Ge force-pushed the remotes/dorisge/fcm-batch-send branch from 65f4435 to bd798b5 Compare March 17, 2023 23:36
@Doris-Ge Doris-Ge requested a review from lahirumaramba March 17, 2023 23:40
testSendEach(), testSendFiveHundredWithSendEach(), testSendEachForMulticast()
@lahirumaramba lahirumaramba added release-note release:stage Stage a release candidate labels Jun 1, 2023
@lahirumaramba lahirumaramba changed the title Implement sendEach, sendEachAsync, sendEachForMulticast and sendEachForMulticastAsync feat(fcm): Implement sendEach, sendEachAsync, sendEachForMulticast and sendEachForMulticastAsync Jun 1, 2023
@lahirumaramba lahirumaramba removed release-note release:stage Stage a release candidate labels Jun 1, 2023
Copy link
Member

@lahirumaramba lahirumaramba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you!!

List<SendResponse> responses = ApiFutures.allAsList(list).get();
return new BatchResponseImpl(responses);
} catch (InterruptedException | ExecutionException e) {
throw new FirebaseMessagingException(ErrorCode.CANCELLED, SERVICE_ID);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one message fails to go through would this throw immediately cancelling the rest of the messages?

@Doris-Ge Doris-Ge merged commit b90979e into fcm-batch-send Jun 2, 2023
Doris-Ge added a commit that referenced this pull request Jun 8, 2023
…st` and `sendEachForMulticastAsync` (#785) (#815)

* feat(fcm): Implement `sendEach`, `sendEachAsync`, `sendEachForMulticast` and `sendEachForMulticastAsync` (#785)

* Add org.hamcrest as dependency for unit tests

* Implement sendEach, sendEachAsync, sendEachForMulticast and sendEachForMulticastAsync

`sendEach` vs `sendAll`
1. `sendEach` sends one HTTP request to V1 Send endpoint for each
    message in the array.
   `sendAll` sends only one HTTP request to V1 Batch Send endpoint
    to send all messages in the array.
2. `sendEach` calls `messagingClient.send` to send each message
    and constructs a `SendResponse` with the returned `messageId`.
    If `messagingClient.send` throws out an exception, `sendEach`
    will catch the exception and also turn it into a `SendResponse`
    with the exception in it.
    `sendEach` calls `ApiFutures.allAsList().get()` to execute all
    `messagingClient.send` calls asynchronously and wait for all of
    them to complete and construct a `BatchResponse` with all
    `SendResponse`s.
    Therefore, unlike `sendAll`, `sendEach` does not always throw
    an error for a total failure. It can also return a `BatchResponse`
    with only errors in it.

`sendEachForMulticast` calls `sendEach` under the hood.
`sendEachAsync` is the async version of `sendEach`.
`sendEachForMulticastAsync` is the async version of `sendEachForMulticast`.

* Add integration tests for batch-send re-implementation:
testSendEach(), testSendFiveHundredWithSendEach(), testSendEachForMulticast()

* Replace all "-- i.e." in the comments with "meaning that"

* Fix the build errors caused by "Line is longer than 100 characters"

* Fix the build errors caused by "package does not exist"

* Address doc review comments
@sarismet
Copy link

sarismet commented Nov 1, 2023

Hi @lahirumaramba @Doris-Ge,

Could you please tell a little bit why you deprecated sendAll and recommend us to use sendEach instead? What was the motivation behind it. I know sendEach looks more reliable to debug failed attempts, however, even if it can send http requests async meaning that it may perform as good as sendAll, does it require a single thread from the thread-pool and cause more memory-usage, more networking and less available threads in pool? Therefore, the sendAll method looks more efficient to use threads to me. We are sending millions of notifications in a couple of minutes and we consider to lower our memory usage. Thanks in advance.

@Doris-Ge
Copy link
Contributor Author

Doris-Ge commented Nov 3, 2023

Hi @sarismet,

On June 20, 2024, we’re reducing the number of Firebase Cloud Messaging (FCM) legacy register APIs and legacy send APIs that provide similar functionality. This step will allow us to provide you with a more consistent experience and align with Google security standards to improve security, reliability and performance.

Because of these API decommissions, some already-deprecated SDKs and features will stop working after June 20, 2024.

Please consult the tables below to find which Firebase Cloud Messaging (FCM) APIs and corresponding services/SDKs/features will be discontinued and replaced with new alternatives.
image

This is because sendAll calls the FCM Batch send API which is deprecated and will be discontinued on June 20, 2024 whereas sendEach calls the FCM V1 send API that we will continue to support.

even if it can send http requests async meaning that it may perform as good as sendAll, does it require a single thread from the thread-pool and cause more memory-usage, more networking and less available threads in pool?

I'm not sure whether it requires a single thread to send one async request. For lowering memory usage, I'd suggest to reduce the number of messages to send per sendEach call.

@amitkumar43
Copy link

Hi @sarismet
What are your learning after moving to sendAll APIs and any optimizations you did for making your system performant at this scale with sendAll APIs.
We are calling sendAll API with a list size of 100, and seeing some gaps around performance.

@sarismet
Copy link

Hi @amitkumar43

We overwrite ThreadManager with custom thread executor. It helps a lot. Before that our Kafka consumer gets into rebalancing.

Nowadays, we are trying to use virtualThreadExecutors in ThreadManager. We reach 2 millions request per minutes but we got errors saying that "Error writing request body to server", "Remote host terminated the handshake", "Unexpected end of file from server". When load is decreased, the error counts decreases as well.

Do you have any idea what causes these errors? Have you ever tried virtual threads in that project? @Doris-Ge @lahirumaramba

Our java docker image: eclipse-temurin:21.0.3_9-jre-alpine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants