Closed Hu1-Li closed 6 years ago
@Hu1-Li I see where this issue is coming from. Ordering the links by created_at desc and then picking the top link as latest will cause this. As for high load websites with many concurrent requests at a time will have many rows with the same created_at time. Wrong ordering is causing while loop and eventually select
queries in MySQL to execute many more times than expected.
This can be fixed by ordering by id
instead of created_at
in latest_default_link
method.
I am bit busy right now, I will fix and test this when I get some time. Would you like to make necessary changes and the above tests and open a PR?
OK, I will try.
Update:
[USING MYSQL]
In my test on the fixed code, which change ordering from created_at
to id
, the test comes with A99
, then it got stucks. As expect, the next short code should be Baa
, as the below line
link_manager.find(short_code=base_str)
it got baa
In Mysql, String compare is case-insensitive, thus, causing the server down ,since it needs to loop a really big cycle, from baa
to A99
then starts over and over again.
So when creating table, the field short_code
should be
short_code
varchar(10) COLLATE utf8_bin DEFAULT NULL,`
In SQLAchemy, This can be achieved by
short_code = Column(Unicode(6, collation="utf8_bin"), unique=True, index=True, default=None)
Pull request #17 fixes this problem, As for test, i get no idea how to achieve the test for the above problem @amitt001
// Update
For sqlite, it should be
Column(Unicode(6, collation="BINARY"), unique=True, index=True, default=None)
For mysql, it should be
Column(Unicode(6, collation="utf8_bin"), unique=True, index=True, default=None)
emmmm,
Fixed in commit: https://github.com/amitt001/pygmy/commit/67260baca6b3ecf11848169f20a857e8fd384762
@Hu1-Li Thanks for fixing this bug and contributing 🍰
Here is the simple test result, about function
next_short_code
Order by Link.ID desc
Order by Link.created_at desc
As you can see, order by
link.created_at desc
is not always corect.And If using
link.created_at desc
will get the incorrect answer ofbase_str
, which will trapped into the while cycle.here is the log of simple call
next_short_code
, which iter fromh2k
toh2x
.If you want to re-appear this situation, write two script
def short(i): r = requests.post( "http://localhost:9119/api/shorten", headers={'Content-Type': 'Application/json'}, data=json.dumps( {"long_url": "https://test.com/a/b/c/{i}".format(i=i)} ) )
for i in range(10000): short(i)
!/usr/bin/env python3
"""Script when you just want to run Pygmy API."""
from pygmy.core.initialize import initialize initialize()
from pygmy.helpers.link_helper import next_short_code import time while True: print(next_short_code())