Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'toString' method into IdleStateEvent #9695

Merged
merged 3 commits into from
Oct 23, 2019
Merged

Add 'toString' method into IdleStateEvent #9695

merged 3 commits into from
Oct 23, 2019

Conversation

ursaj
Copy link
Contributor

@ursaj ursaj commented Oct 20, 2019

Motivation:

IdleStateEvent is very convenient and frequently used type of events. However both in runtime (logs) and in debug you need some manual steps to see their actual content. Default implementation generates worthless trash like this:

io.netty.handler.timeout.IdleStateEvent@27f674d

There are examples already, where event has convenient and useful toString implementation:

  • io.netty.handler.proxy.ProxyConnectionEvent
  • io.netty.handler.ssl.SslCompletionEvent

Modification:

  • Implement 'IdleStateEvent.toString' method.
  • Unit test.

@netty-bot
Copy link

Can one of the admins verify this patch?

Copy link
Member

@carl-mastrangelo carl-mastrangelo left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -31,6 +32,7 @@

private final IdleState state;
private final boolean first;
private String strVal;
Copy link
Member

Choose a reason for hiding this comment

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

You probably don't need to cache this, since it's unlikely to be reused.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @carl-mastrangelo ... @ursaj can you remove this ?

Copy link
Contributor Author

@ursaj ursaj Oct 21, 2019

Choose a reason for hiding this comment

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

you both are mistaken in the way, how this class is used. there are only 6 shared (re-usable) instances of this class in runtime:

public class IdleStateEvent {
    public static final IdleStateEvent FIRST_READER_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.READER_IDLE, true);
    public static final IdleStateEvent READER_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.READER_IDLE, false);
    public static final IdleStateEvent FIRST_WRITER_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.WRITER_IDLE, true);
    public static final IdleStateEvent WRITER_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.WRITER_IDLE, false);
    public static final IdleStateEvent FIRST_ALL_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.ALL_IDLE, true);
    public static final IdleStateEvent ALL_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.ALL_IDLE, false);

So cached string value is used countless times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same applies to inherited instances (if any):

  • either they are used in the same shared model, so this optimisation is useful;
  • or they are created, used and GC-ed in a short while, so there is no harm in this optimisation.

Copy link
Member

Choose a reason for hiding this comment

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

I disagree here. If it is the case that the same idle events are reused heavily, then it stands to reason that they are shared by multiple event loops. That makes this code racy and will trigger the race detector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@carl-mastrangelo generate strings on each invocation doesn't look good either. any ideas or examples how it should look like?

Copy link
Contributor Author

@ursaj ursaj Oct 24, 2019

Choose a reason for hiding this comment

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

ah, I see. you've already tried to fix it in PR #9705

@normanmaurer normanmaurer added this to the 4.1.43.Final milestone Oct 21, 2019
@@ -31,6 +32,7 @@

private final IdleState state;
private final boolean first;
private String strVal;
Copy link
Member

Choose a reason for hiding this comment

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

I agree with @carl-mastrangelo ... @ursaj can you remove this ?

@normanmaurer
Copy link
Member

@netty-bot test this please

@normanmaurer
Copy link
Member

@netty-bot test this please

@normanmaurer
Copy link
Member

@netty-bot test this please

@normanmaurer normanmaurer merged commit a77fe93 into netty:4.1 Oct 23, 2019
@normanmaurer
Copy link
Member

@ursaj thanks a lot!

@ursaj ursaj deleted the add-tostring-into-idle-state-event branch October 23, 2019 08:39
normanmaurer pushed a commit that referenced this pull request Oct 23, 2019
IdleStateEvent is very convenient and frequently used type of events. However both in runtime (logs) and in debug you need some manual steps to see their actual content. Default implementation generates worthless trash like this:

    io.netty.handler.timeout.IdleStateEvent@27f674d

There are examples already, where event has convenient and useful toString implementation:

* io.netty.handler.proxy.ProxyConnectionEvent
* io.netty.handler.ssl.SslCompletionEvent

* Implement 'IdleStateEvent.toString' method.
* Unit test.

More useful String representation of IdleStateEvent
carl-mastrangelo added a commit to carl-mastrangelo/netty that referenced this pull request Oct 24, 2019
Motivation:

In PR netty#9695   IdleStateEvents
were made to cache their string representation.   The reason for this
was to avoid creating garbage as these values would be used frequently.
However, these objects may be used on multiple event loops and this
may cause an unexpected race to occur.

Modification:
Only make the events that Netty creates cache their toString representation.

Result:
No races.
normanmaurer pushed a commit that referenced this pull request Oct 24, 2019
Motivation:

In PR #9695   IdleStateEvents
were made to cache their string representation.   The reason for this
was to avoid creating garbage as these values would be used frequently.
However, these objects may be used on multiple event loops and this
may cause an unexpected race to occur.

Modification:
Only make the events that Netty creates cache their toString representation.

Result:
No races.
normanmaurer pushed a commit that referenced this pull request Oct 24, 2019
Motivation:

In PR #9695   IdleStateEvents
were made to cache their string representation.   The reason for this
was to avoid creating garbage as these values would be used frequently.
However, these objects may be used on multiple event loops and this
may cause an unexpected race to occur.

Modification:
Only make the events that Netty creates cache their toString representation.

Result:
No races.
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.

None yet

4 participants