diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2019-10-11 17:09:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-11 17:09:53 +0200 |
commit | 82241ec5eb67ac74b0909c264060f4f519006227 (patch) | |
tree | 52b3610c00dab080df1bf693cdf8340bbd95a923 /clustercontroller-apputil/src/main | |
parent | 27d03fe0b6a1c31d9e89e91af623b7311902afa9 (diff) | |
parent | d31f043b3cca1a97dc5fb8b81b85c485667c28cf (diff) |
Merge pull request #10964 from vespa-engine/bjorncs/cleanup
Remove dead code
Diffstat (limited to 'clustercontroller-apputil/src/main')
2 files changed, 0 insertions, 316 deletions
diff --git a/clustercontroller-apputil/src/main/java/com/yahoo/vespa/clustercontroller/apputil/communication/http/ApacheAsyncHttpClient.java b/clustercontroller-apputil/src/main/java/com/yahoo/vespa/clustercontroller/apputil/communication/http/ApacheAsyncHttpClient.java deleted file mode 100644 index 11d746ef3ce..00000000000 --- a/clustercontroller-apputil/src/main/java/com/yahoo/vespa/clustercontroller/apputil/communication/http/ApacheAsyncHttpClient.java +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.apputil.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; -import com.yahoo.vespa.clustercontroller.utils.communication.http.AsyncHttpClient; -import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest; -import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult; -import com.yahoo.vespa.clustercontroller.utils.communication.http.SyncHttpClient; - -import java.util.*; -import java.util.concurrent.Executor; -import java.util.logging.Logger; - -/** - * There are some stuff to work around with the apache client. - * - Whether to use a proxy or not is global, not per request. - * - Timeout is not handled per request (and is not a request timeout but seems like a "something happening on TCP" timeout. - * - It is not thread safe. - * - * This class gets around these issues by creating one instance per unique setting, and ensuring only one request use a given instance at a time. - */ -public class ApacheAsyncHttpClient implements AsyncHttpClient<HttpResult> { - - private static final Logger log = Logger.getLogger(ApacheAsyncHttpClient.class.getName()); - - public interface SyncHttpClientFactory { - SyncHttpClient createInstance(String proxyHost, int proxyPort, long timeoutMs); - } - - public static class Settings { - String proxyHost; - int proxyPort; - long timeout; - - Settings(HttpRequest request) { - timeout = request.getTimeoutMillis(); - if (request.getPath() != null - && !request.getPath().isEmpty() - && request.getPath().charAt(0) != '/') - { - proxyHost = request.getHost(); - proxyPort = request.getPort(); - int colo = request.getPath().indexOf(':'); - int slash = request.getPath().indexOf('/', colo); - if (colo < 0 && slash < 0) { - throw new IllegalStateException("Http path '" + request.getPath() + "' looks invalid. " - + "Cannot extract proxy server data. Is it a regular request that " - + "should start with a slash?"); - } - if (colo < 0) { - request.setPort(80); - request.setHost(request.getPath().substring(0, slash)); - } else { - request.setHost(request.getPath().substring(0, colo)); - request.setPort(Integer.valueOf(request.getPath().substring(colo + 1, slash))); - } - request.setPath(request.getPath().substring(slash)); - } - } - - @Override - public boolean equals(Object other) { - Settings o = (Settings) other; - if (timeout != o.timeout || proxyPort != o.proxyPort - || (proxyHost == null ^ o.proxyHost == null) - || (proxyHost != null && !proxyHost.equals(o.proxyHost))) - { - return false; - } - return true; - } - - @Override - public int hashCode() { - return (proxyHost == null ? 0 : proxyHost.hashCode()) ^ proxyPort ^ Long.valueOf(timeout).hashCode(); - } - } - private final Executor executor; - private final SyncHttpClientFactory clientFactory; - private boolean closed = false; - private final int maxInstanceCacheSize; // Maximum number of currently unused instances. - private final Map<Settings, LinkedList<SyncHttpClient>> apacheInstances = new LinkedHashMap<Settings, LinkedList<SyncHttpClient>>() { - protected @Override boolean removeEldestEntry(Map.Entry<Settings,LinkedList<SyncHttpClient>> eldest) { - return getUnusedCacheSize() > maxInstanceCacheSize; - } - }; - - public ApacheAsyncHttpClient(Executor executor) { - this(executor, new SyncHttpClientFactory() { - @Override - public SyncHttpClient createInstance(String proxyHost, int proxyPort, long timeoutMs) { - return new ApacheHttpInstance(proxyHost, proxyPort, timeoutMs); - } - }); - } - - public ApacheAsyncHttpClient(Executor executor, SyncHttpClientFactory clientFactory) { - this.executor = executor; - this.clientFactory = clientFactory; - maxInstanceCacheSize = 16; - log.fine("Starting apache com.yahoo.vespa.clustercontroller.utils.communication.async HTTP client"); - } - - private SyncHttpClient getFittingInstance(Settings settings) { - synchronized (apacheInstances) { - if (closed) throw new IllegalStateException("Http client has been closed for business."); - LinkedList<SyncHttpClient> fittingInstances = apacheInstances.get(settings); - if (fittingInstances == null) { - fittingInstances = new LinkedList<>(); - apacheInstances.put(settings, fittingInstances); - } - if (fittingInstances.isEmpty()) { - return clientFactory.createInstance(settings.proxyHost, settings.proxyPort, settings.timeout); - } else { - return fittingInstances.removeFirst(); - } - } - } - private void insertInstance(Settings settings, SyncHttpClient instance) { - synchronized (apacheInstances) { - LinkedList<SyncHttpClient> fittingInstances = apacheInstances.get(settings); - if (closed || fittingInstances == null) { - instance.close(); - return; - } - fittingInstances.addLast(instance); - } - } - private int getUnusedCacheSize() { - int size = 0; - synchronized (apacheInstances) { - for (LinkedList<SyncHttpClient> list : apacheInstances.values()) { - size += list.size(); - } - } - return size; - } - - @Override - public AsyncOperation<HttpResult> execute(HttpRequest r) { - final HttpRequest request = r.clone(); // Gonna modify it to extract proxy information - final Settings settings = new Settings(request); - final SyncHttpClient instance = getFittingInstance(settings); - final AsyncOperationImpl<HttpResult> op = new AsyncOperationImpl<>(r.toString(), r.toString(true)); - executor.execute(new Runnable() { - @Override - public void run() { - HttpResult result; - Exception failure = null; - try{ - result = instance.execute(request); - } catch (Exception e) { - result = new HttpResult().setHttpCode(500, "Apache client failed to execute request."); - failure = e; - } - insertInstance(settings, instance); - // Must insert back instance before tagging operation complete to ensure a following - // call can reuse same instance - if (failure != null) { - op.setFailure(failure, result); - } else { - op.setResult(result); - } - } - }); - return op; - } - - @Override - public void close() { - synchronized (apacheInstances) { - closed = true; - for (LinkedList<SyncHttpClient> list : apacheInstances.values()) { - for (SyncHttpClient instance : list) { - instance.close(); - } - } - apacheInstances.clear(); - } - } - -} diff --git a/clustercontroller-apputil/src/main/java/com/yahoo/vespa/clustercontroller/apputil/communication/http/ApacheHttpInstance.java b/clustercontroller-apputil/src/main/java/com/yahoo/vespa/clustercontroller/apputil/communication/http/ApacheHttpInstance.java deleted file mode 100644 index 3eafd2ae1d5..00000000000 --- a/clustercontroller-apputil/src/main/java/com/yahoo/vespa/clustercontroller/apputil/communication/http/ApacheHttpInstance.java +++ /dev/null @@ -1,133 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.apputil.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest; -import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult; -import com.yahoo.vespa.clustercontroller.utils.communication.http.SyncHttpClient; -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.entity.StringEntity; -import org.apache.http.util.EntityUtils; - -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.logging.Logger; - -// NOTE: these are all deprecated: -import org.apache.http.conn.params.ConnRoutePNames; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; - -/** - * Synchronous http client using Apache commons. - */ -public class ApacheHttpInstance implements SyncHttpClient { - - private static final Logger log = Logger.getLogger(ApacheHttpInstance.class.getName()); - DefaultHttpClient client; - - public ApacheHttpInstance(String proxyHost, int proxyPort, long timeoutMs) { - if (timeoutMs > Integer.MAX_VALUE) throw new IllegalArgumentException("Cannot handle timeout not contained in an integer"); - HttpParams httpParams = new BasicHttpParams(); - HttpConnectionParams.setConnectionTimeout(httpParams, (int) timeoutMs); - HttpConnectionParams.setSoTimeout(httpParams, (int) timeoutMs); - - if (proxyHost != null && !proxyHost.isEmpty()) { - httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort, "http")); - } - - client = new DefaultHttpClient(httpParams); - } - - /** This function is not threadsafe. */ - public HttpResult execute(HttpRequest r) { - HttpRequest.HttpOp op = r.getHttpOperation(); - if (op == null) { - if (r.getPostContent() != null) { - log.fine("Request " + r + " has no HTTP function specified. Assuming POST as post content is set."); - op = HttpRequest.HttpOp.POST; - } else { - log.fine("Request " + r + " has no HTTP function specified. Assuming GET as post content is set."); - op = HttpRequest.HttpOp.GET; - } - } - if (r.getPostContent() != null - && !(op.equals(HttpRequest.HttpOp.POST) || op.equals(HttpRequest.HttpOp.PUT))) - { - throw new IllegalStateException("A " + op + " operation can't have content"); - } - try { - HttpHost target = new HttpHost(r.getHost(), r.getPort(), "http"); - org.apache.http.HttpRequest req = null; - - String path = r.getPath(); - int uriOption = 0; - for (HttpRequest.KeyValuePair option : r.getUrlOptions()) { - path += (++uriOption == 1 ? '?' : '&'); - path += option.getKey() + '=' + option.getValue(); - } - - switch (op) { - case POST: - HttpPost post = new HttpPost(path); - if (r.getPostContent() != null) { - post.setEntity(new StringEntity(r.getPostContent().toString())); - } - req = post; - break; - case GET: - req = new HttpGet(path); - break; - case PUT: - HttpPut put = new HttpPut(path); - put.setEntity(new StringEntity(r.getPostContent().toString())); - req = put; - break; - case DELETE: - req = new HttpDelete(path); - break; - } - - for (HttpRequest.KeyValuePair header : r.getHeaders()) { - req.addHeader(header.getKey(), header.getValue()); - } - - HttpResponse rsp = client.execute(target, req); - HttpEntity entity = rsp.getEntity(); - - HttpResult result = new HttpResult(); - result.setHttpCode(rsp.getStatusLine().getStatusCode(), rsp.getStatusLine().getReasonPhrase()); - - if (entity != null) { - result.setContent(EntityUtils.toString(entity)); - } - for (Header header : rsp.getAllHeaders()) { - result.addHeader(header.getName(), header.getValue()); - } - - return result; - } catch (Exception e) { - HttpResult result = new HttpResult(); - - StringWriter writer = new StringWriter(); - e.printStackTrace(new PrintWriter(writer)); - writer.flush(); - - result.setHttpCode(500, "Got exception " + writer.toString() + " when sending message."); - return result; - } - } - - public void close() { - client.getConnectionManager().shutdown(); - } - -} |