Search in sources :

Example 1 with DownloadJob

use of org.commonjava.maven.galley.spi.transport.DownloadJob in project galley by Commonjava.

the class DownloadHandler method joinOrStart.

private Transfer joinOrStart(final ConcreteResource resource, final Transfer target, final int timeoutSeconds, final Transport transport, final boolean suppressFailures, final EventMetadata eventMetadata) throws TransferException {
    // if the target file already exists, skip joining.
    if (target.exists()) {
        return target;
    }
    Future<DownloadJob> future;
    synchronized (pending) {
        future = pending.get(target);
        if (future == null) {
            if (transport == null) {
                return null;
            }
            final DownloadJob job = transport.createDownloadJob(resource, target, transferSizes, timeoutSeconds, eventMetadata);
            future = executor.submit(job);
            pending.put(target, future);
        }
    }
    int waitSeconds = (int) (timeoutSeconds * config.getTimeoutOverextensionFactor());
    int tries = 1;
    try {
        while (tries > 0) {
            tries--;
            try {
                final DownloadJob job = future.get(waitSeconds, TimeUnit.SECONDS);
                synchronized (pending) {
                    pending.remove(target);
                }
                final Transfer downloaded = job.getTransfer();
                if (job.getError() != null) {
                    logger.debug("NFC: Download error. Marking as missing: {}\nError was: {}", job.getError(), resource, job.getError().getMessage());
                    nfc.addMissing(resource);
                    if (!suppressFailures) {
                        throw job.getError();
                    }
                } else if (downloaded == null || !downloaded.exists()) {
                    logger.debug("NFC: Download did not complete. Marking as missing: {}", resource);
                    nfc.addMissing(resource);
                }
                return downloaded;
            } catch (final InterruptedException e) {
                if (!suppressFailures) {
                    throw new TransferException("Download interrupted: {}", e, target);
                }
            } catch (final ExecutionException e) {
                if (!suppressFailures) {
                    throw new TransferException("Download failed: {}", e, target);
                }
            } catch (final TimeoutException e) {
                Long size = transferSizes.get(target);
                if (tries > 0) {
                    continue;
                } else if (size != null && size > config.getThresholdWaitRetrySize()) {
                    logger.debug("Downloading a large file: {}. Retrying Future.get() up to {} times.", size, tries);
                    tries = (int) (size / config.getWaitRetryScalingIncrement());
                    continue;
                } else if (!suppressFailures) {
                    throw new TransferTimeoutException(target, "Timed out waiting for execution of: {}", e, target);
                }
            } catch (final TransferException e) {
                if (!suppressFailures) {
                    throw e;
                }
            } catch (final Exception e) {
                if (!suppressFailures) {
                    throw new TransferException("Download failed: {}. Reason: {}", e, resource, e.getMessage());
                }
            }
        }
    } finally {
        transferSizes.remove(target);
    }
    return null;
}
Also used : TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) TransferException(org.commonjava.maven.galley.TransferException) Transfer(org.commonjava.maven.galley.model.Transfer) ExecutionException(java.util.concurrent.ExecutionException) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) TransferException(org.commonjava.maven.galley.TransferException) TimeoutException(java.util.concurrent.TimeoutException) TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException)

Example 2 with DownloadJob

use of org.commonjava.maven.galley.spi.transport.DownloadJob in project galley by Commonjava.

the class HttpDownloadTest method simpleRetrieveOfAvailableUrl.

@Test
public void simpleRetrieveOfAvailableUrl() throws Exception {
    final String fname = "simple-retrieval.html";
    fixture.getServer().expect(fixture.formatUrl(fname), 200, fname);
    final String baseUri = fixture.getBaseUri();
    final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
    final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, fname));
    final String url = fixture.formatUrl(fname);
    Map<Transfer, Long> transferSizes = new HashMap<Transfer, Long>();
    assertThat(transfer.exists(), equalTo(false));
    final HttpDownload dl = new HttpDownload(url, location, transfer, transferSizes, new EventMetadata(), fixture.getHttp(), new ObjectMapper());
    final DownloadJob resultJob = dl.call();
    final TransferException error = dl.getError();
    assertThat(error, nullValue());
    assertThat(resultJob, notNullValue());
    final Transfer result = resultJob.getTransfer();
    assertThat(result, notNullValue());
    assertThat(result.exists(), equalTo(true));
    assertThat(transfer.exists(), equalTo(true));
    final String path = fixture.getUrlPath(url);
    assertThat(fixture.getAccessesFor(path), equalTo(1));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) TransferException(org.commonjava.maven.galley.TransferException) HashMap(java.util.HashMap) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 3 with DownloadJob

use of org.commonjava.maven.galley.spi.transport.DownloadJob in project galley by Commonjava.

the class HttpDownloadTest method simpleRetrieveOfMissingUrl.

@Test
public void simpleRetrieveOfMissingUrl() throws Exception {
    final String fname = "simple-missing.html";
    final String baseUri = fixture.getBaseUri();
    final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
    final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, fname));
    final String url = fixture.formatUrl(fname);
    Map<Transfer, Long> transferSizes = new HashMap<Transfer, Long>();
    assertThat(transfer.exists(), equalTo(false));
    final HttpDownload dl = new HttpDownload(url, location, transfer, transferSizes, new EventMetadata(), fixture.getHttp(), new ObjectMapper());
    final DownloadJob resultJob = dl.call();
    final TransferException error = dl.getError();
    assertThat(error, nullValue());
    assertThat(resultJob, notNullValue());
    final Transfer result = resultJob.getTransfer();
    assertThat(result, notNullValue());
    assertThat(result.exists(), equalTo(false));
    assertThat(transfer.exists(), equalTo(false));
    final String path = fixture.getUrlPath(url);
    assertThat(fixture.getAccessesFor(path), equalTo(1));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) TransferException(org.commonjava.maven.galley.TransferException) HashMap(java.util.HashMap) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 4 with DownloadJob

use of org.commonjava.maven.galley.spi.transport.DownloadJob in project galley by Commonjava.

the class HttpDownloadTest method simpleRetrieveOfUrlWithError.

@Test
public void simpleRetrieveOfUrlWithError() throws Exception {
    final String fname = "simple-error.html";
    final String baseUri = fixture.getBaseUri();
    final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
    final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, fname));
    final String url = fixture.formatUrl(fname);
    final String error = "Test Error.";
    final String path = fixture.getUrlPath(url);
    fixture.registerException(path, error);
    Map<Transfer, Long> transferSizes = new HashMap<Transfer, Long>();
    assertThat(transfer.exists(), equalTo(false));
    final HttpDownload dl = new HttpDownload(url, location, transfer, transferSizes, new EventMetadata(), fixture.getHttp(), new ObjectMapper());
    final DownloadJob resultJob = dl.call();
    final TransferException err = dl.getError();
    assertThat(err, notNullValue());
    assertThat(err.getMessage().contains(error), equalTo(true));
    assertThat(resultJob, notNullValue());
    final Transfer result = resultJob.getTransfer();
    assertThat(result, notNullValue());
    assertThat(transfer.exists(), equalTo(false));
    assertThat(fixture.getAccessesFor(path), equalTo(1));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) TransferException(org.commonjava.maven.galley.TransferException) HashMap(java.util.HashMap) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Aggregations

TransferException (org.commonjava.maven.galley.TransferException)4 Transfer (org.commonjava.maven.galley.model.Transfer)4 DownloadJob (org.commonjava.maven.galley.spi.transport.DownloadJob)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashMap (java.util.HashMap)3 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)3 SimpleHttpLocation (org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation)3 Test (org.junit.Test)3 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 TransferLocationException (org.commonjava.maven.galley.TransferLocationException)1 TransferTimeoutException (org.commonjava.maven.galley.TransferTimeoutException)1