Wednesday, 24 September 2014

Magento Deadlock error on huge load.

Not sure how many of us faced DB deadlock during peak load time.

We've faced deadlock while updating times used of salesrule table during our Black Friday sale . Since we were loosing money at that time, for time being we've removed the salesrule update by overriding Mage_SalesRule_Model_Observer :: sales_order_afterPlace.

Then  we have re-enabled the same after sometime and once we did that, I've realized its happening daily. But It will happen only twice or thrice per thousand orders.

Because of this we were collecting money from the customer and the order was not placed.

After hitting my head in the wall for a long time :). I got a clue about the issue that its because of the order of promo got applied. Here is my theory and reproduce steps.

==============
Steps to Replicate
==============
Promo creation
---------------
i) Create Two "No coupon shopping cart rule" one for simple product and one for virtual product. ( To use below I am naming promos as "Virtual Promo" & "Simple Promo")
ii) You can specify individual skus of simple & virtual product.

Then lets take a scenario

=====================
Customer 1 ::
=====================
i) Adding virtual product to cart.
ii) Virtual Promo will be applied automatically.
iii) Then he is Adding a simple product
iv) Simple Promo will be applied automatically.
v) Up on order placement. Magento will update times_used of Virtual & Simple Promo respectively.

=====================
Customer 2 ::
=====================
i) Adding simple product to cart.
ii) Simple Promo will be applied automatically.
iii) Then he is Adding a virtual product
iv) Virtual Promo will be applied automatically.
v) Up on order placement. Magento will update times_used of Simple & Virtual Promo respectively.

You can see how rule ids have saved in DB from the below screen cast.

http://screencast.com/t/KeYImQgyoW
http://screencast.com/t/BSHabO0d

After order placement magento updating times used in the same order. And it will leads to deadlock on huge load. Because the query will run like the below ( Just an example )

Customer 1

Begin transaction;
Update salesrule set times_used = 3 where rule_id = "1";

Lets say in the mean time second customer's transaction also started

Begin transaction;
Update salesrule set times_used = 3 where rule_id = "2";

Now First customer's  second update ( Update salesrule set times_used = 3 where rule_id = "2"; ) will wait for second customer's update to complete and second customer's second update will wait for the first one to complete. So whichever commits first will go and the second one will be rolled back.

You can easily fix this deadlock just by ordering the update.

Override Mage_SalesRule_Model_Observer in you local module and copy sales_order_afterPlace function to your own observer file and add a sort / natsort here ( http://screencast.com/t/8mFuo2tq ).

 Here is the config.xml update you need to do to override an observer class.

          <salesrule>
                <rewrite>
                    <observer>Rosetta_Promotion_Model_Salesrule_Observer</observer>
                </rewrite>
            </salesrule>

I've also found a potential deadlock on http://amasty.com/multiple-coupons.html.  Please contact me if you are interesting.

GET THE DEADLOCK FIXED AND ENJOY SELLING

Monday, 3 February 2014

Why most of us feel its always difficult to customize Magento ?

i) Its difficult because you can find too many ways to do the same functionality, So we end up in thinking which one is actually correct to the standard.

ii) You cannot find a proper wiki page which could guide you to some customization

iii) If you try to use debug statements like print_r, echo or Zend_debug::dump(), sometimes even this will leads to blank page or keep on loading.

iv) Now I feel tired tired tired, let me write the object in file and see what happens. Its again a big mess Firefox will crash.

v) Ok, No other go let me try to use Xdebug. Its taking me to 100 functions which will make my ass got burn.


AND LAST BUT NOT the least if you find something interesting and try to take a deep breath, comments on top of the function will make you confuse. See the small example below.

Hope you aware of cache config in Magento. You can specify which cache the system needs to use.

So let say I am going for default file cache. See the comment below red in color, if backend empty=file. So instead of leave as empty I have set as file. It throw warning in system.log. After a deep dive I've found that it needs to be empty or File.

But in the comment they mentioned  empty=file . So Beware of this kinda case error & etc etc ... willl make your life terrible.

 <!--Magento Cache-->
        <cache>
            <backend></backend><!-- apc / memcached / xcache / empty=file -->
            <slow_backend></slow_backend> <!-- database / file (default) - used for 2 levels cache setup, necessary for all shared memory storages -->
        </cache>
        <!--Full Page Cache-->
        <full_page_cache>
            <backend></backend><!-- apc / memcached / xcache / empty=file -->
            <slow_backend></slow_backend> <!-- database / file (default) - used for 2 levels cache setup, necessary for all shared memory storages -->
        </full_page_cache>
        <!--Rest of defaults-->

                                                         BE AWARE OF MAGENTO


Saturday, 16 November 2013

Inno DB roww level locking

I was hearing the term row level locking for a very long time. But I really don't know  whats that and TBH i don't even tried to know what it could be.

Nov 14 got a mail from my application Manager. Its starts with Hi Senthil " Please look in to this and Advice". I have replied to that Email saying I've asked for logs from sys-admins.

Got the logs and found that 1 Order number assigned to 2 Customer @ the same time. So now I got the issue.

Yeah I caught your mind voice. Exactly googled .... Got the solution is lock the row.

Then I started thinking how. Don't worry its easy.

i) Change your table engine to INNODB.
ii) Add transaction before start selecting the order number from the number. ( You need to have the running order number in a table, so that you can simply access it )
iii) Select query needs to be like the below

SELECT order_number_column from table_name where blah blah ..... for UPDATE;

for update will lock the row, so even on high traffice the order number will be unique.

iv) Dont forget to commit ( if success) or rollback ( if error only on error condition ) else you will end up in dead lock.

Hope you got some idea about row level locking and the use case. 

Tuesday, 26 March 2013

Funny Typo Errors in Magento EE 1.12

Few Day back i came across an issue in Magento Admin. Though its not a potentially a great functionality bug, i dig in to that because of our great testing team and ended up with funny typo error.


I though its the bug only in Magento EE 1.12, unfortunately :) its there in all the version and in multiple place. Ok Now coming to the issue.

"Success Message is not clearing even after i update something".

Lets say i am applying a promo which gives me success message or let say i am getting some error message in header, even after rectifying that error in cart it remains the same. Means the Error message not cleared.

Problem is because of

 if(this.loadingAreas.indexOf('message'==-1)) this.loadingAreas.push('message');

Refer the indexOf function above which is there in "js/mage/adminhtml/sales.js". It should be  if(this.loadingAreas.indexOf('message')==-1)

element.name.indexOf("[" + name[1] + "]") != -1 && messages[i].value != "") -- Line 821

area.indexOf('items' != -1) -- Line 853

I though it was actidental but it seems like intentional :) .... but any way we have to correct  if you are facing any funniest problem like this in Magento Admin hope you got some idea to dig further......

Sunday, 25 November 2012

Avoid Document Expire while hitting browser back





Using  Get Post Get rule you can avoid this.
  • Let say I have example1.php example2.php & example3.php
  • I am posting some value from example1.php to example2.php then i did all the DB stuff as per my need and rendered the page (Not Redirected - Just posted and the page got rendered).
  • After submitting values from example2.php to sometest.php and I have redirected the page to example3.php. Now if you click browser back the Document will Expire.
    • To Avoid this we can post the values from example1.php to some other PHP and then we need to Redirect the user to example2.php
    • This is the GPG rule which every developer needs to follow to avoid Document expire and also to avoid redundant entry in DB on refreshing the page.

Thursday, 30 August 2012

Magento DB locked while saving product

If you facing db lock while saving your product don't worry just you want to do the below.

Check the problem is due to your local module queries. If you have written any observer while before save or on after save.

If not take a backup of below listed tables and and truncate. Surely it will solve your problem i did the same and its worked for me.
 

truncate table sales_flat_quote;
truncate table sales_flat_quote_address;
truncate table sales_flat_quote_address_item;
truncate table sales_flat_quote_item;
truncate table sales_flat_quote_item_option;
truncate table sales_flat_quote_payment;
truncate table sales_flat_quote_shipping_rate;



Hip Hip Hurrey .............

Enjoy coding in magento............

Monday, 5 March 2012

SMTP Mail configuration Magento

1. You need to override Mage_Core_Model_Email_Template [ app/code/core/Mage/Core/Model/Email/Template.php need to be override].
2. Create a Module in your app/code/local/Company/Core
3. Create a config.xml in app/code/local/Company/Core/etc, copy and paste the below code in that file.

<?xml version="1.0"?>
<config>
        <modules>
        <Companyname_Core>
            <version>0.1.0</version>
        </Companyname_Core>
    </modules>
    <global>
        <models>
            <core>
                <rewrite>                   
                    <email_template>Companyname_Core_Model_Email_Template</email_template>
                </rewrite>
            </core>
        </models>
    </global>
</config>

3. Create a Template.php in app/code/local/Company/Core/Model/Email/, copy and paste the below code in that file.

<?php
class Companyname_Core_Model_Email_Template extends Mage_Core_Model_Email_Template
{
    public function send($email, $name=null, array $variables = array())
    {
   
        if (!$this->isValidForSend()) {
            Mage::logException(new Exception('This letter cannot be sent.')); // translation is intentionally omitted
            return false;
        }

        $emails = array_values((array)$email);
        $names = is_array($name) ? $name : (array)$name;
        $names = array_values($names);
        foreach ($emails as $key => $email) {
            if (!isset($names[$key])) {
                $names[$key] = substr($email, 0, strpos($email, '@'));
            }
        }
       
        $variables['email'] = $email;
        $variables['name'] = $name;
       
        ini_set('SMTP', Mage::getStoreConfig('system/smtp_settings/host'));
        ini_set('smtp_port', Mage::getStoreConfig('system/smtp_settings/port'));
       
        $host = Mage::getStoreConfig('system/smtp_settings/host');
        $smtpuser = Mage::getStoreConfig('system/smtp_settings/username');
        $smtpuserpass = Mage::getStoreConfig('system/smtp_settings/password');
        $port = Mage::getStoreConfig('system/smtp_settings/port');
        $auth = strtolower(Mage::getStoreConfig('system/smtp_settings/auth'));
        $mail = $this->getMail();
       
        $setReturnPath = Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH);
        switch ($setReturnPath) {
            case 1:
                $returnPathEmail = $this->getSenderEmail();
                break;
            case 2:
                $returnPathEmail = Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL);
                break;
            default:
                $returnPathEmail = null;
                break;
        }
       
        if ($returnPathEmail !== null) {
            $mailTransport = new Zend_Mail_Transport_Sendmail("-f".$returnPathEmail);
            Zend_Mail::setDefaultTransport($mailTransport);
        }
       
        foreach ($emails as $key => $email) {
            $mail->addTo($email, '=?utf-8?B?' . base64_encode($names[$key]) . '?=');
        }
       
        $this->setUseAbsoluteLinks(true);
        $text = $this->getProcessedTemplate($variables, true);
       
        if($this->isPlain()) {
            $mail->setBodyText($text);
        } else {
            $mail->setBodyHTML($text);
        }
        $config = array('port' =>$port);
        if($auth != 'NONE'){
            $config = array('port' =>$port, 'auth' => $auth, 'username' => $smtpuser, 'password' => $smtpuserpass);
        }
        $transport = new Zend_Mail_Transport_Smtp($host, $config);
       
        $mail->setSubject('=?utf-8?B?'.base64_encode($this->getProcessedTemplateSubject($variables)).'?=');
        $mail->setFrom($this->getSenderEmail(), $this->getSenderName());
       
        try {
            $mail->send($transport); // Zend_Mail warning.. // Note. here the $transport parameter contains the configuration for smtp authentication
            $this->_mail = null;
        }
        catch (Exception $e) {
            return false;
        }
        return true;
    }
}

Thats all now you can enjoy sending mail with smtp in magento................ :)