2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.pooling.message;
23 import java.util.Arrays;
24 import java.util.HashSet;
27 import lombok.NoArgsConstructor;
29 import org.onap.policy.drools.pooling.PoolingFeatureException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Bucket assignments, which is simply an array of host names.
39 public class BucketAssignments {
41 private static final Logger logger = LoggerFactory.getLogger(BucketAssignments.class);
44 * The number of bits in the maximum number of buckets.
46 private static final int MAX_BUCKET_BITS = 10;
49 * Maximum number of buckets. Must be a power of two.
51 public static final int MAX_BUCKETS = 1 << MAX_BUCKET_BITS;
54 * Used to ensure that a hash code is not negative.
56 private static final int MAX_BUCKETS_MASK = MAX_BUCKETS - 1;
59 * Identifies the host serving a particular bucket.
61 private String[] hostArray = null;
67 * @param hostArray maps a bucket number (i.e., array index) to a host. All values
70 public BucketAssignments(String[] hostArray) {
71 this.hostArray = hostArray;
75 * Gets the leader, which is the host with the minimum UUID.
77 * @return the assignment leader
79 public String getLeader() {
80 if (hostArray == null) {
86 for (String host : hostArray) {
87 if (host != null && (leader == null || host.compareTo(leader) < 0)) {
97 * Determines if a host has an assignment.
99 * @param host host to be checked
100 * @return {@code true} if the host has an assignment, {@code false} otherwise
102 public boolean hasAssignment(String host) {
103 if (hostArray == null) {
107 for (String host2 : hostArray) {
108 if (host.equals(host2)) {
117 * Gets all of the hosts that have an assignment.
119 * @return all of the hosts that have an assignment
121 public Set<String> getAllHosts() {
122 Set<String> set = new HashSet<>();
123 if (hostArray == null) {
127 for (String host : hostArray) {
137 * Gets the host assigned to a given bucket.
139 * @param hashCode hash code of the item whose assignment is desired
140 * @return the assigned host, or {@code null} if the item has no assigned host
142 public String getAssignedHost(int hashCode) {
143 if (hostArray == null || hostArray.length == 0) {
144 logger.error("no buckets have been assigned");
148 return hostArray[(Math.abs(hashCode) & MAX_BUCKETS_MASK) % hostArray.length];
152 * Gets the number of buckets.
154 * @return the number of buckets
157 return (hostArray != null ? hostArray.length : 0);
161 * Checks the validity of the assignments, verifying that all buckets have been
162 * assigned to a host.
164 * @throws PoolingFeatureException if the assignments are invalid
166 public void checkValidity() throws PoolingFeatureException {
167 if (hostArray == null || hostArray.length == 0) {
168 throw new PoolingFeatureException("missing hosts in message bucket assignments");
171 if (hostArray.length > MAX_BUCKETS) {
172 throw new PoolingFeatureException("too many hosts in message bucket assignments");
175 for (var x = 0; x < hostArray.length; ++x) {
176 if (hostArray[x] == null) {
177 throw new PoolingFeatureException("bucket " + x + " has no assignment");
183 public int hashCode() {
184 final var prime = 31;
186 result = prime * result + Arrays.hashCode(hostArray);
191 public boolean equals(Object obj) {
198 if (getClass() != obj.getClass()) {
201 BucketAssignments other = (BucketAssignments) obj;
202 return Arrays.equals(hostArray, other.hostArray);