Skip to content

#174 Speed up messageId generation - #175

Merged
aluccaroni merged 5 commits into
masterfrom
174-uuid-to-long
Jul 15, 2022
Merged

aluccaroni merged 5 commits into
masterfrom
174-uuid-to-long

Conversation

@dmercuriali

Copy link
Copy Markdown
Contributor

Change messages and replies id from uuid to long to speedup the id generation.
The ID is only stored by the sender of the first request, receivers of the message use the messageID to set the replyMessageID, so a simple AtomicLong per host should suffice.

I kept the (reply)messageID as String to avoid changing a lot of places

@dmercuriali

Copy link
Copy Markdown
Contributor Author

Tests are all green on my end.

@eolivelli @diegosalvi thought on this?

private final ExecutorService callbackexecutor = Executors.newCachedThreadPool();
private final ExecutorService executionserializer = Executors.newFixedThreadPool(1);
private String id = UUID.randomUUID().toString();
private String id = CHANNEL_ID_GENERATOR.incrementAndGet() + "";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ID is only used in the toString. In NettyChannel, the id was already a long

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is only a local variable, not related to the write protocol, you can change it to a "long"
btw generating JVMChannels shouldn't be on the hot paths (read/write), so it is not a big deal to change it

@dmercuriali dmercuriali self-assigned this Jul 6, 2022

@diegosalvi diegosalvi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

private final ExecutorService callbackexecutor = Executors.newCachedThreadPool();
private final ExecutorService executionserializer = Executors.newFixedThreadPool(1);
private String id = UUID.randomUUID().toString();
private String id = CHANNEL_ID_GENERATOR.incrementAndGet() + "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is only a local variable, not related to the write protocol, you can change it to a "long"
btw generating JVMChannels shouldn't be on the hot paths (read/write), so it is not a big deal to change it

public class JVMChannel extends Channel {

private static final Logger LOGGER = Logger.getLogger(JVMChannel.class.getName());
private static final AtomicLong ID_GENERATOR = new AtomicLong();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you using a "static" variable ?
this smells to me because we have two cases (I didn't check the wire protocol):

  1. this ID is unique in the scope of the connection (channel?), then it is better to use an instance field
  2. this ID is unique globally......then having a static field is a bad way to generate this value, because every JVM will have its own counter

I guess we are in case 2), so I would use a simple instance field

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The protocol works like this:

  • jvm1 sends a request_message with messageID=x
  • jvm1 stores the entries <messageID, callback> <messageID, request_message>, this are stored in the channel
  • jvm2 receives request_message and sends a reply_message with replyMessageID=request_message.messageID and messageID=k
  • jvm1 receives the reply_message and reads the replyMessageID to invoke the right callback and stuff

Even if the request_message.messageID is stored at channel level, the whole request_message gets passed to callbacks which comes from outside the channel (eg. see CacheClient#touchEntry). So I think it's good to have request_messages' messageID unique across the jvm channels

The reply messageID can conflict with the messageID of the request, but reply_message.messageID is never used. This can also happen with uuid v4 (with a very lower chance)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the messageId is unique only in the scope of a "Channel" and actually you have two series of ids, once per each side of the connection.

even if the message id is leaked in callback, it is a useless information for the caller

I think that having a "static" id generator is only misleading.

I am trying to find a valid usecase, what would you do with a unique id per-JVM (classloader) ? logging the message id and correlate errors ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes logging and tracking the message. With a local message id we would have to expose and log the channel id too.

Right now there are no such logs, It was just a thought for future. I will make the generator local

private volatile SocketChannel socket;
private static final Logger LOGGER = Logger.getLogger(NettyChannel.class.getName());
private static final AtomicLong idGenerator = new AtomicLong();
private static final AtomicLong ID_GENERATOR = new AtomicLong();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comments on JVM Channel

@dmercuriali

Copy link
Copy Markdown
Contributor Author

Pushed the fix for the static (now local) message id generator
@eolivelli

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Do we have CI somewhere ?

I suggest to setup GitHub actions, in order to have immediate feedback on the goodness of the change.

@dmercuriali

Copy link
Copy Markdown
Contributor Author

Yes we are lacking CI and pre-merge checks. Last week I created #176

@aluccaroni

Copy link
Copy Markdown
Contributor

@dmercuriali see #177

@eolivelli

eolivelli commented Jul 14, 2022 •

Copy link
Copy Markdown
Contributor

@dmercuriali
is this a flaky test ?

2022-07-14T09:55:47.1512303Z [ERROR] Failures: 
2022-07-14T09:55:47.1513118Z [ERROR]   ApparentlyStuckClientDueToServerSideErrorTest.test:78

@dmercuriali

Copy link
Copy Markdown
Contributor Author

Yes, It failed after 15 runs on my end. The test expects a client to not receive an invalidation request, but sometimes the client connection to the cache-server is closed earlier and the test fails because the cache-entry it expects to find is null.

Seems unrelated to this PR

@dmercuriali

Copy link
Copy Markdown
Contributor Author

Now this is failing:

ManagementStatusMXBeanTest.statusMBeanEnabled:154 expected:<1> but was:<2>

Here a client is stopped and then we check the ConnectedClients of the server. The assert fail because the server has yet to do the -1 on the counter.
There are no messages sent to the server in the stop method of the client so this should be unrelated too.

I think this can be merged

@aluccaroni
aluccaroni merged commit a0c0198 into master Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants