Skip to content

FCM Batch sending deprecated but no migration path #834

Closed

Description

@19lmyers
  • Library version: 9.1.1
  • Firebase Product: Messaging

I received an email from Firebase about the deprecation of legacy FCM APIs. Notably, the sendAll() method that employs batch sending has been deprecated, and I saw the Node Admin SDK has a new sendEach() method, but the Java SDK does not provide this method.

How should I approach migrating from the sendAll() method?

Activity

  1. AndrWeisR commented on Jun 21, 2023

    @AndrWeisR

    Ditto. I have firebase-admin-9.1.1.jar which only exposes these methods below. I've seen mention that these new methods are in firebase-admin 9.2.0, but that doesn't seem to be available anywhere yet.

    image

  2. xak2000 commented on Jun 21, 2023

    @xak2000

    I see sendEach* methods are already implemented in the master.

    The question is: when 9.2.0 will be released?

  3. adhirajsinghchauhan commented on Jun 25, 2023

    @adhirajsinghchauhan

    The question is: when 9.2.0 will be released?

    @xak2000 v9.2.0 was released just a day after your comment.


    But I wonder if it'd be feasible to walk back the decision to discontinue the batch send API, or at least move it into the Blaze plan? For people who want a fast, fire-and-forget delivery system, sendEach does not fit the bill. Both theoretically and in real-world tests, sendAll far outperforms its replacement.

    Quick tests with 2000 messages (each to a different topic, but with the same config otherwise) on a few of my own servers. No code changes apart from calling a different function, no retry or delay mechanisms in between, apart from what the library itself defaults to:

     sendAll: 5-10s
    sendEach: 1-5m

    The overhead of 2000 HTTP calls instead of just 4 has obvious effects. This only becomes worse as the number of messages, meant to be sent approximately "together", increases.

  4. xak2000 commented on Jun 26, 2023

    @xak2000

    In the FAQ there is a dedicated topic about sending message to multiple tokens in one request.

    Q. Does the HTTP v1 API support sending messages to multiple tokens in one request??
    A. No. This feature, called "multicast" in legacy HTTP APIs, is not supported by the HTTP v1 API.

    For use cases where end-to-end latency is critical, or where total fanout size is small (fewer than 1 million), Google recommends the HTTP V1 API. The HTTP V1 API over HTTP/2 can achieve up to ~1/3 of total message throughput rate compared to legacy multicast when sending on a single connection.

    For use cases where throughput and egress bandwidth are prioritized or where total fanout size is large (greater than 1 million), Google recommends topic messaging. While topic messaging requires a one-time action to subscribe recipients to a topic, it offers up to a 10,000 QPS per project fanout rate without a maximum limit on topic size.

    The question is: does firebase-admin-java use HTTP V1 API over HTTP/2? And, if yes, how to make SDK to use it? Probably, only Async variants of methods will use HTTP/2 multiplexing, right? Checked the code: both async and non-async variants call each request concurrently. The only difference is in handling the result. So, the only question is: does it use HTTP/2 multiplexing or each concurrent HTTP V1 API request creates a TCP connection?

  5. emindeniz99 commented on Aug 6, 2023

    @emindeniz99

    In the FAQ there is a dedicated topic about sending message to multiple tokens in one request.

    Q. Does the HTTP v1 API support sending messages to multiple tokens in one request??
    A. No. This feature, called "multicast" in legacy HTTP APIs, is not supported by the HTTP v1 API.
    For use cases where end-to-end latency is critical, or where total fanout size is small (fewer than 1 million), Google recommends the HTTP V1 API. The HTTP V1 API over HTTP/2 can achieve up to ~1/3 of total message throughput rate compared to legacy multicast when sending on a single connection.
    For use cases where throughput and egress bandwidth are prioritized or where total fanout size is large (greater than 1 million), Google recommends topic messaging. While topic messaging requires a one-time action to subscribe recipients to a topic, it offers up to a 10,000 QPS per project fanout rate without a maximum limit on topic size.

    The question is: does firebase-admin-java use HTTP V1 API over HTTP/2? And, if yes, how to make SDK to use it? Probably, only Async variants of methods will use HTTP/2 multiplexing, right? Checked the code: both async and non-async variants call each request concurrently. The only difference is in handling the result. So, the only question is: does it use HTTP/2 multiplexing or each concurrent HTTP V1 API request creates a TCP connection?

    Hello, i think it creates to many connection instead of using http2.
    We faced with the issue at our system.
    #849

  6. LucasJC commented on Sep 21, 2023

    @LucasJC

    Hey guys! I had the same experience using sendEach from previous comments.
    Are there any plans for improving this new alternative?

  7. meor9zcu12 commented on Sep 26, 2023

    @meor9zcu12

    Hi, I encountered the same issue. I plan to migrate fromsendMulticast to sendEachForMulticast, but the performance of sendEachForMulticast is very bad.

    In my testing, to send 20,000 tokens
    sendMulticast (sendAll API) : 4 seconds
    sendEachForMulticast (sendEach API): 40 seconds

    sendEach / sendEachForMulticast is useless for me until it is confirmed officially that supports http2 and have to restricted to http2 only. Otherwise high frequency of opening connection may make system crash.


    In addition, I can't find any information about google-http-client:1.43.1 that does support http2

  8. self-assigned this
    on Dec 4, 2023
  9. AntQwanlity commented on Jan 14, 2024

    @AntQwanlity

    Found this : https://eladnava.com/send-multicast-notifications-using-node-js-http-2-and-the-fcm-http-v1-api/
    Refers to this package : https://github.com/eladnava/fcm-v1-http2/tree/main
    Might be useful for some.

    However, I believe the Admin SDK should handle this under the hood. Having to manually add an HTTP/2 layer above the SDK is terrible DX. Might as well use OneSignal.

  10. emindeniz99 commented on Jan 14, 2024

    @emindeniz99

    The main issue is caused by google-http-client that does not support http2. default transport interface (NetHttpTransport) is JDK1.1 http connection.
    I have tried different http transport layers for fcm library such as netty-reactor, java11-httpclient, springwebflux
    googleapis/google-http-java-client@main...emindeniz99:google-http-java-client:main
    https://github.com/emindeniz99/google-http-java-client

    note: java 11 http client is not good for http2 requests, you should create multiple client for same target host in order to send multiple requests, it creates only one connection per host. I experienced the issue :D so I switched to netty-reactor
    All of my tries are fast solutions to send http2 requests, can require improvements.

    public FirebaseApp firebaseApp() throws IOException {
      GoogleCredentials credential = GoogleCredentials.fromStream(new FileInputStream(credentialsPath));
    
      ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("Nio-firebase-http-client-%d").build();
    
      ConnectionProvider connectionProvider = ConnectionProvider.builder("myConnectionPool")
        .allocationStrategy(Http2AllocationStrategy.builder()
          .maxConnections(8)
          .minConnections(4)
          .maxConcurrentStreams(100) // google api responds http2 connection with 100 as max concurrent stream header
          .build())
        .pendingAcquireMaxCount(100_000)
        .build();
    
      //        EventLoopGroup bossGroup = new NioEventLoopGroup(2, namedThreadFactory);
      var client = HttpClient.create(connectionProvider
    
        )
        .protocol(HttpProtocol.H2)
      //                               .runOn(bossGroup)
      ;
    
      FirebaseOptions options =
        FirebaseOptions.builder().setCredentials(credential)
        .setHttpTransport(new ReactorNettyHttpTransport(client))
        .build();
      return FirebaseApp.initializeApp(options);
    }

    This is better, it does not open many sockets, etc. HTTP2 is used. It solves our main problem.
    But the fcm library creates a thread for each request, for example sending 6 batch requests with each batch consisting of 500 device tokens. 6*500= 3k threads will be created.
    As I know, each thread requires 1 MB memory, this is another problem.

    Overriding the httptransport (FirebaseOptions...setHttpTransport) does not solve batch request issues completely. I do not want to change Firebase library, it requires a lot of effort and also I am not sure which approach is best for creating reactive or async library (CompletableFuture, Mono ...), and I do not have enough time, we are waiting for an official solution :D

    #849

  11. sntg-p commented on Jan 18, 2024

    @sntg-p

    I was wondering if it makes sense to create a topic, make all your target devices subscribe to it and then send your message to that topic. Would that be an acceptable strategy?

  12. sebphil commented on Jan 22, 2024

    @sebphil

    I was wondering if it makes sense to create a topic, make all your target devices subscribe to it and then send your message to that topic. Would that be an acceptable strategy?

    It would work only if you want to send the same message to multiple devices. The batch feature can (or rather could) also be used to send different messages in a single http request, and there doesn't seem to be a workaround for that.

  13. stager0909 commented on Jan 23, 2024

    @stager0909

    Hello,

    I believe that the sendAll(Async) API should either be maintained or replaced with a better performing API.

    The method of sending messages to devices subscribed to a topic, as you suggested, does not meet various business requirements. This method can only be used in cases where it is okay to receive the same message, such as sports relay messages. However, most apps send personalized messages, so this method cannot be used to send messages. Also, creating and configuring new topics every time the target changes can be difficult.

    In addition, based on the results provided by the testers you mentioned, sendEach is several times slower than sendAll. I'm not sure why someone would want to get rid of sendAll(Async), but an API that provides the same or better performance should be provided.

    Lastly, I hope that Firebase-admin will provide support for netty-based HTTP/2.

    Thank you

  14. 14 remaining items

  15. emindeniz99 commented on May 22, 2024

    @emindeniz99

    @emindeniz99 thanks for sharing another alternative way. Have you tried overriding ThreadManager to have your own pool of threads instead of firing up tons of them by sdk itself?

    And instead of rewriting DGFirebaseMessaging class, isn't it better to use your own HttpTransport implementation and pass that to firebase app builder?

    @bivrantoshakil hello, thank you, I did not know the thread factory option. I think it should have same behavior as virtual thread to optimize the system, it may not be easy. I did not investigate well.

  16. bivrantoshakil commented on May 24, 2024

    @bivrantoshakil

    @emindeniz99 thanks for sharing another alternative way. Have you tried overriding ThreadManager to have your own pool of threads instead of firing up tons of them by sdk itself?
    And instead of rewriting DGFirebaseMessaging class, isn't it better to use your own HttpTransport implementation and pass that to firebase app builder?

    @bivrantoshakil hello, thank you, I did not know the thread factory option. I think it should have same behavior as virtual thread to optimize the system, it may not be easy. I did not investigate well.

    Creating your implementation for ThreadManager is very simple and straight forward. You can pass in your own instance of ExecutorService. I tried with JDK 17 without virtual threads and got very good results

  17. emindeniz99 commented on May 24, 2024

    @emindeniz99

    @emindeniz99 thanks for sharing another alternative way. Have you tried overriding ThreadManager to have your own pool of threads instead of firing up tons of them by sdk itself?
    And instead of rewriting DGFirebaseMessaging class, isn't it better to use your own HttpTransport implementation and pass that to firebase app builder?

    @bivrantoshakil hello, thank you, I did not know the thread factory option. I think it should have same behavior as virtual thread to optimize the system, it may not be easy. I did not investigate well.

    Creating your implementation for ThreadManager is very simple and straight forward. You can pass in your own instance of ExecutorService. I tried with JDK 17 without virtual threads and got very good results

    I could not understand the benefits of the new threads. To limit parallel thread count? Does it slow down the sending notifications? If not, can you share the implementation logic? Thank you

  18. bivrantoshakil commented on May 27, 2024

    @bivrantoshakil

    @emindeniz99 thanks for sharing another alternative way. Have you tried overriding ThreadManager to have your own pool of threads instead of firing up tons of them by sdk itself?
    And instead of rewriting DGFirebaseMessaging class, isn't it better to use your own HttpTransport implementation and pass that to firebase app builder?

    @bivrantoshakil hello, thank you, I did not know the thread factory option. I think it should have same behavior as virtual thread to optimize the system, it may not be easy. I did not investigate well.

    Creating your implementation for ThreadManager is very simple and straight forward. You can pass in your own instance of ExecutorService. I tried with JDK 17 without virtual threads and got very good results

    I could not understand the benefits of the new threads. To limit parallel thread count? Does it slow down the sending notifications? If not, can you share the implementation logic? Thank you

    If you provide your own implementation of ThreadManager class, then you can control the number of threads as per your need. You can also use any of the built in executors in the JDK itself. This is one example in kotlin

    `class FCMThreadManager(maxConcurrentThreadCount: Int) : ThreadManager() {
    private val apps = mutableSetOf()
    private var executorService: ExecutorService = Executors.newFixedThreadPool(maxConcurrentThreadCount, threadFactory)

    override fun getExecutor(app: FirebaseApp): ExecutorService {
        apps.add(app.name)
        return executorService
    }
    
    override fun releaseExecutor(app: FirebaseApp, executor: ExecutorService) {
        if (apps.remove(app.name) && apps.isEmpty()) {
            executorService.shutdownNow()
        }
    }
    
    override fun getThreadFactory(): ThreadFactory {
        return ThreadFactoryBuilder().setNameFormat("fcm-%d").build()
    }
    

    }`

    But as you're using http client with HTTP 2.0, the thread limitations will not reduce your overall throughput.

  19. sarismet commented on Jun 10, 2024

    @sarismet

    Hi,

    I have tried virtual threads by overwriting ThreadManager and built FirebaseApp with it. I can reach 2 million request per minute, however, I got errors saying that "Error writing request body to server", "Remote host terminated the handshake", "Unexpected end of file from server". When I decrease the load by Semaphore, the errors decreases as well. Do you have any idea what causes these errors? Does FCM apply throttling to my IP or should I change my java docker image?

    @bivrantoshakil could you please share us how you initialize firebase app? You mentioned you used virtual threads.

    Docker image: eclipse-temurin:21.0.3_9-jre-alpine

  20. bivrantoshakil commented on Jun 11, 2024

    @bivrantoshakil

    Hi,

    I have tried virtual threads by overwriting ThreadManager and built FirebaseApp with it. I can reach 2 million request per minute, however, I got errors saying that "Error writing request body to server", "Remote host terminated the handshake", "Unexpected end of file from server". When I decrease the load by Semaphore, the errors decreases as well. Do you have any idea what causes these errors? Does FCM apply throttling to my IP or should I change my java docker image?

    @bivrantoshakil could you please share us how you initialize firebase app? You mentioned you used virtual threads.

    Docker image: eclipse-temurin:21.0.3_9-jre-alpine

    @sarismet the implementation you did sounds right. But I think the RPM limit at Firebase is 600k per minute, not 2M.

  21. sarismet commented on Jun 11, 2024

    @sarismet

    Hi,
    I have tried virtual threads by overwriting ThreadManager and built FirebaseApp with it. I can reach 2 million request per minute, however, I got errors saying that "Error writing request body to server", "Remote host terminated the handshake", "Unexpected end of file from server". When I decrease the load by Semaphore, the errors decreases as well. Do you have any idea what causes these errors? Does FCM apply throttling to my IP or should I change my java docker image?
    @bivrantoshakil could you please share us how you initialize firebase app? You mentioned you used virtual threads.
    Docker image: eclipse-temurin:21.0.3_9-jre-alpine

    @sarismet the implementation you did sounds right. But I think the RPM limit at Firebase is 600k per minute, not 2M.

    @bivrantoshakil Yes, you are right, but, I expect the error code and response to be related to "rate-limit", not UNKNOWN. I will try to read the trace logs of com.google.firebase

  22. KrishnakanthYachareni commented on Jun 18, 2024

    @KrishnakanthYachareni

    I have implemented http2 based sendmulticast with spring webflux webclient in our project.

    This class should be on com.google.firebase.messaging package, you can create it own code but package name should match with it in order to access some SendResponse and similar classes (that can be copied but seems ugly in codebase). Our project is java 17 so we can not benefit from virtual threads. By using webclient, the requests do not block threads. 8 webclient thread handle 600k+ rpm on my macbook. We didn't use the code in production yet, it is open to your feedbacks, thanks

    you can use sendMulticastV2

    todo: retry policy should be added, like fcm library.

    package com.google.firebase.messaging;
    
    import com.google.api.client.http.HttpStatusCodes;
    import com.google.auth.oauth2.GoogleCredentials;
    import com.google.auth.oauth2.ServiceAccountCredentials;
    import com.google.common.base.Strings;
    import com.google.common.collect.ImmutableMap;
    import com.google.firebase.ErrorCode;
    import com.google.firebase.FirebaseApp;
    import com.google.firebase.FirebaseException;
    import com.google.firebase.internal.FirebaseProcessEnvironment;
    import com.google.firebase.messaging.internal.MessagingServiceErrorResponse;
    import com.google.firebase.messaging.internal.MessagingServiceResponse;
    import lombok.SneakyThrows;
    import lombok.extern.slf4j.Slf4j;
    import org.jetbrains.annotations.NotNull;
    import org.springframework.http.MediaType;
    import org.springframework.http.client.reactive.ReactorClientHttpConnector;
    import org.springframework.web.reactive.function.client.WebClient;
    import org.springframework.web.reactive.function.client.WebClientResponseException;
    import reactor.core.publisher.Flux;
    import reactor.core.publisher.Mono;
    import reactor.netty.http.HttpProtocol;
    import reactor.netty.http.client.Http2AllocationStrategy;
    import reactor.netty.http.client.HttpClient;
    import reactor.netty.resources.ConnectionProvider;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    
    @Slf4j
    public class DGFirebaseMessaging {
    
        private final FirebaseApp firebaseApp;
        private final WebClient webClient;
        private final GoogleCredentials credential;
    
        public DGFirebaseMessaging(FirebaseApp firebaseApp, GoogleCredentials credential) {
            this.firebaseApp = firebaseApp;
    
            //        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("Nio-firebase-http-client-%d").build();
    
            ConnectionProvider connectionProvider = ConnectionProvider.builder("myConnectionPool")
                                                                      .allocationStrategy(Http2AllocationStrategy.builder()
                                                                                                                 .maxConnections(200)
                                                                                                                 .minConnections(4)
                                                                                                                 .maxConcurrentStreams(100) // google api
                                                                                                                 // responds http2 connection with 100 as max concurrent stream header
                                                                                                                 .build())
                                                                      .pendingAcquireMaxCount(100_000)
                                                                      .maxConnections(200)
                                                                      .build();
    
            //        EventLoopGroup bossGroup = new NioEventLoopGroup(2, namedThreadFactory);
    
            var client = HttpClient.create(connectionProvider
    
            ).protocol(HttpProtocol.H2)
                //                               .runOn(bossGroup)
                ;
    
            client.warmup().block();
    
            this.webClient = WebClient.builder()
    
                                      .baseUrl(FCM_URL.formatted(getProjectId(firebaseApp, credential)))
    
                                      .clientConnector(new ReactorClientHttpConnector(client))
    
                                      .build();
    
            this.credential = credential;
        }
    
        //    com.google.firebase.FirebaseApp.getProjectId
        static String getProjectId(FirebaseApp firebaseApp, GoogleCredentials credential) {
    
            // Try to get project ID from user-specified options.
            String projectId = firebaseApp.getOptions().getProjectId();
    
            // Try to get project ID from the credentials.
            if (Strings.isNullOrEmpty(projectId)) {
                if (credential instanceof ServiceAccountCredentials) {
                    projectId = ((ServiceAccountCredentials) credential).getProjectId();
                }
            }
    
            // Try to get project ID from the environment.
            if (Strings.isNullOrEmpty(projectId)) {
                projectId = FirebaseProcessEnvironment.getenv("GOOGLE_CLOUD_PROJECT");
            }
            if (Strings.isNullOrEmpty(projectId)) {
                projectId = FirebaseProcessEnvironment.getenv("GCLOUD_PROJECT");
            }
            return projectId;
        }
    
        //    com.google.firebase.messaging.Message.wrapForTransport
        private ImmutableMap<String, Object> wrap(Message message, boolean dryRun) {
            ImmutableMap.Builder<String, Object> payload = ImmutableMap.<String, Object>builder().put("message", message);
            if (dryRun) {
                payload.put("validate_only", true);
            }
            return payload.build();
        }
    
        //    com.google.firebase.messaging.FirebaseMessagingClientImpl.fromApp
        @SneakyThrows(IOException.class)
        private String getReqBody(Message message, boolean dryRun) {
            return firebaseApp.getOptions().getJsonFactory().toString(wrap(message, dryRun));
        }
    
        @SneakyThrows(IOException.class)
        private <T> T parse(String value, Class<T> destinationClass) {
            return firebaseApp.getOptions().getJsonFactory().fromString(value, destinationClass);
        }
    
        //com.google.firebase.messaging.FirebaseMessagingClientImpl#FCM_URL
        private static final String FCM_URL = "https://fcm.googleapis.com/v1/projects/%s/messages:send";
    
        //    com.google.firebase.internal.AbstractHttpErrorHandler.HTTP_ERROR_CODES
        private static final Map<Integer, ErrorCode> HTTP_ERROR_CODES = ImmutableMap.<Integer, ErrorCode>builder()
                                                                                    .put(HttpStatusCodes.STATUS_CODE_BAD_REQUEST,
                                                                                         ErrorCode.INVALID_ARGUMENT)
                                                                                    .put(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED,
                                                                                         ErrorCode.UNAUTHENTICATED)
                                                                                    .put(HttpStatusCodes.STATUS_CODE_FORBIDDEN,
                                                                                         ErrorCode.PERMISSION_DENIED)
                                                                                    .put(HttpStatusCodes.STATUS_CODE_NOT_FOUND, ErrorCode.NOT_FOUND)
                                                                                    .put(HttpStatusCodes.STATUS_CODE_CONFLICT, ErrorCode.CONFLICT)
                                                                                    .put(429, ErrorCode.RESOURCE_EXHAUSTED)
                                                                                    .put(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, ErrorCode.INTERNAL)
                                                                                    .put(HttpStatusCodes.STATUS_CODE_SERVICE_UNAVAILABLE,
                                                                                         ErrorCode.UNAVAILABLE)
                                                                                    .build();
    
        //    com.google.firebase.messaging.FirebaseMessagingClientImpl.MessagingBatchCallback.createFirebaseException
        //    private FirebaseException createFirebaseException(MessagingServiceErrorResponse error) {
        //        String status = error.getStatus();
        //        ErrorCode errorCode = Strings.isNullOrEmpty(status) ? ErrorCode.UNKNOWN : Enum.valueOf(ErrorCode.class, status);
        //
        //        String msg = error.getErrorMessage();
        //        if (Strings.isNullOrEmpty(msg)) {
        //            msg = String.format("Unexpected HTTP response: %s", error.toString());
        //        }
        //
        //        return new FirebaseException(errorCode, msg, null);
        //    }
    
        private Mono<MessagingServiceResponse> sendno(Message message, boolean dryRun) {
            var body = getReqBody(message, dryRun);
            return webClient.post().headers(h -> {
    
                                h.setContentType(MediaType.APPLICATION_JSON);
                                try {
                                    credential.getRequestMetadata();
                                    //                com.google.auth.http.HttpCredentialsAdapter.initialize
                                    var accessToken = credential.getAccessToken();
                                    h.setBearerAuth(accessToken.getTokenValue());
                                }
                                catch (IOException e) {
                                    log.error("Error getting request metadata", e);
                                    throw new RuntimeException(e);
                                }
    
                            }).bodyValue(body).retrieve().bodyToMono(String.class).map(i -> {
                                //                com.google.firebase.messaging.FirebaseMessagingClientImpl.MessagingBatchCallback.onSuccess
                                return parse(i, MessagingServiceResponse.class);
    
                            })
                            //                        todo: handle WebClientRequestException
                            .onErrorMap(WebClientResponseException.class, e -> {
                                //com.google.firebase.internal.AbstractHttpErrorHandler.handleHttpResponseException
                                var base = httpResponseErrorToBaseException(e);
                                var resBody = e.getResponseBodyAsString();
                                var errorBody = parse(resBody, MessagingServiceErrorResponse.class);
                                return FirebaseMessagingException.withMessagingErrorCode(base, errorBody.getMessagingErrorCode());
                            });
        }
    
        private Mono<SendResponse> sendnoAndWrapWithResponse(Message message, boolean dryRun) {
    
            return sendno(message, dryRun).map(i -> {
                return SendResponse.fromMessageId(i.getMessageId());
            }).onErrorResume(FirebaseMessagingException.class, i -> {
                return Mono.just(SendResponse.fromException(i));
            });
        }
    
        //    com.google.firebase.internal.AbstractHttpErrorHandler.httpResponseErrorToBaseException
    
        protected FirebaseException superHttpResponseErrorToBaseException(WebClientResponseException webClientResponseException) {
            ErrorCode code = HTTP_ERROR_CODES.get(webClientResponseException.getStatusCode().value());
            if (code == null) {
                code = ErrorCode.UNKNOWN;
            }
    
            String message = String.format("Unexpected HTTP response with status: %d\n%s",
                                           webClientResponseException.getStatusCode().value(),
                                           webClientResponseException.getResponseBodyAsString());
            return new FirebaseException(code, message, webClientResponseException);
        }
    
        //    com.google.firebase.internal.AbstractPlatformErrorHandler.httpResponseErrorToBaseException
        protected final FirebaseException httpResponseErrorToBaseException(WebClientResponseException webClientResponseException) {
            var base = superHttpResponseErrorToBaseException(webClientResponseException);
            var parsedError = parse(webClientResponseException.getResponseBodyAsString(), MessagingServiceErrorResponse.class);
    
            ErrorCode code = base.getErrorCode();
            String status = parsedError.getStatus();
            if (!Strings.isNullOrEmpty(status)) {
                code = Enum.valueOf(ErrorCode.class, parsedError.getStatus());
            }
    
            String message = parsedError.getErrorMessage();
            if (Strings.isNullOrEmpty(message)) {
                message = base.getMessage();
            }
    
            return new FirebaseException(code, message, webClientResponseException);
        }
    
        public Mono<BatchResponse> sendMulticastV2(MulticastMessage message, boolean dryRun) {
            return sendMulticastV2(message.getMessageList(), dryRun);
        }
    
        public @NotNull Mono<BatchResponse> sendMulticastV2(List<Message> messages, boolean dryRun) {
            var ls = messages.stream().map(i -> sendnoAndWrapWithResponse(i, dryRun)).toList();
    
            var finalistMono = Flux.mergeSequential(ls).collectList();
    
            return finalistMono.map(finalist -> {
                return new BatchResponse() {
    
                    public List<SendResponse> getResponses() {
                        return finalist;
                    }
    
                    public int getSuccessCount() {
                        return finalist.stream().filter(i -> i.isSuccessful()).toList().size();
                    }
    
                    public int getFailureCount() {
                        return finalist.size() - getSuccessCount();
                    }
                };
            });
        }
    
    }

    @emindeniz99 I've impemented similar way but our 1% total traffic has beenn due to the following issues. Did you come across these issues and How to fix them?
    1.

    org.springframework.web.reactive.function.client.WebClientRequestException: nested exception is io.netty.handler.timeout.ReadTimeoutException
    	at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:141)
    	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
    Error has been observed at the following site(s):
    	*__checkpoint ⇢ Request to POST https://fcm.googleapis.com/v1/projects/dummy/messages:send [DefaultWebClient]
    Original Stack Trace:
    
    org.springframework.web.reactive.function.client.WebClientRequestException: Connection prematurely closed BEFORE response; nested exception is reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
    	at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:141)
    
    • While doing the performance testing with above WebClient config, did you see the reactor metrics reactor_netty_eventloop_pending_tasks?
    • What is the maximum duration that a connection can remain idle without being closed by Firebase servers?
    • What is the maximum lifespan of a live connection, assuming continuous traffic flow?
    • Are there any restrictions on the number of accepted connections that a given instance can have on Firebase servers?
  23. emindeniz99 commented on Jun 19, 2024

    @emindeniz99

    @KrishnakanthYachareni Hi,
    I transferred the migration process to another team member, he completed the load test etc. But I did not hear the readtimeout exceptions in our tests and production. It can be due to reactor.netty.http.client.HttpClient configs. Can you share your configs? Also, you can implement retry logic for timeouts.
    For the PrematureCloseException, if it is 'warn' log, you can ignore. If not, did you limit the max connection count in the http client config. I have encounter with this error when I set the limit very high, the client can not open new connections and can not get any response etc.

    While doing the performance testing with above WebClient config, did you see the reactor metrics reactor_netty_eventloop_pending_tasks?

    No, we did not see the webclient metrics.

    What is the maximum duration that a connection can remain idle without being closed by Firebase servers? What is the maximum lifespan of a live connection, assuming continuous traffic flow? What is the maximum lifespan of a live connection, assuming continuous traffic flow?

    It is default value, I don't know

    Are there any restrictions on the number of accepted connections that a given instance can have on Firebase servers?

    I did not see any limit on the Firebase docs. But on our side, our one server has a limit to opening connections to the server. So we should limit the connection count. One connection can handle 100 parallel request, so you can set the connection count in the Http2AllocationStrategy to your target value.(to send 3k parallel request, 3000/1000= 30 connection is required). :D We allocated double connection in case a connection is dropped. The others can handle the new requests. Via setting min-max count to the same number, we keep the connections warmed up.

    ConnectionProvider connectionProvider = ConnectionProvider.builder("DGFirebaseMessagingPool")
      .allocationStrategy(Http2AllocationStrategy.builder()
        .maxConnections(120)
        .minConnections(120)
        .maxConcurrentStreams(100) // google api
        // responds http2 connection with 100 as max concurrent stream header
        .build())
      .pendingAcquireMaxCount(100_000)
      .maxConnections(120)
      .build();
    
    var client = HttpClient.create(connectionProvider).protocol(HttpProtocol.H2);
    
    client.warmup().block();
    
    this.webClient = WebClient.builder()
      .baseUrl(FCM_URL.formatted(getProjectId(firebaseApp, credential)))
      .clientConnector(new ReactorClientHttpConnector(client))
      .build();
  24. KrishnakanthYachareni commented on Jun 19, 2024

    @KrishnakanthYachareni

    @emindeniz99 Thanks for the response. We have DLQ has setup for timedout requests. But we've been observing 1% of 180 million requests are getting failed due to above mentioned errors. Following is the WebClient configuration and replicated into 10 kuberenetes pods in the production. Do you have any suggestions on the configs?

     public WebClient webClient() {
            var connectionProvider = ConnectionProvider.builder("fcmConnection")
                    .maxConnections(500) // Max no of connections that the connection pool can maintain at any given time.
                    .build();
            HttpClient httpClient = getHttpClient(connectionProvider);
    
            httpClient.warmup().block(); // Eager Initialization of Event loop group, host name resolver and native transport libs.
    
            return WebClient.builder()
                    .clientConnector(new ReactorClientHttpConnector(httpClient))
                    .baseUrl(firebaseCCMConfig.getFcmHttpEndpoint())
                    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .build();
        }
    
        private HttpClient getHttpClient(ConnectionProvider connectionProvider) {
            return HttpClient.create(connectionProvider)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 45000)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .responseTimeout(ofSeconds(5))
                    .keepAlive(true)
                    .metrics(true, Function.identity())
                    .protocol(HttpProtocol.H2);
        }

    Note: The reactor metric reactor_netty_eventloop_pending_tasks is indeciating how many requests are in pending state and just waiting for worker threads to pickup. So I bumped the reactor threads from 4 - 8 threads.

  25. emindeniz99 commented on Jun 19, 2024

    @emindeniz99

    @emindeniz99 Thanks for the response. We have DLQ has setup for timedout requests. But we've been observing 1% of 180 million requests are getting failed due to above mentioned errors. Following is the WebClient configuration and replicated into 10 kuberenetes pods in the production. Do you have any suggestions on the configs?

     public WebClient webClient() {
            var connectionProvider = ConnectionProvider.builder("fcmConnection")
                    .maxConnections(500) // Max no of connections that the connection pool can maintain at any given time.
                    .build();
            HttpClient httpClient = getHttpClient(connectionProvider);
    
            httpClient.warmup().block(); // Eager Initialization of Event loop group, host name resolver and native transport libs.
    
            return WebClient.builder()
                    .clientConnector(new ReactorClientHttpConnector(httpClient))
                    .baseUrl(firebaseCCMConfig.getFcmHttpEndpoint())
                    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .build();
        }
    
        private HttpClient getHttpClient(ConnectionProvider connectionProvider) {
            return HttpClient.create(connectionProvider)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 45000)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .responseTimeout(ofSeconds(5))
                    .keepAlive(true)
                    .metrics(true, Function.identity())
                    .protocol(HttpProtocol.H2);
        }

    Note: The reactor metric reactor_netty_eventloop_pending_tasks is indeciating how many requests are in pending state and just waiting for worker threads to pickup. So I bumped the reactor threads from 4 - 8 threads.

    Can you increase the readtimeout from 5 sec to higher? Probably, google responds your requests slowly. When using the batch api, we saw very high response times. But after the v1 api, I did not look the response times.

    Edit: I tried to see average response time of post request on mobile, it seems 800 ms on high throughput. Tomorrow i will try to percentile 95 and 99, but we may not be tracking the response time for each request.

    Edit2: Did you investigate/consider the allocationstrategy option that I have shared? Setting the values may help. But I dont hope :) I do not understand/search the difference between maxconn on connection provider and min and max conn on allocation strategy option. I am not sure which one is correct way.

  26. emindeniz99 commented on Jun 24, 2024

    @emindeniz99

    The shutdown date for the old API has been updated. It has been postponed by one month. Additionally, they have provided a form link to request extensions.
    Old date: 6/21/2024 (june)
    New Date: 7/22/2024 (july)

    image image

    New Announcement:
    image

    Source: https://firebase.google.com/support/faq#deprecated-api-shutdown

  27. jonathanedey commented on Oct 9, 2024

    @jonathanedey
    Contributor

    HTTP/2 support has now been added with the release of v9.4.0.
    See our wiki for more information on this change.

    The deprecated APIs will also be removed in our next major release. For more information refer to: https://firebase.google.com/support/faq#fcm-23-deprecation

  28. good-vi commented on Oct 9, 2024

    @good-vi

    It's joke. I need send 400k UNIQUE pushes (topics can't solve that). Your http2 allow maximum 100 pushes in time for once http client:

    nghttp -nv https://fcm.googleapis.com/v1/projects/projectname/messages:send
    
    SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100

    Also the library generate new thread for each request. For throughput which i have before (10k per minute) now i need rewrite lib to fixed threadpool and multiple okhttp clients (each can send only 100 parallel http2 frames). Your marketing results: +1Gb ram and +0.5 cpu and minus lifetime of any developer with massive unique pushes...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions