JdbcPersistenceManager findMessage(): wrong messages order after fetch
Opened this issue · 1 comments
Hi everyone,
I've found a possible issue about messages fetching (I tested it in version 1.7.0 and it's still present in the last release).
The JdbcPersistenceManager declares a SELECT_MESSAGES query for message retrieval and appends to it, in findMessages() method, an "order by MESSAGE_SENT_DATE" clause in order to sort messages in chronological order.
But after the following piece of code (in findMessages() method),
ArchivedMessage archivedMessage = new ArchivedMessage(time, null, null, null, sid);
archivedMessage.setId(id);
archivedMessage.setStanza(stanza);
archivedMessages.put(archivedMessage.getId(), archivedMessage);
the order is wrong because each message, fetched by the query, is putted in a TreeMap (ArchivedMessage)
final TreeMap<Long, ArchivedMessage> archivedMessages = new TreeMap<Long, ArchivedMessage>();
that is indexed and sorted by messageId value.
Even if is difficult that messages have date and id not aligned, it happens in a cluster configuration.
I'll try to explain a possible cause.
If two clients are connected on different nodes of a cluster, sometimes is possible that the server that should route markers (for example received) is faster than the others (which should send messages).
In that case, even if the received marker is obviously more recent compared to the message that generate it, these are in reversed order in the above TreeMap.
In effect, in ofMessagesArchive table, the received marker has a lower id than its message, but the sentDate is more recent.
I hope I made myself clear.
Thanks.