You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Robert Germic <ge...@pcv.cz> on 2004/07/12 10:53:18 UTC

mdat/postgres - major perfomance problem?

Hi,

  I've noticed %mdat in Embperl 2 is not per module/page as in Embperl 
1. Now it is per application, right? (Docs still claim otherwise in 
places...)

  I tried to play with it and it seems that Postgres as a store does 
SELECT ... FOR UPDATE, which is of course correct from data concistency 
point of view, but it makes other apache childs wait... To me it looks 
like only one Apache child is working and other are just waiting for 
session commit. Simple example:

base.html
----------
[- Execute '*' -]
[+ do { use Data::Dumper; Dumper(\%mdat) } +]

page1
------
[- $mdat{first} = 'gerald'; sleep 15 -]

page 2
-------
[- $mdat{last} = 'gerald'; sleep 1 -]

When I load page 1 and then immediately page 2, it takes 15 seconds and 
then they both load at the same time eg. page2 was blocked by page 1.

Per application data is hugely useful, but if all of above is true, 
%mdat is major performance bottleneck. What do you think?

- Robert


PS. When I try to empty %mdat, maybe in base.html like
[-
    delete $mdat{$_} foreach keys %mdat;
    %mdat = ();
-]
it dumps empty session as expected. Now I comment out those delete lines 
and reload and %mdat has the same content as before delete. What's wrong?





---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: mdat/postgres - major perfomance problem?

Posted by RobertCZ <ro...@robert.cz>.
RobertCZ wrote:

> Angus Lees wrote:
>
>>At Tue, 13 Jul 2004 12:30:42 +0200, RobertCZ  wrote:
>>  
>>
>>>Gerald Richter wrote:
>>>    
>>>
>>>>> I tried to play with it and it seems that Postgres as a store does
>>>>>SELECT ... FOR UPDATE,
>>>>>
>>>>>To me it looks like only one Apache child is working and other are
>>>>>just waiting for session commit.
>>>>>        
>>>>>
>>>>Mmmh, yes this seems to be the case.
>>>>
>>>>I am not sure how select for update works, for the other storages the
>>>>locking works the way, that multiple pages can read at the same time, only
>>>>if one page writes it tries to get an exclusive lock. So just do write only
>>>>at the end of the page, normaly solves this locking problem.      
>>>>
>>
>>If thats what every other storage does, then every other storage has a
>>pretty severe race condition.  This is fine if the data you are
>>storing in %mdat (or %udat) isn't dependent on what the previous %mdat
>>data was (eg: storing "the last visitor" is ok, a page counter is not ok).
>>
>>The problem is that perl cannot know when fetching the session data,
>>whether you are intending to modify it or not.  It seems the Postgres
>>backend is taking the conservative approach, unlike every other
>>Apache::Session store (and against the behaviour described in the
>>Apache::Session docs).
>>    
>>

BTW, here's interesting comment from Phalanx project

http://cpanratings.perl.org/d/Apache-Session

- Robert

Re: mdat/postgres - major perfomance problem?

Posted by RobertCZ <ro...@robert.cz>.
Angus Lees wrote:

>At Tue, 13 Jul 2004 12:30:42 +0200, RobertCZ  wrote:
>  
>
>>Gerald Richter wrote:
>>    
>>
>>>> I tried to play with it and it seems that Postgres as a store does
>>>>SELECT ... FOR UPDATE,
>>>>
>>>>To me it looks like only one Apache child is working and other are
>>>>just waiting for session commit.
>>>>        
>>>>
>>>Mmmh, yes this seems to be the case.
>>>
>>>I am not sure how select for update works, for the other storages the
>>>locking works the way, that multiple pages can read at the same time, only
>>>if one page writes it tries to get an exclusive lock. So just do write only
>>>at the end of the page, normaly solves this locking problem.
>>>      
>>>
>
>If thats what every other storage does, then every other storage has a
>pretty severe race condition.  This is fine if the data you are
>storing in %mdat (or %udat) isn't dependent on what the previous %mdat
>data was (eg: storing "the last visitor" is ok, a page counter is not ok).
>
>The problem is that perl cannot know when fetching the session data,
>whether you are intending to modify it or not.  It seems the Postgres
>backend is taking the conservative approach, unlike every other
>Apache::Session store (and against the behaviour described in the
>Apache::Session docs).
>  
>

Well, Apache::Session docs say:

> If you retrieve an existing session, Session immediately restores the 
> object from storage [....] also obtains an non-exclusive lock on the 
> session.

...

> By default, most Apache::Session implementations only do locking to 
> prevent data corruption. The locking scheme does not provide 
> transactional consistency, such as you might get from a relational 
> database. If you desire transactional consistency, you must provide 
> the Transaction argument with a true value when you tie the session 
> hash. For example:
>
> tie %s, 'Apache::Session::File', $id {
>    Directory     => '/tmp/sessions',
>    LockDirectory => '/var/lock/sessions',
>    Transaction   => 1
> };
>
> Note that the Transaction argument has no practical effect on the 
> MySQL and Postgres implementations. The MySQL implementation only 
> supports exclusive locking, and the Postgres implementation uses the 
> transaction features of that database.
>

I believe that using simple read locks is plain stupid for %mdat because 
it works in most simple cases only, eg your 'last-visitor' example. Even 
in %udat you can get quite easy two childs writing to the same session, 
it is enough if user just clicks faster then backend processes requests 
(think of page with frames or long processing or SOAP etc). And when 
some updates are lost and the last child wins, your application is f* up.

I propose

- use write lock for %udat and %mdat as DEFAULT ( Transaction => 1 ) and 
saying that very loudly in the docs
- reverse %mdat back to the per-module behaviour or remove it completely
- maybe add %adat for application data that would be just a simple alias 
to some per-app global hash (and warn %adat will not survive apache 
restart, unlike udat/mdat)

Gerald?


>>>>PS. When I try to empty %mdat, maybe in base.html like
>>>>[-
>>>>   delete $mdat{$_} foreach keys %mdat;
>>>>   %mdat = ();
>>>>-]
>>>>it dumps empty session as expected. Now I comment out those delete
>>>>lines and reload and %mdat has the same content as before delete.
>>>>What's wrong?
>>>>        
>>>>
>
>Deleting all keys like this also deletes _session_id.  Its very silly,
>but it seems the only place Apache::Session stores the session_id is
>actually in $self->{data}->{_session_id}.
>
>In lieu of anyone actually fixing this Apache::Session stupidity - you
>will have to do something like this instead:
>  [-  delete $mdat{$_} foreach grep !/^_/, keys %mdat  -]
>  
>

Ups. I should have thought of this... :-( Thanks, Angus.



Re: mdat/postgres - major perfomance problem?

Posted by Angus Lees <gu...@inodes.org>.
At Tue, 13 Jul 2004 12:30:42 +0200, RobertCZ  wrote:
> Gerald Richter wrote:
> >>  I tried to play with it and it seems that Postgres as a store does
> >>SELECT ... FOR UPDATE,
> >>
> >>To me it looks like only one Apache child is working and other are
> >>just waiting for session commit.
> >
> >Mmmh, yes this seems to be the case.
> >
> >I am not sure how select for update works, for the other storages the
> >locking works the way, that multiple pages can read at the same time, only
> >if one page writes it tries to get an exclusive lock. So just do write only
> >at the end of the page, normaly solves this locking problem.

If thats what every other storage does, then every other storage has a
pretty severe race condition.  This is fine if the data you are
storing in %mdat (or %udat) isn't dependent on what the previous %mdat
data was (eg: storing "the last visitor" is ok, a page counter is not ok).

The problem is that perl cannot know when fetching the session data,
whether you are intending to modify it or not.  It seems the Postgres
backend is taking the conservative approach, unlike every other
Apache::Session store (and against the behaviour described in the
Apache::Session docs).

Removing the "FOR UPDATE" phrase from Apache/Session/Store/Postgres.pm
should make Postgres behave in the same way as other stores - with the
same race.  Btw I notice that the Oracle store also uses FOR UPDATE.

(SELECT ... FOR UPDATE takes a write lock on the selected rows until
the end of the current transaction)

> [...] postgres indeed does wait for first select for update to
> finnish - however only if it's in explicit transaction so one has to
> call BEGIN before and be in the AutoCommit => 0 mode

Yes, AutoCommit => 1 makes "the end of the current transaction" occur
immediately, dropping the FOR UPDATE locks after the SELECT is completed.

> I'd love to use %mdat for all kind of neat trick namely for inter-user 
> sql query caching, inter-user application locking (so 2 users don't edit 
> the same record on the same web form etc), maybe track currectly 
> connected users etcetcetc but this locking thing has to be fixed

For some of these cases (particularly caching), I think %mdat is the
wrong place to store them.  IMO, you're better off simply storing them
as perl "globals" in a perl namespace of your choosing - no locking,
no contention, no delay in retrieval.  For things like prepared DBI
statement handles, these can't be stored outside the perl process (in
%mdat) anyway.

