Skip to content

Commit

Permalink
ManagedSelector dump improvements from #1970 (#2062)
Browse files Browse the repository at this point in the history
* ManagedSelector dump improvements from #1970

ManagedSelector dump improvements from #1970:
 + DumpKeys is now prepended to actions list so it is less likely to be delayed by a stuck/busy selector
 + Timestamps are included for actions and keys which may be separated by time
 + Race removed race for updating dumpKey list while it is being added to.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* moved utility timestamp format method to Log

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* updates after review

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* changes after review

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue #1970 - ManagedSelector dump improvements.

Code cleanups.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>

* Revert "Issue #1970 - ManagedSelector dump improvements."

This reverts commit 4febaf1.

* Fixed imports and other review feedback

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Jan 2, 2018
1 parent e86e8a7 commit 54c55b6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
64 changes: 38 additions & 26 deletions jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -64,7 +66,7 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable

private final Locker _locker = new Locker();
private boolean _selecting = false;
private final Queue<Runnable> _actions = new ArrayDeque<>();
private final Deque<Runnable> _actions = new ArrayDeque<>();
private final SelectorManager _selectorManager;
private final int _id;
private final ExecutionStrategy _strategy;
Expand Down Expand Up @@ -244,20 +246,31 @@ private int getActionSize()
@Override
public void dump(Appendable out, String indent) throws IOException
{
super.dump(out, indent);
Selector selector = _selector;
List<String> keys = null;
List<Runnable> actions = null;
if (selector != null && selector.isOpen())
{
List<Runnable> actions;
DumpKeys dump = new DumpKeys();
String actionsAt;
try (Locker.Lock lock = _locker.lock())
{
actionsAt = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now());
actions = new ArrayList<>(_actions);
_actions.addFirst(dump);
_selecting = false;
}
List<Object> keys = new ArrayList<>(selector.keys().size());
DumpKeys dumpKeys = new DumpKeys(keys);
submit(dumpKeys);
dumpKeys.await(5, TimeUnit.SECONDS);
dump(out, indent, Arrays.asList(new DumpableCollection("keys", keys), new DumpableCollection("actions", actions)));
selector.wakeup();
keys = dump.get(5, TimeUnit.SECONDS);
String keysAt = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now());
if (keys==null)
keys = Collections.singletonList("No dump keys retrieved");
dumpBeans(out, indent, Arrays.asList(new DumpableCollection("actions @ "+actionsAt, actions),
new DumpableCollection("keys @ "+keysAt, keys)));
}
else
{
dumpBeans(out, indent);
}
}

Expand Down Expand Up @@ -495,47 +508,46 @@ public String toString()

private class DumpKeys extends Invocable.NonBlocking
{
private final CountDownLatch latch = new CountDownLatch(1);
private final List<Object> _dumps;

private DumpKeys(List<Object> dumps)
{
this._dumps = dumps;
}

private CountDownLatch latch = new CountDownLatch(1);
private List<String> keys;

@Override
public void run()
{
Selector selector = _selector;
if (selector != null && selector.isOpen())
{
Set<SelectionKey> keys = selector.keys();
_dumps.add(selector + " keys=" + keys.size());
for (SelectionKey key : keys)
{
Set<SelectionKey> selector_keys = selector.keys();
List<String> list = new ArrayList<>(selector_keys.size()+1);
list.add(selector + " keys=" + selector_keys.size());
for (SelectionKey key : selector_keys)
{
try
{
_dumps.add(String.format("SelectionKey@%x{i=%d}->%s", key.hashCode(), key.interestOps(), key.attachment()));
list.add(String.format("SelectionKey@%x{i=%d}->%s", key.hashCode(), key.interestOps(), key.attachment()));
}
catch (Throwable x)
{
_dumps.add(String.format("SelectionKey@%x[%s]->%s", key.hashCode(), x, key.attachment()));
list.add(String.format("SelectionKey@%x[%s]->%s", key.hashCode(), x, key.attachment()));
}
}
keys = list;
}

latch.countDown();
}

public boolean await(long timeout, TimeUnit unit)
public List<String> get(long timeout, TimeUnit unit)
{
try
{
return latch.await(timeout, unit);
latch.await(timeout, unit);
}
catch (InterruptedException x)
{
return false;
LOG.ignore(x);
}
return keys;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import java.io.IOException;
import java.util.Collection;

import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.Dumpable;

public class DumpableCollection implements Dumpable
{
private final String _name;
Expand All @@ -34,7 +31,7 @@ public DumpableCollection(String name,Collection<?> collection)
_name=name;
_collection=collection;
}

@Override
public String dump()
{
Expand All @@ -44,7 +41,7 @@ public String dump()
@Override
public void dump(Appendable out, String indent) throws IOException
{
out.append(_name).append("\n");
out.append(_name).append(System.lineSeparator());
if (_collection!=null)
ContainerLifeCycle.dump(out,indent,_collection);
}
Expand Down

0 comments on commit 54c55b6

Please sign in to comment.