Relay Transforms and Routes¶
Relay transforms rename and reshape messages from feed handlers, and Relay routes decide which Centers each message is sent to.
Overview¶
Every message from a feed handler passes through the transforms first, then the routes:
The Relay reads 3 kinds of file:
- The transforms file selects and renames messages: each line of
data/relay.transformsis one rule that picks messages by type and names the table they are sent to. - Dictionaries rewrite message fields: a transform rule that names a dictionary, defined in the
data/*.relay.dictionaryfiles, maps each message's fields through it. - The routes file picks the Centers: each line of
data/relay.routesis one rule that sends matching messages to a list of Centers.
Both rule files start empty, and an empty file changes nothing:
- While the transforms file holds no rules, every message is forwarded unchanged. Once it holds at least one rule, an object or delete message that no rule sends on is dropped.
- While the routes file holds no rules, every message goes to every Center. Once it holds at least one rule, a message that no rule matches is dropped.
Properties¶
Set these in config/local.properties:
The Relay rereads the transforms, dictionary, and routes files when they change, so edits take effect without a restart. See Checking transforms and routes for what happens when an edited file has an error.
The Centers a route can name are those in ami.centers, described in the Relay configuration guide.
Transform rules¶
Each line of the transforms file is one rule. Blank lines and lines starting with # are ignored. A rule has exactly 8 fields separated by semicolons:
Leave a field empty, but keep its semicolon, to omit it. To use a literal semicolon inside a field, escape it as \;.
| Field | Description |
|---|---|
TransformName |
Unique name of the rule. Required. |
Priority |
Integer. Rules run lowest number first. Rules with the same priority run in alphabetical order of name. |
ObjectTypes |
Comma-delimited list of message types (the T value) the rule handles, and the table each is sent to. See Object types. Empty skips the rule. |
Dictionary |
Name of the dictionary used to rewrite the message's fields. Empty sends the fields unchanged. |
Expression |
Condition evaluated on the message's fields. Must return a boolean. Requires a Dictionary. Empty matches every message. |
Options |
Comma-delimited list. The only option is PASSTHROUGH: include the message's unmapped fields in the output. |
OnTrue |
BREAK or CONTINUE. Applies when the rule sends the message on. Empty means CONTINUE. |
OnFalse |
BREAK or CONTINUE. Applies when the rule does not handle the message type, or the expression returns false. Empty means CONTINUE. |
A line that does not have 8 fields fails with Expecting 8 fields (TransformName;Priority;ObjectTypes;Dictionary;Expression;Options;OnTrue;OnFalse).
Object types¶
Each entry in ObjectTypes takes one of these forms:
| Entry | Effect |
|---|---|
Orders |
Handle Orders messages and keep the type Orders |
* |
Handle every message type and keep its type |
NewOrder=NEW_ORDER |
Handle NEW_ORDER messages and send them as NewOrder |
AllEvents=* |
Send every message type as AllEvents |
=Heartbeat |
Do not handle Heartbeat messages |
Map one source type to more than one target (OrdersA=Orders,OrdersB=Orders) to send a copy to each target table. A type named both as skipped and as mapped is an error.
How transform rules are evaluated¶
Only object messages (O) and delete messages (D) pass through the transforms. Every other message type goes straight to the routes. For each object or delete message, the Relay runs the rules in priority order:
- If the rule does not handle the message's type, or its expression returns false, the rule does not send the message.
OnFalsedecides whether to try the next rule. - Otherwise, the rule sends the message, rewritten through its dictionary, to each target table.
OnTruedecides whether to try the next rule.
Every rule works on the message as it arrived, not on another rule's output. With OnTrue left as CONTINUE, each further rule that matches sends another copy, so a message can reach a Center more than once. Set OnTrue to BREAK on a rule whose output should be final.
Dictionaries¶
A dictionary maps the fields of an incoming message to the fields sent to the Center. A dictionary file holds any number of dictionaries:
Inside the braces, each statement ends with a semicolon:
- Mapping:
target=expression;sets the output fieldtarget. The expression can use the message's fields and AmiScript methods, such astimestamp()andformatDate(). Quote a field name that is not a valid variable name, such as a numeric FIX tag, in backticks:price=`44`;. - Declaration:
Type field;gives a source field a type, so methods of that type can be called on it:String `55`; sym=`55`.toUpper();.
How the output is built:
- Only mapped fields are sent, unless the rule sets
PASSTHROUGH. - With
PASSTHROUGH, fields that no mapping reads are sent unchanged as well. A field that a mapping reads is replaced by the mapping's output. - A mapping whose source field is missing from the message is skipped. A mapping that fails is skipped, and a warning is written to the log.
- A dictionary that extends another inherits its mappings and declarations. A dictionary cannot extend itself, directly or through another dictionary, and each dictionary name must be unique across all dictionary files.
A transform rule's Expression is evaluated against the dictionary's fields, plus 3 fields every message carries:
T: the message type, aStringI: the message ID, aStringE: the expiry, aLong
Warning
Do not declare I in a dictionary. A declaration of String I; fails with Inconsistent type in transform '<name>' for variable I.
To list the methods available in a mapping, run SHOW METHODS; in the AMIDB Shell Tool.
Route rules¶
Each line of the routes file is one rule. Blank lines and lines starting with # are ignored. A rule has exactly 9 fields separated by semicolons:
| Field | Description |
|---|---|
RouteName |
Unique name of the rule. Required. |
Priority |
Integer. Rules run lowest number first. Rules with the same priority run in alphabetical order of name. |
MessageTypes |
Comma-delimited list of message types: O (object), D (delete), C (command), and S (status). * matches all 4. Empty skips the rule. |
ObjectTypes |
Comma-delimited list of object types (the T value) the rule handles. * matches every type. Empty skips the rule. |
ParamTypes |
Comma-delimited list of message fields the expression uses, each as Name Type, e.g: Symbol String. Add nonull (Symbol String nonull) to skip the rule for messages where that field is missing. T and I are available as String without being listed. |
Expression |
Condition evaluated on the fields in ParamTypes. Must return a boolean. Use true to match every message the rule handles. |
RouteList |
Comma-delimited list of Center names from ami.centers. * sends to every Center. Empty sends to none. |
OnTrue |
BREAK or CONTINUE. Applies when the expression returns true. Empty means CONTINUE. |
OnFalse |
BREAK or CONTINUE. Applies when the expression returns false or null. Empty means CONTINUE. |
A line that does not have 9 fields fails with Expecting 9 parts (RouteName;Priority;messageTypes;ObjectTypes;ParamTypes;Expression;RouteList;OnTrue;OnFalse). A Center name not in ami.centers fails with RouteList has unknown center:, followed by the Centers that are defined.
How route rules are evaluated¶
For each message, the Relay runs the rules in priority order:
- If the message's type is not in
MessageTypesorObjectTypes, or anonullfield is missing, the rule is passed over and the next rule is tried. NeitherOnTruenorOnFalseapplies. - If the expression returns true, the Centers in
RouteListare added to the message's destinations, andOnTruedecides whether to try the next rule. - If the expression returns false or null,
OnFalsedecides whether to try the next rule.
The message is then sent once to each Center collected, however many rules named it. If no rule matched, the message is dropped.
Mapping FIX-style fields¶
This example renames 2 numeric fields, adds a timestamp, and adds a formatted time, using 2 transform rules on every message type.
-
In the
data/directory, createsample.relay.dictionarywith the following dictionaries: -
Add the following rules to
data/relay.transforms: -
Send the following message to the Relay on
ami.port(default3289): -
Confirm that the Center's
sample_fixtable holds 1 row withsymset to3FORGE,priceset to0.99, and atimevalue: runSELECT * FROM sample_fix;in the AMIDB Shell Tool:
Both rules have the priority 0 and an empty OnTrue, so both run, and the Relay sends 2 messages:
Transform_fixsendssym,price, andnow. Fields44and55are read by its mappings, soPASSTHROUGHdoes not send them.Transform_numbersendstime. Its dictionary reads neither field, soPASSTHROUGHsends44and55unchanged.
Both messages carry I="01", so the Center applies both to the same row.
Passing other tables through unchanged¶
A catch-all transform rule with no dictionary forwards every message the other rules do not handle, unchanged.
-
Add a catch-all rule with the highest priority number in the transforms file:
-
Set
OnTruetoBREAKon every rule that should not also be followed by the catch-all, e.g:Transform_fix;0;sample_fix;FixDictionary;true;PASSTHROUGH;BREAK;. Otherwise the catch-all sends a second, untransformed copy of the message. -
Confirm that the catch-all is last in the list: run
amiRelayServer.showTransforms()in the f1 console, andPassThroughis the last row underTransforms(Order by Priority).
Routing messages by field value¶
This example sends NewOrder and Cancel messages to one of 4 Centers by the first letter of their symbol, and sends every other message to all Centers.
-
Define the 4 Centers in
ami.centersin the Relay'sconfig/local.properties, and restart the Relay: -
Add the following rules to
data/relay.routes: -
Send the following messages to the Relay on
ami.port: -
Confirm that
RULE0,RULE1, andRULE2each show aMatchCountof 1: runamiRelayServer.showRoutes()in the f1 console.
The messages go to these Centers:
| Message | Matching rule | Sent to |
|---|---|---|
Symbol="AAPL" |
RULE0 |
Center0 |
Symbol="IBM" |
RULE1 |
Center1 |
Symbol="ZVZZT" |
RULE2 |
Center2 and Center3 |
A NewOrder message with no Symbol field skips RULE0 to RULE2, because of nonull, and goes to every Center through RULE3.
Checking transforms and routes¶
Transforms and routes are inspected from the amiRelayServer object in the f1 console:
| Command | Shows |
|---|---|
amiRelayServer.showTransforms() |
The transforms file, the dictionary files, message counts (in, out, and dropped), and each transform rule in priority order with its match, mismatch, and sent counts |
amiRelayServer.showDictionaries() |
Each dictionary, what it extends, and the file it is in |
amiRelayServer.showDictionary("FixDictionary") |
One dictionary's field mappings, including those inherited from dictionaries it extends |
amiRelayServer.showRoutes() |
The routes file, message counts (in, out, and dropped), and each route rule with its target Centers and match count |
amiRelayServer.enableTransformDebug() |
Logs each object message's path through the transform rules to log/AmiOne.log until disabled |
amiRelayServer.disableTransformDebug() |
Stops the transform debug logging |
amiRelayServer.enableRoutesDebug() |
Logs each message's path through the route rules to log/AmiOne.log until disabled |
amiRelayServer.disableRoutesDebug() |
Stops the route debug logging |
Note
The output of showRoutes() labels the routes file Transform File: and the rule table Transforms(Order by Priority). Both refer to the routes.
If an edited transforms, dictionary, or routes file has an error, the Relay keeps using the rules it last loaded. showTransforms() or showRoutes() then shows a CONFIG OUT-OF-DATE: line with the error, and the log holds the details.
With transform debugging on, the log shows, for each object message:
START_TRANSFORM:followed by the message's fields- A
SENDINGorMISMATCHline for each rule tried END_TRANSFORM:followed bySENT <n> MESSAGE(S), orMESSAGE_DROPPEDwhen no rule sent the message
The Relay caches each routing decision, keyed on the message type, the T and I values, and the values of the fields listed in ParamTypes. A later message with the same values reuses the cached decision without running the rules, so it adds nothing to MatchCount and writes no route debug lines. To see every message in the counts and the log, set ami.relay.routes.rules.cache.size=0 while investigating.
With route debugging on, the log shows, for each message whose decision is not cached:
START_ROUTE:followed by the message type and object type- A
MATCH, SEND TO:orMISMATCHline for each rule whose expression ran END_ROUTE:followed bySENDING TOand the Centers, orMESSAGE DROPPED
