Summary:
How to read/take a group of space entries with a common property value, in FIFO order (by order of insertion), without having to maintain a FIFO order for all the entries in the space. OverviewThe FIFO groups features is designed to allow for efficient processing of events with partial ordering constraints. To better understand FIFO groups, let's first examine the constraints of total ordering, i.e. What it takes to process events in the exact order in which they arrive. There are two elements that effectively limit the scalability of processing events with total ordering:
In most cases, your application does not require total ordering, but rather ordering within groups of events. Let's take a flight booking system as an example. Such an system should process the bookings for each flight one by one, to avoid conflicting bookings for the same seats and ensure fairness. But it can process many flights simultaneously, since there's no relevance to the order of processing across different flights. The FIFO groups feature allows you to implement this kind of scenario easily by designating a "FIFO group" field. The value of this field indicates the unique group identifier. In the above example, this would be the flight number, so effectively your application can process many bookings simultaneously for different flights, but it will never process two bookings for the same flight simultaneously. In more generic terms, the FIFO groups capability ensures the following:
The FIFO-Grouping can be used with financial systems to process trade orders , in healthcare systems to processes patient medical data , with transportation systems to process reservations , with airlines systems to process flight schedule , with billing system to processes payments, etc. With the flight reservation system scenario several reservations can be processed simultaneously but the reservations of a particular fight must be processed exclusively and in FIFO order. How it works?FIFO-Grouping ('FG') enables reading/taking certain space entries in FIFO order (by order of insertion), and is mainly used as an enhancement of the openspaces polling-containers. When a property is declared with the SpaceFifoGroupingProperty attribute ('the FG designated property'), a read/take operation with the FIFO_GROUPING_POLL modifier will return all space entries that match the selection template in FIFO order. Different values of the FG property define groups of space entries that match that value - FIFO ordering exists within each group and not between different groups.
Method Of Operation
ExclusivityOnce a FG operation returns with a result, the relevant group(s) is locked to other FG threads until the transaction is terminated. The locked group is the group with the value returned in the FG designated property and matching the selecting template. For example: Interaction with non-FIFO Grouping templatesWhen a FG template locks a group, its first entry is locked under a transaction so it cannot be accessed by any destructive space operation. The other entries in the group are not physically locked and may be operated-upon by non FG templates. If a FG template which is intending to lock a group encounters its first entry locked under a transaction by a non FG template - this group is abandoned in order not to create a gap (by skipping the first entry) Setting the FIFO Grouping propertySpecifying which property of a class is the FG property is done using attributes or gs.xml.
Annotations
[SpaceClass] public class FlightReservation { [SpaceFifoGroupingProperty(Path = "FlightNumber")] public FlightInfo Info { get; set; } } XML <gigaspaces-mapping> <class name="com.gigaspaces.examples.FlightReservation"> <fifo-grouping-property Name="Info" Path=" FlightNumber" /> </class> </gigaspaces-mapping> Setting FIFO Grouping indexSpecifying which properties of a class are a FG index is done using attributes or gs.xml.
Annotations
[SpaceFifoGroupingIndex] public State ProcessingState { get; set; } [SpaceFifoGroupingIndex(Path = "Id")] public Person Customer { get; set; } XML <gigaspaces-mapping> <class name="com.gigaspaces.examples.FlightReservation /> <property name="ProcessingState"> <fifo-grouping-index /> </property> <property name="Customer"> <fifo-grouping-index Path="Id"/> </property> </gigaspaces-mapping> Working PatternsTakeTake the first entry from an available FG (TakeReceiveOperationHandler). Readread the first entry from an available FG. In this pattern entry property may be changed and the operation ending with update. For example- a ProcessingState property which is initially 0, some FG polling container querying for 0 and changing it to 1, other container querying for 1 and changing to 2 etc until a container querying for the final state with take. Read multipleread entries from available FG. No ordering between different groups. Entries of same groups may not be in adjacent positions. (openspaces MultiExclusiveReadReceiveOperationHandler) Query operations with FIFO GroupingUsing READ/TAKE modifiersTo execute read/take operations with FG, use the TakeModifiers.FifoGroupingPoll modifier. For example: proxy.Take<FlightReservation>(new FlightReservation(), transaction, timeout, TakeModifiers.FifoGroupingPoll);
If class FlightReservation isn't declared with a FG property, an exception will be thrown. Using Polling containerWhen registering for polling events use the FifoGroupingReceiveHandler<TData>.UseFifoGrouping = true Here is a simple example of a polling event container construction, using FifoGrouping:
Using EventListenerContainerFactory
[PollingEventDriven] public class FlightReservationEventListener { [EventTemplate] public FlightReservation UnprocessedData() { return new FlightReservation(null, State.NEW_RESERVATION); } [SpaceDataEvent] public FlightReservation EventListener(FlightReservation event) { //process reservation here and return the processed reservation. } [ReceiveHandler] public IReceiveOperationHandler<FlightReservation> ReceiveHandler() { handler = new ExclusiveReadReceiveOperationHandler<FlightReservation>(); handler.UseFifoGrouping = true; return handler; } } Constructing the polling container that uses the FlightReservationEventListener class as the event listener, and starting it. ISpaceProxy spaceProxy = // either create the SpaceProxy or obtain a reference to it IEventListenerContainer<FlightReservation> eventListenerContainer = EventListenerContainerFactory.CreateContainer<FlightReservation>(spaceProxy, new FlightReservationEventListener()); eventListenerContainer.Start(); // when needed to dispose of the container eventListenerContainer.Dispose() PollingEventListenerContainer Code Construction PollingEventListenerContainer<FlightReservation> pollingEventListenerContainer = // create or obtain a reference to a polling container ExclusiveReadReceiveOperationHandler<FlightReservation> receiveHandler = new ExclusiveReadReceiveOperationHandler<FlightReservation>(); receiveHandler.UseFifoGrouping = true; pollingEventListenerContainer.ReceiveOperationHandler = receiveHandler; pollingEventListenerContainer.Template = new FlightReservation(null, State.NEW_RESERVATION); pollingEventListenerContainer.DataEventArrived += new DelegateDataEventArrivedAdapter<FlightReservation,FlightReservation>(FlightReservationProcessData).WriteBackDataEventHandler; // when needed dispose of the container pollingEventListenerContainer.Dispose(); Event processing method public FlightReservationProcessData(IEventListenerContainer<FlightReservation> sender, DataEventArgs<FlightReservation> e) { FlightReservation reservation = e.Data; //process the reservation here and return the processed reservation } SpaceIndex AttributeDeclaring both SpaceFifoGroupingProperty or SpaceFifoGroupingIndex and SpaceIndex (type BASIC or Extended) with the same path will yield one index with the SpaceIndex type. InheritanceAll property's FG declarations (both SpaceFifoGroupingProeprty and SpaceFifoGroupingIndex) are inherited in sub classes.
Considerations
Performance Hints
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |