Search in sources :

Example 31 with FileReference

use of com.yahoo.config.FileReference in project vespa by vespa-engine.

the class FileDirectoryTest method requireThatFileReferenceWithSubDirectoriesWorks.

@Test
public void requireThatFileReferenceWithSubDirectoriesWorks() throws IOException {
    FileDirectory fileDirectory = new FileDirectory(temporaryFolder.getRoot());
    String subdirName = "subdir";
    File subDirectory = new File(temporaryFolder.getRoot(), subdirName);
    createFileInSubDir(subDirectory, "foo");
    FileReference fileReference = fileDirectory.addFile(subDirectory);
    File dir = fileDirectory.getFile(fileReference);
    assertTrue(dir.exists());
    assertTrue(new File(dir, "foo").exists());
    assertFalse(new File(dir, "doesnotexist").exists());
    assertEquals("1315a322fc323608", fileReference.value());
    // Add a file, should be available and file reference should have another value
    createFileInSubDir(subDirectory, "bar");
    fileReference = fileDirectory.addFile(subDirectory);
    dir = fileDirectory.getFile(fileReference);
    assertTrue(new File(dir, "foo").exists());
    assertTrue(new File(dir, "bar").exists());
    assertEquals("9ca074b47a4b510c", fileReference.value());
}
Also used : FileReference(com.yahoo.config.FileReference) File(java.io.File) Test(org.junit.Test)

Example 32 with FileReference

use of com.yahoo.config.FileReference in project vespa by vespa-engine.

the class FileDownloader method getFutureFile.

private Future<Optional<File>> getFutureFile(FileReferenceDownload fileReferenceDownload) {
    FileReference fileReference = fileReferenceDownload.fileReference();
    Objects.requireNonNull(fileReference, "file reference cannot be null");
    File directory = new File(downloadDirectory, fileReference.value());
    log.log(LogLevel.DEBUG, () -> "Checking if there is a file in '" + directory.getAbsolutePath() + "' ");
    Optional<File> file = getFileFromFileSystem(fileReference, directory);
    if (file.isPresent()) {
        SettableFuture<Optional<File>> future = SettableFuture.create();
        future.set(file);
        return future;
    } else {
        log.log(LogLevel.DEBUG, () -> "File reference '" + fileReference.value() + "' not found in " + directory.getAbsolutePath() + ", starting download");
        return queueForAsyncDownload(fileReferenceDownload, timeout);
    }
}
Also used : Optional(java.util.Optional) FileReference(com.yahoo.config.FileReference) File(java.io.File)

Example 33 with FileReference

use of com.yahoo.config.FileReference in project vespa by vespa-engine.

the class FileReceiver method receiveFilePart.

@SuppressWarnings({ "UnusedDeclaration" })
public final void receiveFilePart(Request req) {
    log.log(LogLevel.DEBUG, () -> "Received method call '" + req.methodName() + "' with parameters : " + req.parameters());
    FileReference reference = new FileReference(req.parameters().get(0).asString());
    int sessionId = req.parameters().get(1).asInt32();
    int partId = req.parameters().get(2).asInt32();
    byte[] part = req.parameters().get(3).asData();
    Session session = getSession(sessionId);
    int retval = verifySession(session, sessionId, reference);
    try {
        session.addPart(partId, part);
    } catch (Exception e) {
        log.severe("Got exception " + e);
        retval = 1;
    }
    double completeness = (double) session.currentFileSize / (double) session.fileSize;
    log.log(LogLevel.DEBUG, () -> String.format("%.1f percent of '%s' downloaded", completeness * 100, reference.value()));
    downloader.setDownloadStatus(reference, completeness);
    req.returnValues().add(new Int32Value(retval));
}
Also used : Int32Value(com.yahoo.jrt.Int32Value) FileReference(com.yahoo.config.FileReference) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 34 with FileReference

use of com.yahoo.config.FileReference in project vespa by vespa-engine.

the class FileReceiver method receiveFileMeta.

@SuppressWarnings({ "UnusedDeclaration" })
public final void receiveFileMeta(Request req) {
    log.log(LogLevel.DEBUG, () -> "Received method call '" + req.methodName() + "' with parameters : " + req.parameters());
    FileReference reference = new FileReference(req.parameters().get(0).asString());
    String fileName = req.parameters().get(1).asString();
    String type = req.parameters().get(2).asString();
    long fileSize = req.parameters().get(3).asInt64();
    int sessionId = nextSessionId.getAndIncrement();
    int retval = 0;
    synchronized (sessions) {
        if (sessions.containsKey(sessionId)) {
            retval = 1;
            log.severe("Session id " + sessionId + " already exist, impossible. Request from(" + req.target() + ")");
        } else {
            try {
                sessions.put(sessionId, new Session(downloadDirectory, tmpDirectory, sessionId, reference, FileReferenceData.Type.valueOf(type), fileName, fileSize));
            } catch (Exception e) {
                retval = 1;
            }
        }
    }
    req.returnValues().add(new Int32Value(retval));
    req.returnValues().add(new Int32Value(sessionId));
}
Also used : Int32Value(com.yahoo.jrt.Int32Value) FileReference(com.yahoo.config.FileReference) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 35 with FileReference

use of com.yahoo.config.FileReference in project vespa by vespa-engine.

the class FileReferenceDownloader method startDownload.

private void startDownload(Duration timeout, FileReferenceDownload fileReferenceDownload) {
    FileReference fileReference = fileReferenceDownload.fileReference();
    long end = System.currentTimeMillis() + timeout.toMillis();
    boolean downloadStarted = false;
    while ((System.currentTimeMillis() < end) && !downloadStarted) {
        try {
            if (startDownloadRpc(fileReferenceDownload)) {
                downloadStarted = true;
            } else {
                Thread.sleep(sleepBetweenRetries.toMillis());
            }
        } catch (InterruptedException e) {
        /* ignored */
        }
    }
    if (!downloadStarted) {
        fileReferenceDownload.future().setException(new RuntimeException("Failed getting file reference '" + fileReference.value() + "'"));
        synchronized (downloads) {
            downloads.remove(fileReference);
        }
    }
}
Also used : FileReference(com.yahoo.config.FileReference)

Aggregations

FileReference (com.yahoo.config.FileReference)37 Test (org.junit.Test)10 File (java.io.File)9 Int32Value (com.yahoo.jrt.Int32Value)4 IOException (java.io.IOException)3 Bundle (org.osgi.framework.Bundle)3 StringValue (com.yahoo.jrt.StringValue)2 CompressedFileReference (com.yahoo.vespa.filedistribution.CompressedFileReference)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ByteBuffer (java.nio.ByteBuffer)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 Optional (java.util.Optional)2 ComponentSpecification (com.yahoo.component.ComponentSpecification)1 ConfigBuilder (com.yahoo.config.ConfigBuilder)1 ComponentInfo (com.yahoo.config.application.api.ComponentInfo)1 MockFileRegistry (com.yahoo.config.model.application.provider.MockFileRegistry)1 UserConfigRepo (com.yahoo.config.model.producer.UserConfigRepo)1 MockHosts (com.yahoo.config.model.test.MockHosts)1 BundleInstantiationSpecification (com.yahoo.container.bundle.BundleInstantiationSpecification)1 CloudSubscriberFactory (com.yahoo.container.di.CloudSubscriberFactory)1