You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Greg Stein <gs...@lyra.org> on 2001/01/22 12:36:45 UTC

the ap_r* thing (was: Re: Apache 2.0 beta STATUS)

On Sun, Jan 21, 2001 at 11:56:55PM -0600, William A. Rowe, Jr. wrote:
> > From: Greg Stein [mailto:gstein@lyra.org]
> > Sent: Sunday, January 21, 2001 11:26 PM
> > 
> > On Sun, Jan 21, 2001 at 09:58:20PM -0600, William A. Rowe, Jr. wrote:
> > > As I say, I'm for the module author controlling the unknowns with as little
> > > possible chance for interference from 'invisible forces'.
> > 
> > The invisible forces in this case is the requirements imposed by forcing a
> > module author and even the core Apache code to have to synchronize between
> > two totally different output mechanisms. Each coder must be aware of all the
> > other bits of code which could potentially produce output.  Even worse, they
> > need to know *how* that other code did it. Not just "did they?" but "how?".
> > That kind of invisible coupling across the code base is a recipe for bugs.
> > Insert a sync call here, insert one there. Oops, we forgot to sync the two
> > mechanism over there, too. Oy! :-)
> 
> How is this possible unless we invoke a subrequest?

You've got a DAV plugin. You are implementing the MERGE method. Within its
response definition is a way to return property information. So your code
calls dav_send_propstat().

Now, without looking, how can you tell what output mechanism was used? Was
it ap_r* or was it ap_pass_brigade()? If it doesn't match what I'm using,
then I'm going to need to do some extra work.

[ the mixing problem only occurs with Ryan's patch, so I need to use it here
  as a demonstration of the mixing problem; not to pick on the patch ]

To clarify:

*) assume it used ap_r*. There will be content sitting in r->bb. I construct
   my brigade for other pieces of the MERGE response and call
   ap_pass_brigade(). I've now misordered the response because my brigade
   went before the stuff sitting in r->bb.

*) let's say that I send some data with ap_r* and then call the DAV utility
   function. assume it uses ap_pass_brigade(). It constructs the brigade and
   passes it. Again: a misorder occurs because the brigade from the utility
   went out before the r->bb brigade.

An answer is to tell everybody "you must use r->bb, and never your own
brigade." That is taking away choicse, rather than providing them. Also
recognize that anybody using r->bb must first create it, if it doesn't
exist. Just a little bit more pain.

Oh, and if people are supposed to use r->bb, then they better make sure to
call apr_brigade_flush() on it (watch out: only if it has been created!) to
make sure that if somebody *has* put data in there, that it gets
synchronized properly.


By using a filter in my patch, I've trapped all output to the network.
Nothing can go without passing through that filter. Therefore, I have a
perfect choke point to ensure that I can order all the output properly.

>...
> I'll think on this another night.  And [sorry I already clipped the text],
> on the comment of 'fixing apache' v.s. 'fixing apr', I'm very strongly
> against leaving useful mechanisms in apache when an equivilant mechanism
> exists to migrate them cleanly into apr.  If it were a case of "here's the
> Apache patch, and we'll finish moving it to apr in three weeks", I'd say
> great!  Let's roll the beta and move it afterwards!  But we aren't - again,
> I'm seeing two methods that would both solve our problem, with different
> requirements on the authors.

My patch has *zero* requirements on module authors. Use whatever API you
have been using or want to use. It doesn't matter, and you don't ever have
to worry about what somebody else is using. There is no possibility for
synchronization problems.

I'm all for creating an APR solution, but the number one priority is Apache.
If our solution also happens to work for APR, then great. But I don't
believe that we can necessarily say "this works for APR" and then wedge it
into Apache and make module authors need to be aware of the various output
mechanisms and compensate for potential ordering problems.

> Please, Greg and Ryan, document -simply- where your patch stands up and
> the other falls down, so I can grok this quickly without wadeing chin
> deep in the filtering mechanics!

All right. Here are a few cases:

*) ordering skew. see above.

*) ap_rwrite(my_buffer, 100000, r)

   gstein: flush the existing buffer (in the old_write filter), wrap a
           transient bucket around my_buffer, and deliver the brigade

   rbb: crash due to a memory overwrite in apr_brigade_write() -- it would
        copy the 100k my_buffer into a 9k malloc buffer.
	
	[ assuming we fix that bug... ]

	apr_brigade_write() has nowhere to send the data, so it allocates
	and copies the 100k into a new HEAP bucket, appends it to the
	brigade (r->bb), and then returns.
	
	eek! 100k memory copy!
	
	[ assuming that we patch up ap_rwrite to deal with "big" strings ]
	
	okay, now ap_rwrite() appends a 100k transient bucket to r->bb and
        passes that along.
	
	[ okay, now we have to fix this for ap_rputs and ap_rvputs, too.
	  darn. those are Apache specific now, huh? ]

*) ap_rprintf(r, "<D:compare-report>%s</D:compare-report>", report)
   report == 200k string

   [ assume both patches use apr_vformatter to break their 4k limit on
     ap_[v]rprintf ]

   gstein: the filter's buffer is used as the formatting buffer, so the
	   formatting begins after any existing buffered output.
	   apr_vformatter calls the flush_func after it fills the formatting
	   buffer. this flush_func is simply flush_buffer() in my patch. if
	   apr_vformatter exits without calling flush_func, then we have new
	   content sitting in the filter's output buffer.

   rbb: the flush_func is called and a new bucket is created from the
	formatting buffer and inserted into the brigade. There is no way to
	send this data to the network from within apr_brigade_vprintf().

	[ okay... restructure the assumption: apr_brigade_vprintf is torched
	  because it can hose the working set. move the vformatter call up
	  to ap_vrprintf. ]

        ap_vrprintf now uses a flush_func that can go to the network. but
	wait, we have some code in there to say "oh, but this is too small,
	so let's put this part into the brigade." The patch now has a dual
	path to decide where to send the data: in a brigade and down the
	network, or a call to apr_brigade_write() (which makes another copy)
	
	[ note: the above is now Apache specific ]



What I'd really like to see is a variation of Ryan's patch that uses a
bucket at the tail which is over-allocated so that more data can be
appended. That would allow bucket/brigade users to "write a bunch of little
items", yet still work with buckets. This would also be helpful for other
APR users. However, it doesn't really solve the ap_r* problem like my patch
does (and vice versa: I don't solve the "little bitty bucket output"
problem, nor the other-APR-user problem).

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/

Re: the ap_r* thing (was: Re: Apache 2.0 beta STATUS)

Posted by Greg Marr <gr...@alum.wpi.edu>.
At 02:17 PM 01/22/2001, Greg Stein wrote:
>On Mon, Jan 22, 2001 at 02:10:44PM -0500, Greg Marr wrote:
> > At 01:51 PM 01/22/2001, Greg Stein wrote:
> > >On Mon, Jan 22, 2001 at 01:16:23PM -0500, Greg Marr wrote:
> > > > At 06:36 AM 01/22/2001, Greg Stein wrote:
> > > >...
> > > > >An answer is to tell everybody "you must use r->bb, and 
> never your
> > > > >own brigade."
> > > >
> > > > A better answer is to tell people to ap_rflush() before calling
> > > > functions they don't control that may generate output, and to 
> call
> > > > ap_rflush after generating output using ap_r* if the function 
> may be
> > > > called by functions they don't control.
> > >
> > >This would be inadvisable.  ap_rflush() will deliver to the
> > >network.  The hope is to synchronize the ordering, rather than
> > >generate network packets :-)
> >
> > Okay, so I guess that apr_brigade_flush() would be a better call 
> to
> > use, but that does bring up the question I had in my other 
> message.  :)
>
>Which question? It's hard to keep track, and I read all this stuff. 
>Our audience viewers could be even more confused :-)

Sorry, whether to use ap_rflush or apr_brigade_flush() in any given 
circumstance.

-- 
Greg Marr
gregm@alum.wpi.edu
"We thought you were dead."
"I was, but I'm better now." - Sheridan, "The Summoning"


Re: the ap_r* thing (was: Re: Apache 2.0 beta STATUS)

Posted by Greg Stein <gs...@lyra.org>.
On Mon, Jan 22, 2001 at 02:10:44PM -0500, Greg Marr wrote:
> At 01:51 PM 01/22/2001, Greg Stein wrote:
> >On Mon, Jan 22, 2001 at 01:16:23PM -0500, Greg Marr wrote:
> > > At 06:36 AM 01/22/2001, Greg Stein wrote:
> > >...
> > > >An answer is to tell everybody "you must use r->bb, and never 
> > your
> > > >own brigade."
> > >
> > > A better answer is to tell people to ap_rflush() before calling
> > > functions they don't control that may generate output, and to call
> > > ap_rflush after generating output using ap_r* if the function may 
> > be
> > > called by functions they don't control.
> >
> >This would be inadvisable.  ap_rflush() will deliver to the 
> >network.  The hope is to synchronize the ordering, rather than 
> >generate network packets :-)
> 
> Okay, so I guess that apr_brigade_flush() would be a better call to 
> use, but that does bring up the question I had in my other message.  :)

Which question? It's hard to keep track, and I read all this stuff. Our
audience viewers could be even more confused :-)

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/

Re: the ap_r* thing (was: Re: Apache 2.0 beta STATUS)

Posted by Greg Marr <gr...@alum.wpi.edu>.
At 01:51 PM 01/22/2001, Greg Stein wrote:
>On Mon, Jan 22, 2001 at 01:16:23PM -0500, Greg Marr wrote:
> > At 06:36 AM 01/22/2001, Greg Stein wrote:
> >...
> > >An answer is to tell everybody "you must use r->bb, and never 
> your
> > >own brigade."
> >
> > A better answer is to tell people to ap_rflush() before calling
> > functions they don't control that may generate output, and to call
> > ap_rflush after generating output using ap_r* if the function may 
> be
> > called by functions they don't control.
>
>This would be inadvisable.  ap_rflush() will deliver to the 
>network.  The hope is to synchronize the ordering, rather than 
>generate network packets :-)

Okay, so I guess that apr_brigade_flush() would be a better call to 
use, but that does bring up the question I had in my other message.  :)

The rest of your message did help clear a few other things up, thanks.

-- 
Greg Marr
gregm@alum.wpi.edu
"We thought you were dead."
"I was, but I'm better now." - Sheridan, "The Summoning"


Re: the ap_r* thing (was: Re: Apache 2.0 beta STATUS)

Posted by Greg Stein <gs...@lyra.org>.
On Mon, Jan 22, 2001 at 01:16:23PM -0500, Greg Marr wrote:
> At 06:36 AM 01/22/2001, Greg Stein wrote:
>...
> >An answer is to tell everybody "you must use r->bb, and never your 
> >own brigade."
> 
> A better answer is to tell people to ap_rflush() before calling 
> functions they don't control that may generate output, and to call 
> ap_rflush after generating output using ap_r* if the function may be 
> called by functions they don't control.

This would be inadvisable. ap_rflush() will deliver to the network. The hope
is to synchronize the ordering, rather than generate network packets :-)

> >By using a filter in my patch, I've trapped all output to the 
> >network.  Nothing can go without passing through that 
> >filter.  Therefore, I have a perfect choke point to ensure that I 
> >can order all the output properly.
> 
> Actually, all the output better be ordered properly before it gets to 
> your filter, since your filter can be pushed down the stack by 
> another filter inserting itself higher up.

Apache output is ordered by its entry to the output filter chain. There is
no other definition. It is that chain that transmits it onto the network, so
it must be correct on entry.

[ there may be no filters other than the core_output_filter ]

So, yes: your statement is correct. But it isn't anything new. :-)

> Is there ever the possibility of a filter being inserted into the 
> stack after the first ap_r* call?  If so, and that filter gets placed 
> before your filter, won't that totally hose any data currently in the 
> output filter's buffer?

That is a similar situation to the case where the ap_r* went to the network
before the new filter was inserted.

Basically: dynamic insertion of filters, while content is being generated,
is always a tricky business. The general rule of thumb is that a filter can
only insert another filter *after* itself. But even then, we make no
guarantees about whether that new filter has seen/processes all of the
content.

So, again: nothing new here. :-)

> >My patch has *zero* requirements on module authors.  Use whatever 
> >API you have been using or want to use.  It doesn't matter, and you 
> >don't ever have to worry about what somebody else is using.  There 
> >is no possibility for synchronization problems.
> 
> That's true, but it also has the possibility that the entire thing 
> can be turned off by a filter being inserted above it in the stack, 
> in which case Apache's back at its current performance.

Correct. We can always come up with a pathological case :-). Until somebody
goes and constructs a filter that specifically is intended to precede the
OLD_WRITE filter, then we're going to see all of the performance gains.
Someone has to specifically break it. If you truly believe this is a
possibility, then we can make an allowance within the AP_FTYPE constants for
more insurance. (personally, I don't see the situation arising, but am happy
to buy more insurance :-)

> >I'm all for creating an APR solution, but the number one priority is 
> >Apache.  If our solution also happens to work for APR, then 
> >great.  But I don't believe that we can necessarily say "this works 
> >for APR" and then wedge it into Apache and make module authors need 
> >to be aware of the various output mechanisms and compensate for 
> >potential ordering problems.
> 
> It removes any possibility of output ordering problems, but also adds 
> a possibility of a filter ordering problem rendering it ineffective.

You need to work at it for that to happen :-). There are no filters that are
defined to precende AP_FTYPE_CONTENT. Again, we can make an allowance if
this is deemed a *true* risk. I'm +0 for an AP_FTYPE change to clarify.

> >*) ap_rwrite(my_buffer, 100000, r)
> 
> That one's pretty nasty.

This is what mod_perl, mod_python, etc have done to this point. Content
generators that have large content have been using ap_rwrite. We can fix
those, but the point is to keep the ap_r* APIs working so that we don't
create too high of a bar for module authors.

> >*) ap_rprintf(r, "<D:compare-report>%s</D:compare-report>", report)
> >    report == 200k string
> 
> As is this one.

hehe... Note that the above construct actually works very well in 1.3. As
the vprintf() is performed, it is flushing directly to the network. The
working set characteristics are great (a fixed-size formatting buffer).
Point being that (since it has nice perf characteristics) it is out there in
module code today.

My patch already handles the first, and can have a fixed-size working set
for the second. (both Ryan and I will need to do the vformatter stuff to
properly deal with ap_[v]rprintf; we haven't bothered with our patches since
that is a straight-forward post-patch)

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/

Re: the ap_r* thing (was: Re: Apache 2.0 beta STATUS)

Posted by Greg Marr <gr...@alum.wpi.edu>.
At 06:36 AM 01/22/2001, Greg Stein wrote:
>*) assume it used ap_r*. There will be content sitting in r->bb. I 
>construct
>    my brigade for other pieces of the MERGE response and call
>    ap_pass_brigade(). I've now misordered the response because my 
> brigade
>    went before the stuff sitting in r->bb.
>
>*) let's say that I send some data with ap_r* and then call the DAV 
>utility
>    function. assume it uses ap_pass_brigade(). It constructs the 
> brigade and
>    passes it. Again: a misorder occurs because the brigade from the 
> utility
>    went out before the r->bb brigade.
>
>An answer is to tell everybody "you must use r->bb, and never your 
>own brigade."

A better answer is to tell people to ap_rflush() before calling 
functions they don't control that may generate output, and to call 
ap_rflush after generating output using ap_r* if the function may be 
called by functions they don't control.

>By using a filter in my patch, I've trapped all output to the 
>network.  Nothing can go without passing through that 
>filter.  Therefore, I have a perfect choke point to ensure that I 
>can order all the output properly.

Actually, all the output better be ordered properly before it gets to 
your filter, since your filter can be pushed down the stack by 
another filter inserting itself higher up.

Is there ever the possibility of a filter being inserted into the 
stack after the first ap_r* call?  If so, and that filter gets placed 
before your filter, won't that totally hose any data currently in the 
output filter's buffer?

>My patch has *zero* requirements on module authors.  Use whatever 
>API you have been using or want to use.  It doesn't matter, and you 
>don't ever have to worry about what somebody else is using.  There 
>is no possibility for synchronization problems.

That's true, but it also has the possibility that the entire thing 
can be turned off by a filter being inserted above it in the stack, 
in which case Apache's back at its current performance.

>I'm all for creating an APR solution, but the number one priority is 
>Apache.  If our solution also happens to work for APR, then 
>great.  But I don't believe that we can necessarily say "this works 
>for APR" and then wedge it into Apache and make module authors need 
>to be aware of the various output mechanisms and compensate for 
>potential ordering problems.

It removes any possibility of output ordering problems, but also adds 
a possibility of a filter ordering problem rendering it ineffective.

>*) ap_rwrite(my_buffer, 100000, r)

That one's pretty nasty.

>*) ap_rprintf(r, "<D:compare-report>%s</D:compare-report>", report)
>    report == 200k string

As is this one.

-- 
Greg Marr
gregm@alum.wpi.edu
"We thought you were dead."
"I was, but I'm better now." - Sheridan, "The Summoning"