+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.locator;\r
-\r
-import org.onap.aaf.cadi.Access;\r
-import org.onap.aaf.cadi.Locator;\r
-import org.onap.aaf.cadi.LocatorException;\r
-import org.onap.aaf.cadi.Access.Level;\r
-import org.onap.aaf.cadi.routing.GreatCircle;\r
-\r
-import org.onap.aaf.inno.env.util.Split;\r
-\r
-/**\r
- * This Locator is to handle Hot Peer load protection, when the Servers are \r
- * 1) Static\r
- * 2) Well known client URL\r
- * \r
- * The intention is to change traffic over to the Hot Peer, if a server goes down, and reinstate\r
- * when it is back up.\r
- * \r
- * Example of this kind of Service is a MS Certificate Server\r
- * \r
- *\r
- *\r
- * @param <CLIENT>\r
- */\r
-public abstract class HotPeerLocator<CLIENT> implements Locator<CLIENT> {\r
- private final String[] urlstrs;\r
- private final CLIENT[] clients;\r
- private final long[] failures;\r
- private final double[] distances;\r
- private int preferred;\r
- private long invalidateTime;\r
- private Thread refreshThread;\r
- protected Access access;\r
-\r
- /**\r
- * Construct: Expect one or more Strings in the form:\r
- * 192.555.112.223:39/38.88087/-77.30122\r
- * separated by commas\r
- * \r
- * @param trans\r
- * @param urlstr\r
- * @param invalidateTime\r
- * @param localLatitude\r
- * @param localLongitude\r
- * @throws LocatorException\r
- */\r
- @SuppressWarnings("unchecked")\r
- protected HotPeerLocator(Access access, final String urlstr, final long invalidateTime, final String localLatitude, final String localLongitude) throws LocatorException {\r
- this.access = access;\r
- urlstrs = Split.split(',', urlstr);\r
- clients = (CLIENT[])new Object[urlstrs.length];\r
- failures = new long[urlstrs.length];\r
- distances= new double[urlstrs.length];\r
- this.invalidateTime = invalidateTime;\r
- \r
- double distance = Double.MAX_VALUE;\r
- for(int i=0;i<urlstrs.length;++i) {\r
- String[] info = Split.split('/', urlstrs[i]);\r
- if(info.length<3) {\r
- throw new LocatorException("Configuration needs LAT and LONG, i.e. ip:port/lat/long");\r
- }\r
- try {\r
- clients[i] = _newClient(urlstrs[i]);\r
- failures[i] = 0L;\r
- } catch(LocatorException le) {\r
- failures[i] = System.currentTimeMillis()+invalidateTime;\r
- }\r
- \r
- double d = GreatCircle.calc(info[1],info[2],localLatitude,localLongitude);\r
- distances[i]=d;\r
- \r
- // find preferred server\r
- if(d<distance) {\r
- preferred = i;\r
- distance=d;\r
- }\r
- }\r
- \r
- access.printf(Level.INIT,"Preferred Client is %s",urlstrs[preferred]);\r
- for(int i=0;i<urlstrs.length;++i) {\r
- if(i!=preferred) {\r
- access.printf(Level.INIT,"Alternate Client is %s",urlstrs[i]);\r
- }\r
- }\r
- }\r
- \r
- protected abstract CLIENT _newClient(String hostInfo) throws LocatorException;\r
- /**\r
- * If client can reconnect, then return. Otherwise, destroy and return null;\r
- * @param client\r
- * @return\r
- * @throws LocatorException\r
- */\r
- protected abstract CLIENT _invalidate(CLIENT client);\r
- \r
- protected abstract void _destroy(CLIENT client);\r
- \r
- @Override\r
- public Item best() throws LocatorException {\r
- if(failures[preferred]==0L) {\r
- return new HPItem(preferred);\r
- } else {\r
- long now = System.currentTimeMillis();\r
- double d = Double.MAX_VALUE;\r
- int best = -1;\r
- boolean tickle = false;\r
- // try for best existing client\r
- for(int i=0;i<urlstrs.length;++i) {\r
- if(failures[i]<now && distances[i]<d) {\r
- if(clients[i]!=null) {\r
- best = i;\r
- break;\r
- } else {\r
- tickle = true; // There's some failed clients which can be restored\r
- }\r
- }\r
- }\r
- if(best<0 && tickle) {\r
- tickle=false;\r
- if(refresh()) {\r
- // try again\r
- for(int i=0;i<urlstrs.length;++i) {\r
- if(failures[i]==0L && distances[i]<d) {\r
- if(clients[i]!=null) {\r
- best = i;\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- \r
- /*\r
- * If a valid client is available, but there are some that can refresh, return the client immediately\r
- * but start a Thread to do the background Client setup.\r
- */\r
- if(tickle) {\r
- synchronized(clients) {\r
- if(refreshThread==null) {\r
- refreshThread = new Thread(new Runnable(){\r
- @Override\r
- public void run() {\r
- refresh();\r
- refreshThread = null;\r
- }\r
- });\r
- refreshThread.setDaemon(true);\r
- refreshThread.start();\r
- }\r
- }\r
- }\r
- \r
- if(best<0) {\r
- throw new LocatorException("No Clients available");\r
- }\r
-\r
- \r
- return new HPItem(best);\r
- }\r
- }\r
- \r
-\r
- @Override\r
- public CLIENT get(Item item) throws LocatorException {\r
- HPItem hpi = (HPItem)item;\r
- CLIENT c = clients[hpi.idx];\r
- if(c==null) {\r
- if(failures[hpi.idx]>System.currentTimeMillis()) {\r
- throw new LocatorException("Client requested is invalid"); \r
- } else {\r
- synchronized(clients) {\r
- c = _newClient(urlstrs[hpi.idx]);\r
- failures[hpi.idx]=0L;\r
- }\r
- }\r
- } else if(failures[hpi.idx]>0){\r
- throw new LocatorException("Client requested is invalid");\r
- }\r
- return c;\r
- }\r
- \r
- public String info(Item item) {\r
- HPItem hpi = (HPItem)item;\r
- if(hpi!=null && hpi.idx<urlstrs.length) {\r
- return urlstrs[hpi.idx];\r
- } else {\r
- return "Invalid Item";\r
- }\r
- }\r
-\r
- @Override\r
- public boolean hasItems() {\r
- for(int i=0;i<clients.length;++i) {\r
- if(clients[i]!=null && failures[i]==0L) {\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
- \r
- @Override\r
- public synchronized void invalidate(Item item) throws LocatorException {\r
- HPItem hpi = (HPItem)item;\r
- failures[hpi.idx] = System.currentTimeMillis() + invalidateTime;\r
- CLIENT c = clients[hpi.idx];\r
- clients[hpi.idx] = _invalidate(c);\r
- }\r
- \r
- @Override\r
- public Item first() throws LocatorException {\r
- return new HPItem(0);\r
- }\r
- \r
- @Override\r
- public Item next(Item item) throws LocatorException {\r
- HPItem hpi = (HPItem)item;\r
- if(++hpi.idx>=clients.length) {\r
- return null;\r
- }\r
- return hpi;\r
- }\r
- \r
- @Override\r
- public boolean refresh() {\r
- boolean force = !hasItems(); // If no Items at all, reset\r
- boolean rv = true;\r
- long now = System.currentTimeMillis();\r
- for(int i=0;i<clients.length;++i) {\r
- if(failures[i]>0L && (failures[i]<now || force)) { // retry\r
- try {\r
- synchronized(clients) {\r
- if(clients[i]==null) {\r
- clients[i]=_newClient(urlstrs[i]);\r
- }\r
- failures[i]=0L;\r
- }\r
- } catch (LocatorException e) {\r
- failures[i]=now+invalidateTime;\r
- rv = false;\r
- }\r
- }\r
- }\r
- return rv;\r
- }\r
- \r
- @Override\r
- public void destroy() {\r
- for(int i=0;i<clients.length;++i) {\r
- if(clients[i]!=null) {\r
- _destroy(clients[i]);\r
- clients[i] = null;\r
- }\r
- }\r
- }\r
-\r
- private static class HPItem implements Item {\r
- private int idx;\r
-\r
- public HPItem(int i) {\r
- idx = i;\r
- }\r
- }\r
- \r
-\r
- /*\r
- * Convenience Functions\r
- */\r
- public CLIENT bestClient() throws LocatorException {\r
- return get(best());\r
- }\r
-\r
- public boolean invalidate(CLIENT client) throws LocatorException {\r
- for(int i=0;i<clients.length;++i) {\r
- if(clients[i]==client) { // yes, "==" is appropriate here.. Comparing Java Object Reference\r
- invalidate(new HPItem(i));\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
-\r
- }\r