All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.opentsdb.utils.Threads Maven / Gradle / Ivy

Go to download

OpenTSDB is a distributed, scalable Time Series Database (TSDB) written on top of HBase. OpenTSDB was written to address a common need: store, index and serve metrics collected from computer systems (network gear, operating systems, applications) at a large scale, and make this data easily accessible and graphable.

There is a newer version: 2.4.1
Show newest version
// This file is part of OpenTSDB.
// Copyright (C) 2015  The OpenTSDB Authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see .
package net.opentsdb.utils;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.ThreadNameDeterminer;

/**
 * Utilities dealing with threads, timers and the like.
 */
public class Threads {
  /** Used to count HashedWheelTimers */
  final static AtomicInteger TIMER_ID = new AtomicInteger();
  
  /** Helps give useful names to the Netty threads */
  public static class BossThreadNamer implements ThreadNameDeterminer {
    final static AtomicInteger tid = new AtomicInteger();
    @Override
    public String determineThreadName(String currentThreadName,
        String proposedThreadName) throws Exception {
      return "OpenTSDB I/O Boss #" + tid.incrementAndGet();
    }
  }
  
  /** Helps give useful names to the Netty threads */
  public static class WorkerThreadNamer implements ThreadNameDeterminer {
    final static AtomicInteger tid = new AtomicInteger();
    @Override
    public String determineThreadName(String currentThreadName,
        String proposedThreadName) throws Exception {
      return "OpenTSDB I/O Worker #" + tid.incrementAndGet();
    }
  }
  
  /** Simple prepends "OpenTSDB" to all threads */
  public static class PrependThreadNamer implements ThreadNameDeterminer {
    @Override
    public String determineThreadName(String currentThreadName, String proposedThreadName)
        throws Exception {
      return "OpenTSDB " + proposedThreadName;
    }
  }
  
  /**
   * Returns a new HashedWheelTimer with a name and default ticks
   * @param name The name to add to the thread name
   * @return A timer
   */
  public static HashedWheelTimer newTimer(final String name) {
    return newTimer(100, name);
  }
  
  /**
   * Returns a new HashedWheelTimer with a name and default ticks
   * @param ticks How many ticks per second to sleep between executions, in ms
   * @param name The name to add to the thread name
   * @return A timer
   */
  public static HashedWheelTimer newTimer(final int ticks, final String name) {
    return newTimer(ticks, 512, name);
  }
  
  /**
   * Returns a new HashedWheelTimer with a name and default ticks
   * @param ticks How many ticks per second to sleep between executions, in ms
   * @param ticks_per_wheel The size of the wheel
   * @param name The name to add to the thread name
   * @return A timer
   */
  public static HashedWheelTimer newTimer(final int ticks, 
      final int ticks_per_wheel, final String name) {
    class TimerThreadNamer implements ThreadNameDeterminer {
      @Override
      public String determineThreadName(String currentThreadName,
          String proposedThreadName) throws Exception {
        return "OpenTSDB Timer " + name + " #" + TIMER_ID.incrementAndGet();
      }
    }
    return new HashedWheelTimer(Executors.defaultThreadFactory(), 
        new TimerThreadNamer(), ticks, MILLISECONDS, ticks_per_wheel);
  }
  
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy