Skip to content
Relay

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:

Feed handlers --> relay.transforms --> relay.routes --> Centers

The Relay reads 3 kinds of file:

  • The transforms file selects and renames messages: each line of data/relay.transforms is 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.dictionary files, maps each message's fields through it.
  • The routes file picks the Centers: each line of data/relay.routes is 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:

# File holding the transform rules. Created from a commented template on startup if missing
ami.relay.transforms.file=data/relay.transforms

# Dictionary files. Accepts a wildcard or a comma-delimited list of files
ami.relay.dictionary.files=data/*.relay.dictionary

# Log each message's path through the transforms. Default false
ami.relay.transforms.debug=false

# File holding the route rules. Created from a commented template on startup if missing
ami.relay.routes.file=data/relay.routes

# Log each message's path through the routes. Default false
ami.relay.routes.debug=false

# Number of routing decisions cached (see Checking transforms and routes). Default 10000. Set to 0 to disable the cache
ami.relay.routes.rules.cache.size=10000

# How often, in milliseconds, the Relay checks its configuration files for changes. Default 2000
ami.relay.check.config.file.changes.period.ms=2000

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:

TransformName;Priority;ObjectTypes;Dictionary;Expression;Options;OnTrue;OnFalse

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:

  1. If the rule does not handle the message's type, or its expression returns false, the rule does not send the message. OnFalse decides whether to try the next rule.
  2. Otherwise, the rule sends the message, rewritten through its dictionary, to each target table. OnTrue decides 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:

DictionaryName [extends OtherDictionary, ...] { declarations and mappings }

Inside the braces, each statement ends with a semicolon:

  • Mapping: target=expression; sets the output field target. The expression can use the message's fields and AmiScript methods, such as timestamp() and formatDate(). 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, a String
  • I: the message ID, a String
  • E: the expiry, a Long

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:

RouteName;Priority;MessageTypes;ObjectTypes;ParamTypes;Expression;RouteList;OnTrue;OnFalse
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:

  1. If the message's type is not in MessageTypes or ObjectTypes, or a nonull field is missing, the rule is passed over and the next rule is tried. Neither OnTrue nor OnFalse applies.
  2. If the expression returns true, the Centers in RouteList are added to the message's destinations, and OnTrue decides whether to try the next rule.
  3. If the expression returns false or null, OnFalse decides 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.

  1. In the data/ directory, create sample.relay.dictionary with the following dictionaries:

    1
    2
    3
    4
    5
    6
    RawFix { price=`44`; }
    FixDictionary extends RawFix { String `55`; sym=`55`.toUpper(); now=timestamp(); }
    
    TimeString {
      time=formatDate(timestamp(),"MM-dd-yy HH:mm:ss", "GMT");
    }
    
  2. Add the following rules to data/relay.transforms:

    Transform_fix;0;*;FixDictionary;true;PASSTHROUGH;;
    Transform_number;0;*;TimeString;true;PASSTHROUGH;;
    
  3. Send the following message to the Relay on ami.port (default 3289):

    O|I="01"|T="sample_fix"|44=0.99|55="3forge"
    
  4. Confirm that the Center's sample_fix table holds 1 row with sym set to 3FORGE, price set to 0.99, and a time value: run SELECT * FROM sample_fix; in the AMIDB Shell Tool:

    The sample_fix table with columns sym, price, now, I, 44, 55, and time in one row

Both rules have the priority 0 and an empty OnTrue, so both run, and the Relay sends 2 messages:

  • Transform_fix sends sym, price, and now. Fields 44 and 55 are read by its mappings, so PASSTHROUGH does not send them.
  • Transform_number sends time. Its dictionary reads neither field, so PASSTHROUGH sends 44 and 55 unchanged.

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.

  1. Add a catch-all rule with the highest priority number in the transforms file:

    PassThrough;100;*;;;;;
    
  2. Set OnTrue to BREAK on 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.

  3. Confirm that the catch-all is last in the list: run amiRelayServer.showTransforms() in the f1 console, and PassThrough is the last row under Transforms(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.

  1. Define the 4 Centers in ami.centers in the Relay's config/local.properties, and restart the Relay:

    ami.centers=Center0=host0:3270,Center1=host1:3270,Center2=host2:3270,Center3=host3:3270
    
  2. Add the following rules to data/relay.routes:

    1
    2
    3
    4
    RULE0;0;O,D;NewOrder,Cancel;Symbol String nonull;Symbol < "F";Center0;BREAK;
    RULE1;1;O,D;NewOrder,Cancel;Symbol String nonull;Symbol < "Q";Center1;BREAK;
    RULE2;2;O,D;NewOrder,Cancel;Symbol String nonull;true;Center2,Center3;BREAK;
    RULE3;3;*;*;;true;*;BREAK;
    
  3. Send the following messages to the Relay on ami.port:

    1
    2
    3
    O|T="NewOrder"|Symbol="AAPL"
    O|T="NewOrder"|Symbol="IBM"
    O|T="NewOrder"|Symbol="ZVZZT"
    
  4. Confirm that RULE0, RULE1, and RULE2 each show a MatchCount of 1: run amiRelayServer.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 SENDING or MISMATCH line for each rule tried
  • END_TRANSFORM: followed by SENT <n> MESSAGE(S), or MESSAGE_DROPPED when 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: or MISMATCH line for each rule whose expression ran
  • END_ROUTE: followed by SENDING TO and the Centers, or MESSAGE DROPPED