Samples :: Pluggable Storage

Pluggable Storage Sample

Description

Pluggable session storage allows to customize completely the persistence of a session state and messages.

The biz.onixs.fix.engine.storage.SessionStorage and biz.onixs.fix.engine.storage.StorageRepository interfaces should be implemented.

Please see MyStorageRepository and MySessionStorage implementations. The custom storage just stores the messages into maps.

After implementing above mentioned interfaces instance of the implemented storage repository should be registered in storage repository manager using the biz.onixs.fix.engine.storage.StorageRepositoryManager.register(String, StorageRepository) method (please see in PluggableStorageSample).

Then session with corresponding storage type can be created.

Sample also contains StoragePertMeter which illustrates how the performance of a session storage can be measured. The sample measures the performance of File Based Session Storage.

Usage

  • Run the sample:
    • win: PluggableStorageSample.bat
    • linux: PluggableStorageSample.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.engine.EngineSettings;
import biz.onixs.fix.engine.Session;
import biz.onixs.fix.engine.storage.SessionStorageType;
import biz.onixs.fix.engine.storage.StorageRepository;
import biz.onixs.fix.engine.storage.StorageRepositoryManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
public class PluggableStorageSample {
    private static final Logger LOG = LoggerFactory.getLogger(PluggableStorageSample.class);
    private static final String MY_STORAGE_TYPE_ID = "MyStorage";
    private static final String SENDER_CID = "A";
    private static final String TARGET_CID = "B";
    private static final Version FIX_VERSION = Version.FIX50_SP2;
    private static final int PORT = 10001;
    private SessionStorageType myStorageType = null;
  
    private void run() {
        try {
            final Engine engine = initEngine();
            LOG.info("Engine inited.");
            //
            final Session initiator = createSession(SENDER_CID, TARGET_CID);
            final Session acceptor = createSession(TARGET_CID, SENDER_CID);
            //
            LOG.info("Establishing sessions...");
            acceptor.logonAsAcceptor();
            initiator.logonAsInitiator("localhost", PORT);
            //
            LOG.info("Closing sessions...");
            initiator.logout();
            acceptor.logout();
            initiator.dispose();
            acceptor.dispose();
            //
            engine.shutdown();
            LOG.info("Engine shutdown.");
        } catch (final RuntimeException e) {
            LOG.error("{}", e.getMessage(), e);
        }
    }
  
    private Engine initEngine() {
        final EngineSettings engineSettings = new EngineSettings();
        engineSettings.addListenPort(PORT);
        // Set the storage folder name, but in this sample it is not used.
        // The custom session storage stores messages in the map.
        engineSettings.setStorageFolder("storage-PluggableStorageSample");
        //
        final Engine engine = Engine.init(engineSettings);
        // Get the storage repository manager in order to register a custom session storage repository.
        final StorageRepositoryManager repositoryManager = engine.getStorageRepositoryManager();
        // Create instance of the custom session storage repository.
        final StorageRepository myStorageRepository = new MyStorageRepository();
        // Register custom session storage.
        myStorageType = repositoryManager.register(MY_STORAGE_TYPE_ID, myStorageRepository);
        //
        return engine;
    }
  
    private Session createSession(final String senderCompId, final String targetCompId) {
        // Create a session with the custom session storage.
        return new Session(senderCompId, targetCompId, FIX_VERSION, myStorageType);
    }
  
    public static void main(final String[] args) {
        try {
            final PluggableStorageSample sample = new PluggableStorageSample();
            sample.run();
        } catch (final Throwable throwable) {
            LOG.error(throwable.getMessage(), throwable);
        }
    }
}

The following classes are used in the sample:

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import biz.onixs.fix.engine.SessionId;
import biz.onixs.fix.engine.storage.SessionStorage;
import biz.onixs.fix.engine.storage.StorageRepository;
  
public class MyStorageRepository implements StorageRepository {
    private int maxStorageSize = Integer.MAX_VALUE;
  
    @Override
    public SessionStorage create(final SessionId sessionId, final boolean cleanStart) {
        final SessionStorage storage = new MySessionStorage(sessionId);
        storage.setMaxStorageSize(maxStorageSize);
        return storage;
    }
  
    @Override
    public int getMaxStorageSize() {
        return maxStorageSize;
    }
  
    @Override
    public void setMaxStorageSize(final int maxStorageSize) {
        this.maxStorageSize = maxStorageSize;
    }
}
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import biz.onixs.fix.engine.SessionId;
import biz.onixs.fix.engine.storage.SessionStorage;
import biz.onixs.fix.parser.FixFlatMessage;
import biz.onixs.fix.parser.FixMessage;
import biz.onixs.util.ByteBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
  
public class MySessionStorage implements SessionStorage {
    private static final Logger LOG = LoggerFactory.getLogger(MySessionStorage.class);
    private final SortedMap<Long, ByteBuffer> outMessages = new TreeMap<>();
    private final String id;
    private long inSeqNum = 0L;
    private long outSeqNum = 0L;
    private int maxStorageSize = Integer.MAX_VALUE;
    private boolean closed = false;
    private boolean terminated = false;
  
    MySessionStorage(final SessionId sessionId) {
        Objects.requireNonNull(sessionId, "null == sessionId");
        id = sessionId.getSenderCompId() + '-' + sessionId.getTargetCompId() + '-' +
                sessionId.getFixVersion().getId() + '-' + sessionId.getToken();
    }
  
    @Override
    public synchronized void clear() {
        inSeqNum = 0L;
        outSeqNum = 0L;
        outMessages.clear();
    }
  
    @Override
    public void close(final boolean terminated) {
        closed = true;
        this.terminated = terminated;
    }
  
    @Override
    public synchronized List<FixMessage> getOutboundMessages(final long beginSequenceNumber,
                                                             final long endSequenceNumber) {
        if (1L > beginSequenceNumber) throw new IllegalArgumentException("beginSequenceNumber < 1");
        final List<FixMessage> result = new ArrayList<>();
        if (outMessages.isEmpty()) {
            return result;
        }
        for (long seqNum = beginSequenceNumber; seqNum <= endSequenceNumber; seqNum++) {
            final FixMessage message = createMessage(outMessages.get(seqNum));
            if (null != message) {
                result.add(message);
            }
        }
        return result;
    }
  
    @Override
    public void storeInboundMessage(final ByteBuffer rawMessage, final long msgSeqNum,
                                    final boolean isOriginal) {
        if ((inSeqNum + 1L) == msgSeqNum) {
            inSeqNum = msgSeqNum;
        }
    }
  
    @Override
    public void storeOutboundMessage(final ByteBuffer rawMessage, final long msgSeqNum,
                                     final boolean isOriginal, final boolean warmUp) {
        assert null != rawMessage;
        assert 1L <= msgSeqNum;
        //
        if (closed) return;
        if (warmUp) return;
        //
        if (outSeqNum < msgSeqNum) {
            outSeqNum = msgSeqNum;
        }
        if (0 < getMaxStorageSize()) {
            if (!isOriginal && outMessages.containsKey(msgSeqNum)) {
                return;
            }
            outMessages.put(msgSeqNum, rawMessage.copy());
            if (outMessages.size() > getMaxStorageSize()) {
                outMessages.remove(outMessages.firstKey());
            }
        }
    }
  
    @Override
    public String getId() {
        return id;
    }
  
    @Override
    public long getInSeqNum() {
        return inSeqNum;
    }
  
    @Override
    public void setInSeqNum(final long msgSeqNum) {
        inSeqNum = msgSeqNum;
    }
  
    @Override
    public long getOutSeqNum() {
        return outSeqNum;
    }
  
    @Override
    public void setOutSeqNum(final long msgSeqNum) {
        outSeqNum = msgSeqNum;
    }
  
    @Override
    public boolean isTerminated() {
        return terminated;
    }
  
    @Override
    public void setTerminated(final boolean terminated) {
        this.terminated = terminated;
    }
  
    @Override
    public int getMaxStorageSize() {
        return maxStorageSize;
    }
  
    @Override
    public void setMaxStorageSize(final int maxStorageSize) {
        this.maxStorageSize = maxStorageSize;
    }
  
    private static FixMessage createMessage(final ByteBuffer buffer) {
        FixMessage message = null;
        if (null != buffer) {
            message = new FixFlatMessage(buffer.get(), buffer.length());
        }
        return message;
    }
}

Storage Performance Meter

Description

This app measures session storage performance.

Usage

  • Run the sample:
    • win: StoragePerfMeter.bat
    • linux: StoragePerfMeter.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.engine.SessionId;
import biz.onixs.fix.engine.storage.SessionStorage;
import biz.onixs.fix.engine.storage.SessionStorageType;
import biz.onixs.fix.engine.storage.StorageRepository;
import biz.onixs.fix.engine.storage.StorageRepositoryManager;
import biz.onixs.fix.parser.Message;
import biz.onixs.util.ByteBuffer;
import biz.onixs.util.PrecisionTimer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
/**
 * This app measures session storage performance.
 */
public class StoragePerfMeter {
    private static final Logger LOG = LoggerFactory.getLogger(StoragePerfMeter.class);
    private static final String MSG = "8=FIX.4.0\u00019=82\u000135=D\u000149=0\u000156=0\u000134=1\u0001" +
                                        "52=99990909-17:17:17\u000111=ClOrdID\u000121=1\u000155=IBM\u0001" +
                                        "54=1\u000138=1000\u000140=1\u000110=014\u0001";
    private static final int MSG_NUM = 1000000;
    private final ByteBuffer rawMessage = new ByteBuffer();
    private Message message = null;
  
    private void run() {
        try {
            LOG.info("StoragePerf started");
            Engine.init();
            message = new Message(MSG);
            rawMessage.clear();
            message.assemble(rawMessage);
            test(SessionStorageType.FileBasedStorage);
            Engine.getInstance().shutdown();
        } catch (final RuntimeException e) {
            LOG.error(e.getMessage(), e);
        }
    }
  
    private void test(final SessionStorageType storageType) {
        LOG.info("Storage type: {}", storageType);
        //
        final StorageRepositoryManager storageRepositoryManager = Engine.getInstance().getStorageRepositoryManager();
        final StorageRepository storageRepository = storageRepositoryManager.get(storageType);
        //
        final SessionId sessionId = new SessionId("A", "B", Version.FIX50_SP2);
        final PrecisionTimer timer = new PrecisionTimer();
        //
        final SessionStorage sessionStorage = storageRepository.create(sessionId, true);
        //
        LOG.info("Storing {} messages...", MSG_NUM);
        timer.start();
        for (int i = 0; MSG_NUM > i; i++) {
            sessionStorage.storeOutboundMessage(rawMessage, message.getSeqNum(), message.isOriginal(), false);
        }
        timer.stop();
        LOG.info("Messages stored in {} msecs.", timer.getElapsedMilliSeconds());
        sessionStorage.close(false);
        //
        LOG.info("Re-opening the session storage...");
        timer.start();
        final SessionStorage sessionStorageReopen = storageRepository.create(sessionId, false);
        timer.stop();
        LOG.info("Session storage is re-opened in {} msecs.", timer.getElapsedMilliSeconds());
        sessionStorageReopen.close(false);
    }
  
    public static void main(final String[] args) {
        (new StoragePerfMeter()).run();
    }
}