You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@trafficserver.apache.org by Kevin Giles <kg...@gmx.com> on 2011/05/04 12:38:59 UTC

a simple plugin that writes to the cache

Hi,

 I have written a plug to write an entry to the cache on the HTTP_SEND_RESPONSE_HDR_HOOK.

 It appears to work fine, the plugin is receiving the correct events, but when I inspect the cache using the cache regex lookup my entry does not appear? 

 It is just a simple test to write an entry to the cache, it does not have to be on the HTTP_SEND_RESPONSE_HDR_HOOK event.

 I am hoping it is something simple, would you mind giving my code a quick look as I figure out what step I am missing.

 20110504.10h19m15s TS_EVENT_HTTP_TXN_START

 20110504.10h19m15s TS_HTTP_SEND_RESPONSE_HDR_HOOK
 20110504.10h19m15s Cache Write Start
 20110504.10h19m15s TS_EVENT_CACHE_OPEN_WRITE
 20110504.10h19m15s TS_EVENT_VCONN_WRITE_COMPLETE
 20110504.10h19m15s TS_EVENT_HTTP_TXN_CLOSE

 Regards,

 Kevin.

#include<stdio.h>#include<string.h>#include<limits.h>#include<stdlib.h>#include<ts/ts.h>#include<ts/experimental.h>#definePLUGIN_NAME"my-plugin"staticTSContglobal_contp;staticTSTextLogObjectlog;intcheck_ts_version();staticinthttp_event_handler(TSContcontp,TSEventevent,void*edata);staticintcache_write_complete=0;typedefstruct_MyDS{TSHttpTxntxnp;TSCacheKeykey;char*url;TSActionpendingAction;TSVConnvconn;TSVIOvio;TSIOBufferbufp;TSIOBufferReaderbufp_reader;}MyDS;/***ThismethodiscalledwhenwereceivetheTS_EVENT_HTTP_SEND_RESPONSE_HDRevent.*Itisasimpletesttowriteanentrytothecachebeforelettingtheoriginal*requestresume.*/staticint startCacheWrite(TSContcontp){if(cache_write_complete==1){if(log){TSTextLogObjectWrite(log,(char*)"CacheWriteAlreadyPreformed");}returnTS_ERROR;}cache_write_complete=1;if(log){TSTextLogObjectWrite(log,(char*)"CacheWriteStart");}constchar*my_url="http://trafficserver.apache.org/";MyDS*mds=(MyDS*)TSContDataGet(contp);mds->key=TSCacheKeyCreate();if(TSCacheKeyDiges
 tSet(mds->key,my_url,strlen(my_url))!=TS_SUCCESS){if(log){TSTextLogObjectWrite(log,(char*)"TSCacheKeyDigestSetFailed");}returnTS_ERROR;}intis_ready=0;TSCacheReady(&is_ready);if(is_ready==0){if(log){TSTextLogObjectWrite(log,(char*)"Cacheisnotready");}returnTS_ERROR;}TSCacheWrite(contp,mds->key);returnTS_SUCCESS;}staticvoid handle_txn_start(TSContcontp,TSHttpTxntxnp){TSConttxn_contp;MyDS*mds;txn_contp=TSContCreate((TSEventFunc)http_event_handler,TSMutexCreate());mds=(MyDS*)TSmalloc(sizeof(MyDS));TSContDataSet(txn_contp,mds);mds->txnp=txnp;mds->url=NULL;mds->key=NULL;mds->pendingAction=NULL;mds->bufp=NULL;mds->bufp_reader=NULL;mds->vio=NULL;TSHttpTxnHookAdd(txnp,TS_HTTP_SEND_RESPONSE_HDR_HOOK,txn_contp);TSHttpTxnHookAdd(txnp,TS_HTTP_TXN_CLOSE_HOOK,txn_contp);TSHttpTxnReenable(txnp,TS_EVENT_HTTP_CONTINUE);}staticvoid destroy_continuation(TSHttpTxntxnp,TSContcontp){MyDS*mds=NULL;mds=(MyDS*)TSContDataGet(contp);if(mds!=NULL){if(mds->url){TSfree(mds->url);}if(mds->key){TSCacheKeyDe
 stroy(mds->key);}if(mds->bufp){TSIOBufferDestroy(mds->bufp);}TSfree(mds);}TSContDestroy(contp);TSHttpTxnReenable(txnp,TS_EVENT_HTTP_CONTINUE);return;}//----------------------------------------------------------------------------//handlerforhttptxneventsstaticint http_event_handler(TSContcontp,TSEventevent,void*edata){intstatus=TS_SUCCESS;inturl_length=0;TSHttpTxntxnp=(TSHttpTxn)edata;MyDS*mds=0;switch(event){caseTS_EVENT_HTTP_TXN_START:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_HTTP_TXN_START");}txnp=(TSHttpTxn)edata;handle_txn_start(contp,txnp);return0;caseTS_EVENT_HTTP_READ_REQUEST_HDR:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_HTTP_READ_REQUEST_HDR");}if(contp!=global_contp){mds=(MyDS*)TSContDataGet(contp);mds->url=TSHttpTxnEffectiveUrlStringGet(txnp,&url_length);if(!mds->url){TSError("[%s]couldn'tretrieverequesturl\n",PLUGIN_NAME);status=TS_ERROR;}if(status==TS_SUCCESS){if(log){TSTextLogObjectWrite(log,(char*)"Requestfor%sreceived",mds->url);}}}TSHttpTxnRee
 nable(txnp,TS_EVENT_HTTP_CONTINUE);break;caseTS_EVENT_HTTP_SEND_RESPONSE_HDR:if(log){TSTextLogObjectWrite(log,(char*)"TS_HTTP_SEND_RESPONSE_HDR_HOOK");}if(contp!=global_contp){if(startCacheWrite(contp)==TS_ERROR){TSHttpTxnReenable(txnp,TS_EVENT_HTTP_CONTINUE);}}else{TSHttpTxnReenable(txnp,TS_EVENT_HTTP_CONTINUE);}break;caseTS_EVENT_HTTP_TXN_CLOSE:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_HTTP_TXN_CLOSE");}txnp=(TSHttpTxn)edata;if(contp!=global_contp){destroy_continuation(txnp,contp);}break;caseTS_EVENT_CACHE_OPEN_WRITE:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_CACHE_OPEN_WRITE");}if(contp!=global_contp){mds=(MyDS*)TSContDataGet(contp);mds->vconn=(TSVConn)edata;constchar*my_url="http://trafficserver.apache.org/";mds->bufp=TSIOBufferCreate();if(TSIOBufferWrite(mds->bufp,my_url,strlen(my_url))==TS_ERROR){if(log){TSTextLogObjectWrite(log,(char*)"TSIOBufferWriteError");}returnTS_ERROR;}mds->bufp_reader=TSIOBufferReaderAlloc(mds->bufp);mds->vio=TSVConnWrite(mds->vc
 onn,contp,mds->bufp_reader,strlen(my_url));}break;caseTS_EVENT_VCONN_WRITE_READY:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_VCONN_WRITE_READY");}if(contp!=global_contp){mds=(MyDS*)TSContDataGet(contp);TSVIOReenable(mds->vio);}break;caseTS_EVENT_VCONN_WRITE_COMPLETE:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_VCONN_WRITE_COMPLETE\n");}mds=(MyDS*)TSContDataGet(contp);TSVConnClose(mds->vconn);TSIOBufferReaderFree(mds->bufp_reader);TSHttpTxnReenable(mds->txnp,TS_EVENT_HTTP_CONTINUE);break;caseTS_EVENT_CACHE_OPEN_WRITE_FAILED:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_CACHE_OPEN_WRITE_FAILED");}if(contp!=global_contp){mds=(MyDS*)TSContDataGet(contp);}TSVConnClose(mds->vconn);TSHttpTxnReenable(mds->txnp,TS_EVENT_HTTP_CONTINUE);break;default:break;}returnstatus;}//----------------------------------------------------------------------------void TSPluginInit(intargc,constchar*argv[]){TSPluginRegistrationInfoinfo;info.plugin_name=(char*)PLUGIN_NAME;info.vendor_name
 =(char*)"vendor";info.support_email=(char*)"support@email.com";if(TSPluginRegister(TS_SDK_VERSION_3_0,&info)!=TS_SUCCESS){TSError("Pluginregistrationfailed.\n");}if(!check_ts_version()){TSError("PluginrequiresTrafficServer3.0orlater\n");return;}interror=TSTextLogObjectCreate("my-plugin",TS_LOG_MODE_ADD_TIMESTAMP,&log);if(!log||error==TS_ERROR){TSError("[%s]Errorcreatinglogfile\n",PLUGIN_NAME);}global_contp=TSContCreate(http_event_handler,TSMutexCreate());TSHttpHookAdd(TS_HTTP_TXN_START_HOOK,global_contp);TSDebug(PLUGIN_NAME,"TSPluginInitComplete!\n");}//----------------------------------------------------------------------------int check_ts_version(){constchar*ts_version=TSTrafficServerVersionGet();intresult=0;if(ts_version){intmajor_ts_version=0;intminor_ts_version=0;intpatch_ts_version=0;if(sscanf(ts_version,"%d.%d.%d",&major_ts_version,&minor_ts_version,&patch_ts_version)!=3){return0;}/*NeedatleastTS2.0*/if(major_ts_version>=2){result=1;}}returnresult;}

Re: a simple plugin that writes to the cache

Posted by Leif Hedstrom <zw...@apache.org>.
On 05/04/2011 11:24 AM, John Plevyak wrote:
> The way the regex query works is that it scans the cache on disk.   However,
> it does not scan
>
> the write aggregation buffer which is probably where you document is.
>
>
> You might want to file that as a bug....

Also (and John probably knows this better than me), but I thought it 
wasn't possible to use the existing APIs to write into the cache as an 
"HTTP" object. At least that's what the documentation says. I.e. you can 
cache your own content and retrieve those from the plugin, but it won't 
be in a way that the HTTP core state machine would recognize. Afaik, it 
has to do with the fact that there are actually two cache objects for 
each HTTP object, and we don't expose how those two are linked to the 
plugin APIs.

We really ought to have a way to do HTTP cache read / write APIs. Unless 
of course the documentation is wrong, but I think I checked this before, 
and unless the plugin "steals" some code from the core, and manage the 
links between the two HTTP cache entries itself, it wouldn't work.

Cheers,

-- Leif

 From the docs:

Note that the cache APIs differentiates between HTTP data and plugin 
data. The cache
APIs do not allow you to write HTTP docs in the cache. You can only 
write plugin specific
data which is a specific type of data which is different from the HTTP type.


Re: a simple plugin that writes to the cache

Posted by John Plevyak <jp...@gmail.com>.
The way the regex query works is that it scans the cache on disk.   However,
it does not scan

the write aggregation buffer which is probably where you document is.


You might want to file that as a bug....


In the meantime, if you do a Lookup instead of a regex scan it should find
it because that

will check the write aggregation buffer.


john


On May 4, 2011 6:28 AM, "Kevin Giles" <kg...@gmx.com> wrote:
> Hi,
>
> I have written a plug to write an entry to the cache on the
HTTP_SEND_RESPONSE_HDR_HOOK.
>
> It appears to work fine, the plugin is receiving the correct events, but
when I inspect the cache using the cache regex lookup my entry does not
appear?
>
> It is just a simple test to write an entry to the cache, it does not have
to be on the HTTP_SEND_RESPONSE_HDR_HOOK event.
>
> I am hoping it is something simple, would you mind giving my code a quick
look as I figure out what step I am missing.
>
> 20110504.10h19m15s TS_EVENT_HTTP_TXN_START
>
> 20110504.10h19m15s TS_HTTP_SEND_RESPONSE_HDR_HOOK
> 20110504.10h19m15s Cache Write Start
> 20110504.10h19m15s TS_EVENT_CACHE_OPEN_WRITE
> 20110504.10h19m15s TS_EVENT_VCONN_WRITE_COMPLETE
> 20110504.10h19m15s TS_EVENT_HTTP_TXN_CLOSE
>
> Regards,
>
> Kevin.
>
>
#include<stdio.h>#include<string.h>#include<limits.h>#include<stdlib.h>#include<ts/ts.h>#include<ts/experimental.h>#definePLUGIN_NAME"my-plugin"staticTSContglobal_contp;staticTSTextLogObjectlog;intcheck_ts_version();staticinthttp_event_handler(TSContcontp,TSEventevent,void*edata);staticintcache_write_complete=0;typedefstruct_MyDS{TSHttpTxntxnp;TSCacheKeykey;char*url;TSActionpendingAction;TSVConnvconn;TSVIOvio;TSIOBufferbufp;TSIOBufferReaderbufp_reader;}MyDS;/***ThismethodiscalledwhenwereceivetheTS_EVENT_HTTP_SEND_RESPONSE_HDRevent.*Itisasimpletesttowriteanentrytothecachebeforelettingtheoriginal*requestresume.*/staticint
startCacheWrite(TSContcontp){if(cache_write_complete==1){if(log){TSTextLogObjectWrite(log,(char*)"CacheWriteAlreadyPreformed");}returnTS_ERROR;}cache_write_complete=1;if(log){TSTextLogObjectWrite(log,(char*)"CacheWriteStart");}constchar*my_url="
http://trafficserver.apache.org/
";MyDS*mds=(MyDS*)TSContDataGet(contp);mds->key=TSCacheKeyCreate();if(TSCacheKeyDiges
>
tSet(mds->key,my_url,strlen(my_url))!=TS_SUCCESS){if(log){TSTextLogObjectWrite(log,(char*)"TSCacheKeyDigestSetFailed");}returnTS_ERROR;}intis_ready=0;TSCacheReady(&is_ready);if(is_ready==0){if(log){TSTextLogObjectWrite(log,(char*)"Cacheisnotready");}returnTS_ERROR;}TSCacheWrite(contp,mds->key);returnTS_SUCCESS;}staticvoid
handle_txn_start(TSContcontp,TSHttpTxntxnp){TSConttxn_contp;MyDS*mds;txn_contp=TSContCreate((TSEventFunc)http_event_handler,TSMutexCreate());mds=(MyDS*)TSmalloc(sizeof(MyDS));TSContDataSet(txn_contp,mds);mds->txnp=txnp;mds->url=NULL;mds->key=NULL;mds->pendingAction=NULL;mds->bufp=NULL;mds->bufp_reader=NULL;mds->vio=NULL;TSHttpTxnHookAdd(txnp,TS_HTTP_SEND_RESPONSE_HDR_HOOK,txn_contp);TSHttpTxnHookAdd(txnp,TS_HTTP_TXN_CLOSE_HOOK,txn_contp);TSHttpTxnReenable(txnp,TS_EVENT_HTTP_CONTINUE);}staticvoid
destroy_continuation(TSHttpTxntxnp,TSContcontp){MyDS*mds=NULL;mds=(MyDS*)TSContDataGet(contp);if(mds!=NULL){if(mds->url){TSfree(mds->url);}if(mds->key){TSCacheKeyDe
>
stroy(mds->key);}if(mds->bufp){TSIOBufferDestroy(mds->bufp);}TSfree(mds);}TSContDestroy(contp);TSHttpTxnReenable(txnp,TS_EVENT_HTTP_CONTINUE);return;}//----------------------------------------------------------------------------//handlerforhttptxneventsstaticint
http_event_handler(TSContcontp,TSEventevent,void*edata){intstatus=TS_SUCCESS;inturl_length=0;TSHttpTxntxnp=(TSHttpTxn)edata;MyDS*mds=0;switch(event){caseTS_EVENT_HTTP_TXN_START:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_HTTP_TXN_START");}txnp=(TSHttpTxn)edata;handle_txn_start(contp,txnp);return0;caseTS_EVENT_HTTP_READ_REQUEST_HDR:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_HTTP_READ_REQUEST_HDR");}if(contp!=global_contp){mds=(MyDS*)TSContDataGet(contp);mds->url=TSHttpTxnEffectiveUrlStringGet(txnp,&url_length);if(!mds->url){TSError("[%s]couldn'tretrieverequesturl\n",PLUGIN_NAME);status=TS_ERROR;}if(status==TS_SUCCESS){if(log){TSTextLogObjectWrite(log,(char*)"Requestfor%sreceived",mds->url);}}}TSHttpTxnRee
>
nable(txnp,TS_EVENT_HTTP_CONTINUE);break;caseTS_EVENT_HTTP_SEND_RESPONSE_HDR:if(log){TSTextLogObjectWrite(log,(char*)"TS_HTTP_SEND_RESPONSE_HDR_HOOK");}if(contp!=global_contp){if(startCacheWrite(contp)==TS_ERROR){TSHttpTxnReenable(txnp,TS_EVENT_HTTP_CONTINUE);}}else{TSHttpTxnReenable(txnp,TS_EVENT_HTTP_CONTINUE);}break;caseTS_EVENT_HTTP_TXN_CLOSE:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_HTTP_TXN_CLOSE");}txnp=(TSHttpTxn)edata;if(contp!=global_contp){destroy_continuation(txnp,contp);}break;caseTS_EVENT_CACHE_OPEN_WRITE:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_CACHE_OPEN_WRITE");}if(contp!=global_contp){mds=(MyDS*)TSContDataGet(contp);mds->vconn=(TSVConn)edata;constchar*my_url="
http://trafficserver.apache.org/
";mds->bufp=TSIOBufferCreate();if(TSIOBufferWrite(mds->bufp,my_url,strlen(my_url))==TS_ERROR){if(log){TSTextLogObjectWrite(log,(char*)"TSIOBufferWriteError");}returnTS_ERROR;}mds->bufp_reader=TSIOBufferReaderAlloc(mds->bufp);mds->vio=TSVConnWrite(mds->vc
>
onn,contp,mds->bufp_reader,strlen(my_url));}break;caseTS_EVENT_VCONN_WRITE_READY:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_VCONN_WRITE_READY");}if(contp!=global_contp){mds=(MyDS*)TSContDataGet(contp);TSVIOReenable(mds->vio);}break;caseTS_EVENT_VCONN_WRITE_COMPLETE:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_VCONN_WRITE_COMPLETE\n");}mds=(MyDS*)TSContDataGet(contp);TSVConnClose(mds->vconn);TSIOBufferReaderFree(mds->bufp_reader);TSHttpTxnReenable(mds->txnp,TS_EVENT_HTTP_CONTINUE);break;caseTS_EVENT_CACHE_OPEN_WRITE_FAILED:if(log){TSTextLogObjectWrite(log,(char*)"TS_EVENT_CACHE_OPEN_WRITE_FAILED");}if(contp!=global_contp){mds=(MyDS*)TSContDataGet(contp);}TSVConnClose(mds->vconn);TSHttpTxnReenable(mds->txnp,TS_EVENT_HTTP_CONTINUE);break;default:break;}returnstatus;}//----------------------------------------------------------------------------void
TSPluginInit(intargc,constchar*argv[]){TSPluginRegistrationInfoinfo;info.plugin_name=(char*)PLUGIN_NAME;info.vendor_name
> =(char*)"vendor";info.support_email=(char*)"support@email.com";if(TSPluginRegister(TS_SDK_VERSION_3_0,&info)!=TS_SUCCESS){TSError("Pluginregistrationfailed.\n");}if(!check_ts_version()){TSError("PluginrequiresTrafficServer3.0orlater\n");return;}interror=TSTextLogObjectCreate("my-plugin",TS_LOG_MODE_ADD_TIMESTAMP,&log);if(!log||error==TS_ERROR){TSError("[%s]Errorcreatinglogfile\n",PLUGIN_NAME);}global_contp=TSContCreate(http_event_handler,TSMutexCreate());TSHttpHookAdd(TS_HTTP_TXN_START_HOOK,global_contp);TSDebug(PLUGIN_NAME,"TSPluginInitComplete!\n");}//----------------------------------------------------------------------------int
check_ts_version(){constchar*ts_version=TSTrafficServerVersionGet();intresult=0;if(ts_version){intmajor_ts_version=0;intminor_ts_version=0;intpatch_ts_version=0;if(sscanf(ts_version,"%d.%d.%d",&major_ts_version,&minor_ts_version,&patch_ts_version)!=3){return0;}/*NeedatleastTS2.0*/if(major_ts_version>=2){result=1;}}returnresult;}