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");
178 return new HPItem(best);
184 public CLIENT get(Item item) throws LocatorException {
185 HPItem hpi = (HPItem)item;
186 CLIENT c = clients[hpi.idx];
188 if(failures[hpi.idx]>System.currentTimeMillis()) {
189 throw new LocatorException("Client requested is invalid");
191 synchronized(clients) {
192 c = _newClient(urlstrs[hpi.idx]);
193 failures[hpi.idx]=0L;
196 } else if(failures[hpi.idx]>0){
197 throw new LocatorException("Client requested is invalid");
202 public String info(Item item) {
203 HPItem hpi = (HPItem)item;
204 if(hpi!=null && hpi.idx<urlstrs.length) {
205 return urlstrs[hpi.idx];
207 return "Invalid Item";
212 public boolean hasItems() {
213 for(int i=0;i<clients.length;++i) {
214 if(clients[i]!=null && failures[i]==0L) {
222 public synchronized void invalidate(Item item) throws LocatorException {
223 HPItem hpi = (HPItem)item;
224 failures[hpi.idx] = System.currentTimeMillis() + invalidateTime;
225 CLIENT c = clients[hpi.idx];
226 clients[hpi.idx] = _invalidate(c);
230 public Item first() throws LocatorException {
231 return new HPItem(0);
235 public Item next(Item item) throws LocatorException {
236 HPItem hpi = (HPItem)item;
237 if(++hpi.idx>=clients.length) {
244 public boolean refresh() {
245 boolean force = !hasItems(); // If no Items at all, reset
247 long now = System.currentTimeMillis();
248 for(int i=0;i<clients.length;++i) {
249 if(failures[i]>0L && (failures[i]<now || force)) { // retry
251 synchronized(clients) {
252 if(clients[i]==null) {
253 clients[i]=_newClient(urlstrs[i]);
257 } catch (LocatorException e) {
258 failures[i]=now+invalidateTime;
267 public void destroy() {
268 for(int i=0;i<clients.length;++i) {
269 if(clients[i]!=null) {
270 _destroy(clients[i]);
276 private static class HPItem implements Item {
279 public HPItem(int i) {
286 * Convenience Functions
288 public CLIENT bestClient() throws LocatorException {
292 public boolean invalidate(CLIENT client) throws LocatorException {
293 for(int i=0;i<clients.length;++i) {
294 if(clients[i]==client) { // yes, "==" is appropriate here.. Comparing Java Object Reference
295 invalidate(new HPItem(i));