com.google.common.collect.EvictingQueue Java Examples

The following examples show how to use com.google.common.collect.EvictingQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FailoverStrategy.java    From ThriftJ with Apache License 2.0 6 votes vote down vote up
public void fail(T object) {
	logger.info("Server {}:{} failed.", ((ThriftServer)object).getHost(),((ThriftServer)object).getPort());
	boolean addToFail = false;
	try {
		EvictingQueue<Long> evictingQueue = failCountMap.get(object);
		synchronized (evictingQueue) {
			evictingQueue.add(System.currentTimeMillis());
			if (evictingQueue.remainingCapacity() == 0 && evictingQueue.element() >= (System.currentTimeMillis() - failDuration)) {
				addToFail = true;
			}
		}
	} catch (ExecutionException e) {
		logger.error("Ops.", e);
	}
	if (addToFail) {
		failedList.put(object, Boolean.TRUE);
		logger.info("Server {}:{} failed. Add to fail list.", ((ThriftServer)object).getHost(), ((ThriftServer)object).getPort());
	}
}
 
Example #2
Source File: EvictingQueueUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenEvictingQueue_whenAddElementToFull_thenShouldEvictOldestItem() {
    //given
    Queue<Integer> evictingQueue = EvictingQueue.create(10);

    //when
    IntStream.range(0, 10).forEach(evictingQueue::add);

    //then
    assertThat(evictingQueue).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

    //and
    evictingQueue.add(100);

    //then
    assertThat(evictingQueue).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 100);
}
 
Example #3
Source File: InProcessSampledSpanStoreImpl.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private static void getSamplesFilteredByLatency(
    long latencyLowerNs,
    long latencyUpperNs,
    int maxSpansToReturn,
    List<RecordEventsSpanImpl> output,
    EvictingQueue<RecordEventsSpanImpl> queue) {
  for (RecordEventsSpanImpl span : queue) {
    if (output.size() >= maxSpansToReturn) {
      break;
    }
    long spanLatencyNs = span.getLatencyNs();
    if (spanLatencyNs >= latencyLowerNs && spanLatencyNs < latencyUpperNs) {
      output.add(span);
    }
  }
}
 
Example #4
Source File: KafkaIngestionHealthCheck.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
public KafkaIngestionHealthCheck(Config config, KafkaExtractorStatsTracker statsTracker) {
  this.config = config;
  this.slidingWindowSize = ConfigUtils.getInt(config, KAFKA_INGESTION_HEALTH_CHECK_SLIDING_WINDOW_SIZE_KEY, DEFAULT_KAFKA_INGESTION_HEALTH_CHECK_SLIDING_WINDOW_SIZE);
  this.ingestionLatencyThresholdMinutes = ConfigUtils.getLong(config, KAFKA_INGESTION_HEALTH_CHECK_LATENCY_THRESHOLD_MINUTES_KEY, DEFAULT_KAFKA_INGESTION_HEALTH_CHECK_LATENCY_THRESHOLD_MINUTES);
  this.consumptionRateDropOffFraction = ConfigUtils.getDouble(config, KAFKA_INGESTION_HEALTH_CHECK_CONSUMPTION_RATE_DROPOFF_FRACTION_KEY, DEFAULT_KAFKA_INGESTION_HEALTH_CHECK_CONSUMPTION_RATE_DROPOFF_FRACTION);
  this.expectedConsumptionRate = ConfigUtils.getDouble(config, KAFKA_INGESTION_HEALTH_CHECK_EXPECTED_CONSUMPTION_RATE_MBPS_KEY, DEFAULT_KAFKA_INGESTION_HEALTH_CHECK_EXPECTED_CONSUMPTION_RATE_MBPS);
  this.increasingLatencyCheckEnabled = ConfigUtils.getBoolean(config, KAFKA_INGESTION_HEALTH_CHECK_INCREASING_LATENCY_CHECK_ENABLED_KEY, DEFAULT_KAFKA_INGESTION_HEALTH_CHECK_INCREASING_LATENCY_CHECK_ENABLED);
  this.ingestionLatencies = EvictingQueue.create(this.slidingWindowSize);
  this.consumptionRateMBps = EvictingQueue.create(this.slidingWindowSize);
  EventBus eventBus;
  try {
    eventBus = EventBusFactory.get(ContainerHealthCheckFailureEvent.CONTAINER_HEALTH_CHECK_EVENT_BUS_NAME,
        SharedResourcesBrokerFactory.getImplicitBroker());
  } catch (IOException e) {
    log.error("Could not find EventBus instance for container health check", e);
    eventBus = null;
  }
  this.eventBus = eventBus;
  this.statsTracker = statsTracker;
}
 
Example #5
Source File: FailoverCheckingStrategy.java    From thrift-pool-client with Artistic License 2.0 6 votes vote down vote up
/**
 * <p>
 * fail.
 * </p>
 *
 * @param object a T object.
 */
public void fail(T object) {
    logger.trace("server {} failed.", object);
    boolean addToFail = false;
    try {
        EvictingQueue<Long> evictingQueue = failCountMap.get(object);
        synchronized (evictingQueue) {
            evictingQueue.add(System.currentTimeMillis());
            if (evictingQueue.remainingCapacity() == 0
                    && evictingQueue.element() >= System.currentTimeMillis() - failDuration) {
                addToFail = true;
            }
        }
    } catch (ExecutionException e) {
        logger.error("Ops.", e);
    }
    if (addToFail) {
        failedList.put(object, TRUE);
        logger.trace("server {} failed. add to fail list.", object);
    }
}
 
Example #6
Source File: ArimaProcess.java    From java-timeseries with MIT License 6 votes vote down vote up
private ArimaProcess(Builder builder) {
    this.coefficients = builder.coefficients;
    this.distribution = builder.distribution;
    this.period = builder.period;
    this.seasonalCycle = builder.seasonalCycle;
    this.startTime = builder.startTime;
    this.currentTime = startTime;
    int seasonalFrequency = (int) builder.period.frequencyPer(builder.seasonalCycle);
    double[] arSarCoeffs = ArimaCoefficients.expandArCoefficients(coefficients.arCoeffs(),
                                                                  coefficients.seasonalARCoeffs(),

                                                                  seasonalFrequency);
    double[] maSmaCoeffs = ArimaCoefficients.expandMaCoefficients(coefficients.maCoeffs(),
                                                                  coefficients.seasonalMACoeffs(),
                                                                  seasonalFrequency);
    this.errors = EvictingQueue.create(maSmaCoeffs.length);
    this.diffSeries = EvictingQueue.create(arSarCoeffs.length);
    this.series = EvictingQueue.create(coefficients.d() + coefficients.D() * seasonalFrequency);
    this.maPoly = LagPolynomial.movingAverage(maSmaCoeffs);
    this.arPoly = LagPolynomial.autoRegressive(arSarCoeffs);
    this.diffPoly = LagPolynomial.differences(coefficients.d())
                                 .times(LagPolynomial.seasonalDifferences(seasonalFrequency, coefficients.D()));
}
 
Example #7
Source File: PaperTimingsCommand.java    From LagMonitor with MIT License 6 votes vote down vote up
@Override
protected void sendTimings(CommandSender sender) {
    EvictingQueue<TimingHistory> history = Reflection.getField(TimingsManager.class, "HISTORY", EvictingQueue.class)
            .get(null);

    TimingHistory lastHistory = history.peek();
    if (lastHistory == null) {
        sendError(sender, "Not enough data collected yet");
        return;
    }

    List<BaseComponent[]> lines = new ArrayList<>();
    printTimings(lines, lastHistory);

    Pages pagination = new Pages("Paper Timings", lines);
    pagination.send(sender);

    plugin.getPageManager().setPagination(sender.getName(), pagination);
}
 
Example #8
Source File: PaperTimingsCommand.java    From LagMonitor with MIT License 6 votes vote down vote up
@Override
protected void sendTimings(CommandSender sender) {
    EvictingQueue<TimingHistory> history = Reflection.getField(TimingsManager.class, "HISTORY", EvictingQueue.class)
            .get(null);

    TimingHistory lastHistory = history.peek();
    if (lastHistory == null) {
        sendError(sender, "Not enough data collected yet");
        return;
    }

    List<BaseComponent[]> lines = new ArrayList<>();
    printTimings(lines, lastHistory);

    Pages pagination = new Pages("Paper Timings", lines);
    pagination.send(sender);

    plugin.getPageManager().setPagination(sender.getName(), pagination);
}
 
Example #9
Source File: ProducerBatch.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
public ProducerBatch(
    GroupKey groupKey,
    String packageId,
    int batchSizeThresholdInBytes,
    int batchCountThreshold,
    int maxReservedAttempts,
    long nowMs) {
  this.groupKey = groupKey;
  this.packageId = packageId;
  this.createdMs = nowMs;
  this.batchSizeThresholdInBytes = batchSizeThresholdInBytes;
  this.batchCountThreshold = batchCountThreshold;
  this.curBatchCount = 0;
  this.curBatchSizeInBytes = 0;
  this.reservedAttempts = EvictingQueue.create(maxReservedAttempts);
  this.attemptCount = 0;
}
 
Example #10
Source File: ProductionPipelineRunner.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private void retainErrorRecordsInMemory(Map<String, List<Record>> errorRecords) {
  // Shortcut to avoid synchronization
  if(errorRecords.isEmpty()) {
    return;
  }

  synchronized (stageToErrorRecordsMap) {
    for (Map.Entry<String, List<Record>> e : errorRecords.entrySet()) {
      EvictingQueue<Record> errorRecordList = stageToErrorRecordsMap.computeIfAbsent(e.getKey(),
          k -> EvictingQueue.create(configuration.get(Constants.MAX_ERROR_RECORDS_PER_STAGE_KEY,
              Constants.MAX_ERROR_RECORDS_PER_STAGE_DEFAULT
          ))
      );
      // replace with a data structure with an upper cap
      errorRecordList.addAll(errorRecords.get(e.getKey()));
    }
  }
}
 
Example #11
Source File: ProductionPipelineRunner.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private void retainErrorMessagesInMemory(Map<String, List<ErrorMessage>> errorMessages) {
  // Shortcut to avoid synchronization
  if(errorMessages.isEmpty()) {
    return;
  }

  synchronized (stageToErrorMessagesMap) {
    for (Map.Entry<String, List<ErrorMessage>> e : errorMessages.entrySet()) {
      EvictingQueue<ErrorMessage> errorMessageList = stageToErrorMessagesMap.computeIfAbsent(e.getKey(),
          k -> EvictingQueue.create(configuration.get(Constants.MAX_PIPELINE_ERRORS_KEY,
              Constants.MAX_PIPELINE_ERRORS_DEFAULT
          ))
      );
      errorMessageList.addAll(errorMessages.get(e.getKey()));
    }
  }
}
 
Example #12
Source File: SimulatedAnealingMinimizer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the simulated annealing algorithm and produces a model with new coefficients that, theoretically
 * fit the data better and generalizes to future forecasts without overfitting.
 *
 * @param model         The MovAvgModel to be optimized for
 * @param train         A training set provided to the model, which predictions will be
 *                      generated from
 * @param test          A test set of data to compare the predictions against and derive
 *                      a cost for the model
 * @return              A new, minimized model that (theoretically) better fits the data
 */
public static MovAvgModel minimize(MovAvgModel model, EvictingQueue<Double> train, double[] test) {

    double temp = 1;
    double minTemp = 0.0001;
    int iterations = 100;
    double alpha = 0.9;

    MovAvgModel bestModel = model;
    MovAvgModel oldModel = model;

    double oldCost = cost(model, train, test);
    double bestCost = oldCost;

    while (temp > minTemp) {
        for (int i = 0; i < iterations; i++) {
            MovAvgModel newModel = oldModel.neighboringModel();
            double newCost = cost(newModel, train, test);

            double ap = acceptanceProbability(oldCost, newCost, temp);
            if (ap > Math.random()) {
                oldModel = newModel;
                oldCost = newCost;

                if (newCost < bestCost) {
                    bestCost = newCost;
                    bestModel = newModel;
                }
            }
        }

        temp *= alpha;
    }

    return bestModel;
}
 
Example #13
Source File: InProcessSampledSpanStoreImpl.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void getSamples(
    int maxSpansToReturn,
    List<RecordEventsSpanImpl> output,
    EvictingQueue<RecordEventsSpanImpl> queue) {
  for (RecordEventsSpanImpl span : queue) {
    if (output.size() >= maxSpansToReturn) {
      break;
    }
    output.add(span);
  }
}
 
Example #14
Source File: SmaPointForecaster.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
public SmaPointForecaster(SmaPointForecasterParams params) {
    notNull(params, "params can't be null");
    params.validate();
    this.params = params;
    this.periodOfValues = EvictingQueue.create(params.getLookBackPeriod());

    if (params.getInitialPeriodOfValues() != null) {
        params.getInitialPeriodOfValues().forEach(this::updateMeanEstimate);
    }
}
 
Example #15
Source File: ChunkLogger.java    From ForgeHax with MIT License 5 votes vote down vote up
@Override
protected void onEnabled() {
  chunkLock.lock();
  try {
    if (max_chunks.get() <= 0) {
      chunks = Queues.newArrayDeque();
    } else {
      chunks = EvictingQueue.create(max_chunks.get());
    }
  } finally {
    chunkLock.unlock();
  }
}
 
Example #16
Source File: StatisticsHelper.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Update the statistics 
 * 
 * @param region
 * @param regionSize
 */
public static void updateStatisticsHistory(final String regionIdentifier, final double regionSize) {
	
	synchronized (statisticsHistory) {
		statisticsHistory
			.computeIfAbsent(regionIdentifier, (e) -> EvictingQueue.create(HISTORY_LENGTH))
			.add(regionSize);
	}

}
 
Example #17
Source File: SimulatedAnealingMinimizer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the "cost" of a model.  E.g. when run on the training data, how closely do the  predictions
 * match the test data
 *
 * Uses Least Absolute Differences to calculate error.  Note that this is not scale free, but seems
 * to work fairly well in practice
 *
 * @param model     The MovAvgModel we are fitting
 * @param train     A training set of data given to the model, which will then generate predictions from
 * @param test      A test set of data to compare against the predictions
 * @return          A cost, or error, of the model
 */
private static double cost(MovAvgModel model, EvictingQueue<Double> train, double[] test) {
    double error = 0;
    double[] predictions = model.predict(train, test.length);

    assert(predictions.length == test.length);

    for (int i = 0; i < predictions.length; i++) {
        error += Math.abs(test[i] - predictions[i]) ;
    }

    return error;
}
 
Example #18
Source File: CachedJobExecutionProvider.java    From spring-batch-rest with Apache License 2.0 5 votes vote down vote up
void add(JobExecution jobExecution) {
    lock.writeLock().lock();
    try {
        jobExecutionsByExitCode.computeIfAbsent(jobExecution.getExitStatus().getExitCode(),
                (exitCode) -> EvictingQueue.create(cacheSize)).add(jobExecution);
        jobExecutions.add(jobExecution);
    } finally {
        lock.writeLock().unlock();
    }
}
 
