|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.internal.async; |
| 20 | + |
| 21 | +import io.netty.bootstrap.Bootstrap; |
| 22 | +import io.netty.channel.Channel; |
| 23 | +import io.netty.channel.EventLoopGroup; |
| 24 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 25 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 26 | +import io.netty.util.concurrent.DefaultThreadFactory; |
| 27 | +import io.netty.util.concurrent.FastThreadLocalThread; |
| 28 | + |
| 29 | +import java.util.concurrent.Executor; |
| 30 | +import java.util.concurrent.Future; |
| 31 | +import java.util.concurrent.ThreadFactory; |
| 32 | + |
| 33 | +import org.neo4j.driver.v1.Session; |
| 34 | + |
| 35 | +/** |
| 36 | + * Manages creation of Netty {@link EventLoopGroup}s, which are basically {@link Executor}s that perform IO operations. |
| 37 | + */ |
| 38 | +public final class EventLoopGroupFactory |
| 39 | +{ |
| 40 | + private static final String THREAD_NAME_PREFIX = "Neo4jDriverIO"; |
| 41 | + private static final int THREAD_PRIORITY = Thread.MAX_PRIORITY; |
| 42 | + |
| 43 | + private EventLoopGroupFactory() |
| 44 | + { |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get class of {@link Channel} for {@link Bootstrap#channel(Class)} method. |
| 49 | + * |
| 50 | + * @return class of the channel, which should be consistent with {@link EventLoopGroup}s returned by |
| 51 | + * {@link #newEventLoopGroup()} and {@link #newEventLoopGroup(int)}. |
| 52 | + */ |
| 53 | + public static Class<? extends Channel> channelClass() |
| 54 | + { |
| 55 | + return NioSocketChannel.class; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Create new {@link EventLoopGroup} with specified thread count. Returned group should by given to |
| 60 | + * {@link Bootstrap#group(EventLoopGroup)}. |
| 61 | + * |
| 62 | + * @param threadCount amount of IO threads for the new group. |
| 63 | + * @return new group consistent with channel class returned by {@link #channelClass()}. |
| 64 | + */ |
| 65 | + public static EventLoopGroup newEventLoopGroup( int threadCount ) |
| 66 | + { |
| 67 | + return new DriverEventLoopGroup( threadCount ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Create new {@link EventLoopGroup} with default thread count. Returned group should by given to |
| 72 | + * {@link Bootstrap#group(EventLoopGroup)}. |
| 73 | + * |
| 74 | + * @return new group consistent with channel class returned by {@link #channelClass()}. |
| 75 | + */ |
| 76 | + public static EventLoopGroup newEventLoopGroup() |
| 77 | + { |
| 78 | + return new DriverEventLoopGroup(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Assert that current thread is not an event loop used for async IO operations. This check is needed because |
| 83 | + * blocking API methods like {@link Session#run(String)} are implemented on top of corresponding async API methods |
| 84 | + * like {@link Session#runAsync(String)} using basically {@link Future#get()} calls. Deadlocks might happen when IO |
| 85 | + * thread executes blocking API call and has to wait for itself to read from the network. |
| 86 | + * |
| 87 | + * @throws IllegalStateException when current thread is an event loop IO thread. |
| 88 | + */ |
| 89 | + public static void assertNotInEventLoopThread() throws IllegalStateException |
| 90 | + { |
| 91 | + if ( Thread.currentThread() instanceof DriverThread ) |
| 92 | + { |
| 93 | + throw new IllegalStateException( |
| 94 | + "Blocking operation can't be executed in IO thread because it might result in a deadlock. " + |
| 95 | + "Please do not use blocking API when chaining futures returned by async API methods." ); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Same as {@link NioEventLoopGroup} but uses a different {@link ThreadFactory} that produces threads of |
| 101 | + * {@link DriverThread} class. Such threads can be recognized by {@link #assertNotInEventLoopThread()}. |
| 102 | + */ |
| 103 | + private static class DriverEventLoopGroup extends NioEventLoopGroup |
| 104 | + { |
| 105 | + DriverEventLoopGroup() |
| 106 | + { |
| 107 | + } |
| 108 | + |
| 109 | + DriverEventLoopGroup( int nThreads ) |
| 110 | + { |
| 111 | + super( nThreads ); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected ThreadFactory newDefaultThreadFactory() |
| 116 | + { |
| 117 | + return new DriverThreadFactory(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Same as {@link DefaultThreadFactory} created by {@link NioEventLoopGroup} by default, except produces threads of |
| 123 | + * {@link DriverThread} class. Such threads can be recognized by {@link #assertNotInEventLoopThread()}. |
| 124 | + */ |
| 125 | + private static class DriverThreadFactory extends DefaultThreadFactory |
| 126 | + { |
| 127 | + DriverThreadFactory() |
| 128 | + { |
| 129 | + super( THREAD_NAME_PREFIX, THREAD_PRIORITY ); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + protected Thread newThread( Runnable r, String name ) |
| 134 | + { |
| 135 | + return new DriverThread( threadGroup, r, name ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Same as default thread created by {@link DefaultThreadFactory} except this dedicated class can be easily |
| 141 | + * recognized by {@link #assertNotInEventLoopThread()}. |
| 142 | + */ |
| 143 | + private static class DriverThread extends FastThreadLocalThread |
| 144 | + { |
| 145 | + DriverThread( ThreadGroup group, Runnable target, String name ) |
| 146 | + { |
| 147 | + super( group, target, name ); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments