2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.pooling.message;
24 import java.util.Arrays;
25 import java.util.HashSet;
28 import lombok.NoArgsConstructor;
30 import org.onap.policy.drools.pooling.PoolingFeatureException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Bucket assignments, which is simply an array of host names.
40 public class BucketAssignments {
42 private static final Logger logger = LoggerFactory.getLogger(BucketAssignments.class);
45 * The number of bits in the maximum number of buckets.
47 private static final int MAX_BUCKET_BITS = 10;
50 * Maximum number of buckets. Must be a power of two.
52 public static final int MAX_BUCKETS = 1 << MAX_BUCKET_BITS;
55 * Used to ensure that a hash code is not negative.
57 private static final int MAX_BUCKETS_MASK = MAX_BUCKETS - 1;
60 * Identifies the host serving a particular bucket.
62 private String[] hostArray = null;
68 * @param hostArray maps a bucket number (i.e., array index) to a host. All values
71 public BucketAssignments(String[] hostArray) {
72 this.hostArray = hostArray;
76 * Gets the leader, which is the host with the minimum UUID.
78 * @return the assignment leader
80 public String getLeader() {
81 if (hostArray == null) {
87 for (String host : hostArray) {
88 if (host != null && (leader == null || host.compareTo(leader) < 0)) {
98 * Determines if a host has an assignment.
100 * @param host host to be checked
101 * @return {@code true} if the host has an assignment, {@code false} otherwise
103 public boolean hasAssignment(String host) {
104 if (hostArray == null) {
108 for (String host2 : hostArray) {
109 if (host.equals(host2)) {
118 * Gets all the hosts that have an assignment.
120 * @return all the hosts that have an assignment
122 public Set<String> getAllHosts() {
123 Set<String> set = new HashSet<>();
124 if (hostArray == null) {
128 for (String host : hostArray) {
138 * Gets the host assigned to a given bucket.
140 * @param hashCode hash code of the item whose assignment is desired
141 * @return the assigned host, or {@code null} if the item has no assigned host
143 public String getAssignedHost(int hashCode) {
144 if (hostArray == null || hostArray.length == 0) {
145 logger.error("no buckets have been assigned");
149 return hostArray[(Math.abs(hashCode) & MAX_BUCKETS_MASK) % hostArray.length];
153 * Gets the number of buckets.
155 * @return the number of buckets
158 return (hostArray != null ? hostArray.length : 0);
162 * Checks the validity of the assignments, verifying that all buckets have been
163 * assigned to a host.
165 * @throws PoolingFeatureException if the assignments are invalid
167 public void checkValidity() throws PoolingFeatureException {
168 if (hostArray == null || hostArray.length == 0) {
169 throw new PoolingFeatureException("missing hosts in message bucket assignments");
172 if (hostArray.length > MAX_BUCKETS) {
173 throw new PoolingFeatureException("too many hosts in message bucket assignments");
176 for (var x = 0; x < hostArray.length; ++x) {
177 if (hostArray[x] == null) {
178 throw new PoolingFeatureException("bucket " + x + " has no assignment");
184 public int hashCode() {
185 final var prime = 31;
187 result = prime * result + Arrays.hashCode(hostArray);
192 public boolean equals(Object obj) {
199 if (getClass() != obj.getClass()) {
202 BucketAssignments other = (BucketAssignments) obj;
203 return Arrays.equals(hostArray, other.hostArray);