2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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 com.fasterxml.jackson.annotation.JsonIgnore;
24 import java.util.Arrays;
25 import java.util.HashSet;
27 import org.onap.policy.drools.pooling.PoolingFeatureException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Bucket assignments, which is simply an array of host names.
34 public class BucketAssignments {
37 private static final Logger logger = LoggerFactory.getLogger(BucketAssignments.class);
40 * The number of bits in the maximum number of buckets.
42 private static final int MAX_BUCKET_BITS = 10;
45 * Maximum number of buckets. Must be a power of two.
47 public static final int MAX_BUCKETS = 1 << MAX_BUCKET_BITS;
50 * Used to ensure that a hash code is not negative.
52 private static final int MAX_BUCKETS_MASK = MAX_BUCKETS - 1;
55 * Identifies the host serving a particular bucket.
57 private String[] hostArray = null;
62 public BucketAssignments() {
69 * @param hostArray maps a bucket number (i.e., array index) to a host. All values
72 public BucketAssignments(String[] hostArray) {
73 this.hostArray = hostArray;
76 public String[] getHostArray() {
80 public void setHostArray(String[] hostArray) {
81 this.hostArray = hostArray;
85 * Gets the leader, which is the host with the minimum UUID.
87 * @return the assignment leader
90 public String getLeader() {
91 if (hostArray == null) {
97 for (String host : hostArray) {
98 if (host != null && (leader == null || host.compareTo(leader) < 0)) {
108 * Determines if a host has an assignment.
110 * @param host host to be checked
111 * @return {@code true} if the host has an assignment, {@code false} otherwise
114 public boolean hasAssignment(String host) {
115 if (hostArray == null) {
119 for (String host2 : hostArray) {
120 if (host.equals(host2)) {
129 * Gets all of the hosts that have an assignment.
131 * @return all of the hosts that have an assignment
134 public Set<String> getAllHosts() {
135 Set<String> set = new HashSet<>();
136 if (hostArray == null) {
140 for (String host : hostArray) {
150 * Gets the host assigned to a given bucket.
152 * @param hashCode hash code of the item whose assignment is desired
153 * @return the assigned host, or {@code null} if the item has no assigned host
156 public String getAssignedHost(int hashCode) {
157 if (hostArray == null || hostArray.length == 0) {
158 logger.error("no buckets have been assigned");
162 return hostArray[(hashCode & MAX_BUCKETS_MASK) % hostArray.length];
166 * Gets the number of buckets.
168 * @return the number of buckets
172 return (hostArray != null ? hostArray.length : 0);
176 * Checks the validity of the assignments, verifying that all buckets have been
177 * assigned to a host.
179 * @throws PoolingFeatureException if the assignments are invalid
182 public void checkValidity() throws PoolingFeatureException {
183 if (hostArray == null || hostArray.length == 0) {
184 throw new PoolingFeatureException("missing hosts in message bucket assignments");
187 if (hostArray.length > MAX_BUCKETS) {
188 throw new PoolingFeatureException("too many hosts in message bucket assignments");
191 for (int x = 0; x < hostArray.length; ++x) {
192 if (hostArray[x] == null) {
193 throw new PoolingFeatureException("bucket " + x + " has no assignment");
199 public int hashCode() {
200 final int prime = 31;
202 result = prime * result + Arrays.hashCode(hostArray);
207 public boolean equals(Object obj) {
214 if (getClass() != obj.getClass()) {
217 BucketAssignments other = (BucketAssignments) obj;
218 return Arrays.equals(hostArray, other.hostArray);