Yii Framework Forum: Optimistic locking in v2? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Optimistic locking in v2?

#1 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 29 April 2012 - 09:09 AM

Hi,

I messed with this subject for some time now. To the best of my knowledge there is no optimistic locking in Yii v1.x.

Having some sort of solution for concurrency problems is a must in any web application besides the most trivial ones, due to the concurrent nature of a web app. I think that if Yii v2 would have some sort of solution that would be very good for alleviating Yii's value for anyone who's smart enough to appreciate this. To me, if its going to enter, it will part of the 'professional' slogan of Yii.

If it is considered, here's some suggestions:
  • Do have a "StaleObjectErroeException" of some sort. Its important that application specific logic could determine what to do in the case of stale object being attempted saving. In some cases the logic will be to try several times and then fire a more 'conventional' exception (e.g. CHttpException). In other cases the logic would be to ignore this exception as present the user with the same "thank you for doing this or that" message.
  • Its important to have the optimistic locking in the 'standard' API calls like save() or update() as those are used for example if beforeSave() is desired (kind of like the full fledged API methods, contrary to using the aux methods like updateByPk()).


I am aware of this forum thread and also initiated during my research on optimistic locking that forum thread. Both are relevant.

Thanks!
Boaz.
Therapeutic PHP sessions My LinkedIn Profile
0

#2 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 03 May 2012 - 07:50 AM

I really get your point and I know that this is a very useful feature BUT the idea of Yii is to provide you with the most basic and non-bloated piece of code that is used in 99% of all cases and I personally think that versioning is something that should explicitly implemented by the developer. However, the way one has to go to implement this is really tedious (basically replicating CActiveRecord just for this simple case) and prone to error. That brings me to another point: It should be easier to extend CActiveRecord itself to make life easier. I am thinking about a base class that could be extended more easily.
0

#3 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 03 May 2012 - 08:03 AM

View PostHaensel, on 03 May 2012 - 07:50 AM, said:

I really get your point and I know that this is a very useful feature BUT the idea of Yii is to provide you with the most basic and non-bloated piece of code that is used in 99% of all cases and I personally think that versioning is something that should explicitly implemented by the developer. However, the way one has to go to implement this is really tedious (basically replicating CActiveRecord just for this simple case) and prone to error. That brings me to another point: It should be easier to extend CActiveRecord itself to make life easier. I am thinking about a base class that could be extended more easily.


I disagree. I think that optimistic locking is an killing feature and a feature that will lure enterprise users.

As for the implementation, I have already implemented it myself, by the way of extending CActiveRecord and having all my model extend this class. In it I have: safelyUpdateByPk(), safelyDeleteByPk() and safelyUpdateByPkWithRetry() which is the same as the update method, but also accepts two more parameters - number of retries and interval between retries. My implementation works (I think :) ) but I think that this deserves to be in the CActiveRecord API directly.

Just MHO... and thanks for the feedback!
Therapeutic PHP sessions My LinkedIn Profile
0

#4 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 03 May 2012 - 08:20 AM

I would say you should create a pull request on Github as soon as Yii 2.0 alpha is available and see what the core members think about it. As long as it is really unobtrusive and configurable you might have a chance of getting it into the core yourself ;)
0

#5 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 03 May 2012 - 08:22 AM

I'm wondering if that's another usecase for intercepting behaviours in CActiveRecord ...
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#6 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 03 May 2012 - 08:31 AM

View PostDa:Sourcerer, on 03 May 2012 - 08:22 AM, said:

I'm wondering if that's another usecase for intercepting behaviours in CActiveRecord ...


What's "intercepting behaviors"? I'll be happy for some information/links. For all I know, behaviors are a way to "dress" a class with extra functionality (methods), kind of a "poor man's multiple inheritance".
Thanks!
Therapeutic PHP sessions My LinkedIn Profile
0

#7 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 03 May 2012 - 08:34 AM

That are behaviours with the power to intercept method calls such as findByPk() etc. A bit like what filters are to controllers. This would allow for more fine grained query caching or even identity maps.
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
1

#8 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 03 May 2012 - 09:45 AM

View PostDa:Sourcerer, on 03 May 2012 - 08:34 AM, said:

That are behaviours with the power to intercept method calls such as findByPk() etc. A bit like what filters are to controllers. This would allow for more fine grained query caching or even identity maps.




Gotcha.
Could very well be such a case, but wouldn't it be cleaner to have it directly in the API? Is "optimistic locking support" directly in the API doesn't fit right? I don't think that by principal this is a feature that should be out of the (lean and mean) framework itself. I know that there are good frameworks that include optimistic (and pessimistic) locking.

But then again, getting it done is a matter of resource allocation I guess more than theoretical argument and I cannot help much with the available resource that Yii dev team has (or that I have... :) ).
Therapeutic PHP sessions My LinkedIn Profile
0

#9 User is online   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,735
  • Joined: 17-January 09
  • Location:Russia

Posted 03 May 2012 - 11:04 AM

Boaz
That's very application specific and depends on what backend you're using to store your data. Sometimes it's required to use pessimistic locking, sometimes you want to actually try merging changes automatically, sometimes you want just to overwrite changes. In some cases it's much more better to rely on DB isolation levels rather than on the code.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#10 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 03 May 2012 - 01:54 PM

View Postsamdark, on 03 May 2012 - 11:04 AM, said:

Boaz
That's very application specific and depends on what backend you're using to store your data. Sometimes it's required to use pessimistic locking, sometimes you want to actually try merging changes automatically, sometimes you want just to overwrite changes. In some cases it's much more better to rely on DB isolation levels rather than on the code.


Hi samdark:
It doesn't have to be a case of full blown solution or nothing. I think that optimistic locking API methods only, that include a matching "int" locking field in the relevant AR class/table ("lock version" indicator), and a little warning that while "safer", those methods sacrifices just a little bit of performance, is feasible and isn't dependent on specific DB. Other needs or cases, such as pessimistic locking are not included in this improvement, but nor do they are likely to contradict with this improvement, should the time come to handle those as well (if at all - I agree that more complicated solutions are starting to introduce complexity like DB specific peculiarities).
Also, I want to state that I wish I had the resources to help (be the resources time, experience, etc). Unfortunately I don't. I'm rather new in the Yii neighborhood and my time to contribute will come soon enough, I hope.



Therapeutic PHP sessions My LinkedIn Profile
0

#11 User is online   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,735
  • Joined: 17-January 09
  • Location:Russia

Posted 03 May 2012 - 02:40 PM

Boaz
All this sounds like a good candidate for a behavior. In Yii2 we'll have new events system so this one will fit. Maybe we'll even include it into the core or mark as officially supported extension.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#12 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 03 May 2012 - 02:45 PM

View Postsamdark, on 03 May 2012 - 02:40 PM, said:

Boaz
All this sounds like a good candidate for a behavior. In Yii2 we'll have new events system so this one will fit. Maybe we'll even include it into the core or mark as officially supported extension.


Nice. Having such a thing as a behavior makes the application design even more modular (in comparison to some master class) - plug, prey and hopefully play - or to the ashtray :)
Y2 will gotta have good documentation from the start, but I guess this is off-topic to this thread.
Therapeutic PHP sessions My LinkedIn Profile
0

#13 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,084
  • Joined: 16-February 11
  • Location:Japan

Posted 04 May 2012 - 07:19 AM

I think I've read that Yii2 AR will hold the original values that have been loaded from the storage. And we will have some property/method to check if the data has been edited or not ... IsDirty() or something like that.

If it is the case, then we can implement an optimistic locking without an additional column for version controlling ... as they are doing in .NET-Framework.
update some_table
set col_a = val_a, col_b = val_b, ... col_n = val_n
where col_a = val_a_org and col_b = val_b_org and ... col_n = val_n_org

0

#14 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 04 May 2012 - 08:55 AM

View Postsoftark, on 04 May 2012 - 07:19 AM, said:

I think I've read that Yii2 AR will hold the original values that have been loaded from the storage. And we will have some property/method to check if the data has been edited or not ... IsDirty() or something like that.

If it is the case, then we can implement an optimistic locking without an additional column for version controlling ... as they are doing in .NET-Framework.
update some_table
set col_a = val_a, col_b = val_b, ... col_n = val_n
where col_a = val_a_org and col_b = val_b_org and ... col_n = val_n_org



I'm not into the details of the suggested design. Skimming through it, I think that isDirty() would not be good for optimistic locking: even if calling this method will trigger a check against the DB (which I'm not sure of - possibly it refers to the state of the object at hand after you've read(=instantiated) it) and even if you check "just before" the saving, there's still little time between the check and the actual save - and this will lead to problems and its hardly a locking of any kind.
The check and saving should be atomic and since the save is done in the DB, the check should be there as well, in a single atomic action together with the saving.
Today I achieve this with updateByPk() that is called with a $condition, which is translated to an SQL statement that resembles "UPDATE tablename set some_col = "value" WHERE lock_version=135 (in blue is the condition which makes this atomic and guarentees the data integrity, at the end of the day(=request :) ).

Your second paragraph isn't related to the isDirty directly, but rather relies on the AR object to retain the original values it was filled with when instantiated. If that's going to be in Yii's v2 AR then indeed no extra column for version checking is needed but rather a check with your suggested code could do it. But, notice that on tables with lots of columns its possible that a single "lock_version" column to check will give better performance than the suggested check that will compare lots of columns, and also this is error prone while coding.



Therapeutic PHP sessions My LinkedIn Profile
0

#15 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,084
  • Joined: 16-February 11
  • Location:Japan

Posted 04 May 2012 - 06:43 PM

View Postsoftark, on 04 May 2012 - 07:19 AM, said:

If it is the case, then we can implement an optimistic locking without an additional column for version controlling ... as they are doing in .NET-Framework.


Well, I meant to say that yii developers can make use of the stored values for dirty checking to implement an optimistic locking version of updateByPk().
And, yes, the performance penalty could be an obstacle, but they are doing this in .NET.
0

#16 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 05 May 2012 - 12:20 AM

View Postsoftark, on 04 May 2012 - 06:43 PM, said:

Well, I meant to say that yii developers can make use of the stored values for dirty checking to implement an optimistic locking version of updateByPk().
And, yes, the performance penalty could be an obstacle, but they are doing this in .NET.


Well, Yii and PHP communities do not hold the world by the b&^$s (at least, that's how I see things where I live - Israel - the "the dot net nation") so the those communities cannot afford to act as Microsoft... :) . If there is a workaround over the performance penalty in the first place why not take it? The extra column seems like a little burden to me.
In any case, I think we're putting the cart before the horses... :) .
Therapeutic PHP sessions My LinkedIn Profile
0

#17 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,084
  • Joined: 16-February 11
  • Location:Japan

Posted 05 May 2012 - 12:47 AM

Okay, I think I've got your point, and hopefully you've got mine.
We have different opinions but it's OK with me. :)

BTW, I don't really care Yii and PHP people could have adopted some nice idea from microsoft, if any.
Indeed, the naming convention of Yii core classes reminded me of those in MFC. ::)
0

#18 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 05 May 2012 - 09:59 AM

View Postsoftark, on 05 May 2012 - 12:47 AM, said:

Okay, I think I've got your point, and hopefully you've got mine.
We have different opinions but it's OK with me. :)

BTW, I don't really care Yii and PHP people could have adopted some nice idea from microsoft, if any.
Indeed, the naming convention of Yii core classes reminded me of those in MFC. ::)


I agree - good ideas are good to be inspired by, no matter where they came from.
Therapeutic PHP sessions My LinkedIn Profile
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users