> I guess only real way to fix this problem is patch Apache::Session and 
> Embperl to support read-only sessions, but that would be quit a lot of 
> patching.

Perhaps we could just reduce the impact by having a "per file data"
hash again?  Then contention would only occur if multiple requests
used the same embperl file, rather than every request for the same
application.

> >>PS. When I try to empty %mdat, maybe in base.html like
> >>[-
> >>    delete $mdat{$_} foreach keys %mdat;
> >>    %mdat = ();
> >>-]
> >>it dumps empty session as expected. Now I comment out those delete
> >>lines and reload and %mdat has the same content as before delete.
> >>What's wrong?

Deleting all keys like this also deletes _session_id.  Its very silly,
but it seems the only place Apache::Session stores the session_id is
actually in $self->{data}->{_session_id}.

In lieu of anyone actually fixing this Apache::Session stupidity - you
will have to do something like this instead:
  [-  delete $mdat{$_} foreach grep !/^_/, keys %mdat  -]

-- 
 - Gus

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: Is there a point in using the request object for db connections when using Apache::DBI?

Posted by "Luiz Fernando B. Ribeiro" <lu...@engenhosolucoes.com.br>.
On Tue, 13 Jul 2004 15:07:30 -0700, John P. Gibbons <jo...@dashing.com>
wrote:

> I was reading this article:  
> http://www.perl.com/pub/a/2001/03/embperl.html and of course this  
> mailing list and have seen reference to people using the $req object to  
> have one database connection.
>
> On my sites I did not do it that way. I use Apache::DBI to handle the  
> DBI connection issue and do a separate connects  in my base.html and in  
> all other referenced Embperl files/code. Apache::DBI of course makes  
> sure we really only have one connection per httpd, as you all know.
>
> I was wondering if there is any benefit or reason to use the request  
> object method in my scenario? I'm looking at ways to optimize my code as  
> it has grown dramatically and this has been in the back of my mind for  
> awhile. I remember reading something that in order to give db access to  
> an include embperl file with only subroutines you'd have to use that  
> method. But I can't recall now. I was also thinking that there are cases  
> when I call .epl files directly and do not want them wrapped in  
> base.html. If I were to use the request object method then I would  
> either be out of luck or would have to code around it somehow.

The request object can be useful to share variables across pages during
_that_ request.

For big projects the recommended solution is to abstract the logic and
database operations into a module and let Embperl handle the interface
generation.

If you use EmbperlObject you can instantiate your class in your template
file, usually base.epr, and share the object across the other
pages/components _in that request_ using the request object. I think this
is a better approach than to use global class variables.

Unless you known what you are doing do not use your own namespace across
pages to be able to share variables. The same applies to CLEANUP. Embperl
handles the namespace and encapsulates each page to avoid leaks and
collisions.

I recommend you to read about EmbperlObject since it has many features to
improve your application design.

-- 
Luiz Fernando B. Ribeiro
Engenho Soluções para a Internet
+55 11 4485-0136

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: Is there a point in using the request object for db connections when using Apache::DBI?

Posted by Gerald Richter <ri...@ecos.de>.
Hi,

>
> What's more, is this the appropriate place for these sorts of
> questions?

I think the list is an appropriate place for such a discussion.

Even better would be if the results of such a discussion, becomes a HOWTO or
something similar.

The Embperl web site has the possibility to add things like this and in case
it does not fit, we can enhance the site (there is nothing that could not
made better). I would prefer to have the Embperl website as the central
place for all Embperl knowledge and do discussions on this list.

Gerald


---------------------------------------------------------------------------
Gerald Richter            ecos electronic communication services gmbh
IT-Securitylösungen * Webapplikationen mit Apache/Perl/mod_perl/Embperl

Post:       Tulpenstrasse 5          D-55276 Dienheim b. Mainz
E-Mail:     richter@ecos.de          Voice:   +49 6133 939-122
WWW:        http://www.ecos.de/      Fax:     +49 6133 939-333
---------------------------------------------------------------------------
ECOS BB-5000 Firewall- und IT-Security Appliance: www.bb-5000.info
---------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Is there a point in using the request object for db connections when using Apache::DBI?

Posted by "John P. Gibbons" <jo...@dashing.com>.
I was reading this article: 
http://www.perl.com/pub/a/2001/03/embperl.html and of course this 
mailing list and have seen reference to people using the $req object to 
have one database connection.

On my sites I did not do it that way. I use Apache::DBI to handle the 
DBI connection issue and do a separate connects  in my base.html and in 
all other referenced Embperl files/code. Apache::DBI of course makes 
sure we really only have one connection per httpd, as you all know.

I was wondering if there is any benefit or reason to use the request 
object method in my scenario? I'm looking at ways to optimize my code as 
it has grown dramatically and this has been in the back of my mind for 
awhile. I remember reading something that in order to give db access to 
an include embperl file with only subroutines you'd have to use that 
method. But I can't recall now. I was also thinking that there are cases 
when I call .epl files directly and do not want them wrapped in 
base.html. If I were to use the request object method then I would 
either be out of luck or would have to code around it somehow.

I think I've made a lot of mistakes in the techniques I've used to 
develop my core site software and I'd like to correct them.

Does anyone have any thoughts on this?

What's more, is this the appropriate place for these sorts of questions? 
I have an interest in discussing Embperl with other developers who are 
actually making websites and learning about how they did things, etc. 
All I usually see on the list are tech support related discussions. If 
this is the wrong place for that, is there another list or site that 
might be better? If not would it be at all interesting if someone 
started one? I'd be open minded to that as a side project. I already 
have a complete content management system and community (with forums, 
user profiles, etc) all written in Embperl. So I could do it in short order.

Cheers,
John


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: mdat/postgres - major perfomance problem?

Posted by Gerald Richter <ri...@ecos.de>.
>>
>> Sorry for binary attachement - screenshot shows postgres indeed does
>> wait for first select for update to finnish - however only if it's in
>> explicit transaction so one has to call BEGIN before and be in the
>> AutoCommit => 0 mode
>>
>> The docs  for Apache::Session::Postgres say you have to manually
>> specify Commit => X argument to the tie %hash,
>> 'Apache::Session::Postgres', $id, ... call to make clear what the
>> commit policy is, but right now I can't figure out how Embperl 2
>> does it
>>

You can pass parameters via

Embperl_Session_Args

see Config.pod



>> If called with Commit => 1 (which means AutoCommit => 1 I hope),
>> postgres DOES NOT wait for first SELECT FOR UPDATE but then I don't
>> see
>> how consistency is to be maintened
>>

That will be the problem....

>> I'd love to use %mdat for all kind of neat trick namely for
>> inter-user
>> sql query caching, inter-user application locking (so 2 users don't
>> edit the same record on the same web form etc), maybe track currectly
>> connected users etcetcetc but this locking thing has to be fixed
>>

As far as I see not much people using %mdat. It seem like we need a
different locking strategie...

>> Or maybe some other backend is better? I can't believe file or dbm
>> will
>> be any better and mysql is probably the same thing
>>

I recently discovered Cache::FastMMap, this might be an alterntive, also it
lacks synchronisation, which might be a problem for some applications.


>> I guess only real way to fix this problem is patch Apache::Session
>> and Embperl to support read-only sessions, but that would be quit a
>> lot of patching. Or maybe just passing from Embperl page Commit => X
>> to Apache::Session would do the trick, I'm not sure - but that
>> should be
>> much easier to do, maybe just adding one method to the $req object?
>>

see above

>>
>> I don't understand... Apache::Session::Store::DBI has only
>> $self->update which does the obvious SQL UPDATE,
>> Apache::Session::Store::Postgres inherits it without change (it
>> modifies only connect and materialize methods), so they both use
>> plain Apache::Session method clear which is
>> in turn one line $self->{data} = {};
>>
>> My guess %mdat = () should just work...
>>

Yes, you are right. I can't say what wrong here at the moment

Gerald


>> - Robert
>>
>>
>>
>>
>
>
>
>
>
>
>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
>> For additional commands, e-mail: embperl-help@perl.apache.org

---------------------------------------------------------------------------
Gerald Richter            ecos electronic communication services gmbh
IT-Securitylösungen * Webapplikationen mit Apache/Perl/mod_perl/Embperl

Post:       Tulpenstrasse 5          D-55276 Dienheim b. Mainz
E-Mail:     richter@ecos.de          Voice:   +49 6133 939-122
WWW:        http://www.ecos.de/      Fax:     +49 6133 939-333
---------------------------------------------------------------------------
ECOS BB-5000 Firewall- und IT-Security Appliance: www.bb-5000.info
---------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: mdat/postgres - major perfomance problem?

Posted by RobertCZ <ro...@robert.cz>.
Gerald Richter wrote:

