Search in sources :

Example 1 with SystemResourceUsage

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;
}
Also used : ResourceUsage(org.apache.pulsar.policies.data.loadbalancer.ResourceUsage) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage)

Example 2 with SystemResourceUsage

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;
}
Also used : SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage)

Example 3 with 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;
}
Also used : SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) BrokerFilterException(org.apache.pulsar.broker.loadbalance.BrokerFilterException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 4 with SystemResourceUsage

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;
}
Also used : SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with SystemResourceUsage

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;
}
Also used : SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage)

Aggregations

SystemResourceUsage (org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage)22 ResourceUsage (org.apache.pulsar.policies.data.loadbalancer.ResourceUsage)11 LoadReport (org.apache.pulsar.policies.data.loadbalancer.LoadReport)10 Test (org.testng.annotations.Test)10 HashMap (java.util.HashMap)8 Map (java.util.Map)7 NamespaceBundleStats (org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats)6 SimpleResourceUnit (org.apache.pulsar.broker.loadbalance.impl.SimpleResourceUnit)5 TreeMap (java.util.TreeMap)4 ResourceQuota (org.apache.pulsar.common.policies.data.ResourceQuota)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 LocalBrokerData (org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)2 ResourceUnit (org.apache.pulsar.broker.loadbalance.ResourceUnit)2 PulsarResourceDescription (org.apache.pulsar.broker.loadbalance.impl.PulsarResourceDescription)2 ResourceAvailabilityRanker (org.apache.pulsar.broker.loadbalance.impl.ResourceAvailabilityRanker)2