Search in sources :

Example 31 with Location

use of org.apache.twill.filesystem.Location in project cdap by caskdata.

the class SeekableInputStreamTestBase method testClosedStream.

@Test
public void testClosedStream() throws IOException {
    Location location = getLocationFactory().create("testClosed");
    byte[] bytes = new byte[1024];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) (i & 0xff);
    }
    // Writes 1024 bytes to the output, and close the stream
    OutputStream output = Locations.newOutputSupplier(location).getOutput();
    output.write(bytes);
    output.close();
    // Create a SeekableInputStream for the location
    InputSupplier<? extends SeekableInputStream> inputSupplier = Locations.newInputSupplier(location);
    SeekableInputStream input = inputSupplier.getInput();
    // The stream size should be 1024
    Assert.assertEquals(bytes.length, input.size());
    // Seek to some random location and verify the byte read
    Random random = new Random();
    for (int i = 0; i < 100; i++) {
        long pos = random.nextInt(bytes.length);
        input.seek(pos);
        Assert.assertEquals(pos % 256, input.read());
    }
    input.close();
}
Also used : Random(java.util.Random) OutputStream(java.io.OutputStream) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 32 with Location

use of org.apache.twill.filesystem.Location in project cdap by caskdata.

the class SeekableInputStreamTestBase method testLiveStream.

@Test
public void testLiveStream() throws IOException {
    Location location = getLocationFactory().create("testSeekable");
    byte[] bytes = new byte[1024];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) (i & 0xff);
    }
    // Writes 1024 bytes to the output, and keep the output stream open
    OutputStream output = Locations.newOutputSupplier(location).getOutput();
    output.write(bytes);
    sync(output);
    // Create a SeekableInputStream for the location
    InputSupplier<? extends SeekableInputStream> inputSupplier = Locations.newInputSupplier(location);
    SeekableInputStream input = inputSupplier.getInput();
    // The stream size should be 1024
    Assert.assertEquals(bytes.length, input.size());
    // Seek to some random location and verify the byte read
    Random random = new Random();
    for (int i = 0; i < 100; i++) {
        long pos = random.nextInt(bytes.length);
        input.seek(pos);
        Assert.assertEquals(pos % 256, input.read());
    }
    input.close();
    // Write another 1024 bytes and keep the output stream open
    output.write(bytes);
    sync(output);
    // Reopen the input stream
    input = inputSupplier.getInput();
    // The stream size should be 2048
    Assert.assertEquals(bytes.length * 2, input.size());
    // Seek to some random location and verify the byte read
    for (int i = 0; i < 100; i++) {
        long pos = random.nextInt(bytes.length * 2);
        input.seek(pos);
        Assert.assertEquals(pos % 256, input.read());
    }
    output.close();
    input.close();
}
Also used : Random(java.util.Random) OutputStream(java.io.OutputStream) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 33 with Location

use of org.apache.twill.filesystem.Location in project cdap by caskdata.

the class AppJarHelper method createDeploymentJar.

public static Location createDeploymentJar(LocationFactory locationFactory, Class<?> clz, Manifest manifest, ClassAcceptor classAcceptor, File... bundleEmbeddedJars) throws IOException {
    // Exclude all classes that are visible form the system to the program classloader.
    ApplicationBundler bundler = new ApplicationBundler(classAcceptor);
    Location jarLocation = locationFactory.create(clz.getName()).getTempFile(".jar");
    ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(clz.getClassLoader());
    try {
        bundler.createBundle(jarLocation, clz);
    } finally {
        ClassLoaders.setContextClassLoader(oldClassLoader);
    }
    Location deployJar = locationFactory.create(clz.getName()).getTempFile(".jar");
    Manifest jarManifest = new Manifest(manifest);
    jarManifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    jarManifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, clz.getName());
    // Create the program jar for deployment. It removes the "classes/" prefix as that's the convention taken
    // by the ApplicationBundler inside Twill.
    Set<String> seenEntries = new HashSet<>();
    try (JarOutputStream jarOutput = new JarOutputStream(deployJar.getOutputStream(), jarManifest);
        JarInputStream jarInput = new JarInputStream(jarLocation.getInputStream())) {
        JarEntry jarEntry = jarInput.getNextJarEntry();
        while (jarEntry != null) {
            boolean isDir = jarEntry.isDirectory();
            String entryName = jarEntry.getName();
            if (!entryName.equals("classes/")) {
                if (entryName.startsWith("classes/")) {
                    jarEntry = new JarEntry(entryName.substring("classes/".length()));
                } else {
                    jarEntry = new JarEntry(entryName);
                }
                // create a manifest programmatically so it's possible to have a duplicate entry here
                if ("META-INF/MANIFEST.MF".equalsIgnoreCase(jarEntry.getName())) {
                    jarEntry = jarInput.getNextJarEntry();
                    continue;
                }
                if (seenEntries.add(jarEntry.getName())) {
                    jarOutput.putNextEntry(jarEntry);
                    if (!isDir) {
                        ByteStreams.copy(jarInput, jarOutput);
                    }
                }
            }
            jarEntry = jarInput.getNextJarEntry();
        }
        for (File embeddedJar : bundleEmbeddedJars) {
            jarEntry = new JarEntry("lib/" + embeddedJar.getName());
            if (seenEntries.add(jarEntry.getName())) {
                jarOutput.putNextEntry(jarEntry);
                Files.copy(embeddedJar, jarOutput);
            }
        }
    }
    return deployJar;
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) ApplicationBundler(org.apache.twill.internal.ApplicationBundler) File(java.io.File) Location(org.apache.twill.filesystem.Location) HashSet(java.util.HashSet)

Example 34 with Location

