Example usage for io.netty.util HashedWheelTimer HashedWheelTimer

List of usage examples for io.netty.util HashedWheelTimer HashedWheelTimer

Introduction

In this page you can find the example usage for io.netty.util HashedWheelTimer HashedWheelTimer.

Prototype

public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel) 

Source Link

Document

Creates a new timer.

Usage

From source file:com.baidu.jprotobuf.pbrpc.transport.RpcClient.java

License:Apache License

private static Timer createTimer() {
    Timer timer = new HashedWheelTimer(Executors.defaultThreadFactory(), DEFAULT_TICK_DURATION,
            TimeUnit.MILLISECONDS, DEFAULT_TICKS_PER_WHEEL);
    return timer;
}

From source file:com.heliosapm.streams.collector.timeout.TimeoutService.java

License:Apache License

private TimeoutService() {
    tickDuration = ConfigurationHelper.getLongSystemThenEnvProperty(CONFIG_TICK_DURATION,
            DEFAULT_TICK_DURATION);// w w w  .j  ava2  s  .c  om
    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");
}

From source file:com.mpush.core.server.ServerConnectionManager.java

License:Apache License

@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);
    }/*from   w  w w .jav  a2s .  c  o  m*/
}

From source file:com.mpush.netty.http.NettyHttpClient.java

License:Apache License

@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);/*from w  ww  .  ja v  a 2 s  . c o m*/
    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();
}

From source file:com.turn.gatherer.HashedWheelGatherer.java

License:Apache License

/**
 *
 * @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.
 *//* w w  w  . j a v  a  2  s . c om*/
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;
}

From source file:org.apache.bookkeeper.client.BookKeeper.java

License:Apache License

/**
 * Constructor for use with the builder. Other constructors also use it.
 *//*ww w  .  j a v  a 2 s .co m*/
@SuppressWarnings("deprecation")
@VisibleForTesting
BookKeeper(ClientConfiguration conf, ZooKeeper zkc, EventLoopGroup eventLoopGroup,
        ByteBufAllocator byteBufAllocator, StatsLogger rootStatsLogger, DNSToSwitchMapping dnsResolver,
        HashedWheelTimer requestTimer, FeatureProvider featureProvider)
        throws IOException, InterruptedException, BKException {
    this.conf = conf;
    // initialize feature provider
    if (null == featureProvider) {
        this.featureProvider = SettableFeatureProvider.DISABLE_ALL;
    } else {
        this.featureProvider = featureProvider;
    }

    this.internalConf = ClientInternalConf.fromConfigAndFeatureProvider(conf, this.featureProvider);

    // initialize resources
    this.scheduler = OrderedScheduler.newSchedulerBuilder().numThreads(1).name("BookKeeperClientScheduler")
            .build();
    this.mainWorkerPool = OrderedExecutor.newBuilder().name("BookKeeperClientWorker")
            .numThreads(conf.getNumWorkerThreads()).statsLogger(rootStatsLogger)
            .traceTaskExecution(conf.getEnableTaskExecutionStats())
            .preserveMdcForTaskExecution(conf.getPreserveMdcForTaskExecution())
            .traceTaskWarnTimeMicroSec(conf.getTaskExecutionWarnTimeMicros())
            .enableBusyWait(conf.isBusyWaitEnabled()).build();

    // initialize stats logger
    this.statsLogger = rootStatsLogger.scope(BookKeeperClientStats.CLIENT_SCOPE);
    this.clientStats = BookKeeperClientStats.newInstance(this.statsLogger);

    // initialize metadata driver
    try {
        String metadataServiceUriStr = conf.getMetadataServiceUri();
        if (null != metadataServiceUriStr) {
            this.metadataDriver = MetadataDrivers.getClientDriver(URI.create(metadataServiceUriStr));
        } else {
            checkNotNull(zkc, "No external zookeeper provided when no metadata service uri is found");
            this.metadataDriver = MetadataDrivers.getClientDriver("zk");
        }
        this.metadataDriver.initialize(conf, scheduler, rootStatsLogger, java.util.Optional.ofNullable(zkc));
    } catch (ConfigurationException ce) {
        LOG.error("Failed to initialize metadata client driver using invalid metadata service uri", ce);
        throw new IOException("Failed to initialize metadata client driver", ce);
    } catch (MetadataException me) {
        LOG.error("Encountered metadata exceptions on initializing metadata client driver", me);
        throw new IOException("Failed to initialize metadata client driver", me);
    }

    // initialize event loop group
    if (null == eventLoopGroup) {
        this.eventLoopGroup = EventLoopUtil.getClientEventLoopGroup(conf,
                new DefaultThreadFactory("bookkeeper-io"));
        this.ownEventLoopGroup = true;
    } else {
        this.eventLoopGroup = eventLoopGroup;
        this.ownEventLoopGroup = false;
    }

    if (byteBufAllocator != null) {
        this.allocator = byteBufAllocator;
    } else {
        this.allocator = ByteBufAllocatorBuilder.create().poolingPolicy(conf.getAllocatorPoolingPolicy())
                .poolingConcurrency(conf.getAllocatorPoolingConcurrency())
                .outOfMemoryPolicy(conf.getAllocatorOutOfMemoryPolicy())
                .leakDetectionPolicy(conf.getAllocatorLeakDetectionPolicy()).build();
    }

    // initialize bookie client
    this.bookieClient = new BookieClientImpl(conf, this.eventLoopGroup, this.allocator, this.mainWorkerPool,
            scheduler, rootStatsLogger);

    if (null == requestTimer) {
        this.requestTimer = new HashedWheelTimer(
                new ThreadFactoryBuilder().setNameFormat("BookieClientTimer-%d").build(),
                conf.getTimeoutTimerTickDurationMs(), TimeUnit.MILLISECONDS, conf.getTimeoutTimerNumTicks());
        this.ownTimer = true;
    } else {
        this.requestTimer = requestTimer;
        this.ownTimer = false;
    }

    // initialize the ensemble placement
    this.placementPolicy = initializeEnsemblePlacementPolicy(conf, dnsResolver, this.requestTimer,
            this.featureProvider, this.statsLogger);

    this.bookieWatcher = new BookieWatcherImpl(conf, this.placementPolicy,
            metadataDriver.getRegistrationClient(), this.statsLogger.scope(WATCHER_SCOPE));
    if (conf.getDiskWeightBasedPlacementEnabled()) {
        LOG.info("Weighted ledger placement enabled");
        ThreadFactoryBuilder tFBuilder = new ThreadFactoryBuilder()
                .setNameFormat("BKClientMetaDataPollScheduler-%d");
        this.bookieInfoScheduler = Executors.newSingleThreadScheduledExecutor(tFBuilder.build());
        this.bookieInfoReader = new BookieInfoReader(this, conf, this.bookieInfoScheduler);
        this.bookieWatcher.initialBlockingBookieRead();
        this.bookieInfoReader.start();
    } else {
        LOG.info("Weighted ledger placement is not enabled");
        this.bookieInfoScheduler = null;
        this.bookieInfoReader = new BookieInfoReader(this, conf, null);
        this.bookieWatcher.initialBlockingBookieRead();
    }

    // initialize ledger manager
    try {
        this.ledgerManagerFactory = this.metadataDriver.getLedgerManagerFactory();
    } catch (MetadataException e) {
        throw new IOException("Failed to initialize ledger manager factory", e);
    }
    this.ledgerManager = new CleanupLedgerManager(ledgerManagerFactory.newLedgerManager());
    this.ledgerIdGenerator = ledgerManagerFactory.newLedgerIdGenerator();

    scheduleBookieHealthCheckIfEnabled(conf);
}

From source file:org.apache.bookkeeper.client.TestRackawareEnsemblePlacementPolicy.java

License:Apache License

