|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.loadbalance.extensions.data; |
| 20 | + |
| 21 | +import lombok.Data; |
| 22 | +import org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData; |
| 23 | +import org.apache.pulsar.policies.data.loadbalancer.ResourceUsage; |
| 24 | +import org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage; |
| 25 | + |
| 26 | +/** |
| 27 | + * Contains all the data that is maintained locally on each broker. |
| 28 | + * |
| 29 | + * Migrate from {@link org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData}. |
| 30 | + * And removed the lookup data, see {@link BrokerLookupData} |
| 31 | + */ |
| 32 | +@Data |
| 33 | +public class BrokerLoadData { |
| 34 | + |
| 35 | + // Most recently available system resource usage. |
| 36 | + private ResourceUsage cpu; |
| 37 | + private ResourceUsage memory; |
| 38 | + private ResourceUsage directMemory; |
| 39 | + |
| 40 | + private ResourceUsage bandwidthIn; |
| 41 | + private ResourceUsage bandwidthOut; |
| 42 | + |
| 43 | + // Message data from the most recent namespace bundle stats. |
| 44 | + private double msgThroughputIn; |
| 45 | + private ResourceUsage msgThroughputInUsage; |
| 46 | + private double msgThroughputOut; |
| 47 | + private ResourceUsage msgThroughputOutUsage; |
| 48 | + private double msgRateIn; |
| 49 | + private double msgRateOut; |
| 50 | + |
| 51 | + public BrokerLoadData() { |
| 52 | + cpu = new ResourceUsage(); |
| 53 | + memory = new ResourceUsage(); |
| 54 | + directMemory = new ResourceUsage(); |
| 55 | + bandwidthIn = new ResourceUsage(); |
| 56 | + bandwidthOut = new ResourceUsage(); |
| 57 | + msgThroughputInUsage = new ResourceUsage(); |
| 58 | + msgThroughputOutUsage = new ResourceUsage(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Using the system resource usage and bundle stats acquired from the Pulsar client, update this LocalBrokerData. |
| 63 | + * |
| 64 | + * @param systemResourceUsage |
| 65 | + * System resource usage (cpu, memory, and direct memory). |
| 66 | + */ |
| 67 | + public void update(final SystemResourceUsage systemResourceUsage) { |
| 68 | + updateSystemResourceUsage(systemResourceUsage); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Using another LocalBrokerData, update this. |
| 73 | + * |
| 74 | + * @param other |
| 75 | + * LocalBrokerData to update from. |
| 76 | + */ |
| 77 | + public void update(final BrokerLoadData other) { |
| 78 | + updateSystemResourceUsage(other.cpu, other.memory, other.directMemory, other.bandwidthIn, other.bandwidthOut); |
| 79 | + } |
| 80 | + |
| 81 | + // Set the cpu, memory, and direct memory to that of the new system resource usage data. |
| 82 | + private void updateSystemResourceUsage(final SystemResourceUsage systemResourceUsage) { |
| 83 | + updateSystemResourceUsage(systemResourceUsage.cpu, systemResourceUsage.memory, systemResourceUsage.directMemory, |
| 84 | + systemResourceUsage.bandwidthIn, systemResourceUsage.bandwidthOut); |
| 85 | + } |
| 86 | + |
| 87 | + // Update resource usage given each individual usage. |
| 88 | + private void updateSystemResourceUsage(final ResourceUsage cpu, final ResourceUsage memory, |
| 89 | + final ResourceUsage directMemory, final ResourceUsage bandwidthIn, |
| 90 | + final ResourceUsage bandwidthOut) { |
| 91 | + this.cpu = cpu; |
| 92 | + this.memory = memory; |
| 93 | + this.directMemory = directMemory; |
| 94 | + this.bandwidthIn = bandwidthIn; |
| 95 | + this.bandwidthOut = bandwidthOut; |
| 96 | + } |
| 97 | + |
| 98 | + public double getMaxResourceUsage() { |
| 99 | + return LocalBrokerData.max(cpu.percentUsage(), directMemory.percentUsage(), bandwidthIn.percentUsage(), |
| 100 | + bandwidthOut.percentUsage()) / 100; |
| 101 | + } |
| 102 | + |
| 103 | + public double getMaxResourceUsageWithWeight(final double cpuWeight, final double memoryWeight, |
| 104 | + final double directMemoryWeight, final double bandwidthInWeight, |
| 105 | + final double bandwidthOutWeight) { |
| 106 | + return LocalBrokerData.max(cpu.percentUsage() * cpuWeight, memory.percentUsage() * memoryWeight, |
| 107 | + directMemory.percentUsage() * directMemoryWeight, bandwidthIn.percentUsage() * bandwidthInWeight, |
| 108 | + bandwidthOut.percentUsage() * bandwidthOutWeight) / 100; |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments