-
Notifications
You must be signed in to change notification settings - Fork 155
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
Prohibit blocking operations in IO threads #434
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
driver/src/main/java/org/neo4j/driver/internal/async/EventLoopGroupFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.driver.internal.async; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import io.netty.util.concurrent.DefaultThreadFactory; | ||
import io.netty.util.concurrent.FastThreadLocalThread; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
import org.neo4j.driver.v1.Session; | ||
|
||
/** | ||
* Manages creation of Netty {@link EventLoopGroup}s, which are basically {@link Executor}s that perform IO operations. | ||
*/ | ||
public final class EventLoopGroupFactory | ||
{ | ||
private static final String THREAD_NAME_PREFIX = "Neo4jDriverIO"; | ||
private static final int THREAD_PRIORITY = Thread.MAX_PRIORITY; | ||
|
||
private EventLoopGroupFactory() | ||
{ | ||
} | ||
|
||
/** | ||
* Get class of {@link Channel} for {@link Bootstrap#channel(Class)} method. | ||
* | ||
* @return class of the channel, which should be consistent with {@link EventLoopGroup}s returned by | ||
* {@link #newEventLoopGroup()} and {@link #newEventLoopGroup(int)}. | ||
*/ | ||
public static Class<? extends Channel> channelClass() | ||
{ | ||
return NioSocketChannel.class; | ||
} | ||
|
||
/** | ||
* Create new {@link EventLoopGroup} with specified thread count. Returned group should by given to | ||
* {@link Bootstrap#group(EventLoopGroup)}. | ||
* | ||
* @param threadCount amount of IO threads for the new group. | ||
* @return new group consistent with channel class returned by {@link #channelClass()}. | ||
*/ | ||
public static EventLoopGroup newEventLoopGroup( int threadCount ) | ||
{ | ||
return new DriverEventLoopGroup( threadCount ); | ||
} | ||
|
||
/** | ||
* Create new {@link EventLoopGroup} with default thread count. Returned group should by given to | ||
* {@link Bootstrap#group(EventLoopGroup)}. | ||
* | ||
* @return new group consistent with channel class returned by {@link #channelClass()}. | ||
*/ | ||
public static EventLoopGroup newEventLoopGroup() | ||
{ | ||
return new DriverEventLoopGroup(); | ||
} | ||
|
||
/** | ||
* Assert that current thread is not an event loop used for async IO operations. This check is needed because | ||
* blocking API methods like {@link Session#run(String)} are implemented on top of corresponding async API methods | ||
* like {@link Session#runAsync(String)} using basically {@link Future#get()} calls. Deadlocks might happen when IO | ||
* thread executes blocking API call and has to wait for itself to read from the network. | ||
* | ||
* @throws IllegalStateException when current thread is an event loop IO thread. | ||
*/ | ||
public static void assertNotInEventLoopThread() throws IllegalStateException | ||
{ | ||
if ( Thread.currentThread() instanceof DriverThread ) | ||
{ | ||
throw new IllegalStateException( | ||
"Blocking operation can't be executed in IO thread because it might result in a deadlock. " + | ||
"Please do not use blocking API when chaining futures returned by async API methods." ); | ||
} | ||
} | ||
|
||
/** | ||
* Same as {@link NioEventLoopGroup} but uses a different {@link ThreadFactory} that produces threads of | ||
* {@link DriverThread} class. Such threads can be recognized by {@link #assertNotInEventLoopThread()}. | ||
*/ | ||
private static class DriverEventLoopGroup extends NioEventLoopGroup | ||
{ | ||
DriverEventLoopGroup() | ||
{ | ||
} | ||
|
||
DriverEventLoopGroup( int nThreads ) | ||
{ | ||
super( nThreads ); | ||
} | ||
|
||
@Override | ||
protected ThreadFactory newDefaultThreadFactory() | ||
{ | ||
return new DriverThreadFactory(); | ||
} | ||
} | ||
|
||
/** | ||
* Same as {@link DefaultThreadFactory} created by {@link NioEventLoopGroup} by default, except produces threads of | ||
* {@link DriverThread} class. Such threads can be recognized by {@link #assertNotInEventLoopThread()}. | ||
*/ | ||
private static class DriverThreadFactory extends DefaultThreadFactory | ||
{ | ||
DriverThreadFactory() | ||
{ | ||
super( THREAD_NAME_PREFIX, THREAD_PRIORITY ); | ||
} | ||
|
||
@Override | ||
protected Thread newThread( Runnable r, String name ) | ||
{ | ||
return new DriverThread( threadGroup, r, name ); | ||
} | ||
} | ||
|
||
/** | ||
* Same as default thread created by {@link DefaultThreadFactory} except this dedicated class can be easily | ||
* recognized by {@link #assertNotInEventLoopThread()}. | ||
*/ | ||
private static class DriverThread extends FastThreadLocalThread | ||
{ | ||
DriverThread( ThreadGroup group, Runnable target, String name ) | ||
{ | ||
super( group, target, name ); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should stick with
Thread.NORM_PRIORITY
. Maybe we can depend on a system property to override the default, if such a requirement arises.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAX_PRIORITY
is what netty uses inside. Here I'm basically duplicating default behaviour just to create threads of our own class. Such thread can then be easily detected with simpleinstanceof
.