Package org.apache.twill.filesystem

Examples of org.apache.twill.filesystem.Location


      .setVirtualCores(1)
      .setMemory(512, ResourceSpecification.SizeUnit.MEGA)
      .setInstances(1)
      .build();

    Location programLocation = program.getJarLocation();

    return TwillSpecification.Builder.with()
      .setName(String.format("%s.%s.%s.%s",
                             ProgramType.MAPREDUCE.name().toLowerCase(),
                             program.getAccountId(), program.getApplicationId(), spec.getName()))
      .withRunnable()
        .add(spec.getName(),
             new MapReduceTwillRunnable(spec.getName(), "hConf.xml", "cConf.xml"),
             resourceSpec)
        .withLocalFiles()
          .add(programLocation.getName(), programLocation.toURI())
          .add("hConf.xml", hConfig.toURI())
          .add("cConf.xml", cConfig.toURI()).apply()
      .anyOrder().withEventHandler(eventHandler).build();
  }
View Full Code Here


      if (!streamLocation.isDirectory()) {
        throw new IOException("Stream " + streamLocation.getName() + " not exist in " + streamLocation.toURI());
      }

      Location partitionDirectory = StreamUtils.createPartitionLocation(streamLocation,
                                                                        partitionStart, partitionDuration);
      // Always try to create the directory
      partitionDirectory.mkdirs();

      // Try to find the file of this bucket with the highest sequence number.
      int maxSequence = -1;
      for (Location location : partitionDirectory.list()) {
        String fileName = location.getName();
        if (fileName.startsWith(fileNamePrefix)) {
          StreamUtils.getSequenceId(fileName);

          int idx = fileName.lastIndexOf('.');
          if (idx < fileNamePrefix.length()) {
            LOG.warn("Ignore file with invalid stream file name {}", location.toURI());
            continue;
          }

          try {
            // File name format is [prefix].[sequenceId].[dat|idx]
            int seq = StreamUtils.getSequenceId(fileName);
            if (seq > maxSequence) {
              maxSequence = seq;
            }
          } catch (NumberFormatException e) {
            LOG.warn("Ignore stream file with invalid sequence id {}", location.toURI());
          }
        }
      }

      // Create the event and index file with the max sequence + 1
      int fileSequence = maxSequence + 1;
      Location eventFile = StreamUtils.createStreamLocation(partitionDirectory, fileNamePrefix,
                                                            fileSequence, StreamFileType.EVENT);
      Location indexFile = StreamUtils.createStreamLocation(partitionDirectory, fileNamePrefix,
                                                            fileSequence, StreamFileType.INDEX);
      // The creation should succeed, as it's expected to only have one process running per fileNamePrefix.
      if (!eventFile.createNew() || !indexFile.createNew()) {
        throw new IOException("Failed to create new file at " + eventFile.toURI() + " and " + indexFile.toURI());
      }

      LOG.debug("New stream file created at {}", eventFile.toURI());
      return new StreamDataFileWriter(createOutputSupplier(eventFile), createOutputSupplier(indexFile), indexInterval);
    }
View Full Code Here

   * Overwrites a stream config file.
   *
   * @param config The new configuration.
   */
  private void saveConfig(StreamConfig config) throws IOException {
    Location configLocation = config.getLocation().append(AbstractStreamFileAdmin.CONFIG_FILE_NAME);
    Location tempLocation = configLocation.getTempFile("tmp");
    try {
      CharStreams.write(GSON.toJson(config), CharStreams.newWriterSupplier(
        Locations.newOutputSupplier(tempLocation), Charsets.UTF_8));

      Preconditions.checkState(tempLocation.renameTo(configLocation) != null,
                               "Rename {} to {} failed", tempLocation, configLocation);
    } finally {
      if (tempLocation.exists()) {
        tempLocation.delete();
      }
    }
  }
View Full Code Here

    // See if newer file is available.
    nextCheckTime = now + newFileCheckInterval;

    // See if there is a higher sequence file available.
    Location eventLocation = StreamUtils.createStreamLocation(reader.getPartitionLocation(),
                                                              offset.getNamePrefix(),
                                                              offset.getSequenceId() + 1,
                                                              StreamFileType.EVENT);

    StreamPositionTransformFileReader nextReader = createReader(eventLocation, true, offset.getGeneration());
