use of org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage in project incubator-pulsar by apache.
the class LinuxBrokerHostUsageImpl method calculateBrokerHostUsage.
private void calculateBrokerHostUsage() {
List<String> nics = getNics();
double totalNicLimit = getTotalNicLimitKbps(nics);
double totalNicUsageTx = getTotalNicUsageTxKb(nics);
double totalNicUsageRx = getTotalNicUsageRxKb(nics);
double totalCpuLimit = getTotalCpuLimit();
CpuStat cpuStat = getTotalCpuUsage();
SystemResourceUsage usage = new SystemResourceUsage();
long now = System.currentTimeMillis();
if (lastCollection == 0L) {
usage.setMemory(getMemUsage());
usage.setBandwidthIn(new ResourceUsage(0d, totalNicLimit));
usage.setBandwidthOut(new ResourceUsage(0d, totalNicLimit));
usage.setCpu(new ResourceUsage(0d, totalCpuLimit));
} else {
double elapsedSeconds = (now - lastCollection) / 1000d;
double nicUsageTx = (totalNicUsageTx - lastTotalNicUsageTx) / elapsedSeconds;
double nicUsageRx = (totalNicUsageRx - lastTotalNicUsageRx) / elapsedSeconds;
if (cpuStat != null && lastCpuStat != null) {
// we need two non null stats to get a usage report
long cpuTimeDiff = cpuStat.getTotalTime() - lastCpuStat.getTotalTime();
long cpuUsageDiff = cpuStat.getUsage() - lastCpuStat.getUsage();
double cpuUsage = ((double) cpuUsageDiff / (double) cpuTimeDiff) * totalCpuLimit;
usage.setCpu(new ResourceUsage(cpuUsage, totalCpuLimit));
}
usage.setMemory(getMemUsage());
usage.setBandwidthIn(new ResourceUsage(nicUsageRx, totalNicLimit));
usage.setBandwidthOut(new ResourceUsage(nicUsageTx, totalNicLimit));
}
lastTotalNicUsageTx = totalNicUsageTx;
lastTotalNicUsageRx = totalNicUsageRx;
lastCpuStat = cpuStat;
lastCollection = System.currentTimeMillis();
this.usage = usage;
}
use of org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage in project incubator-pulsar by apache.
the class LoadManagerShared method getSystemResourceUsage.
// Get the system resource usage for this broker.
public static SystemResourceUsage getSystemResourceUsage(final BrokerHostUsage brokerHostUsage) throws IOException {
SystemResourceUsage systemResourceUsage = brokerHostUsage.getBrokerHostUsage();
// Override System memory usage and limit with JVM heap usage and limit
long maxHeapMemoryInBytes = Runtime.getRuntime().maxMemory();
long memoryUsageInBytes = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
systemResourceUsage.memory.usage = (double) memoryUsageInBytes / MIBI;
systemResourceUsage.memory.limit = (double) maxHeapMemoryInBytes / MIBI;
// Collect JVM direct memory
systemResourceUsage.directMemory.usage = (double) (JvmMetrics.getJvmDirectMemoryUsed() / MIBI);
systemResourceUsage.directMemory.limit = (double) (PlatformDependent.maxDirectMemory() / MIBI);
return systemResourceUsage;
}
use of org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage in project incubator-pulsar by apache.
the class ModularLoadManagerImpl method updateLocalBrokerData.
/**
* As any broker, retrieve the namespace bundle stats and system resource usage to update data local to this broker.
* @return
*/
@Override
public LocalBrokerData updateLocalBrokerData() {
try {
final SystemResourceUsage systemResourceUsage = LoadManagerShared.getSystemResourceUsage(brokerHostUsage);
localData.update(systemResourceUsage, getBundleStats());
} catch (Exception e) {
log.warn("Error when attempting to update local broker data: {}", e);
}
return localData;
}
use of org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage in project incubator-pulsar by apache.
the class PulsarLoadReportImpl method parse.
public static LoadReport parse(String loadReportJson) {
PulsarLoadReportImpl pulsarLoadReport = new PulsarLoadReportImpl();
ObjectMapper mapper = ObjectMapperFactory.create();
try {
org.apache.pulsar.policies.data.loadbalancer.LoadReport report = mapper.readValue(loadReportJson, org.apache.pulsar.policies.data.loadbalancer.LoadReport.class);
SystemResourceUsage sru = report.getSystemResourceUsage();
String resourceUnitName = report.getName();
pulsarLoadReport.resourceDescription = new PulsarResourceDescription();
if (sru.bandwidthIn != null)
pulsarLoadReport.resourceDescription.put("bandwidthIn", sru.bandwidthIn);
if (sru.bandwidthOut != null)
pulsarLoadReport.resourceDescription.put("bandwidthOut", sru.bandwidthOut);
if (sru.memory != null)
pulsarLoadReport.resourceDescription.put("memory", sru.memory);
if (sru.cpu != null)
pulsarLoadReport.resourceDescription.put("cpu", sru.cpu);
pulsarLoadReport.resourceUnit = new SimpleResourceUnit(resourceUnitName, pulsarLoadReport.resourceDescription);
} catch (Exception e) {
log.warn("Failed Parsing Load Report from JSON string [{}]", e);
}
return pulsarLoadReport;
}
use of org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage in project incubator-pulsar by apache.
the class SimpleLoadManagerImpl method fromLoadReport.
/*
* temp method, remove it in future, in-place to make this glue code to make load balancing work
*/
private PulsarResourceDescription fromLoadReport(LoadReport report) {
SystemResourceUsage sru = report.getSystemResourceUsage();
PulsarResourceDescription resourceDescription = new PulsarResourceDescription();
if (sru == null) {
return resourceDescription;
}
if (sru.bandwidthIn != null)
resourceDescription.put("bandwidthIn", sru.bandwidthIn);
if (sru.bandwidthOut != null)
resourceDescription.put("bandwidthOut", sru.bandwidthOut);
if (sru.memory != null)
resourceDescription.put("memory", sru.memory);
if (sru.cpu != null)
resourceDescription.put("cpu", sru.cpu);
return resourceDescription;
}
Aggregations