Exemplo n.º 1
0
/* This timer is used for caches and such
 * It is a default timer.
 * It should never be used direct.
 * There should always be a fall back.
 * By default this will reset the time every 100 times it is called to the latest
 * System.nanoTime
 * At most 1 thread will incur the cost of calling nanoTime about every 100 invocations.
 *
 */
public class TimeKeeperBasic implements TimeKeeper {

  private final AtomicInteger callEveryNowAndThen = new AtomicInteger();
  private final AtomicLong time = new AtomicLong();
  private final int TIME_KEEPER_FREQUENCY = Sys.sysProp("org.boon.timekeeper.frequency", 100);

  private final ReentrantLock lock = new ReentrantLock();

  private final AtomicLong lastDeltaTime = new AtomicLong();

  @Override
  public final long time() {

    long limit = callEveryNowAndThen.incrementAndGet();
    long time;
    boolean shouldGetTime = false;

    if (limit > TIME_KEEPER_FREQUENCY) {
      callEveryNowAndThen.set(0);
      shouldGetTime = true;
    }

    /* Somewhat Ensure two calls to time do not return the exact same value. */
    time = this.time.get() + limit;

    if (!shouldGetTime && (limit % 20 == 0)) {
      checkForDrift(time);
    }

    return time;
  }

  /* Never let the drift get greater than 200 ms. */
  private long checkForDrift(long time) {
    long delta = Math.abs(System.currentTimeMillis() - time);
    long lastDelta = lastDeltaTime.getAndSet(delta);
    if (delta > lastDelta + 200) {
      return getTheTime(time);
    }
    return time;
  }

  private long getTheTime(long time) {
    boolean locked = lock.tryLock(); // make sure two or more threads are not calling nanoTime.
    if (locked) {
      try {
        // I don't want more than one thread calling nanoTime
        time = System.nanoTime() / 1000000;
        this.time.set(time);
        return time;

      } finally {
        lock.unlock();
      }
    }
    return time;
  }
}
Exemplo n.º 2
0
Arquivo: IO.java Projeto: nremond/boon
  private static String getWindowsPathIfNeeded(String path) {
    if (Sys.isWindows()) {

      if (!path.startsWith("http")
          && !path.startsWith(CLASSPATH_SCHEMA)
          && !path.startsWith(JAR_SCHEMA)) {
        path = path.replace('/', Sys.windowsPathSeparator());
        if (slc(path, 0, 6).equals("file:\\")) {
          path = slc(path, 6);
        }
      }

      if (path.startsWith(".\\")) {
        path = slc(path, 2);
      }
    }
    return path;
  }
Exemplo n.º 3
0
Arquivo: IO.java Projeto: nremond/boon
 public static Path uriToPath(URI uri) {
   Path thePath = null;
   if (Sys.isWindows()) {
     String newPath = uri.getPath();
     if (newPath.startsWith("/C:")) {
       newPath = slc(newPath, 3);
     }
     thePath = FileSystems.getDefault().getPath(newPath);
   } else {
     thePath = FileSystems.getDefault().getPath(uri.getPath());
   }
   return thePath;
 }
Exemplo n.º 4
0
Arquivo: IO.java Projeto: nremond/boon
  public static URI createURI(final String path) {
    if (!Sys.isWindows()) {
      return URI.create(path);

    } else {

      if (path.contains("\\") || path.startsWith("C:") || path.startsWith("D:")) {
        String newPath = new File(path).toURI().toString();
        if (newPath.startsWith("file:/C:")) {
          newPath = slc(newPath, 8);
          return URI.create(newPath);
        } else {
          return URI.create(newPath);
        }

      } else {
        return URI.create(path);
      }
    }
  }
Exemplo n.º 5
0
/** Created by Richard on 7/1/14. */
public class DataStoreClientConfig {

  private static final String FILE_LOCATION =
      Sys.sysProp("org.boon.slumberdb.DataStoreClientConfig", "/opt/org/slumberdb/client.json");

  private List<Bucket> buckets;

  private String websocketURI;

  private String restURI;

  private String clientId;
  @JsonIgnore private AtomicLong count = new AtomicLong();

  private int maxFrameSize = 0; // true

  private int batchResultSize = 15; // true

  public DataStoreClientConfig(Bucket... buckets) {
    this(list(buckets));
  }

  public DataStoreClientConfig(List<Bucket> buckets) {
    this.buckets = buckets;

    sequence();
  }

  public static DataStoreClientConfig load() {

    puts("Config for data store client", FILE_LOCATION);

    if (IO.exists(FILE_LOCATION)) {
      try {
        return new JsonParserFactory()
            .create()
            .parseFile(DataStoreClientConfig.class, FILE_LOCATION);
      } catch (Exception ex) {
        Exceptions.handle(
            ex, "Unable to read config file", FILE_LOCATION, "for data store client config");
        return null;
      }
    } else {

      puts("WARNING", FILE_LOCATION, "does not exist for data store client config!!!");
      return new DataStoreClientConfig();
    }
  }

  public static DataStoreClientConfig config() {
    return new DataStoreClientConfig();
  }

  public static DataStoreClientConfig config(Bucket... buckets) {
    return new DataStoreClientConfig(buckets);
  }

  public List<Bucket> buckets() {
    return buckets;
  }

  public DataStoreClientConfig buckets(List<Bucket> buckets) {
    this.buckets = buckets;

    sequence();

    return this;
  }

  public DataStoreClientConfig buckets(Bucket... buckets) {
    this.buckets = list(buckets);

    sequence();

    return this;
  }

  private void sequence() {
    int index = 0;
    for (Bucket bucket : buckets) {
      bucket.index(index);
      index++;
    }
  }

  @Override
  public boolean equals(Object o) {
    sequence();
    if (this == o) return true;
    if (!(o instanceof DataStoreClientConfig)) return false;

    DataStoreClientConfig that = (DataStoreClientConfig) o;

    if (buckets != null ? !buckets.equals(that.buckets) : that.buckets != null) return false;

    return true;
  }

  @Override
  public int hashCode() {

    sequence();
    return buckets != null ? buckets.hashCode() : 0;
  }

  @Override
  public String toString() {

    sequence();
    return "DataStoreClientConfig{" + "buckets=" + buckets + '}';
  }

  public Bucket pickBucket(String key) {

    int hash = key.hashCode() % buckets.size();

    hash = hash >= 0 ? hash : hash * -1;

    Bucket bucket = buckets.get(hash);

    return bucket;
  }

  public String websocketURI() {
    return websocketURI == null ? ProtocolConstants.DEFAULT_WEBSOCKET_URI : websocketURI;
  }

  public DataStoreClientConfig websocketURI(String websocketURI) {
    this.websocketURI = websocketURI;
    return this;
  }

  public String restURI() {
    return restURI == null ? ProtocolConstants.DEFAULT_WEBSOCKET_URI : restURI;
  }

  public DataStoreClientConfig restURI(String restURI) {
    this.restURI = restURI;
    return this;
  }

  public int maxFrameSize() {
    return maxFrameSize == 0 ? 20_000_000 : maxFrameSize;
  }

  public DataStoreClientConfig maxFrameSize(int maxFrameSize) {
    this.maxFrameSize = maxFrameSize;
    return this;
  }

  public String clientId() {

    if (count == null) {
      count = new AtomicLong();
    }
    if (clientId == null) {
      clientId = UUID.randomUUID().toString();
    }
    return Str.add(clientId, ".", "" + count.incrementAndGet());
  }

  public DataStoreClientConfig clientId(String clientId) {
    this.clientId = clientId;
    return this;
  }
}
Exemplo n.º 6
0
 public static DataStoreServerConfig load() {
   String fileLocation = Sys.sysProp("DataStoreServerConfig", DEFAULT_FILE_LOCATION);
   return Sys.loadFromFileLocation(DataStoreServerConfig.class, fileLocation);
 }