@Override
protected void setUp() throws Exception {
    super.setUp();
    StaticDNSResolver.reset();//ww w.  java  2s.com
    StaticDNSResolver.addNodeToRack(InetAddress.getLocalHost().getHostAddress(),
            NetworkTopology.DEFAULT_REGION_AND_RACK);
    StaticDNSResolver.addNodeToRack("127.0.0.1", NetworkTopology.DEFAULT_REGION_AND_RACK);
    StaticDNSResolver.addNodeToRack("localhost", NetworkTopology.DEFAULT_REGION_AND_RACK);
    LOG.info("Set up static DNS Resolver.");
    conf.setProperty(REPP_DNS_RESOLVER_CLASS, StaticDNSResolver.class.getName());
    conf.setMinNumRacksPerWriteQuorum(minNumRacksPerWriteQuorumConfValue);
    addr1 = new BookieSocketAddress("127.0.0.2", 3181);
    addr2 = new BookieSocketAddress("127.0.0.3", 3181);
    addr3 = new BookieSocketAddress("127.0.0.4", 3181);
    addr4 = new BookieSocketAddress("127.0.0.5", 3181);
    // update dns mapping
    StaticDNSResolver.addNodeToRack(addr1.getHostName(), NetworkTopology.DEFAULT_REGION + "/rack1");
    StaticDNSResolver.addNodeToRack(addr2.getHostName(), NetworkTopology.DEFAULT_REGION_AND_RACK);
    StaticDNSResolver.addNodeToRack(addr3.getHostName(), NetworkTopology.DEFAULT_REGION_AND_RACK);
    StaticDNSResolver.addNodeToRack(addr4.getHostName(), NetworkTopology.DEFAULT_REGION + "/rack2");
    ensemble.add(addr1);
    ensemble.add(addr2);
    ensemble.add(addr3);
    ensemble.add(addr4);
    writeSet = writeSetFromValues(0, 1, 2, 3);

    timer = new HashedWheelTimer(new ThreadFactoryBuilder().setNameFormat("TestTimer-%d").build(),
            conf.getTimeoutTimerTickDurationMs(), TimeUnit.MILLISECONDS, conf.getTimeoutTimerNumTicks());

    repp = new RackawareEnsemblePlacementPolicy();
    repp.initialize(conf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL, NullStatsLogger.INSTANCE);
    repp.withDefaultRack(NetworkTopology.DEFAULT_REGION_AND_RACK);
}

From source file:org.apache.bookkeeper.client.TestRackawareEnsemblePlacementPolicyUsingScript.java

License:Apache License

@Before
public void setUp() throws Exception {
    conf.setProperty(REPP_DNS_RESOLVER_CLASS, ScriptBasedMapping.class.getName());
    conf.setProperty(CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY,
            "src/test/resources/networkmappingscript.sh");
    timer = new HashedWheelTimer(new ThreadFactoryBuilder().setNameFormat("TestTimer-%d").build(),
            conf.getTimeoutTimerTickDurationMs(), TimeUnit.MILLISECONDS, conf.getTimeoutTimerNumTicks());

    repp = new RackawareEnsemblePlacementPolicy();
    repp.initialize(conf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL, NullStatsLogger.INSTANCE);
}

From source file:org.apache.bookkeeper.client.TestRackawareEnsemblePlacementPolicyUsingScript.java

License:Apache License

@Test
public void testNetworkTopologyScriptFileNameIsEmpty() throws Exception {
    ignoreTestIfItIsWindowsOS();/*from ww w .  j a v  a2s  . co  m*/
    repp.uninitalize();

    ClientConfiguration newConf = new ClientConfiguration();
    newConf.setProperty(REPP_DNS_RESOLVER_CLASS, ScriptBasedMapping.class.getName());
    newConf.setProperty(CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY, "");
    newConf.setEnforceMinNumRacksPerWriteQuorum(false);
    timer = new HashedWheelTimer(new ThreadFactoryBuilder().setNameFormat("TestTimer-%d").build(),
            newConf.getTimeoutTimerTickDurationMs(), TimeUnit.MILLISECONDS, newConf.getTimeoutTimerNumTicks());

    repp = new RackawareEnsemblePlacementPolicy();
    try {
        repp.initialize(newConf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL,
                NullStatsLogger.INSTANCE);
    } catch (RuntimeException re) {
        fail("EnforceMinNumRacksPerWriteQuorum is not set, so repp.initialize should succeed even if"
                + " networkTopologyScriptFileName is empty");
    }
    repp.uninitalize();

    newConf.setEnforceMinNumRacksPerWriteQuorum(true);
    repp = new RackawareEnsemblePlacementPolicy();
    try {
        repp.initialize(newConf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL,
                NullStatsLogger.INSTANCE);
        fail("EnforceMinNumRacksPerWriteQuorum is set, so repp.initialize should fail if"
                + " networkTopologyScriptFileName is empty");
    } catch (RuntimeException re) {
    }
    repp.uninitalize();

    newConf.setProperty(CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY,
            "src/test/resources/networkmappingscript.sh");
    try {
        repp.initialize(newConf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL,
                NullStatsLogger.INSTANCE);
    } catch (RuntimeException re) {
        fail("EnforceMinNumRacksPerWriteQuorum is set and networkTopologyScriptFileName is not empty,"
                + " so it should succeed");
    }
    repp.uninitalize();
}

From source file:org.apache.bookkeeper.client.TestRackawareEnsemblePlacementPolicyUsingScript.java

License:Apache License

@Test
public void testIfValidateConfFails() throws Exception {
    ignoreTestIfItIsWindowsOS();/* w  ww .  ja v  a 2 s .  c  o  m*/
    repp.uninitalize();

    ClientConfiguration newConf = new ClientConfiguration();
    newConf.setProperty(REPP_DNS_RESOLVER_CLASS, ScriptBasedMapping.class.getName());
    /*
     * this script, exits with error value if no argument is passed to it.
     * So mapping.validateConf will fail.
     */
    newConf.setProperty(CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY,
            "src/test/resources/networkmappingscriptwithargs.sh");
    timer = new HashedWheelTimer(new ThreadFactoryBuilder().setNameFormat("TestTimer-%d").build(),
            newConf.getTimeoutTimerTickDurationMs(), TimeUnit.MILLISECONDS, newConf.getTimeoutTimerNumTicks());

    repp = new RackawareEnsemblePlacementPolicy();
    repp.initialize(newConf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL,
            NullStatsLogger.INSTANCE);

    repp.uninitalize();
    repp = new RackawareEnsemblePlacementPolicy();
    try {
        repp.initialize(newConf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL,
                NullStatsLogger.INSTANCE);
    } catch (RuntimeException re) {
        fail("EnforceMinNumRacksPerWriteQuorum is not set, so repp.initialize should succeed"
                + " even if mapping.validateConf fails");
    }

    newConf.setEnforceMinNumRacksPerWriteQuorum(true);
    repp.uninitalize();
    repp = new RackawareEnsemblePlacementPolicy();
    try {
        repp.initialize(newConf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL,
                NullStatsLogger.INSTANCE);
        fail("EnforceMinNumRacksPerWriteQuorum is set, so repp.initialize should fail"
                + " if mapping.validateConf fails");
    } catch (RuntimeException re) {

    }

    /*
     * this script returns successfully even if no argument is passed to it.
     * So mapping.validateConf will succeed.
     */
    newConf.setProperty(CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY,
            "src/test/resources/networkmappingscript.sh");
    repp.uninitalize();
    repp = new RackawareEnsemblePlacementPolicy();
    try {
        repp.initialize(newConf, Optional.<DNSToSwitchMapping>empty(), timer, DISABLE_ALL,
                NullStatsLogger.INSTANCE);
    } catch (RuntimeException re) {
        fail("EnforceMinNumRacksPerWriteQuorum is set, and mapping.validateConf succeeds."
                + " So repp.initialize should succeed");
    }
}