Example #19
Source File: EdmxDetector.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
public EdmxDetector(UUID uuid, EdmxHyperparams hyperparams, boolean trusted) {
    notNull(uuid, "uuid can't be null");
    notNull(hyperparams, "hyperparams can't be null");
    hyperparams.validate();

    log.info("Creating EdmxDetector: uuid={}, hyperparams={}", uuid, hyperparams);
    this.uuid = uuid;
    this.hyperparams = hyperparams;
    this.buffer = EvictingQueue.create(hyperparams.getBufferSize());
    this.trusted = trusted;
}
 
Example #20
Source File: FailoverStrategy.java    From ThriftJ with Apache License 2.0 5 votes vote down vote up
/**
 * 自定义 failover 策略
 * @param failCount 失败次数
 * @param failDuration 失效持续时间
 * @param recoverDuration 恢复持续时间
 */
public FailoverStrategy(final int failCount, long failDuration, long recoverDuration) {
	this.failDuration = failDuration;
	this.failedList = CacheBuilder.newBuilder().weakKeys().expireAfterWrite(recoverDuration, TimeUnit.MILLISECONDS).build();
	this.failCountMap = CacheBuilder.newBuilder().weakKeys().build(new CacheLoader<T, EvictingQueue<Long>>() {
		@Override
		public EvictingQueue<Long> load(T key) throws Exception {
			return EvictingQueue.create(failCount);
		}
	});
}
 
Example #21
Source File: ChatHistoryPlugin.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void startUp()
{
	messageQueue = EvictingQueue.create(100);
	friends = new ArrayDeque<>(FRIENDS_MAX_SIZE + 1);
	keyManager.registerKeyListener(this);
}
 
Example #22
Source File: OfferOutcomeTrackerV2.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
public OfferOutcomeSummary() {
  this.acceptedCount = 0;
  this.rejectedCount = 0;
  this.outcomes = EvictingQueue.create(DEFAULT_CAPACITY);
  this.failureReasons = new HashMap<>();
  this.rejectedAgents = new HashMap<>();
}
 
Example #23
Source File: PendingTransactions.java    From besu with Apache License 2.0 5 votes vote down vote up
public PendingTransactions(
    final int maxTransactionRetentionHours,
    final int maxPendingTransactions,
    final int maxPooledTransactionHashes,
    final Clock clock,
    final MetricsSystem metricsSystem,
    final Supplier<BlockHeader> chainHeadHeaderSupplier,
    final Optional<EIP1559> eip1559,
    final Percentage priceBump) {
  this.maxTransactionRetentionHours = maxTransactionRetentionHours;
  this.maxPendingTransactions = maxPendingTransactions;
  this.clock = clock;
  this.newPooledHashes = EvictingQueue.create(maxPooledTransactionHashes);
  this.chainHeadHeaderSupplier = chainHeadHeaderSupplier;
  this.transactionReplacementHandler = new TransactionPoolReplacementHandler(eip1559, priceBump);
  final LabelledMetric<Counter> transactionAddedCounter =
      metricsSystem.createLabelledCounter(
          BesuMetricCategory.TRANSACTION_POOL,
          "transactions_added_total",
          "Count of transactions added to the transaction pool",
          "source");
  localTransactionAddedCounter = transactionAddedCounter.labels("local");
  remoteTransactionAddedCounter = transactionAddedCounter.labels("remote");
  localTransactionHashesAddedCounter = transactionAddedCounter.labels("pool");

  transactionRemovedCounter =
      metricsSystem.createLabelledCounter(
          BesuMetricCategory.TRANSACTION_POOL,
          "transactions_removed_total",
          "Count of transactions removed from the transaction pool",
          "source",
          "operation");
}
 
