Fabio Sinibaldi 2016-08-30 15:39:15 +00:00
parent 7f8b2880fd
commit 60b717d1f6
16 changed files with 521 additions and 91 deletions

View File

@ -16,6 +16,7 @@ import org.gcube.data.transfer.library.faults.SourceNotSetException;
import org.gcube.data.transfer.library.faults.UnreachableNodeException; import org.gcube.data.transfer.library.faults.UnreachableNodeException;
import org.gcube.data.transfer.library.transferers.Transferer; import org.gcube.data.transfer.library.transferers.Transferer;
import org.gcube.data.transfer.library.transferers.TransfererBuilder; import org.gcube.data.transfer.library.transferers.TransfererBuilder;
import org.gcube.data.transfer.model.TransferCapabilities;
@Slf4j @Slf4j
public class DataTransferClient { public class DataTransferClient {
@ -36,6 +37,11 @@ public class DataTransferClient {
return new DataTransferClient(TransfererBuilder.getTransfererByhostingNodeId(id)); return new DataTransferClient(TransfererBuilder.getTransfererByhostingNodeId(id));
} }
public TransferCapabilities getDestinationCapabilities() throws InitializationException{
return this.transferer.getDestinationCapabilities();
}
@Synchronized("transferer") @Synchronized("transferer")
public TransferResult localFile(String path) throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{ public TransferResult localFile(String path) throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
if(transferer==null) throw new RuntimeException("Transferer not set, please set destination before trying to transfer"); if(transferer==null) throw new RuntimeException("Transferer not set, please set destination before trying to transfer");

View File

@ -11,11 +11,11 @@ public class CapabilitiesCache extends TTLCache<TransferCapabilities> {
private static CapabilitiesCache instance=null; private static CapabilitiesCache instance=null;
@Synchronized // @Synchronized
public static CapabilitiesCache getInstance(){ // public static CapabilitiesCache getInstance(){
if(instance==null)instance=new CapabilitiesCache(); // if(instance==null)instance=new CapabilitiesCache();
return instance; // return instance;
} // }
private CapabilitiesCache(){ private CapabilitiesCache(){
super(5*60*1000l,2*60*1000l,"Capabilities"); super(5*60*1000l,2*60*1000l,"Capabilities");
@ -23,7 +23,7 @@ public class CapabilitiesCache extends TTLCache<TransferCapabilities> {
@Override @Override
protected TransferCapabilities getNew(String id) { protected TransferCapabilities getNew(String id) throws Exception{
log.debug("Getting capabilties for host "+id); log.debug("Getting capabilties for host "+id);
return new Client(id).getCapabilties(); return new Client(id).getCapabilties();
} }

View File

@ -5,11 +5,9 @@ import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.gcube.data.transfer.model.TransferCapabilities;
import lombok.EqualsAndHashCode;
import lombok.Synchronized; import lombok.Synchronized;
import lombok.ToString; import lombok.ToString;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -21,26 +19,32 @@ public abstract class TTLCache<T> {
// STATIC // STATIC
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); // private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1,new ThreadFactory(){
// public Thread newThread(Runnable r) {
// Thread t = Executors.defaultThreadFactory().newThread(r);
// t.setDaemon(true);
// return t;
// }
// });
private static final HashSet<TTLCache<?>> createdMaps=new HashSet<TTLCache<?>>(); private static final HashSet<TTLCache<?>> createdMaps=new HashSet<TTLCache<?>>();
static { static {
scheduler.scheduleAtFixedRate(new Runnable() { // scheduler.scheduleAtFixedRate(new Runnable() {
//
//
//
@Override // @Override
public void run() { // public void run() {
log.debug("Running Maps Cleaner, maps count : "+createdMaps.size()); // log.debug("Running Maps Cleaner, maps count : "+createdMaps.size());
int removed=0; // int removed=0;
for(TTLCache<?> theMap:createdMaps) // for(TTLCache<?> theMap:createdMaps)
theMap.purgeItems(); // theMap.purgeItems();
log.debug("Removed "+removed+" old tickets"); // log.debug("Removed "+removed+" old tickets");
//
} // }
}, 3, 3, TimeUnit.MINUTES); // }, 3, 3, TimeUnit.MINUTES);
//
} }
@ -65,7 +69,7 @@ public abstract class TTLCache<T> {
private String cacheName; private String cacheName;
@Synchronized @Synchronized
public T getObject(String id){ public T getObject(String id) throws Exception{
if(!theMap.contains(id)||System.currentTimeMillis()-theMap.get(id).getCreationTime()>objectTTL) if(!theMap.contains(id)||System.currentTimeMillis()-theMap.get(id).getCreationTime()>objectTTL)
theMap.put(id, new TTLContainer<T>(getNew(id))); theMap.put(id, new TTLContainer<T>(getNew(id)));
return theMap.get(id).getTheObject(); return theMap.get(id).getTheObject();
@ -85,6 +89,6 @@ public abstract class TTLCache<T> {
return removed; return removed;
} }
protected abstract T getNew(String id); protected abstract T getNew(String id) throws Exception;
} }

View File

@ -4,56 +4,93 @@ import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity; import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget; import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBElement; import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.gcube.data.transfer.library.faults.CommunicationException;
import org.gcube.data.transfer.library.faults.DataTransferException;
import org.gcube.data.transfer.library.faults.RemoteServiceException;
import org.gcube.data.transfer.library.faults.ServiceNotFoundException;
import org.gcube.data.transfer.model.ServiceConstants; import org.gcube.data.transfer.model.ServiceConstants;
import org.gcube.data.transfer.model.TransferCapabilities; import org.gcube.data.transfer.model.TransferCapabilities;
import org.gcube.data.transfer.model.TransferRequest; import org.gcube.data.transfer.model.TransferRequest;
import org.gcube.data.transfer.model.TransferTicket; import org.gcube.data.transfer.model.TransferTicket;
import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.ClientConfig;
import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class Client { public class Client {
private static ClientConfig config=null; private static ClientConfig config=null;
static{ static{
log.debug("Creating configuration .."); log.debug("Creating configuration ..");
config=new ClientConfig(); config=new ClientConfig();
config.register(AuthorizationFilter.class); config.register(AuthorizationFilter.class);
config.register(JAXBElement.class);
} }
private String endpoint; private String endpoint;
private WebTarget rootTarget; private WebTarget rootTarget;
public Client(String endpoint){ public Client(String endpoint) throws ServiceNotFoundException{
log.debug("Creating client for base "+endpoint); try{
this.endpoint=endpoint+""; log.debug("Creating client for base "+endpoint);
rootTarget= ClientBuilder.newClient(config).target(endpoint).path("/data-transfer-service/").path(ServiceConstants.APPLICATION_PATH); this.endpoint=endpoint+"";
rootTarget= ClientBuilder.newClient(config).target(endpoint).path("data-transfer-service").path(ServiceConstants.APPLICATION_PATH);
// checkResponse(rootTarget.request().get());
log.debug("Root Taget IS {} ",rootTarget.getUri());
}catch(Exception e){
throw new ServiceNotFoundException(e);
}
} }
public String getEndpoint() { public String getEndpoint() {
return endpoint; return endpoint;
} }
public TransferCapabilities getCapabilties(){ public TransferCapabilities getCapabilties() throws CommunicationException{
log.debug("Getting capabilities to {} ",endpoint); WebTarget capabilitiesTarget=rootTarget.path(ServiceConstants.CAPABILTIES_SERVLET_NAME);
return rootTarget.path(ServiceConstants.CAPABILTIES_SERVLET_NAME).request(MediaType.APPLICATION_XML_TYPE).get(TransferCapabilities.class); log.debug("Getting capabilities from {}, path is {} ",endpoint,capabilitiesTarget.getUri());
try{
Response resp=capabilitiesTarget.request().accept(MediaType.APPLICATION_XML_TYPE).get();
checkResponse(resp);
return resp.readEntity(TransferCapabilities.class);
}catch(Exception e){
throw new CommunicationException(e);
}
} }
public TransferTicket submit(TransferRequest request){ public TransferTicket submit(TransferRequest request) throws RemoteServiceException{
log.debug("Sending request {} to {}",request,endpoint); log.debug("Sending request {} to {}",request,endpoint);
return rootTarget.path(ServiceConstants.REQUESTS_SERVLET_NAME).request(MediaType.APPLICATION_XML_TYPE).post(Entity.entity(request,MediaType.APPLICATION_XML),TransferTicket.class); try{
Response resp=rootTarget.path(ServiceConstants.REQUESTS_SERVLET_NAME).request(MediaType.APPLICATION_XML_TYPE).post(Entity.entity(request,MediaType.APPLICATION_XML));
checkResponse(resp);
return resp.readEntity(TransferTicket.class);
}catch(Exception e){
throw new RemoteServiceException(e);
}
} }
public TransferTicket getTransferStatus(String transferId){ public TransferTicket getTransferStatus(String transferId) throws RemoteServiceException{
log.debug("Requesting transfer status [id = {}, endpoint={}]",transferId,endpoint); log.debug("Requesting transfer status [id = {}, endpoint={}]",transferId,endpoint);
return rootTarget.path(ServiceConstants.STATUS_SERVLET_NAME).path(transferId).request(MediaType.APPLICATION_XML).get(TransferTicket.class); try{
Response resp=rootTarget.path(ServiceConstants.STATUS_SERVLET_NAME).path(transferId).request(MediaType.APPLICATION_XML).get();
checkResponse(resp);
return resp.readEntity(TransferTicket.class);
}catch(Exception e){
throw new RemoteServiceException(e);
}
}
protected void checkResponse(Response toCheck) throws Exception{
switch(toCheck.getStatusInfo().getFamily()){
case SUCCESSFUL : break;
default : throw new Exception("Unexpected Response code : "+toCheck.getStatus());
}
} }
} }

View File

@ -0,0 +1,35 @@
package org.gcube.data.transfer.library.faults;
public class CommunicationException extends DataTransferException {
/**
*
*/
private static final long serialVersionUID = 1L;
public CommunicationException() {
// TODO Auto-generated constructor stub
}
public CommunicationException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CommunicationException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public CommunicationException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public CommunicationException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.data.transfer.library.faults;
public class RemoteServiceException extends DataTransferException {
/**
*
*/
private static final long serialVersionUID = 5320978791087129997L;
public RemoteServiceException() {
// TODO Auto-generated constructor stub
}
public RemoteServiceException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public RemoteServiceException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public RemoteServiceException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public RemoteServiceException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -19,7 +19,11 @@ public class StorageSource extends Source<String> {
@Override @Override
public boolean validate() throws InvalidSourceException { public boolean validate() throws InvalidSourceException {
if(!StorageUtils.checkStorageId(id)) throw new InvalidSourceException("Invalid storage ID "+id); try{
if(!StorageUtils.checkStorageId(id)) throw new Exception("Not valid");
}catch(Exception e){
throw new InvalidSourceException("Invalid storage ID "+id);
}
return true; return true;
} }

View File

@ -7,9 +7,11 @@ import lombok.extern.slf4j.Slf4j;
import org.gcube.data.transfer.library.TransferResult; import org.gcube.data.transfer.library.TransferResult;
import org.gcube.data.transfer.library.client.Client; import org.gcube.data.transfer.library.client.Client;
import org.gcube.data.transfer.library.faults.CommunicationException;
import org.gcube.data.transfer.library.faults.FailedTransferException; import org.gcube.data.transfer.library.faults.FailedTransferException;
import org.gcube.data.transfer.library.faults.InitializationException; import org.gcube.data.transfer.library.faults.InitializationException;
import org.gcube.data.transfer.library.faults.InvalidSourceException; import org.gcube.data.transfer.library.faults.InvalidSourceException;
import org.gcube.data.transfer.library.faults.RemoteServiceException;
import org.gcube.data.transfer.library.faults.SourceNotSetException; import org.gcube.data.transfer.library.faults.SourceNotSetException;
import org.gcube.data.transfer.library.model.LocalSource; import org.gcube.data.transfer.library.model.LocalSource;
import org.gcube.data.transfer.library.model.Source; import org.gcube.data.transfer.library.model.Source;
@ -29,9 +31,9 @@ public abstract class Transferer {
this.client=client; this.client=client;
} }
protected Source source=null; protected Source source=null;
protected boolean prepared=false; protected boolean prepared=false;
@ -75,21 +77,25 @@ public abstract class Transferer {
} }
protected TransferResult doTheTransfer(TransferRequest request) throws FailedTransferException{ protected TransferResult doTheTransfer(TransferRequest request) throws FailedTransferException{
TransferTicket submissionResponse= client.submit(request); try{
boolean continuePolling=true; TransferTicket submissionResponse= client.submit(request);
TransferTicket ticket=null; boolean continuePolling=true;
do{ TransferTicket ticket=null;
ticket=client.getTransferStatus(submissionResponse.getId()); do{
System.out.println("Status : "+ticket); ticket=client.getTransferStatus(submissionResponse.getId());
continuePolling=ticket.getStatus().equals(Status.PENDING)||ticket.getStatus().equals(Status.TRANSFERRING)||ticket.getStatus().equals(Status.WAITING); System.out.println("Status : "+ticket);
try{ continuePolling=ticket.getStatus().equals(Status.PENDING)||ticket.getStatus().equals(Status.TRANSFERRING)||ticket.getStatus().equals(Status.WAITING);
Thread.sleep(500); try{
}catch(InterruptedException e){} Thread.sleep(500);
}while(continuePolling); }catch(InterruptedException e){}
if(ticket.getStatus().equals(Status.ERROR)) throw new FailedTransferException("Remote Message : "+ticket.getMessage()); }while(continuePolling);
if(ticket.getStatus().equals(Status.STOPPED)) throw new FailedTransferException("Stopped transfer : "+ticket.getMessage()); if(ticket.getStatus().equals(Status.ERROR)) throw new FailedTransferException("Remote Message : "+ticket.getMessage());
long elapsedTime=System.currentTimeMillis()-ticket.getSubmissionTime().value.getTimeInMillis(); if(ticket.getStatus().equals(Status.STOPPED)) throw new FailedTransferException("Stopped transfer : "+ticket.getMessage());
return new TransferResult(source, client.getEndpoint(), elapsedTime, ticket.getTransferredBytes(), ticket.getDestinationFileName()); long elapsedTime=System.currentTimeMillis()-ticket.getSubmissionTime().value.getTimeInMillis();
return new TransferResult(source, client.getEndpoint(), elapsedTime, ticket.getTransferredBytes(), ticket.getDestinationFileName());
}catch(RemoteServiceException e){
throw new FailedTransferException(e);
}
} }
protected void checkSource() throws SourceNotSetException, InvalidSourceException{ protected void checkSource() throws SourceNotSetException, InvalidSourceException{
@ -104,12 +110,16 @@ public abstract class Transferer {
prepared=true; prepared=true;
} }
protected void clean(){ protected void clean(){
} }
public TransferCapabilities getDestinationCapabilities(){ public TransferCapabilities getDestinationCapabilities() throws InitializationException {
return client.getCapabilties(); try{
return client.getCapabilties();
}catch(Exception e){
throw new InitializationException(e);
}
} }
} }

View File

@ -44,12 +44,12 @@ public class TransfererBuilder {
// //
// if(!Utils.pingURL(finalHost, timeout)) throw new ServiceNotFoundException("No DT Service found @ "+finalHost); // if(!Utils.pingURL(finalHost, timeout)) throw new ServiceNotFoundException("No DT Service found @ "+finalHost);
// log.debug("Host is ok, getting targetCapabilities"); // log.debug("Host is ok, getting targetCapabilities");
TransferCapabilities cap=CapabilitiesCache.getInstance().getObject(baseUrl); // TransferCapabilities cap=CapabilitiesCache.getInstance().getObject(baseUrl);
// TODO determine method by capabilities checking // TODO determine method by capabilities checking
return new HTTPTransferer(new Client(baseUrl)); return new HTTPTransferer(new Client(baseUrl));
}catch(MalformedURLException e){ }catch(Exception e){
throw new ServiceNotFoundException(e); throw new ServiceNotFoundException(e);
} }
} }

View File

@ -7,6 +7,7 @@ import java.io.FileNotFoundException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.service.IClient; import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import org.gcube.contentmanager.storageclient.wrapper.AccessType; import org.gcube.contentmanager.storageclient.wrapper.AccessType;
@ -29,7 +30,8 @@ public class StorageUtils {
} }
public static final boolean checkStorageId(String id){ public static final boolean checkStorageId(String id){
return ObjectId.isValid(id); ScopeProvider.instance.get();
return getClient().getHttpUrl().RFile(id)!=null;
} }
public static final String getUrlById(String id){ public static final String getUrlById(String id){

View File

@ -0,0 +1,19 @@
package org.gcube.data.transfer.library;
import org.gcube.data.transfer.model.TransferCapabilities;
public class ErrorReport extends TransferReport {
String id;
public ErrorReport(String id) {
super(null);
this.id=id;
}
@Override
public String print() {
return "ERROR : "+id;
}
}

View File

@ -0,0 +1,156 @@
package org.gcube.data.transfer.library;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.transfer.library.TransferReport.ReportType;
import org.gcube.data.transfer.library.faults.FailedTransferException;
import org.gcube.data.transfer.library.faults.InitializationException;
import org.gcube.data.transfer.library.faults.InvalidSourceException;
import org.gcube.data.transfer.library.faults.SourceNotSetException;
import ch.qos.logback.core.util.ExecutorServiceUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MultipleTransferBenchmark {
static List<String> endpoints;
static List<String> nodeIds;
static List<String> files;
static List<String> uris;
static List<String> storageIds;
static ExecutorService executor=ExecutorServiceUtil.newExecutorService();
static CountDownLatch doneSignal;
static ArrayList<TransferReport> reports=new ArrayList<>();
static String scope="/gcube/devsec";
static{
endpoints=Arrays.asList(new String[]{
"http://node4-d-d4s.d4science.org:80/data-transfer-service/gcube/service",
"http://node3-d-d4s.d4science.org:80/data-transfer-service/gcube/service"
});
doneSignal=new CountDownLatch(endpoints.size());
nodeIds=new ArrayList<>();
files=new ArrayList<>();
files.add("/home/fabio/Documents/Pictures/web_trend_map.png");
files.add("/home/fabio/Documents/Pictures/web_trend_map.png");
files.add("/home/fabio/Documents/Pictures/web_trend_map.png");
files.add("/home/fabio/Documents/Pictures/web_trend_map.png");
uris=new ArrayList<>();
uris.add("http://goo.gl/r5jFZ9");
storageIds=new ArrayList<>();
}
public static void main(String[] args) throws InitializationException{
ScopeProvider.instance.set(scope);
Map<String,DataTransferClient> clients=new HashMap<>();
for(String endpoint:endpoints)
clients.put(endpoint,DataTransferClient.getInstanceByEndpoint(endpoint));
for(String id:nodeIds)
clients.put(id,DataTransferClient.getInstanceByEndpoint(id));
HashSet<DataTransferClient> startedTests=new HashSet<>();
for(final Entry<String,DataTransferClient> entry:clients.entrySet()){
if(!startedTests.contains(entry.getValue())){
startedTests.add(entry.getValue());
executor.execute(new Runnable(){
final DataTransferClient dt=entry.getValue();
final String id=entry.getKey();
@Override
public void run() {
try{
TransferReport report=new TransferReport(dt.getDestinationCapabilities());
ScopeProvider.instance.set(scope);
log.debug("Sending files to {} ",dt.getDestinationCapabilities());
for(String f:files){
try {
TransferResult res=dt.localFile(f);
report.addReport(ReportType.local,f, res.getTransferedBytes(), res.getElapsedTime());
} catch (InvalidSourceException | SourceNotSetException
| FailedTransferException
| InitializationException e) {
log.error("Unable to send file {} to {}, error message : {}",f,dt.getDestinationCapabilities().getHostName(),e.getMessage());
}
}
log.debug("Sending uris to {}",dt.getDestinationCapabilities());
for(String f:uris){
try {
TransferResult res=dt.httpSource(f);
report.addReport(ReportType.uri,f, res.getTransferedBytes(), res.getElapsedTime());
} catch (InvalidSourceException | SourceNotSetException
| FailedTransferException
| InitializationException e) {
log.error("Unable to send uri {} to {}, error message : {}",f,dt.getDestinationCapabilities().getHostName(),e.getMessage());
}
}
log.debug("Sending storageIds to {}",dt.getDestinationCapabilities());
for(String f:storageIds){
try {
TransferResult res=dt.storageId(f);
report.addReport(ReportType.storage,f, res.getTransferedBytes(), res.getElapsedTime());
} catch (InvalidSourceException | SourceNotSetException
| FailedTransferException
| InitializationException e) {
log.error("Unable to send storageId {} to {}, error message : {}",f,dt.getDestinationCapabilities().getHostName(),e.getMessage());
}
}
reports.add(report);
}catch(Exception e){
reports.add(new ErrorReport(id));
}finally{
doneSignal.countDown();
}
}
});
}
}
try {
doneSignal.await();
} catch (InterruptedException e) {
}
System.out.println("*****************************");
for(TransferReport r:reports)System.out.println(r.print());
ExecutorServiceUtil.shutdown(executor);
}
}

View File

@ -0,0 +1,21 @@
package org.gcube.data.transfer.library;
import java.io.File;
import java.io.FileNotFoundException;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import org.gcube.data.transfer.library.utils.StorageUtils;
public class StorageTest {
static String scope="/gcube/devsec";
public static void main(String[] args) throws RemoteBackendException, FileNotFoundException {
ScopeProvider.instance.set(scope);
String toUpload="/home/fabio/Documents/Personal/DND/Incantesimi 3.5 - Mago e Stregone.pdf";
String id=StorageUtils.putOntoStorage(new File(toUpload));
System.out.println(StorageUtils.getUrlById(id));
}
}

View File

@ -4,11 +4,17 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.transfer.library.client.AuthorizationFilter; import org.gcube.data.transfer.library.client.AuthorizationFilter;
import org.gcube.data.transfer.library.client.Client; import org.gcube.data.transfer.library.client.Client;
import org.gcube.data.transfer.library.faults.CommunicationException;
import org.gcube.data.transfer.library.faults.RemoteServiceException;
import org.gcube.data.transfer.library.faults.ServiceNotFoundException;
import org.gcube.data.transfer.model.ServiceConstants;
import org.gcube.data.transfer.model.TransferCapabilities;
import org.gcube.data.transfer.model.TransferRequest; import org.gcube.data.transfer.model.TransferRequest;
import org.gcube.data.transfer.model.TransferTicket; import org.gcube.data.transfer.model.TransferTicket;
import org.gcube.data.transfer.model.TransferTicket.Status; import org.gcube.data.transfer.model.TransferTicket.Status;
@ -21,26 +27,27 @@ import org.junit.Test;
public class TestClientCalls { public class TestClientCalls {
static String hostname="http://pc-fabio.isti.cnr.it:8080"; static String hostname="http://node3-d-d4s.d4science.org:80";
// static String hostname="http://pc-fabio.isti.cnr.it:8080";
static String scope="/gcube/devNext"; static String scope="/gcube/devNext";
static Client client; static Client client;
@BeforeClass @BeforeClass
public static void init(){ public static void init() throws ServiceNotFoundException{
ScopeProvider.instance.set(scope); ScopeProvider.instance.set(scope);
client=new Client(hostname); client=new Client(hostname);
} }
@Test @Test
public void getCapabilties(){ public void getCapabilties() throws CommunicationException{
System.out.println(client.getCapabilties()); System.out.println(client.getCapabilties());
} }
@Test @Test
public void doTheTransfer() throws MalformedURLException{ public void doTheTransfer() throws MalformedURLException, RemoteServiceException{
TransferRequest request= new TransferRequest("", new HttpDownloadSettings(new URL("http://goo.gl/oLP7zG"), HttpDownloadOptions.DEFAULT)); TransferRequest request= new TransferRequest("", new HttpDownloadSettings(new URL("http://goo.gl/oLP7zG"), HttpDownloadOptions.DEFAULT));
System.out.println("Submitting "+request); System.out.println("Submitting "+request);
TransferTicket ticket=client.submit(request); TransferTicket ticket=client.submit(request);
@ -62,8 +69,11 @@ public class TestClientCalls {
@Test @Test
public void directCall(){ public void directCall(){
javax.ws.rs.client.Client client = ClientBuilder.newClient(new ClientConfig().register(AuthorizationFilter.class)); javax.ws.rs.client.Client client = ClientBuilder.newClient(new ClientConfig().register(AuthorizationFilter.class));
System.out.println(client.target("http://pc-fabio.isti.cnr.it:8080/data-transfer-service/gcube/service/Capabilities"). WebTarget target=client.target(hostname+"/data-transfer-service"+ServiceConstants.APPLICATION_PATH+"Capabilities");
request(MediaType.APPLICATION_XML).get()); // WebTarget target=client.target(hostname+"/data-transfer-service/gcube/service/Capabilities");
System.out.println("Asking capabilities to target : "+target.getUri());
System.out.println(target.
request(MediaType.APPLICATION_XML).get(TransferCapabilities.class));
} }
} }

View File

@ -0,0 +1,89 @@
package org.gcube.data.transfer.library;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.gcube.data.transfer.library.model.Source;
import org.gcube.data.transfer.model.TransferCapabilities;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
public class TransferReport {
public static enum ReportType{
local,uri,storage
}
private static final char end='\n';
@AllArgsConstructor
@Getter
@ToString
private static class ReportItem{
String source;
long size;
long elapsed;
// bytes/msec
public long getAvgSpeed(){
return size/elapsed;
}
}
protected TransferCapabilities host;
private Map<ReportType,ArrayList<ReportItem>> reports=new HashMap<>();
public TransferReport(TransferCapabilities host) {
super();
this.host = host;
}
public void addReport(ReportType sourceType,String theSource,long size,long elapsed){
if(!reports.containsKey(sourceType)) reports.put(sourceType, new ArrayList<ReportItem>());
reports.get(sourceType).add(new ReportItem(theSource, size, elapsed));
}
public String print(){
StringBuilder builder=new StringBuilder(" Report for : "+host+end);
for(Entry<ReportType,ArrayList<ReportItem>> entry:reports.entrySet()){
builder.append("Source type "+entry.getKey()+end);
ReportItem maxSizeItem=null;
ReportItem maxAvgSpeedItem=null;
ReportItem minSizeItem=null;
ReportItem minAvgSpeedItem=null;
long currentAvgSpeedCounter=0l;
for(ReportItem item:entry.getValue()){
if(maxSizeItem==null||maxSizeItem.getSize()<item.getSize()) maxSizeItem=item;
if(maxAvgSpeedItem==null||maxAvgSpeedItem.getAvgSpeed()<item.getAvgSpeed()) maxAvgSpeedItem=item;
if(minSizeItem==null||minSizeItem.getSize()>item.getSize()) minSizeItem=item;
if(minAvgSpeedItem==null||minAvgSpeedItem.getAvgSpeed()>item.getAvgSpeed()) minAvgSpeedItem=item;
currentAvgSpeedCounter+=item.getAvgSpeed();
}
builder.append("Max Size Item : "+maxSizeItem+end);
builder.append("Max Avg Speed Item : "+maxAvgSpeedItem+end);
builder.append("Min Size Item : "+minSizeItem+end);
builder.append("Min Avg Speed Item : "+minAvgSpeedItem+end);
builder.append("Total avg speed : "+currentAvgSpeedCounter/entry.getValue().size()+end);
}
return builder.toString();
};
}

View File

@ -19,22 +19,22 @@ import org.junit.Test;
public class TransfererTest { public class TransfererTest {
static String hostname="http://pc-fabio.isti.cnr.it:8080/data-transfer-service/gcube/service"; static String hostname="http://node3-d-d4s.d4science.org:80";
static String nodeId="462b68c5-463f-4295-86da-37d6c0abc7ea"; static String nodeId="462b68c5-463f-4295-86da-37d6c0abc7ea";
static String scope="/gcube/devNext"; static String scope="/gcube/devsec";
static DataTransferClient client; static DataTransferClient client;
@BeforeClass @BeforeClass
public static void init() throws UnreachableNodeException, ServiceNotFoundException, HostingNodeNotFoundException{ public static void init() throws UnreachableNodeException, ServiceNotFoundException, HostingNodeNotFoundException{
ScopeProvider.instance.set(scope); ScopeProvider.instance.set(scope);
// client=DataTransferClient.getInstanceByEndpoint(hostname); client=DataTransferClient.getInstanceByEndpoint(hostname);
client=DataTransferClient.getInstanceByNodeId(nodeId); // client=DataTransferClient.getInstanceByNodeId(nodeId);
} }
@Test @Test
public void localFile() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{ public void localFile() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
String localFile="/home/fabio/Downloads/Puntata 3.mp3"; String localFile="/home/fabio/Dropbox/Mindless/01- COMA - Mindless.mp3";
System.out.println(client.localFile(localFile)); System.out.println(client.localFile(localFile));
} }
@ -48,9 +48,11 @@ public class TransfererTest {
@Test @Test
public void storage() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException, RemoteBackendException, FileNotFoundException{ public void storage() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException, RemoteBackendException, FileNotFoundException{
String toUpload="/home/fabio/Downloads/Incantesimi3_5.doc"; ScopeProvider.instance.set(scope);
String toUpload="/home/fabio/Documents/Personal/DND/Incantesimi 3.5 - Mago e Stregone.pdf";
String id=StorageUtils.putOntoStorage(new File(toUpload)); String id=StorageUtils.putOntoStorage(new File(toUpload));
System.out.println(client.storageId(id)); System.out.println(client.storageId(id));
// System.out.println(id);
} }
@Test(expected=InvalidSourceException.class) @Test(expected=InvalidSourceException.class)