Skip to content

error handling infinite loop issue #67

@oscar2song

Description

@oscar2song

We experienced high CPU usage issue on our production when publishing events were happening after we upgraded the com.contentstack.sdk to the new version:

com.contentstack.sdk java 1.10.1

after investigation, we found the issue is from the following code that caused error handling infinite loop when we fetch resources from contentstack, which in further caused the some piece of memory never released and also made the thread never released until our CMS application was killed/restarted.

We have a workaround solution to make changes of the source code to solve the issue.

Here is the origin code com.contentstack.sdk.Query# execQuery

Here is our customed code:

protected void execQuery(SingleQueryResultCallback callBack, QueryResultsCallBack callback) {
        String urlString = "content_types/" + contentTypeUid + "/entries";
        queryResultCallback = callback;
        singleQueryResultCallback = callBack;
        setQueryJson();
        urlQueries.put(Constants.ENVIRONMENT, this.headers.get(Constants.ENVIRONMENT));
        includeLivePreview();
        mainJSON.put(QUERY, urlQueries);
        fetchFromNetwork(urlString, mainJSON, callback, callBack);
}

The main difference is removing the try catch to allow exception to be thrown out from ContentStack SDK to give a user opportunity to control exception and do some retries when an error occurs.

For example:

public ContentStackQueryService(Query query) {
    this.query = query.limit(Pagination.PAGE_SIZE).includeCount(); // ensure we do have count
    this.callBack = new QueryResultsCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
            if (error != null) {
                final String failedMsg = String.format("Query failed: ResponseType=%s, ErrorCode=%s, ErrorMessage=%s, ErrorDetail=%s,", responseType, error.getErrorCode(), error.getErrorMessage(), error.getErrorDetail());
                throw new RuntimeException(failedMsg); // in this way, we will retry unless we failed finally
            } else {
                int itemsTotalCount = queryresult.getCount();
                if (itemsTotalCount == 0) {
                    endOfResults.set(true); // no results
                    return;
                }
   ‘’’’’’’

please make some changes and fix the issue in this SDK, we will upgrade the SDk once you update the code and publish a new version in maven repository.

Thanks
Oscar Song

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions