google-ads-java icon indicating copy to clipboard operation
google-ads-java copied to clipboard

Google Ads API RunTime exception

Open saurabhsumanbbsr opened this issue 3 years ago • 7 comments

Hi Team, While testing Google Ads API after the request is posted and response is received, we are getting below exception regarding channel shutdown for maybe GoogleAdsServiceClient.Please suggest.

Logs

Mar 03, 2022 10:55:38 PM io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference cleanQueue

SEVERE: ~~~ Channel ManagedChannelImpl{logId=21, target=googleads.googleapis.com:443} was not shutdown properly!!! ~~~

Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.

java.lang.RuntimeException: ManagedChannel allocation site

            at io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.<init>(ManagedChannelOrphanWrapper.java:93)

            at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:53)

            at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:44)

            at io.grpc.internal.ManagedChannelImplBuilder.build(ManagedChannelImplBuilder.java:625)

            at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:264)

            at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:383)

            at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.access$1900(InstantiatingGrpcChannelProvider.java:82)

            at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider$1.createSingleChannel(InstantiatingGrpcChannelProvider.java:239)

            at com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:72)

            at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:249)

            at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:227)

            at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:205)

            at com.google.ads.googleads.v9.services.stub.GrpcGoogleAdsServiceStub.create(GrpcGoogleAdsServiceStub.java:96)

            at com.google.ads.googleads.v9.services.stub.GoogleAdsServiceStubSettings.createStub(GoogleAdsServiceStubSettings.java:185)

            at com.google.ads.googleads.v9.services.GoogleAdsServiceClient.<init>(GoogleAdsServiceClient.java:137)

            at com.google.ads.googleads.v9.services.GoogleAdsServiceClient.create(GoogleAdsServiceClient.java:118)

            at com.google.ads.googleads.lib.catalog.GeneratedCatalog$V9Client.createGoogleAdsServiceClient(GeneratedCatalog.java:5710)

            at com.business.sem.google.API$31.run(API.java:3033)

            at com.business.sem.google.API$31.run(API.java:1)

saurabhsumanbbsr avatar Mar 07 '22 10:03 saurabhsumanbbsr

Hi,

Please make sure you are closing the GoogleAdsServiceClient when you are done making requests. You can do this with a try-with-resources block as shown in examples:

    try (GoogleAdsServiceClient googleAdsServiceClient =
        googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
      // Issue requests and process responses...
    }

or by calling close() explicitly on the service client.

Thanks, Josh

jradcliff avatar Mar 07 '22 14:03 jradcliff

Hey, i add this in the code.Now i cannot face this runtime exception.But when code execution is finished.JVM cannot release its memory and server ram is exhausted and server is down. GoogleAdsServiceClient serviceClient = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient();
Code: serviceClient.shutdown(); serviceClient.awaitTermination(2, TimeUnit.SECONDS); serviceClient.close();

Ashirkhan857 avatar Mar 31 '22 15:03 Ashirkhan857

Hi,

Just calling serviceClient.close() should suffice. Are you sure that you are closing all of the service clients you create, even if you encounter exceptions at some point in the process after they're created?

One advantage to using a try-with-resources block instead of explicitly calling close() is that it'll ensure the client is closed no matter what, similar to a finally block that calls close().

Thanks, Josh

jradcliff avatar Mar 31 '22 16:03 jradcliff

Code i'm using right now...

List<String> GAQL_QUERY_STRINGS = ImmutableList.of(query); GoogleAdsServiceClient serviceClient = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient(); for (String gaqlQuery : GAQL_QUERY_STRINGS) { // Starts the report download in a background thread. SearchGoogleAdsStreamRequest request =SearchGoogleAdsStreamRequest.newBuilder().setCustomerId(client).setQuery(gaqlQuery).build(); ResponseCountingObserver responseObserver = new ResponseCountingObserver(Long.parseLong(client)); serviceClient.searchStreamCallable().call(request,responseObserver); ServerStream<SearchGoogleAdsStreamResponse> stream =serviceClient.searchStreamCallable().call(request); for (SearchGoogleAdsStreamResponse response : stream) { for (GoogleAdsRow googleAdsRow : response.getResultsList()) { myWriter.write(googleAdsRow.getCustomerClient().getId() + ";"); myWriter.write('"'+googleAdsRow.getCustomerClient().getDescriptiveName()+'"'

  • ""); myWriter.write("\n");

    } } myWriter.close(); try { serviceClient.shutdown(); serviceClient.awaitTermination(5, TimeUnit.SECONDS); serviceClient.close(); } catch (InterruptedException e) {//e.printStackTrace();} }

On Thu, Mar 31, 2022 at 9:28 PM Josh Radcliff @.***> wrote:

Hi,

Just calling serviceClient.close() should suffice. Are you sure that you are closing all of the service clients you create, even if you encounter exceptions at some point in the process after they're created?

One advantage to using a try-with-resources block instead of explicitly calling close() is that it'll ensure the client is closed no matter what, similar to a finally block that calls close().

Thanks, Josh

— Reply to this email directly, view it on GitHub https://github.com/googleads/google-ads-java/issues/586#issuecomment-1084817417, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJ2KXNJ4BX6WV6NQ4U76WITVCXHBXANCNFSM5QC46TZQ . You are receiving this because you commented.Message ID: @.***>

-- Name: Muhammad Ashir Abbas Khan Department: Computer Science University: Bahria University(A Project Of Pakistan Navy)

Ashirkhan857 avatar Apr 01 '22 09:04 Ashirkhan857

To add some outside context here, we also run into this error on first hit post startup, but we cache our BackgroundResource and the error never comes up again.

I believe the error to be innocuous ( first hit doesn't fail and returns accurate results )

private List<GoogleAdsRow> search( final GoogleAdsClient googleAdsClient, final SearchGoogleAdsStreamRequest request ) {

        final GoogleAdsServiceClient googleAdsServiceClient = ( GoogleAdsServiceClient ) googleBackgroundResourceCache.getServiceClient( googleAdsClient, ServiceClientEnum.GoogleAdsServiceClient, request.getCustomerId() );

        for( int i = 0; i <= MAX_RETRIES; i++ ) {

            try {

                final List<GoogleAdsRow> rows = Lists.newArrayList();

                final ServerStream<SearchGoogleAdsStreamResponse> stream = googleAdsServiceClient
                        .searchStreamCallable()
                        .call( request, GrpcCallContext.createDefault().withTimeout( Duration.of( MAX_TIMEOUT_LENGTH, MAX_TIMEOUT_UNITS ) ) );

                for( final SearchGoogleAdsStreamResponse response : stream ) {
                    rows.addAll( response.getResultsList() );
                }

                return rows;

            } catch( Exception e ) {
                GoogleRetryService.handleRetry( e, i );

                i++;

            }
        }

        return null;
    }
switch ( serviceClientEnum ) {
            case GoogleAdsServiceClient:
                backgroundResource = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient();
                break;

We don't manually call shutdown on the search client and coupled with the cache we achieve very high performance.

Pete

PeterLavetsky avatar Apr 01 '22 12:04 PeterLavetsky

Thanks, @PeterLavetsky . I've been trying mightily to get this error to occur in my local tests, but haven't been able to so far. Are you saying that immediately after startup, the code you shared will hit the catch block and retry?

Also, @Ashirkhan857 , in your example I noticed you are actually making two calls to the API:

// First call with your responseObserver.
serviceClient.searchStreamCallable().call(request,responseObserver);
// Second call without an observer.
ServerStream<SearchGoogleAdsStreamResponse> stream=serviceClient.searchStreamCallable().call(request);

If your ResponseCountingObserver is just counting responses by customer ID, it would be more efficient to do that in the for loop that's iterating over each SearchGoogleAdsStreamResponse instead of calling the API twice.

Thanks, Josh

jradcliff avatar Apr 01 '22 15:04 jradcliff

@jradcliff to more accurately state my observations, after deploying / restarting in the production environment I always see this error within a minute of startup when the above search() method is inevitably called. I can't for sure say it's the first hit, but I can say with certainty that per JVM we see it once, and then never again, presumably because of the BackgroundResource reuse.

I never see this running locally through IntelliJ. Obviously the environments are different, but the code / libraries are the same.

  2022-04-01 12:10:42,098 ERROR ManagedChannelOrphanWrapper$ManagedChannelReference - *~*~*~ Channel ManagedChannelImpl{logId=1, target=googleads.googleapis.com:443} was not shutdown properly!!! ~*~*~*
  Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
  java.lang.RuntimeException: ManagedChannel allocation site
  at io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.<init>(ManagedChannelOrphanWrapper.java:93)
  at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:53)
  at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:44)
  at io.grpc.internal.ManagedChannelImplBuilder.build(ManagedChannelImplBuilder.java:625)
  at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:264)
  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:383)
  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.access$1900(InstantiatingGrpcChannelProvider.java:82)
  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider$1.createSingleChannel(InstantiatingGrpcChannelProvider.java:239)
  at com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:72)
  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:249)
  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:227)
  at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:205)
  at com.google.ads.googleads.v10.services.stub.GrpcGoogleAdsServiceStub.create(GrpcGoogleAdsServiceStub.java:96)
  at com.google.ads.googleads.v10.services.stub.GoogleAdsServiceStubSettings.createStub(GoogleAdsServiceStubSettings.java:185)
  at com.google.ads.googleads.v10.services.GoogleAdsServiceClient.<init>(GoogleAdsServiceClient.java:137)
  at com.google.ads.googleads.v10.services.GoogleAdsServiceClient.create(GoogleAdsServiceClient.java:118)
  at com.google.ads.googleads.lib.catalog.GeneratedCatalog$V10Client.createGoogleAdsServiceClient(GeneratedCatalog.java:5771)
  at inc.fluency.google.service.google.GoogleBackgroundResourceCache.getServiceClient(GoogleBackgroundResourceCache.java:51)

Pete

PeterLavetsky avatar Apr 01 '22 16:04 PeterLavetsky