Example #24
Source File: MessagesReSender.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
public void resend(String endpointId) {
  Queue<DelayedMessage> delayedMessages = delayedMessageRegistry.remove(endpointId);

  if (delayedMessages == null || delayedMessages.isEmpty()) {
    return;
  }

  Optional<Session> sessionOptional = registry.get(endpointId);

  if (!sessionOptional.isPresent()) {
    return;
  }

  Queue<DelayedMessage> backingQueue = EvictingQueue.create(delayedMessages.size());
  while (!delayedMessages.isEmpty()) {
    backingQueue.offer(delayedMessages.poll());
  }

  Session session = sessionOptional.get();
  for (DelayedMessage delayedMessage : backingQueue) {
    if (session.isOpen()) {
      session.getAsyncRemote().sendText(delayedMessage.message);
    } else {
      delayedMessages.add(delayedMessage);
    }
  }

  if (!delayedMessages.isEmpty()) {
    delayedMessageRegistry.put(endpointId, delayedMessages);
  }
}
 
Example #25
Source File: BoostVHTActiveLearningNode.java    From incubator-samoa with Apache License 2.0 5 votes vote down vote up
public BoostVHTActiveLearningNode(double[] classObservation, int parallelism_hint, SplittingOption splitOption, int maxBufferSize) {
  super(classObservation, parallelism_hint);
  weightSeenAtLastSplitEvaluation = this.getWeightSeen();
  id = VerticalHoeffdingTree.LearningNodeIdGenerator.generate();
  attributeContentEventKeys = new HashMap<>();
  isSplitting = false;
  parallelismHint = parallelism_hint;
  this.splittingOption = splitOption;
  this.maxBufferSize = maxBufferSize;
  this.buffer = EvictingQueue.create(maxBufferSize);
}
 
Example #26
Source File: SystemProcessImpl.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public SimpleFileTailer(File file) {
  this.file = file;
  this.history = EvictingQueue.create(2500);
  this.inbuf = new byte[8192 * 8];
  try {
    this.randomAccessFile = new RandomAccessFile(file, "r");
  } catch (FileNotFoundException e) {
    throw new RuntimeException(Utils.format("Unexpected error reading output file '{}': {}", file, e), e);
  }
}
 
Example #27
Source File: AggregatorDataProvider.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an AggregatorDataProvider for a family of Aggregators that will close data windows together (atomically)
 *
 * @param windowsToKeep number of data windows to keep in memory, including the live one.
 */
public AggregatorDataProvider(int windowsToKeep, WindowType windowType) {
  Utils.checkArgument(windowsToKeep > 0, "windows to keep must be greater than zero");
  aggregators = new HashSet<>();
  dataWindowQueue = EvictingQueue.create(windowsToKeep);
  dataWindowList = Collections.emptyList();
  this.windowType = windowType;
}
 
Example #28
Source File: FailoverCheckingStrategy.java    From thrift-pool-client with Artistic License 2.0 5 votes vote down vote up
/**
 * <p>
 * Constructor for FailoverCheckingStrategy.
 * </p>
 *
 * @param failDuration a long.
 * @param recoveryDuration a long.
 */
public FailoverCheckingStrategy(int failCount, long failDuration, long recoveryDuration) {
    this.failDuration = failDuration;
    this.failedList = newBuilder().weakKeys().expireAfterWrite(recoveryDuration, MILLISECONDS)
            .build();
    this.failCountMap = newBuilder().weakKeys().build(
            new CacheLoader<T, EvictingQueue<Long>>() {

                @Override
                public EvictingQueue<Long> load(T key) {
                    return create(failCount);
                }
            });
}
 
Example #29
Source File: FilterableListAdapter.java    From tapchat-android with Apache License 2.0 5 votes vote down vote up
public void updateItems(List<T> items) {
    synchronized (mLock) {
        Collection<T> newItems = (mCapacity > 0) ? EvictingQueue.<T>create(mCapacity) : new ArrayList<T>();
        if (items != null) {
            newItems.addAll(items);
        }
        mOriginalItems = newItems;
    }
    notifyDataSetChanged();
}
 
Example #30
Source File: SchedulingURLBuffer.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
void addTiming(long t, String queueName) {
    Queue<Long> times;
    try {
        times = timings.get(queueName, () -> {
            return EvictingQueue.create(historySize);
        });
        times.add(t);
    } catch (ExecutionException e) {
        LOG.error("ExecutionException", e);
    }
}