Skip to content

Commit

Permalink
Merge pull request #81 from nicolasmoreau/dev
Browse files Browse the repository at this point in the history
Modification in PreviewThread and PreviewManager classes to handle qu…
  • Loading branch information
nicolasmoreau authored Jun 13, 2018
2 parents ba4c09c + 97a6a3f commit f2e12c1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public HttpHeadResponse(){
public HttpHeadResponse(String ivoaID, HttpURLConnection connection) {
this.ivoaID = ivoaID;
this.fullQueryURL = connection.getURL().toString();
this.status = retrieveStatus(connection);
this.status = retrieveStatus(connection);
retrieveHeaders(connection);
if (truncated>0 && truncated<100)
this.status=Response.TRUNCATED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,61 @@

@Name("preview")
@Scope(ScopeType.PAGE)
public class PreviewManager implements Serializable{
public class PreviewManager implements Serializable {

private static final long serialVersionUID = 2029452631857959114L;

@Logger
Log log;

@In transient QueryData queryData;
@In
transient QueryData queryData;

@In(create = true)
transient RegistryFacade registryFacade;

@In(create = true)
transient ConsumerRequestRegistry consumers;

//private volatile boolean completeEventCalled = false;

@In(create=true) transient RegistryFacade registryFacade;

@In(create=true) transient ConsumerRequestRegistry consumers;

private volatile boolean completeEventCalled=false;

private Collection<Future<HttpHeadResponse>> nodeFutureResponses = new ArrayList<Future<HttpHeadResponse>>();
private long startTime;

private long percentDone;

public void initiate(){

private Boolean securedProtocolsAreSet = false;

public void initiate() {
this.setSSLProtocols();
if (!nodeFutureResponses.isEmpty())
return;

Collection<String> activeNodes = queryData.getActiveNodes();

if (activeNodes.isEmpty())
return;

ExecutorService executor = Executors.newFixedThreadPool(activeNodes.size());

for (String ivoaID:activeNodes){
try{
nodeFutureResponses.add(executor.submit(new PreviewThread(ivoaID,registryFacade.getVamdcTapMirrors(ivoaID),queryData.getQueryString())));
}catch (IllegalArgumentException e){
ExecutorService executor = Executors.newFixedThreadPool(activeNodes
.size());

for (String ivoaID : activeNodes) {
try {
nodeFutureResponses.add(executor.submit(new PreviewThread(
ivoaID, registryFacade.getVamdcTapMirrors(ivoaID),
queryData.getQueryString())));
} catch (IllegalArgumentException e) {
}
}

executor.shutdown();
startTime = new Date().getTime();
}


public List<HttpHeadResponse> getNodes(){
TreeSet<HttpHeadResponse> nodes = new TreeSet<HttpHeadResponse>(new HttpHeadResponseComparator());
for (Future<HttpHeadResponse> task:nodeFutureResponses){
if (task.isDone()&& !task.isCancelled()){
public List<HttpHeadResponse> getNodes() {
TreeSet<HttpHeadResponse> nodes = new TreeSet<HttpHeadResponse>(
new HttpHeadResponseComparator());
for (Future<HttpHeadResponse> task : nodeFutureResponses) {
if (task.isDone() && !task.isCancelled()) {
try {
HttpHeadResponse response = task.get();
nodes.add(response);
Expand All @@ -91,78 +98,91 @@ public List<HttpHeadResponse> getNodes(){
return new ArrayList<HttpHeadResponse>(nodes);
}

private class HttpHeadResponseComparator implements Comparator<HttpHeadResponse>{
private class HttpHeadResponseComparator implements
Comparator<HttpHeadResponse> {

@Override
public int compare(HttpHeadResponse o1, HttpHeadResponse o2) {
if (o1==null || o2==null)
if (o1 == null || o2 == null)
return 0;
Integer value1 = Integer.valueOf(o1.getStatus().ordinal());
Integer value2 = Integer.valueOf(o2.getStatus().ordinal());
int compare = value1.compareTo(value2);
if (compare!=0)
return compare;
else if(o1.getProcesses()!=o2.getProcesses())
return o2.getProcesses()-o1.getProcesses();
else
return o1.getIvoaID().compareTo(o2.getIvoaID());
if (compare != 0)
return compare;
else if (o1.getProcesses() != o2.getProcesses())
return o2.getProcesses() - o1.getProcesses();
else
return o1.getIvoaID().compareTo(o2.getIvoaID());

}



}


public boolean isDone(){
if(this.percentDone <= 100){
for (Future<HttpHeadResponse> task:nodeFutureResponses){
if (!task.isDone()){
public boolean isDone() {
if (this.percentDone <= 100) {
for (Future<HttpHeadResponse> task : nodeFutureResponses) {
if (!task.isDone()) {
return false;
}
}
}
}

return true;
}
public Long getPercentsDone(){

public Long getPercentsDone() {
Long result;
if (isDone()){
result=101L;
} else{
if (isDone()) {
result = 101L;
} else {
Long now = new Date().getTime();
result = (100L*(now-startTime)/Settings.HTTP_HEAD_TIMEOUT.getInt());
result = (100L * (now - startTime) / Settings.HTTP_HEAD_TIMEOUT
.getInt());
}
this.percentDone = result;
return result;
}

public String getStringStatus(){
public String getStringStatus() {
if (isDone())
return "Done";
return ""+getNodes().size()+" nodes of "+nodeFutureResponses.size()+" responded";
return "" + getNodes().size() + " nodes of "
+ nodeFutureResponses.size() + " responded";
}
public void cancel(){
for (Future<HttpHeadResponse> task:nodeFutureResponses){

public void cancel() {
for (Future<HttpHeadResponse> task : nodeFutureResponses) {
if (!task.isDone())
task.cancel(true);
}
}
public void clear(){

public void clear() {
cancel();
nodeFutureResponses=new ArrayList<Future<HttpHeadResponse>>();
nodeFutureResponses = new ArrayList<Future<HttpHeadResponse>>();
}
public String refine(){

public String refine() {
clear();
if(!queryData.isGuidedQuery())
if (!queryData.isGuidedQuery())
return RedirectPage.QUERY;
else
return RedirectPage.QUERYTREE;
}


/**
* Set SSL protocols to be used.
* Fix a bug found when switching from java 7 to 8 :
* If this list is not set, the HTTPS request failed with java 8 but not
* with java 7
*
*/
private void setSSLProtocols() {
if (!securedProtocolsAreSet) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
this.securedProtocolsAreSet = true;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,71 @@
import java.util.Collection;
import java.util.concurrent.Callable;

import javax.net.ssl.HttpsURLConnection;

import org.jboss.seam.annotations.Logger;
import org.jboss.seam.log.Log;
import org.vamdc.portal.Settings;
import org.vamdc.portal.entity.query.HttpHeadResponse;

public class PreviewThread implements Callable<HttpHeadResponse> {


public class PreviewThread implements Callable<HttpHeadResponse>{
@Logger
Log log;

private String ivoaID;
private String query;
private Collection<URL> mirrors;
public PreviewThread(String ivoaID, Collection<URL> mirrors, String query){

public PreviewThread(String ivoaID, Collection<URL> mirrors, String query) {
this.ivoaID = ivoaID;
this.query = query;
this.mirrors = mirrors;
this.mirrors = mirrors;
}

private URL getQuery(URL mirror) {
URL result=null;
URL result = null;
try {
result = new URL(mirror+"sync?LANG=VSS2&REQUEST=doQuery&FORMAT=XSAMS&QUERY="+URLEncoder.encode(query,"UTF-8"));
result = new URL(mirror
+ "sync?LANG=VSS2&REQUEST=doQuery&FORMAT=XSAMS&QUERY="
+ URLEncoder.encode(query, "UTF-8"));
} catch (MalformedURLException e) {
log.info(e);
} catch (UnsupportedEncodingException e) {
log.info(e);
}

return result;
}


/**
* Send a HEAD request to a node by testing its mirrors
*/
@Override
public HttpHeadResponse call() throws Exception {
HttpHeadResponse response =null;
for (URL mirror:mirrors){
HttpHeadResponse response = null;
for (URL mirror : mirrors) {
HttpURLConnection connection = null;
URL queryURL = getQuery(mirror);
HttpURLConnection connection = (HttpURLConnection) queryURL.openConnection();


//http request
if (!mirror.getProtocol().equals("https")) {
connection = (HttpURLConnection) queryURL.openConnection();
}
//https request
else {
connection = (HttpsURLConnection) queryURL.openConnection();
}

connection.setRequestMethod("HEAD");
connection.setReadTimeout(Settings.HTTP_HEAD_TIMEOUT.getInt());
response = new HttpHeadResponse(ivoaID,connection);

response = new HttpHeadResponse(ivoaID, connection);
if (response.isOk())
return response;
}

return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,20 @@ public void querySpecies(String ivoaId, SpeciesResult resultBean){
resultBean.setReady(true);
return;
}

System.out.println("### test ");
resultBean.setMirrorCount(mirrors.size());
for(URL node:mirrors){
resultBean.setMessage("Timeout : query execution was too long.");
resultBean.nextMirror();
URL queryUrl=node;
System.out.println("query");
System.out.println(query);
System.out.println(queryUrl);
try {
queryUrl = new URL(node.toExternalForm()+query);

System.out.print("queryUrl");
System.out.print(queryUrl);
resultBean.setFormattedResult( formatRequestResult(queryUrl));
resultBean.setMessage("");
resultBean.setReady(true);
Expand All @@ -85,6 +91,8 @@ public void querySpecies(String ivoaId, SpeciesResult resultBean){
resultBean.setMessage("Incorrect service URL "+queryUrl);
} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
resultBean.setMessage("Error while proceeding request");
} catch (TransformerException e) {
// TODO Auto-generated catch block
Expand All @@ -110,7 +118,10 @@ public void querySpecies(String ivoaId, SpeciesResult resultBean){
* @throws TransformerException
*/
private static String formatRequestResult(URL url) throws IOException, TransformerException, PortalHttpException{
System.out.print("###");
System.out.print(url);
HttpURLConnection c = (HttpURLConnection)url.openConnection();

if(c.getResponseCode() == 200){
c.setConnectTimeout(Settings.HTTP_CONNECT_TIMEOUT.getInt());
c.setReadTimeout(Settings.HTTP_DATA_TIMEOUT.getInt());
Expand Down

0 comments on commit f2e12c1

Please sign in to comment.