@Test public void testTryWithResourcesShouldCloseAllClustersButNotTimerIfProvided() throws Exception { EventLoopGroup eventLoop; Timer timer = new HashedWheelTimer(); try (Server server = Server.builder().withTimer(timer).build()) { // Do nothing here, since this is a unit test, we don't want to create any inet sockets // which is what Server does by default. eventLoop = server.eventLoopGroup; } // event loop should have been closed since a custom one was not provided. assertThat(eventLoop.isShutdown()).isTrue(); // timer should not have been closed since a custom one was provided. timer.newTimeout( timeout -> { // noop }, 1, TimeUnit.SECONDS); timer.stop(); }
/** * 生成默认的httpclient config * * @return the config */ public static AsyncHttpClientConfig createConfig(int connectTimeout, int requestTimeout) { HashedWheelTimer timer = new HashedWheelTimer(); timer.start(); DefaultChannelPool channelPool = new DefaultChannelPool(60000, -1, DefaultChannelPool.PoolLeaseStrategy.LIFO, timer, 3000); return new DefaultAsyncHttpClientConfig.Builder() .setConnectTimeout(connectTimeout) .setRequestTimeout(requestTimeout) .setMaxConnectionsPerHost(10000) .setValidateResponseHeaders(false) .setMaxRequestRetry(0) .setChannelPool(channelPool) .build(); }
protected void initTimer(MasterSlaveServersConfig config) { int[] timeouts = new int[]{config.getRetryInterval(), config.getTimeout(), config.getReconnectionTimeout()}; Arrays.sort(timeouts); int minTimeout = timeouts[0]; if (minTimeout % 100 != 0) { minTimeout = (minTimeout % 100) / 2; } else if (minTimeout == 100) { minTimeout = 50; } else { minTimeout = 100; } timer = new HashedWheelTimer(Executors.defaultThreadFactory(), minTimeout, TimeUnit.MILLISECONDS, 1024); // to avoid assertion error during timer.stop invocation try { Field leakField = HashedWheelTimer.class.getDeclaredField("leak"); leakField.setAccessible(true); leakField.set(timer, null); } catch (Exception e) { throw new IllegalStateException(e); } connectionWatcher = new IdleConnectionWatcher(this, config); }
public void init() throws SyncException { cg = new DefaultChannelGroup("Cluster Bootstrap", GlobalEventExecutor.INSTANCE); workerExecutor = new NioEventLoopGroup(); timer = new HashedWheelTimer(); bootstrap = new Bootstrap() .group(workerExecutor) .channel(NioSocketChannel.class) .option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_SNDBUF, RPCService.SEND_BUFFER_SIZE) .option(ChannelOption.SO_RCVBUF, RPCService.SEND_BUFFER_SIZE) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, RPCService.CONNECT_TIMEOUT); pipelineFactory = new BootstrapChannelInitializer(timer, this); bootstrap.handler(pipelineFactory); }
@Override public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { shutdown = false; workerExecutor = new NioEventLoopGroup(); timer = new HashedWheelTimer(); pipelineFactory = new RemoteSyncChannelInitializer(timer, this); final Bootstrap bootstrap = new Bootstrap() .channel(NioSocketChannel.class) .group(workerExecutor) .option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_SNDBUF, RPCService.SEND_BUFFER_SIZE) .option(ChannelOption.SO_RCVBUF, RPCService.SEND_BUFFER_SIZE) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, RPCService.CONNECT_TIMEOUT) .handler(pipelineFactory); clientBootstrap = bootstrap; }
@Override protected void doStart(Listener listener) throws Throwable { workerGroup = new NioEventLoopGroup(http_work, new DefaultThreadFactory(ThreadNames.T_HTTP_CLIENT)); b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_REUSEADDR, true); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new HttpResponseDecoder()); ch.pipeline().addLast("aggregator", new HttpObjectAggregator(maxContentLength)); ch.pipeline().addLast("encoder", new HttpRequestEncoder()); ch.pipeline().addLast("handler", new HttpClientHandler(NettyHttpClient.this)); } }); timer = new HashedWheelTimer(new NamedThreadFactory(T_HTTP_TIMER), 1, TimeUnit.SECONDS, 64); listener.onSuccess(); }
public PulsarClientImpl(String serviceUrl, ClientConfiguration conf, EventLoopGroup eventLoopGroup, ConnectionPool cnxPool) throws PulsarClientException { if (isBlank(serviceUrl) || conf == null || eventLoopGroup == null) { throw new PulsarClientException.InvalidConfigurationException("Invalid client configuration"); } this.eventLoopGroup = eventLoopGroup; this.conf = conf; conf.getAuthentication().start(); this.cnxPool = cnxPool; if (serviceUrl.startsWith("http")) { lookup = new HttpLookupService(serviceUrl, conf, eventLoopGroup); } else { lookup = new BinaryProtoLookupService(this, serviceUrl, conf.isUseTls()); } timer = new HashedWheelTimer(new DefaultThreadFactory("pulsar-timer"), 1, TimeUnit.MILLISECONDS); externalExecutorProvider = new ExecutorProvider(conf.getListenerThreads(), "pulsar-external-listener"); producers = Maps.newIdentityHashMap(); consumers = Maps.newIdentityHashMap(); state.set(State.Open); }
/** * * @param handler Called with a request when all parts are received or it expires. * @param numParts number of parts for each request * @param timeoutDuration Expiration time for incomplete requests * @param unit time unit for timeoutDuration * @param timeoutMaxError a value in range (0 and 1], where the timeout might happen at time timeoutDuration + timeoutMaxError*timeoutDuration. A larger value means a more efficient data structure. */ public HashedWheelGatherer(RequestHandler<T> handler, int numParts, long timeoutDuration, TimeUnit unit, double timeoutMaxError) { if (timeoutMaxError <= 0 || timeoutMaxError > 1) { throw new IllegalArgumentException(String.format("timeoutMaxError must be in range (0, 1] (got %f)", timeoutMaxError)); } timeoutDurationNs = unit.toNanos(timeoutDuration); inflightRequests = new ConcurrentHashMap<>(); // create the wheel timer int numSteps = (int)Math.round(1. / timeoutMaxError); long tickDurationNs = Math.max(unit.toNanos(timeoutDuration) / numSteps, 1); hashedWheelTimer = new HashedWheelTimer(r -> { // Use daemon threads Thread t = Executors.defaultThreadFactory().newThread(r); t.setDaemon(true); return t; }, tickDurationNs, TimeUnit.NANOSECONDS, numSteps); hashedWheelTimer.start(); this.numParts = numParts; this.handler = handler; }
/** * 私有构造函数 */ private NettyCenter() { int maybeThreadSize = Runtime.getRuntime().availableProcessors(); if (maybeThreadSize == 1) maybeThreadSize += 2; else if (maybeThreadSize == 8) maybeThreadSize = 2; else if (maybeThreadSize > 8) maybeThreadSize /= 2; /** * 构造事件循环组 */ eventLoopGroup = new NioEventLoopGroup(maybeThreadSize, new DefaultThreadFactory("NettyNioLoopGroup")); /** * 构造定时器 */ hashedWheelTimer = new HashedWheelTimer(new DefaultThreadFactory("NettyHashedWheelTimer")); /** * 构造 SSL 环境 */ try { SslContextBuilder sslContextBuilder = SslContextBuilder.forClient(); sslContextBuilder.clientAuth(ClientAuth.OPTIONAL); simpleClientSslContext = sslContextBuilder.build(); } catch (Throwable e) { log.error("NettyCenter :: initialize client sslcontext error!", e); } }
private void setup() { MetricsRegistry registry = new MetricsRegistry(); _timedExecutor = new ScheduledThreadPoolExecutor(1); _service = new ThreadPoolExecutor(10, 10, 10, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>()); _eventLoopGroup = new NioEventLoopGroup(10); _timer = new HashedWheelTimer(); NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_"); PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(_eventLoopGroup, _timer, clientMetrics); _pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>(1, _maxActiveConnections, 300000, 10, rm, _timedExecutor, MoreExecutors.sameThreadExecutor(), registry); rm.setPool(_pool); _scatterGather = new ScatterGatherImpl(_pool, _service); for (AsyncReader r : _readerThreads) { r.start(); } }
public EtherNetIpClientConfig(String hostname, int port, int vendorId, int serialNumber, Duration timeout, ExecutorService executor, EventLoopGroup eventLoop, HashedWheelTimer wheelTimer, Consumer<Bootstrap> bootstrapConsumer) { this.hostname = hostname; this.port = port; this.vendorId = vendorId; this.serialNumber = serialNumber; this.timeout = timeout; this.executor = executor; this.eventLoop = eventLoop; this.wheelTimer = wheelTimer; this.bootstrapConsumer = bootstrapConsumer; }
public ModbusTcpMasterConfig(String address, int port, Duration timeout, @Deprecated boolean autoConnect, Optional<String> instanceId, ExecutorService executor, EventLoopGroup eventLoop, HashedWheelTimer wheelTimer, Consumer<Bootstrap> bootstrapConsumer) { this.address = address; this.port = port; this.timeout = timeout; this.autoConnect = autoConnect; this.instanceId = instanceId; this.executor = executor; this.eventLoop = eventLoop; this.wheelTimer = wheelTimer; this.bootstrapConsumer = bootstrapConsumer; }
/** * Create a new instance with client resources. * * @param clientResources the client resources. If {@literal null}, the client will create a new dedicated instance of * client resources and keep track of them. */ protected AbstractRedisClient(ClientResources clientResources) { if (clientResources == null) { sharedResources = false; this.clientResources = DefaultClientResources.create(); } else { sharedResources = true; this.clientResources = clientResources; } unit = TimeUnit.SECONDS; genericWorkerPool = this.clientResources.eventExecutorGroup(); channels = new DefaultChannelGroup(genericWorkerPool.next()); timer = (HashedWheelTimer) this.clientResources.timer(); }
@BeforeMethod public void setUp() throws Exception { _countDownLatch = new CountDownLatch(1); NettyTestUtils.LatchControlledRequestHandler requestHandler = new NettyTestUtils.LatchControlledRequestHandler(_countDownLatch); requestHandler.setResponse(NettyTestUtils.DUMMY_RESPONSE); NettyTestUtils.LatchControlledRequestHandlerFactory handlerFactory = new NettyTestUtils.LatchControlledRequestHandlerFactory(requestHandler); _nettyTCPServer = new NettyTCPServer(NettyTestUtils.DEFAULT_PORT, handlerFactory, null); Thread serverThread = new Thread(_nettyTCPServer, "NettyTCPServer"); serverThread.start(); // Wait for at most 10 seconds for server to start NettyTestUtils.waitForServerStarted(_nettyTCPServer, 10 * 1000L); ServerInstance clientServer = new ServerInstance("localhost", NettyTestUtils.DEFAULT_PORT); _nettyTCPClientConnection = new NettyTCPClientConnection(clientServer, new NioEventLoopGroup(), new HashedWheelTimer(), new NettyClientMetrics(null, "abc")); }
@BeforeMethod public void setUp() throws Exception { _requestHandler = new NettyTestUtils.LatchControlledRequestHandler(null); _requestHandler.setResponse(NettyTestUtils.DUMMY_RESPONSE); NettyTestUtils.LatchControlledRequestHandlerFactory handlerFactory = new NettyTestUtils.LatchControlledRequestHandlerFactory(_requestHandler); _nettyTCPServer = new NettyTCPServer(NettyTestUtils.DEFAULT_PORT, handlerFactory, null); Thread serverThread = new Thread(_nettyTCPServer, "NettyTCPServer"); serverThread.start(); // Wait for at most 10 seconds for server to start NettyTestUtils.waitForServerStarted(_nettyTCPServer, 10 * 1000L); _clientServer = new ServerInstance("localhost", NettyTestUtils.DEFAULT_PORT); _nettyTCPClientConnection = new NettyTCPClientConnection(_clientServer, new NioEventLoopGroup(), new HashedWheelTimer(), new NettyClientMetrics(null, "abc")); }
private void setup() { MetricsRegistry registry = new MetricsRegistry(); _timedExecutor = new ScheduledThreadPoolExecutor(1); _service = new ThreadPoolExecutor(10, 10, 10, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>()); _eventLoopGroup = new NioEventLoopGroup(10); _timer = new HashedWheelTimer(); NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_"); PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(_eventLoopGroup, _timer, clientMetrics); _pool = new KeyedPoolImpl<PooledNettyClientResourceManager.PooledClientConnection>(1, _maxActiveConnections, 300000, 10, rm, _timedExecutor, MoreExecutors.sameThreadExecutor(), registry); rm.setPool(_pool); _scatterGather = new ScatterGatherImpl(_pool, _service); for (AsyncReader r : _readerThreads) { r.start(); } }
/** * Starts Socket.IO server with current configuration settings. * * @throws IllegalStateException * if server already started */ public synchronized void start() { if (isStarted()) { throw new IllegalStateException("Failed to start Socket.IO server: server already started"); } log.info("Socket.IO server starting"); // Configure heartbeat scheduler timer = new HashedWheelTimer(); timer.start(); SocketIOHeartbeatScheduler.setHashedWheelTimer(timer); SocketIOHeartbeatScheduler.setHeartbeatInterval(configuration.getHeartbeatInterval()); SocketIOHeartbeatScheduler.setHeartbeatTimeout(configuration.getHeartbeatTimeout()); // Configure and bind server ServerBootstrapFactory bootstrapFactory = serverBootstrapFactory != null ? serverBootstrapFactory : new DefaultServerBootstrapFactory(configuration); bootstrap = bootstrapFactory.createServerBootstrap(); bootstrap.childHandler(new SocketIOChannelInitializer(configuration, listener, pipelineModifier)); bootstrap.bind(configuration.getPort()).syncUninterruptibly(); state = State.STARTED; log.info("Socket.IO server started: {}", configuration); }
@Test public void testTryWithResourcesShouldCloseAllClustersButNotEventLoopAndTimerIfProvided() throws Exception { EventLoopGroup eventLoop = new DefaultEventLoopGroup(); Timer timer = new HashedWheelTimer(); BoundCluster cluster; MockClient client; try (Server server = Server.builder() .withAddressResolver(localAddressResolver) .withTimer(timer) .withEventLoopGroup(eventLoop, LocalServerChannel.class) .build()) { cluster = server.register(ClusterSpec.builder().withNodes(5)); BoundNode node = cluster.node(0); SocketAddress address = node.getAddress(); client = new MockClient(eventLoop); client.connect(address); } // event loop should not have been closed. assertThat(eventLoop.isShutdown()).isFalse(); // timer should not have since a custom one was not provided. cluster .getServer() .timer .newTimeout( timeout -> { // noop }, 1, TimeUnit.SECONDS); eventLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS); timer.stop(); }
/** * Initialize internal data structures */ public void init(Map<String, String> configParams) throws FloodlightModuleException { this.moduleLoaderState = ModuleLoaderState.INIT; // These data structures are initialized here because other // module's startUp() might be called before ours this.messageListeners = new ConcurrentHashMap<OFType, ListenerDispatcher<OFType, IOFMessageListener>>(); this.haListeners = new ListenerDispatcher<HAListenerTypeMarker, IHAListener>(); this.controllerNodeIPsCache = new HashMap<String, String>(); this.updates = new LinkedBlockingQueue<IUpdate>(); this.providerMap = new HashMap<String, List<IInfoProvider>>(); this.completionListeners = new ConcurrentLinkedQueue<IControllerCompletionListener>(); setConfigParams(configParams); HARole initialRole = getInitialRole(configParams); this.notifiedRole = initialRole; this.shutdownService = new ShutdownServiceImpl(); this.roleManager = new RoleManager(this, this.shutdownService, this.notifiedRole, INITIAL_ROLE_CHANGE_DESCRIPTION); this.timer = new HashedWheelTimer(); // Switch Service Startup this.switchService.registerLogicalOFMessageCategory(LogicalOFMessageCategory.MAIN); this.switchService.addOFSwitchListener(new NotificationSwitchListener()); this.counters = new ControllerCounters(debugCounterService); }
@Before public void setUp() throws Exception { factory = OFFactories.getFactory(OFVersion.OF_13); switchId = DatapathId.of(1); timer = new HashedWheelTimer(); channel = EasyMock.createMock(Channel.class); IDebugCounterService debugCounterService = new DebugCounterServiceImpl(); debugCounterService.registerModule(OFConnectionCounters.COUNTER_MODULE); conn = new OFConnection(switchId, factory, channel, OFAuxId.MAIN, debugCounterService, timer); eventLoop = new TestEventLoop(); expect(channel.eventLoop()).andReturn(eventLoop).anyTimes(); }
public static HashedWheelTimerCloseable newInstance(@Nullable final ThreadFactory threadFactory, @Nullable final Long duration, @Nullable final Integer ticksPerWheel) { TimeUnit unit = TimeUnit.MILLISECONDS; if(!nullOrNonPositive(duration) && threadFactory == null && nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit)); } if(!nullOrNonPositive(duration) && threadFactory == null && !nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit, ticksPerWheel)); } if(nullOrNonPositive(duration) && threadFactory != null && nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable(new HashedWheelTimer(threadFactory)); } if(!nullOrNonPositive(duration) && threadFactory != null && nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable( new HashedWheelTimer(threadFactory, duration, unit)); } if(!nullOrNonPositive(duration) && threadFactory != null && !nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable( new HashedWheelTimer(threadFactory, duration, unit, ticksPerWheel)); } return new HashedWheelTimerCloseable(new HashedWheelTimer()); }
@Override public void init() { if (heartbeatCheck) { long tickDuration = TimeUnit.SECONDS.toMillis(1);//1s 每秒钟走一步,一个心跳周期内大致走一圈 int ticksPerWheel = (int) (CC.mp.core.max_heartbeat / tickDuration); this.timer = new HashedWheelTimer( new NamedThreadFactory(ThreadNames.T_CONN_TIMER), tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel ); } }
@Override public EnsemblePlacementPolicy initialize(ClientConfiguration conf, Optional<DNSToSwitchMapping> optionalDnsResolver, HashedWheelTimer hashedWheelTimer, FeatureProvider featureProvider, StatsLogger statsLogger) { return this; }
private TimeoutService() { tickDuration = ConfigurationHelper.getLongSystemThenEnvProperty(CONFIG_TICK_DURATION, DEFAULT_TICK_DURATION); tickCount = ConfigurationHelper.getIntSystemThenEnvProperty(CONFIG_TICK_COUNT, DEFAULT_TICK_COUNT); timer = new HashedWheelTimer(this, tickDuration, TimeUnit.MILLISECONDS, tickCount); timer.start(); JMXHelper.registerMBean(this, OBJECT_NAME); log.info("TimeoutService started"); }