io.cdap.cdap.internal.app.runtime.AbstractResourceReporter Maven / Gradle / Ivy
/*
* Copyright © 2014-2017 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.cdap.cdap.internal.app.runtime;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.AbstractScheduledService;
import io.cdap.cdap.api.metrics.MetricsContext;
import io.cdap.cdap.app.runtime.ProgramResourceReporter;
import org.apache.twill.common.Threads;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Abstract implementation of a {@link io.cdap.cdap.app.runtime.ProgramResourceReporter}
* writes out resource metrics at a fixed rate that defaults to 60 seconds, but can be specified
* in the constructor.
*/
public abstract class AbstractResourceReporter extends AbstractScheduledService implements ProgramResourceReporter {
private static final Logger LOG = LoggerFactory.getLogger(ProgramResourceReporter.class);
private static final int DEFAULT_REPORT_INTERVAL = 20;
protected static final String METRIC_CONTAINERS = "resources.used.containers";
protected static final String METRIC_MEMORY_USAGE = "resources.used.memory";
protected static final String METRIC_VIRTUAL_CORE_USAGE = "resources.used.vcores";
protected final MetricsContext metricsContext;
private final LoadingCache