>>(Docs still claim otherwise in places...)
>>    
>>
>
>this should be fixed...
>  
>

most confusing is 
http://perl.apache.org/embperl/pod/doc/Embperl.-page-6-.htm, it looks 
like it's master doc...

>>  I tried to play with it and it seems that Postgres as a store does
>>SELECT ... FOR UPDATE,
>>    
>>
>>To me it looks like only one Apache child is working and other are
>>just waiting for session commit.
>>    
>>
>
>Mmmh, yes this seems to be the case.
>
>I am not sure how select for update works, for the other storages the
>locking works the way, that multiple pages can read at the same time, only
>if one page writes it tries to get an exclusive lock. So just do write only
>at the end of the page, normaly solves this locking problem.
>  
>

Sorry for binary attachement - screenshot shows postgres indeed does 
wait for first select for update to finnish - however only if it's in 
explicit transaction so one has to call BEGIN before and be in the 
AutoCommit => 0 mode

The docs  for Apache::Session::Postgres say you have to manually specify 
Commit => X argument to the tie %hash, 'Apache::Session::Postgres', $id, 
... call to make clear what the commit policy is, but right now I can't 
figure out how Embperl 2 does it

If called with Commit => 1 (which means AutoCommit => 1 I hope), 
postgres DOES NOT wait for first SELECT FOR UPDATE but then I don't see 
how consistency is to be maintened

I'd love to use %mdat for all kind of neat trick namely for inter-user 
sql query caching, inter-user application locking (so 2 users don't edit 
the same record on the same web form etc), maybe track currectly 
connected users etcetcetc but this locking thing has to be fixed

Or maybe some other backend is better? I can't believe file or dbm will 
be any better and mysql is probably the same thing

I guess only real way to fix this problem is patch Apache::Session and 
Embperl to support read-only sessions, but that would be quit a lot of 
patching. Or maybe just passing from Embperl page Commit => X to 
Apache::Session would do the trick, I'm not sure - but that should be 
much easier to do, maybe just adding one method to the $req object?

>>PS. When I try to empty %mdat, maybe in base.html like
>>[-
>>    delete $mdat{$_} foreach keys %mdat;
>>    %mdat = ();
>>-]
>>it dumps empty session as expected. Now I comment out those delete
>>lines and reload and %mdat has the same content as before delete.
>>What's wrong?
>>    
>>
>
>Looks like Apache::Session (or the PG store) does not implement the CLEAR
>method
>  
>

I don't understand... Apache::Session::Store::DBI has only $self->update 
which does the obvious SQL UPDATE, Apache::Session::Store::Postgres 
inherits it without change (it modifies only connect and materialize 
methods), so they both use plain Apache::Session method clear which is 
in turn one line $self->{data} = {};

My guess %mdat = () should just work...

- Robert




Re: mdat/postgres - major perfomance problem?

Posted by Gerald Richter <ri...@ecos.de>.
Hi,

>
>   I've noticed %mdat in Embperl 2 is not per module/page as in Embperl
> 1. Now it is per application, right?

Yes

>(Docs still claim otherwise in
> places...)
>

this should be fixed...

>   I tried to play with it and it seems that Postgres as a store does
> SELECT ... FOR UPDATE,

yes

> To me it looks like only one Apache child is working and other are
> just waiting for session commit.

Mmmh, yes this seems to be the case.

I am not sure how select for update works, for the other storages the
locking works the way, that multiple pages can read at the same time, only
if one page writes it tries to get an exclusive lock. So just do write only
at the end of the page, normaly solves this locking problem.

>
> PS. When I try to empty %mdat, maybe in base.html like
> [-
>     delete $mdat{$_} foreach keys %mdat;
>     %mdat = ();
> -]
> it dumps empty session as expected. Now I comment out those delete
> lines and reload and %mdat has the same content as before delete.
> What's wrong?
>

Looks like Apache::Session (or the PG store) does not implement the CLEAR
method

Gerald



---------------------------------------------------------------------------
Gerald Richter            ecos electronic communication services gmbh
IT-Securitylösungen * Webapplikationen mit Apache/Perl/mod_perl/Embperl

Post:       Tulpenstrasse 5          D-55276 Dienheim b. Mainz
E-Mail:     richter@ecos.de          Voice:   +49 6133 939-122
WWW:        http://www.ecos.de/      Fax:     +49 6133 939-333
---------------------------------------------------------------------------
ECOS BB-5000 Firewall- und IT-Security Appliance: www.bb-5000.info
---------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org