use of org.apache.twill.filesystem.Location in project cdap by caskdata.

the class GatewayFastTestsSuite method buildAppArtifact.

private static File buildAppArtifact(Class<?> cls, String name, File tmpFolder) throws IOException {
    if (!name.endsWith(".jar")) {
        name += ".jar";
    }
    LocationFactory locationFactory = new LocalLocationFactory(tmpFolder);
    Location appJar = AppJarHelper.createDeploymentJar(locationFactory, cls);
    File destination = new File(DirUtils.createTempDir(tmpFolder), name);
    Files.copy(Locations.newInputSupplier(appJar), destination);
    return destination;
}
Also used : LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) File(java.io.File) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) LocationFactory(org.apache.twill.filesystem.LocationFactory) Location(org.apache.twill.filesystem.Location)

Example 35 with Location

use of org.apache.twill.filesystem.Location in project cdap by caskdata.

the class MultiLiveStreamFileReaderTestBase method testMultiFileReader.

@Test
public void testMultiFileReader() throws Exception {
    String streamName = "multiReader";
    StreamId streamId = NamespaceId.DEFAULT.stream(streamName);
    Location location = getLocationFactory().create(streamName);
    location.mkdirs();
    // Create a stream with 1 partition.
    StreamConfig config = new StreamConfig(streamId, Long.MAX_VALUE, 10000, Long.MAX_VALUE, location, null, 1000);
    // Write out 200 events in 5 files, with interleaving timestamps
    List<FileWriter<StreamEvent>> writers = Lists.newArrayList();
    for (int i = 0; i < 5; i++) {
        FileWriter<StreamEvent> writer = createWriter(config, "bucket" + i);
        writers.add(writer);
        for (int j = 0; j < 200; j++) {
            long timestamp = j * 5 + i;
            writer.append(StreamFileTestUtils.createEvent(timestamp, "Testing " + timestamp));
        }
    }
    // Flush all writers.
    for (FileWriter<StreamEvent> writer : writers) {
        writer.flush();
    }
    // Create a multi stream file reader
    List<StreamFileOffset> sources = Lists.newArrayList();
    Location partitionLocation = StreamUtils.createPartitionLocation(config.getLocation(), 0, Long.MAX_VALUE);
    for (int i = 0; i < 5; i++) {
        Location eventFile = StreamUtils.createStreamLocation(partitionLocation, "bucket" + i, 0, StreamFileType.EVENT);
        sources.add(new StreamFileOffset(eventFile, 0L, 0));
    }
    // Reads all events written so far.
    MultiLiveStreamFileReader reader = new MultiLiveStreamFileReader(config, sources);
    List<StreamEvent> events = Lists.newArrayList();
    long expectedTimestamp = 0L;
    for (int i = 0; i < 10; i++) {
        Assert.assertEquals(100, reader.read(events, 100, 0, TimeUnit.SECONDS));
        Assert.assertEquals(100, events.size());
        for (StreamEvent event : events) {
            Assert.assertEquals(expectedTimestamp, event.getTimestamp());
            Assert.assertEquals("Testing " + expectedTimestamp, Charsets.UTF_8.decode(event.getBody()).toString());
            expectedTimestamp++;
        }
        events.clear();
    }
    Assert.assertEquals(0, reader.read(events, 1, 1, TimeUnit.SECONDS));
    // Writes some more events to the first three writers.
    for (int i = 0; i < 3; i++) {
        FileWriter<StreamEvent> writer = writers.get(i);
        for (int j = 0; j < 10; j++) {
            long timestamp = 1000 + j * 3 + i;
            writer.append(StreamFileTestUtils.createEvent(timestamp, "Testing " + timestamp));
        }
    }
    // Close all writers
    for (FileWriter<StreamEvent> writer : writers) {
        writer.close();
    }
    // Continue to read
    Assert.assertEquals(30, reader.read(events, 30, 2, TimeUnit.SECONDS));
    Assert.assertEquals(30, events.size());
    for (StreamEvent event : events) {
        Assert.assertEquals(expectedTimestamp, event.getTimestamp());
        Assert.assertEquals("Testing " + expectedTimestamp, Charsets.UTF_8.decode(event.getBody()).toString());
        expectedTimestamp++;
    }
    // Should get no more events.
    Assert.assertEquals(0, reader.read(events, 1, 1, TimeUnit.SECONDS));
    reader.close();
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) FileWriter(co.cask.cdap.data.file.FileWriter) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) StreamConfig(co.cask.cdap.data2.transaction.stream.StreamConfig) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Aggregations

Location (org.apache.twill.filesystem.Location)272 Test (org.junit.Test)110 IOException (java.io.IOException)67 File (java.io.File)45 FileSet (co.cask.cdap.api.dataset.lib.FileSet)32 LocationFactory (org.apache.twill.filesystem.LocationFactory)32 LocalLocationFactory (org.apache.twill.filesystem.LocalLocationFactory)31 PartitionedFileSet (co.cask.cdap.api.dataset.lib.PartitionedFileSet)27 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)27 CConfiguration (co.cask.cdap.common.conf.CConfiguration)20 HashMap (java.util.HashMap)20 NamespaceId (co.cask.cdap.proto.id.NamespaceId)19 Manifest (java.util.jar.Manifest)18 StreamId (co.cask.cdap.proto.id.StreamId)17 ArrayList (java.util.ArrayList)15 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)13 OutputStream (java.io.OutputStream)13 TimePartitionedFileSet (co.cask.cdap.api.dataset.lib.TimePartitionedFileSet)11 ApplicationManager (co.cask.cdap.test.ApplicationManager)11 HashSet (java.util.HashSet)11