View Full Code Here

  private void addModule(String moduleName, Class<?> typeClass) throws DatasetManagementException {
    try {
      File tempFile = File.createTempFile(typeClass.getName(), ".jar");
      try {
        Location tempJarPath = createDeploymentJar(typeClass, new LocalLocationFactory().create(tempFile.toURI()));
        client.addModule(moduleName, typeClass.getName(), tempJarPath);
      } finally {
        tempFile.delete();
      }
    } catch (IOException e) {
View Full Code Here

      throw new DatasetManagementException(msg, e);
    }
  }

  private static Location createDeploymentJar(Class<?> clz, Location destination) throws IOException {
    Location tempBundle = destination.getTempFile(".jar");
    try {
      ClassLoader remembered = Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader(clz.getClassLoader());
      try {
        ApplicationBundler bundler = new ApplicationBundler(ImmutableList.of("co.cask.cdap.api",
                                                                             "org.apache.hadoop",
                                                                             "org.apache.hbase",
                                                                             "org.apache.hive"));
        bundler.createBundle(tempBundle, clz);
      } finally {
        Thread.currentThread().setContextClassLoader(remembered);
      }

      // Create the program jar for deployment. It removes the "classes/" prefix as that's the convention taken
      // by the ApplicationBundler inside Twill.
      JarOutputStream jarOutput = new JarOutputStream(destination.getOutputStream());
      try {
        JarInputStream jarInput = new JarInputStream(tempBundle.getInputStream());
        try {
          Set<String> seen = Sets.newHashSet();
          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);
              }
              if (seen.add(jarEntry.getName())) {
                jarOutput.putNextEntry(jarEntry);

                if (!isDir) {
                  ByteStreams.copy(jarInput, jarOutput);
                }
              }
            }

            jarEntry = jarInput.getNextJarEntry();
          }
        } finally {
          jarInput.close();
        }

      } finally {
        jarOutput.close();
      }

      return destination;
    } finally {
      tempBundle.delete();
    }
  }
View Full Code Here

                                             new DataSetsModules().getDistributedModule(),
                                             new LocationRuntimeModule().getDistributedModules());

    StreamAdmin streamAdmin = injector.getInstance(StreamAdmin.class);
    StreamConfig streamConfig = streamAdmin.getConfig(streamName);
    Location streamLocation = streamConfig.getLocation();
    List<Location> eventFiles = Lists.newArrayList();

    for (Location partition : streamLocation.list()) {
      if (!partition.isDirectory()) {
        continue;
      }

      for (Location file : partition.list()) {
View Full Code Here

    // Get the current generation and remove every generations smaller then the current one.
    int generation = StreamUtils.getGeneration(config);

    for (int i = 0; i < generation; i++) {
      Location generationLocation = StreamUtils.createGenerationLocation(config.getLocation(), i);

      // Special case for generation 0
      if (generationLocation.equals(config.getLocation())) {
        for (Location location : generationLocation.list()) {
          // Only delete partition directories
          if (isPartitionDirector(location)) {
            location.delete(true);
          }
        }
      } else {
        generationLocation.delete(true);
      }
    }

    // For current generation, remove all partition directories ended older than TTL
    long expireTime = currentTime - config.getTTL();
    Location generationLocation = StreamUtils.createGenerationLocation(config.getLocation(), generation);
    for (Location location : generationLocation.list()) {
      // Only interested in partition directories
      if (!isPartitionDirector(location)) {
        continue;
      }
      long partitionEndTime = StreamUtils.getPartitionEndTime(location.getName());
View Full Code Here

  @Override
  public FileWriter<StreamEvent> create(StreamConfig config, int generation) throws IOException {
    try {
      Preconditions.checkNotNull(config.getLocation(), "Location for stream {} is unknown.", config.getName());

      Location baseLocation = StreamUtils.createGenerationLocation(config.getLocation(), generation);
      Locations.mkdirsIfNotExists(baseLocation);

      return new TimePartitionedStreamFileWriter(baseLocation, config.getPartitionDuration(),
                                                 filePrefix, config.getIndexInterval());
View Full Code Here

    long duration = in.readLong() - partitionStart;
    String prefix = in.readUTF();
    int seqId = in.readInt();
    long offset = in.readLong();

    Location baseLocation = config.getLocation();
    if (generation > 0) {
      baseLocation = createGenerationLocation(baseLocation, generation);
    }
    Location partitionLocation = createPartitionLocation(baseLocation, partitionStart, duration);
    Location eventLocation = createStreamLocation(partitionLocation, prefix, seqId, StreamFileType.EVENT);
    return new StreamFileOffset(eventLocation, offset, generation);
  }
View Full Code Here

TOP

Related Classes of org.apache.twill.filesystem.Location

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.