From 6913298a6dcadd455314eb96fc940128db49965d Mon Sep 17 00:00:00 2001 From: IanHowell Date: Thu, 26 Apr 2018 15:46:25 -0500 Subject: Improve coverage of cadi-client Issue-ID: AAF-224 Change-Id: Iff629aaf3551a962932c65b42f254cbfbdf3b608 Signed-off-by: IanHowell --- .../org/onap/aaf/cadi/locator/HotPeerLocator.java | 453 ++++++++++----------- .../onap/aaf/cadi/locator/test/JU_DNSLocator.java | 16 +- .../locator/test/JU_HClientHotPeerLocator.java | 151 +++++++ 3 files changed, 385 insertions(+), 235 deletions(-) create mode 100644 cadi/client/src/test/java/org/onap/aaf/cadi/locator/test/JU_HClientHotPeerLocator.java (limited to 'cadi') diff --git a/cadi/client/src/main/java/org/onap/aaf/cadi/locator/HotPeerLocator.java b/cadi/client/src/main/java/org/onap/aaf/cadi/locator/HotPeerLocator.java index 17f9bafb..fd8e99dc 100644 --- a/cadi/client/src/main/java/org/onap/aaf/cadi/locator/HotPeerLocator.java +++ b/cadi/client/src/main/java/org/onap/aaf/cadi/locator/HotPeerLocator.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -29,274 +29,273 @@ import org.onap.aaf.cadi.routing.GreatCircle; import org.onap.aaf.misc.env.util.Split; /** - * This Locator is to handle Hot Peer load protection, when the Servers are + * This Locator is to handle Hot Peer load protection, when the Servers are * 1) Static * 2) Well known client URL - * + * * The intention is to change traffic over to the Hot Peer, if a server goes down, and reinstate * when it is back up. - * + * * Example of this kind of Service is a MS Certificate Server - * + * * @author Jonathan * * @param */ public abstract class HotPeerLocator implements Locator { - private final String[] urlstrs; - private final CLIENT[] clients; - private final long[] failures; - private final double[] distances; - private int preferred; - private long invalidateTime; - private Thread refreshThread; - protected Access access; + private final String[] urlstrs; + private final CLIENT[] clients; + private final long[] failures; + private final double[] distances; + private int preferred; + private long invalidateTime; + private Thread refreshThread; + protected Access access; - /** - * Construct: Expect one or more Strings in the form: - * 192.555.112.223:39/38.88087/-77.30122 - * separated by commas - * - * @param trans - * @param urlstr - * @param invalidateTime - * @param localLatitude - * @param localLongitude - * @throws LocatorException - */ - @SuppressWarnings("unchecked") - protected HotPeerLocator(Access access, final String urlstr, final long invalidateTime, final String localLatitude, final String localLongitude) throws LocatorException { - this.access = access; - urlstrs = Split.split(',', urlstr); - clients = (CLIENT[])new Object[urlstrs.length]; - failures = new long[urlstrs.length]; - distances= new double[urlstrs.length]; - this.invalidateTime = invalidateTime; - - double distance = Double.MAX_VALUE; - for(int i=0;iSystem.currentTimeMillis()) { - throw new LocatorException("Client requested is invalid"); - } else { - synchronized(clients) { - c = _newClient(urlstrs[hpi.idx]); - failures[hpi.idx]=0L; + /* + * If a valid client is available, but there are some that can refresh, return the client immediately + * but start a Thread to do the background Client setup. + */ + if(tickle) { + synchronized(clients) { + if(refreshThread==null) { + refreshThread = new Thread(new Runnable(){ + @Override + public void run() { + refresh(); + refreshThread = null; + } + }); + refreshThread.setDaemon(true); + refreshThread.start(); } } - } else if(failures[hpi.idx]>0){ - throw new LocatorException("Client requested is invalid"); } - return c; - } - - public String info(Item item) { - HPItem hpi = (HPItem)item; - if(hpi!=null && hpi.idxSystem.currentTimeMillis()) { + throw new LocatorException("Client requested is invalid"); + } else { + synchronized(clients) { + c = _newClient(urlstrs[hpi.idx]); + failures[hpi.idx]=0L; } } - return false; - } - - @Override - public synchronized void invalidate(Item item) throws LocatorException { - HPItem hpi = (HPItem)item; - failures[hpi.idx] = System.currentTimeMillis() + invalidateTime; - CLIENT c = clients[hpi.idx]; - clients[hpi.idx] = _invalidate(c); + } else if(failures[hpi.idx]>0){ + throw new LocatorException("Client requested is invalid"); } - - @Override - public Item first() throws LocatorException { - return new HPItem(0); + return c; + } + + public String info(Item item) { + HPItem hpi = (HPItem)item; + if(hpi!=null && hpi.idx=clients.length) { - return null; + } + + @Override + public boolean hasItems() { + for(int i=0;i0L && (failures[i]=clients.length) { + return null; + } + return hpi; + } + + @Override + public boolean refresh() { + boolean force = !hasItems(); // If no Items at all, reset + boolean rv = true; + long now = System.currentTimeMillis(); + for(int i=0;i0L && (failures[i]