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====================================================
22 package org.onap.aaf.cadi.locator;
24 import org.onap.aaf.cadi.Access;
25 import org.onap.aaf.cadi.Locator;
26 import org.onap.aaf.cadi.LocatorException;
27 import org.onap.aaf.cadi.Access.Level;
28 import org.onap.aaf.cadi.routing.GreatCircle;
29 import org.onap.aaf.misc.env.util.Split;
32 * This Locator is to handle Hot Peer load protection, when the Servers are
34 * 2) Well known client URL
36 * The intention is to change traffic over to the Hot Peer, if a server goes down, and reinstate
39 * Example of this kind of Service is a MS Certificate Server
45 public abstract class HotPeerLocator<CLIENT> implements Locator<CLIENT> {
46 private final String[] urlstrs;
47 private final CLIENT[] clients;
48 private final long[] failures;
49 private final double[] distances;
50 private int preferred;
51 private long invalidateTime;
52 private Thread refreshThread;
53 protected Access access;
56 * Construct: Expect one or more Strings in the form:
57 * 192.555.112.223:39/38.88087/-77.30122
62 * @param invalidateTime
63 * @param localLatitude
64 * @param localLongitude
65 * @throws LocatorException
67 @SuppressWarnings("unchecked")
68 protected HotPeerLocator(Access access, final String urlstr, final long invalidateTime, final String localLatitude, final String localLongitude) throws LocatorException {
70 urlstrs = Split.split(',', urlstr);
71 clients = (CLIENT[])new Object[urlstrs.length];
72 failures = new long[urlstrs.length];
73 distances= new double[urlstrs.length];
74 this.invalidateTime = invalidateTime;
76 double distance = Double.MAX_VALUE;
77 for (int i=0;i<urlstrs.length;++i) {
78 String[] info = Split.split('/', urlstrs[i]);
80 throw new LocatorException("Configuration needs LAT and LONG, i.e. ip:port/lat/long");
83 clients[i] = _newClient(urlstrs[i]);
85 } catch (LocatorException le) {
86 failures[i] = System.currentTimeMillis()+invalidateTime;
89 double d = GreatCircle.calc(info[1],info[2],localLatitude,localLongitude);
92 // find preferred server
99 access.printf(Level.INIT,"Preferred Client is %s",urlstrs[preferred]);
100 for (int i=0;i<urlstrs.length;++i) {
102 access.printf(Level.INIT,"Alternate Client is %s",urlstrs[i]);
107 protected abstract CLIENT _newClient(String hostInfo) throws LocatorException;
109 * If client can reconnect, then return. Otherwise, destroy and return null;
112 * @throws LocatorException
114 protected abstract CLIENT _invalidate(CLIENT client);
116 protected abstract void _destroy(CLIENT client);
119 public Item best() throws LocatorException {
120 if (failures[preferred]==0L) {
121 return new HPItem(preferred);
123 long now = System.currentTimeMillis();
124 double d = Double.MAX_VALUE;
126 boolean tickle = false;
127 // try for best existing client
128 for (int i=0;i<urlstrs.length;++i) {
129 if (failures[i]<now && distances[i]<d) {
130 if (clients[i]!=null) {
134 tickle = true; // There's some failed clients which can be restored
138 if (best<0 && tickle) {
142 for (int i=0;i<urlstrs.length;++i) {
143 if (failures[i]==0L && distances[i]<d) {
144 if (clients[i]!=null) {
154 * If a valid client is available, but there are some that can refresh, return the client immediately
155 * but start a Thread to do the background Client setup.
158 synchronized(clients) {
159 if (refreshThread==null) {
160 refreshThread = new Thread(new Runnable(){
164 refreshThread = null;
167 refreshThread.setDaemon(true);
168 refreshThread.start();
174 throw new LocatorException("No Clients available");
177 return new HPItem(best);
183 public CLIENT get(Item item) throws LocatorException {
184 HPItem hpi = (HPItem)item;
185 CLIENT c = clients[hpi.idx];
187 if (failures[hpi.idx]>System.currentTimeMillis()) {
188 throw new LocatorException("Client requested is invalid");
190 synchronized(clients) {
191 c = _newClient(urlstrs[hpi.idx]);
192 failures[hpi.idx]=0L;
195 } else if (failures[hpi.idx]>0){
196 throw new LocatorException("Client requested is invalid");
201 public String info(Item item) {
202 HPItem hpi = (HPItem)item;
203 if (hpi!=null && hpi.idx<urlstrs.length) {
204 return urlstrs[hpi.idx];
206 return "Invalid Item";
211 public boolean hasItems() {
212 for (int i=0;i<clients.length;++i) {
213 if (clients[i]!=null && failures[i]==0L) {
221 public synchronized void invalidate(Item item) throws LocatorException {
222 HPItem hpi = (HPItem)item;
223 failures[hpi.idx] = System.currentTimeMillis() + invalidateTime;
224 CLIENT c = clients[hpi.idx];
225 clients[hpi.idx] = _invalidate(c);
229 public Item first() throws LocatorException {
230 return new HPItem(0);
234 public Item next(Item item) throws LocatorException {
235 HPItem hpi = (HPItem)item;
236 if (++hpi.idx>=clients.length) {
243 public boolean refresh() {
244 boolean force = !hasItems(); // If no Items at all, reset
246 long now = System.currentTimeMillis();
247 for (int i=0;i<clients.length;++i) {
248 if (failures[i]>0L && (failures[i]<now || force)) { // retry
250 synchronized(clients) {
251 if (clients[i]==null) {
252 clients[i]=_newClient(urlstrs[i]);
256 } catch (LocatorException e) {
257 failures[i]=now+invalidateTime;
266 public void destroy() {
267 for (int i=0;i<clients.length;++i) {
268 if (clients[i]!=null) {
269 _destroy(clients[i]);
275 private static class HPItem implements Item {
278 public HPItem(int i) {
285 * Convenience Functions
287 public CLIENT bestClient() throws LocatorException {
291 public boolean invalidate(CLIENT client) throws LocatorException {
292 for (int i=0;i<clients.length;++i) {
293 if (clients[i]==client) { // yes, "==" is appropriate here.. Comparing Java Object Reference
294 invalidate(new HPItem(i));