Skip to content

Add 'toString' method into IdleStateEvent#9695

Merged
normanmaurer merged 3 commits intonetty:4.1from
ursaj:add-tostring-into-idle-state-event
Oct 23, 2019
Merged

Add 'toString' method into IdleStateEvent#9695
normanmaurer merged 3 commits intonetty:4.1from
ursaj:add-tostring-into-idle-state-event

Conversation

@ursaj
Copy link
Copy Markdown
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
Copy Markdown

Can one of the admins verify this patch?

Copy link
Copy Markdown
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


private final IdleState state;
private final boolean first;
private String strVal;
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
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 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

private final IdleState state;
private final boolean first;
private String strVal;
Copy link
Copy Markdown
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
Copy Markdown
Member

@netty-bot test this please

@normanmaurer
Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer
Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer normanmaurer merged commit a77fe93 into netty:4.1 Oct 23, 2019
@normanmaurer
Copy link
Copy Markdown
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.

4 participants