Asynchronous Completion Token an Object Behavioral Pattern for Efficient Asynchronous Event
- 格式:pdf
- 大小:793.21 KB
- 文档页数:8
AsynchronousCompletionTokenAnObjectBehavioralPatternforEfficientAsynchronousEventHandling
TimothyH.HarrisonandDouglasC.SchmidtDepartmentofComputerScienceWashingtonUniversity,St.Louis,Missouri,63130
IrfanPyaraliEastmanKodakCompanyDallas,Texas,75240
ThispaperappearedinProceedingsofthe3annualPat-ternLanguagesofProgrammingconferenceheldatAllertonPark,Illinois,September4-6,1996.
AbstractContemporaryapplicationsmustrespondtomanytypesofevents,rangingfromuserinterfacenotificationstonetworkmessages.Delay-sensitiveapplications,suchasnetworkmanagementsystems,oftenperformlong-runningoperationsasynchronouslytoavoidblockingtheprocessingofotherpendingevents.Whentheseasynchronousoperationscom-plete,applicationsmayneedmoreinformationthansimplythenotificationitselftoproperlyhandletheevent.Thispa-perdescribestheAsynchronousCompletionTokenpattern,whichallowsapplicationstoefficientlyassociatestatewiththecompletionofasynchronousoperations.
1IntentToefficientlyassociatestatewiththecompletionofasyn-chronousoperations.
2AlsoKnownAs“MagicCookie”3Motivation3.1ContextToillustratetheAsynchronousCompletionTokenpattern,considerthestructureofanetworkManagementApplicationthatmonitorstheperformanceandstatusofmultiplecom-ponentsinadistributedElectronicMedicalImagingSystem(EMIS)[1].Figure1showsasimplifiedviewoftheMan-agementApplicationandseveralEMIScomponents.1: ImageServer
: Agent
: ManagementApplication
MONITORING STATION
1:REGISTER FOR IMAGE
SERVER CONNECTIONINFO
3:NOTIFY MANAGEMENT
APPLICATION ANDRETURN CONNECTIONINFO
2:NOTIFY AGENT OF
CONNECTION EVENT
REMOTE HOST
Figure2:EventRegistrationandCallbacklocaldatabase.SincetheManagementApplicationcanreg-isterformanytypesofeventsfromanynumberofAgents,itmustefficientlydeterminetheappropriateactionforeachevent.Morespecifically,AgentsmustprovideautilityfortheManagementApplicationtoassociateadditionalstatewithaneventnotification.ThisassociatedstatemustbesufficientfortheManagementApplicationtodeterminetheappropriatere-sponse(e.g.,displayinginawindoworloggingtoadatabase)fortheevent.Ingeneral,whenprovidinganasynchronousservice(e.g.,EMISAgentspropagatingeventstomaintenanceapplica-tions),theservicemustallowclientstoassociatestatewithasynchronousoperations.Clientsusethestatetodecidetheappropriateactionstoperformuponcompletionoftheop-erations.Thekeydesignchallengeexaminedinthispaperhowtoefficientlyassociateapplication-specificstatewiththecompletionnotifications.
3.2CommonTrapsandPitfallsOnewaytoexecutemultiplelongdurationoperationssimul-taneouslyistoutilizethreads.Forinstance,theManagementApplicationcouldspawnaseparatethreadforeachrequesttoanAgent.Eachrequestwouldexecutesynchronously,blockinguntilitsAgentreplied.Inthisapproach,thestateinformationrequiredtohandletheAgentrepliescouldbestoredimplicitlyinthecontextofeachthread’srun-timestack.Thereareseveraldrawbackswithacompletelysyn-chronousapproachbasedonthreading,however:
Increasedcomplexity–Threadingmayrequirecomplexconcurrencycontrolschemes;
Poorperformance–Threadingmayleadtopoorperfor-manceduetocontextswitching,synchronization,anddatamovement[2];
Lackofportability–ThreadingmaynotbeavailableontheOSplatform.
Anotherwaytoassociatestatewiththecompletionofanasynchronousoperationistodependontheinformationre-turnedbytheasynchronousservice.Forexample,AgentsmustprovidesufficientinformationinthecallbacksotheManagementApplicationcandistinguishbetweenallpossi-bleevents.Thereareseverallimitationstothisapproach,however:
Excessivebandwidth–Providingsufficientdatatotheclientmayrequireanexcessiveamountofdataex-change.Thismaybeparticularlycostlyfordistributedservices.
Lackofcontext–Itmaybetoodifficultfortheservicetoknowwhatdataisneededbytheclients.Forinstance,EMISAgentscannotknowthecontextinwhichtheManagementApplicationmadetherequest.
Performancedegradation–Theclientmayhavetoper-formtimeconsumingprocessing(e.g.,searchingalargetable)touniquelyidentifythecompletionofanasyn-chronouseventbasedonthedatareturnedfromtheser-vice.Thisextraprocessingcandegradetheperformanceoftheclient.
3.3SolutionOften,amoreefficientandflexiblewaytoassociatestatewithcompletioneventsistousetheAsynchronousComple-tionToken(ACT)pattern.TheACTpatternresolvesthefollowingforcesinvolvedwithassociatingstatewithasyn-chronousoperations:
Timeefficiency:ACTsdonotrequirecomplexparsingofdatareturnedwiththenotification.Rather,theACTcanbeanindexordirectpointertothenecessarystate.
Spaceefficiency:ACTsdonotneedbelargeinordertoprovideapplicationswithhooksforassociatingstatewithasynchronousoperations.Forexample,inCandC++,ACTsthatarefourbytevoid*’scanserveasreferencestoanysizeobject.