Skip to content

Too much memory consumption when Klarna is doing maintenance on table klarna_logs #34515

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

Closed
TekniskSupport opened this issue Nov 3, 2021 · 7 comments
Assignees
Labels
Component: Bundled Extension Issue is caused by one of the bundled extensions Issue: ready for confirmation

Comments

@TekniskSupport
Copy link

TekniskSupport commented Nov 3, 2021

Hi,

When the cron job Klarna\Core\Cron\UpdateApiLog is run it creates a collection from the table klarna_logs and sets the result to class variable, this loads the entire table into memory, as it contains a lot of JSON, the result is it consumes a lot of RAM.

Klarna\Core\Cron\UpdateApiLog

    private function updateIncrementId(array $incrementIds)
    {
        $this->searchCriteriaBuilder->addFilter('increment_id', null, 'null');
        $searchCriteria = $this->searchCriteriaBuilder->create();

        $logs = $this->logRepository->getList($searchCriteria);
        foreach ($logs->getItems() as $item) {
            if (!isset($incrementIds[$item->getKlarnaId()])) {
                continue;
            }
            $item->setIncrementId($incrementIds[$item->getKlarnaId()]);
            $this->logRepository->save($item);
        }
    }

From method getList in
Klarna\Core\Model\LogRepository

        $collection = $this->getCollection($searchCriteria);

        $searchResults->setItems($collection->getItems());

In our case this routine was using well over 4 GB of memory each time it ran the cron job, this caused memory related issues.
We solved it by adding a few addAttributeToSelect()'s to the collection, so that it no longer loads data it doesn't even use.
Consumtion from that job is now at less than 10% what it was (and that is including bootstrapping Magento).

For what it's worth you could optimize it further by doing a DISTINCT select for the incrementIDs.
Or even better, not even doing collections and selects just perform the task in SQL by doing an update from join.

UPDATE klarna_logs k
INNER JOIN klarna_logs kb ON k.klarna_id = kb.klarna_id 
	AND kb.increment_id IS NOT NULL
SET k.increment_id = kb.increment_id
WHERE k.klarna_id = kb.klarna_id

There are similar issues in the Klarna\Core\Cron\CleanLogs

ps.
Also note that the use of private methods made it impossible to do a simple interceptor or just overriding the problematic method.
ds.

Preconditions

1, use Magento open source edition 2.4.3
2, have klarna checkout installed and configured

Steps to reproduce

  1. Receive a lot of klarna payments in a relative short amount of time
  2. have cron jobs enabled

Expected result

No issues with unnecessary memory consumtion.

Actual result

Crashing cron jobs due to memory issues

@m2-assistant
Copy link

m2-assistant bot commented Nov 3, 2021

Hi @TekniskSupport. Thank you for your report.
To speed up processing of this issue, make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@bjornmeat
Copy link

Hi @TekniskSupport - I believe this issue should be directed to Klarna. https://marketplace.magento.com/klarna-m2-checkout.html#product.info.details.support

@fritzmg
Copy link

fritzmg commented Nov 15, 2021

@bjornmeat Magento currently requires a pinned version of the Klarna extension, for some reason. I am assuming this is because the Magento development team actually works on the Klarna extension themselves? So even if this is reported to Klarna directly and fixed, Magento still needs to release a new version of itself in order to be able to actually fix it in Magento.

@TekniskSupport
Copy link
Author

An update in the matter!

I did contact Klarna and they (somewhat) acknowledged the issue. So at least their developers are informed in the matter.

@engcom-November engcom-November self-assigned this Dec 10, 2021
@m2-assistant
Copy link

m2-assistant bot commented Dec 10, 2021

Hi @engcom-November. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-November
Copy link
Contributor

Hi @TekniskSupport
This is an issue related with 3rd party extension and code of this extension is not part of https://github.com/magento/magento2 git repository. We are closing this issue as community is not able to provide fix for it in this repository. All questions, issue reports and fix for them should be addressed to the corresponding extension owners(support) on the Magento Market place page
Thank you.

@engcom-November engcom-November added the Component: Bundled Extension Issue is caused by one of the bundled extensions label Dec 13, 2021
@fritzmg
Copy link

fritzmg commented Dec 13, 2021

@engcom-November again, there is still need for action for Magento itself, see my explanation here: #34634 (comment)

Reporting this with Klarna won't matter as Magento still would have to release a new versino, once Klarna released a new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Bundled Extension Issue is caused by one of the bundled extensions Issue: ready for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants