Skip to content

Commit

Permalink
Use connection pooling to avoid using up all connections during massi…
Browse files Browse the repository at this point in the history
…ve ingestions with URILookup
  • Loading branch information
Paul Cuddihy committed May 5, 2021
1 parent 4857142 commit bcd14d0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
Expand Down Expand Up @@ -154,7 +155,7 @@ public JSONObject executeUpload(byte[] owl, String filename) throws Authorizatio
this.authorizeUpload();

HttpHost targetHost = this.buildHttpHost();
CloseableHttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
HttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
BasicHttpContext localcontext = this.buildHttpContext(targetHost);
HttpPost httppost = new HttpPost(this.getUploadURL());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
Expand Down Expand Up @@ -150,7 +151,7 @@ public JSONObject executeUpload(byte[] owl, String filename) throws Authorizatio
this.authorizeUpload();

HttpHost targetHost = this.buildHttpHost();
CloseableHttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
HttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
BasicHttpContext localcontext = this.buildHttpContext(targetHost);
HttpPost httppost = new HttpPost(this.getUploadURL());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
Expand All @@ -67,16 +69,12 @@

import com.ge.research.semtk.auth.AuthorizationException;
import com.ge.research.semtk.auth.AuthorizationManager;
import com.ge.research.semtk.belmont.ValueConstraint;
import com.ge.research.semtk.belmont.XSDSupportedType;
import com.ge.research.semtk.ontologyTools.OntologyInfo;
import com.ge.research.semtk.resultSet.GeneralResultSet;
import com.ge.research.semtk.resultSet.NodeGroupResultSet;
import com.ge.research.semtk.resultSet.SimpleResultSet;
import com.ge.research.semtk.resultSet.Table;
import com.ge.research.semtk.resultSet.TableResultSet;
import com.ge.research.semtk.sparqlX.FusekiSparqlEndpointInterface;
import com.ge.research.semtk.sparqlX.VirtuosoSparqlEndpointInterface;
import com.ge.research.semtk.utility.LocalLogger;
import com.ge.research.semtk.utility.Utility;

Expand Down Expand Up @@ -134,6 +132,14 @@ public abstract class SparqlEndpointInterface {

protected int retries = 0;

private static HttpClientConnectionManager manager = buildConnectionManager();

private static HttpClientConnectionManager buildConnectionManager() {
PoolingHttpClientConnectionManager ret = new PoolingHttpClientConnectionManager();
ret.setDefaultMaxPerRoute(10);
return ret;
}

/**
* Constructor
* @param serverAndPort e.g. "http://localhost:2420"
Expand Down Expand Up @@ -708,7 +714,7 @@ public JSONObject executeQueryPost(String query, SparqlResultTypes resultType) t

// get client, adding userName/password credentials if any exist
HttpHost targetHost = this.buildHttpHost();
CloseableHttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
HttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
// get context with digest auth if there is a userName, else null context
BasicHttpContext localcontext = this.buildHttpContext(targetHost);

Expand Down Expand Up @@ -742,9 +748,8 @@ public JSONObject executeQueryPost(String query, SparqlResultTypes resultType) t
// parse response
return this.parseResponse(resultType, responseTxt);
} finally {
httpclient.close();
if (entity != null) {
entity.getContent().close();
EntityUtils.consume(entity);
}
}
}
Expand Down Expand Up @@ -828,13 +833,14 @@ public void dropGraph() throws Exception {
}

/**
* Get an CloseableHttpClient, handling credentials and HTTPS if needed
* Get an HttpClient, handling credentials and HTTPS if needed
* NOTE: for HTTPS connections, does not validate certificate chain.
* @schemeName http or https
*/
protected CloseableHttpClient buildHttpClient(String schemeName) throws Exception {
protected HttpClient buildHttpClient(String schemeName) throws Exception {

HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setConnectionManager(SparqlEndpointInterface.manager);

// add userName and password, if any
if (this.isAuth()) {
Expand Down Expand Up @@ -1098,7 +1104,7 @@ private JSONObject executeQueryAuthGet(String queryAndUrl, SparqlResultTypes res
}

HttpHost targetHost = this.buildHttpHost();
CloseableHttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
HttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
BasicHttpContext localcontext = this.buildHttpContext(targetHost);

LocalLogger.logToStdErr(queryAndUrl);
Expand All @@ -1118,8 +1124,7 @@ private JSONObject executeQueryAuthGet(String queryAndUrl, SparqlResultTypes res
try {
return this.parseResponse(resultType, responseTxt);
} finally {
httpclient.close();
entity.getContent().close();
EntityUtils.consume(entity);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import com.ge.research.semtk.resultSet.SimpleResultSet;
import com.ge.research.semtk.auth.AuthorizationException;
import com.ge.research.semtk.auth.AuthorizationManager;
import com.ge.research.semtk.sparqlX.SparqlEndpointInterface;

/**
* Interface to Virtuoso SPARQL endpoint
Expand Down Expand Up @@ -144,7 +142,7 @@ public JSONObject executeUpload(byte[] owl) throws AuthorizationException, Excep
this.authorizeUpload();

HttpHost targetHost = this.buildHttpHost();
CloseableHttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
HttpClient httpclient = this.buildHttpClient(targetHost.getSchemeName());
BasicHttpContext localcontext = this.buildHttpContext(targetHost);
HttpPost httppost = new HttpPost(this.getUploadURL());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public static void main(String[] args) throws Exception {
try {

//addSimpleRows(10, 10000);
addBatteryDescriptions(1000, 500000);
//addBatteryDescriptions(1000, 500000);
addBatteryDescriptions(40000, 75000);
//addBatteryDescriptionsVaryingThreadsAndSize(0);
//addSimpleBiggerRows(10, 50000);

Expand Down Expand Up @@ -234,7 +235,7 @@ private static void addBatteryDescriptions(int rows_per_pass, int max_triples) t
Dataset ds1 = new CSVDataset(content1.toString(), true);
DataLoader dl1 = new DataLoader(sgJson1, ds1, "dba", "dba");
startTask("addBatteryDescriptions load simple, rows, " + rows_per_pass + ",total rows," + total_rows);
dl1.importData(NO_PRECHECK);
dl1.importData(PRECHECK);
lastSec[0] = endTask();
}

Expand All @@ -244,7 +245,7 @@ private static void addBatteryDescriptions(int rows_per_pass, int max_triples) t
DataLoader dl2 = new DataLoader(sgJson2, ds2, "dba", "dba");
dl2.setLogPerformance(LOG_QUERY_PERFORMANCE);
startTask("addBatteryDescriptions load lookup class, rows," + rows_per_pass/2 + ",total rows," + total_rows);
dl2.importData(NO_PRECHECK);
dl2.importData(PRECHECK);
lastSec[1] = endTask();
}

Expand All @@ -254,7 +255,7 @@ private static void addBatteryDescriptions(int rows_per_pass, int max_triples) t
DataLoader dl3 = new DataLoader(sgJson3, ds3, "dba", "dba");
dl3.setLogPerformance(LOG_QUERY_PERFORMANCE);
startTask("addBatteryDescriptions load lookup superclass, rows," + rows_per_pass /2 + ",total rows," + total_rows);
dl3.importData(NO_PRECHECK);
dl3.importData(PRECHECK);
lastSec[2] = endTask();
}

Expand Down

0 comments on commit bcd14d0

Please sign in to comment.