public Target getTarget(Timer timer) throws InterruptedException { TimeUnit unit = TimeUnit.MILLISECONDS; targetLock.tryLock(timer.timeLeft(unit) , unit ); try { if (target == null || !target.isValid()) connect(timer); return target; } finally { targetLock.unlock(); } } }
private void connect(Timer timer) throws InterruptedException { while (timer.isTimeLeft()) { pause(); target = supervisor.connectSync(spec); if (target.isValid()) { log.log(LogLevel.DEBUG, "Successfully connected to '" + spec + "', this = " + System.identityHashCode(this)); pauseTime = 0; logCount = 0; return; } else { logWarning(); } } }
public void invokeAsync(Request req, double timeout, RequestWaiter waiter) { // TODO: Consider replacing this by a watcher on the target synchronized(this) { // ensure we have exactly 1 valid connection across threads if (target == null || ! target.isValid()) target = supervisor.connect(new Spec(hostname, port)); } target.invokeAsync(req, timeout, waiter); }
/** * Closes all unused target connections. Unless the force argument is true, this method will allow a grace period * for all connections after last use before it starts closing them. This allows the most recently used connections * to stay open. * * @param force Whether or not to force flush. */ public synchronized void flushTargets(boolean force) { Iterator<Entry> it = targets.values().iterator(); long currentTime = timer.milliTime(); long expireTime = currentTime - expireMillis; while (it.hasNext()) { Entry entry = it.next(); RPCTarget target = entry.target; if (target.getJRTTarget().isValid()) { if (target.getRefCount() > 1) { entry.lastUse = currentTime; continue; // someone is using this } if (!force) { if (entry.lastUse > expireTime) { continue; // not sufficiently idle } } } target.subRef(); it.remove(); } }
public static void main(String[] args) { //String fileReference = args[0]; String fileReference = "59f93f445438c9db7ccbf1629f583c2aa004a68b"; String filename = "com.yahoo.vespatest.ExtraHitSearcher-1.0.0-deploy.jar"; File file = new File(String.format("/tmp/%s/%s", fileReference, filename)); byte[] blob = null; try { blob = IOUtils.readFileBytes(file); } catch (IOException e) { e.printStackTrace(); } log.log(LogLevel.INFO, "Read blob from " + file.getAbsolutePath()); Supervisor supervisor = new Supervisor(new Transport()); Spec spec = new Spec("tcp/localhost:19090"); log.log(LogLevel.INFO, "Connecting to " + spec); Target target = supervisor.connect(spec); if (! target.isValid()) { log.log(LogLevel.INFO, "Could not connect"); System.exit(1); } else { log.log(LogLevel.INFO, "Connected to " + spec); } new RpcTester(target).call(fileReference, filename, blob); }
/** * This method will return a target for the given address. If a target does not currently exist for the given * address, it will be created and added to the internal map. Each target is also reference counted so that an * active target is never expired. * * @param orb The supervisor to use to connect to the target. * @param address The address to resolve to a target. * @return A target for the given address. */ public RPCTarget getTarget(Supervisor orb, RPCServiceAddress address) { Spec spec = address.getConnectionSpec(); String key = spec.toString(); RPCTarget ret; synchronized (this) { Entry entry = targets.get(key); if (entry != null) { if (entry.target.getJRTTarget().isValid()) { entry.target.addRef(); entry.lastUse = timer.milliTime(); return entry.target; } entry.target.subRef(); targets.remove(key); } ret = new RPCTarget(spec, orb); targets.put(key, new Entry(ret, timer.milliTime())); } ret.addRef(); return ret; }