2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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;
26 import org.onap.policy.drools.pooling.PoolingFeatureException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Bucket assignments, which is simply an array of host names.
33 public class BucketAssignments {
35 private static final Logger logger = LoggerFactory.getLogger(BucketAssignments.class);
38 * The number of bits in the maximum number of buckets.
40 private static final int MAX_BUCKET_BITS = 10;
43 * Maximum number of buckets. Must be a power of two.
45 public static final int MAX_BUCKETS = 1 << MAX_BUCKET_BITS;
48 * Used to ensure that a hash code is not negative.
50 private static final int MAX_BUCKETS_MASK = MAX_BUCKETS - 1;
53 * Identifies the host serving a particular bucket.
55 private String[] hostArray = null;
60 public BucketAssignments() {
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;
74 public String[] getHostArray() {
78 public void setHostArray(String[] hostArray) {
79 this.hostArray = hostArray;
83 * Gets the leader, which is the host with the minimum UUID.
85 * @return the assignment leader
87 public String getLeader() {
88 if (hostArray == null) {
94 for (String host : hostArray) {
95 if (host != null && (leader == null || host.compareTo(leader) < 0)) {
105 * Determines if a host has an assignment.
107 * @param host host to be checked
108 * @return {@code true} if the host has an assignment, {@code false} otherwise
110 public boolean hasAssignment(String host) {
111 if (hostArray == null) {
115 for (String host2 : hostArray) {
116 if (host.equals(host2)) {
125 * Gets all of the hosts that have an assignment.
127 * @return all of the hosts that have an assignment
129 public Set<String> getAllHosts() {
130 Set<String> set = new HashSet<>();
131 if (hostArray == null) {
135 for (String host : hostArray) {
145 * Gets the host assigned to a given bucket.
147 * @param hashCode hash code of the item whose assignment is desired
148 * @return the assigned host, or {@code null} if the item has no assigned host
150 public String getAssignedHost(int hashCode) {
151 if (hostArray == null || hostArray.length == 0) {
152 logger.error("no buckets have been assigned");
156 return hostArray[(hashCode & MAX_BUCKETS_MASK) % hostArray.length];
160 * Gets the number of buckets.
162 * @return the number of buckets
165 return (hostArray != null ? hostArray.length : 0);
169 * Checks the validity of the assignments, verifying that all buckets have been
170 * assigned to a host.
172 * @throws PoolingFeatureException if the assignments are invalid
174 public void checkValidity() throws PoolingFeatureException {
175 if (hostArray == null || hostArray.length == 0) {
176 throw new PoolingFeatureException("missing hosts in message bucket assignments");
179 if (hostArray.length > MAX_BUCKETS) {
180 throw new PoolingFeatureException("too many hosts in message bucket assignments");
183 for (int x = 0; x < hostArray.length; ++x) {
184 if (hostArray[x] == null) {
185 throw new PoolingFeatureException("bucket " + x + " has no assignment");
191 public int hashCode() {
192 final int prime = 31;
194 result = prime * result + Arrays.hashCode(hostArray);
199 public boolean equals(Object obj) {
206 if (getClass() != obj.getClass()) {
209 BucketAssignments other = (BucketAssignments) obj;
210 return Arrays.equals(hostArray, other.hostArray);