示例#1
0
  /**
   * Initialize the local directories for a particular user on this TT. This involves creation and
   * setting permissions of the following directories
   *
   * <ul>
   *   <li>$mapred.local.dir/taskTracker/$user
   *   <li>$mapred.local.dir/taskTracker/$user/jobcache
   *   <li>$mapred.local.dir/taskTracker/$user/distcache
   * </ul>
   *
   * @param user
   * @throws IOException
   */
  public void initializeUserDirs(String user) throws IOException {

    if (user == null) {
      // This shouldn't happen in general
      throw new IOException("User is null. Cannot initialized user-directories.");
    }

    AtomicBoolean localizedUser;
    synchronized (localizedUsers) {
      if (!localizedUsers.containsKey(user)) {
        localizedUsers.put(user, new AtomicBoolean(false));
      }
      localizedUser = localizedUsers.get(user);
    }

    synchronized (localizedUser) {
      if (localizedUser.get()) {
        // User-directories are already localized for this user.
        LOG.info(
            "User-directories for the user "
                + user
                + " are already initialized on this TT. Not doing anything.");
        return;
      }

      LOG.info("Initializing user " + user + " on this TT.");

      boolean userDirStatus = false;
      boolean jobCacheDirStatus = false;
      boolean distributedCacheDirStatus = false;

      for (String localDir : localDirs) {

        Path userDir = new Path(localDir, TaskTracker.getUserDir(user));

        // Set up the user-directory.
        if (fs.exists(userDir) || fs.mkdirs(userDir)) {

          // Set permissions on the user-directory
          FsPermission userOnly = new FsPermission((short) 0700);
          FileUtil.setPermission(new File(userDir.toUri().getPath()), userOnly);
          userDirStatus = true;

          // Set up the jobcache directory
          File jobCacheDir = new File(localDir, TaskTracker.getJobCacheSubdir(user));
          if (jobCacheDir.exists() || jobCacheDir.mkdirs()) {
            // Set permissions on the jobcache-directory
            FileUtil.setPermission(jobCacheDir, userOnly);
            jobCacheDirStatus = true;
          } else {
            LOG.warn("Unable to create job cache directory : " + jobCacheDir);
          }

          // Set up the cache directory used for distributed cache files
          File distributedCacheDir =
              new File(localDir, TaskTracker.getPrivateDistributedCacheDir(user));
          if (distributedCacheDir.exists() || distributedCacheDir.mkdirs()) {
            // Set permissions on the distcache-directory
            FileUtil.setPermission(distributedCacheDir, userOnly);
            distributedCacheDirStatus = true;
          } else {
            LOG.warn("Unable to create distributed-cache directory : " + distributedCacheDir);
          }
        } else {
          LOG.warn("Unable to create the user directory : " + userDir);
        }
      }

      if (!userDirStatus) {
        throw new IOException(
            "Not able to initialize user directories "
                + "in any of the configured local directories for user "
                + user);
      }
      if (!jobCacheDirStatus) {
        throw new IOException(
            "Not able to initialize job-cache directories "
                + "in any of the configured local directories for user "
                + user);
      }
      if (!distributedCacheDirStatus) {
        throw new IOException(
            "Not able to initialize distributed-cache directories "
                + "in any of the configured local directories for user "
                + user);
      }

      // Localization of the user is done
      localizedUser.set(true);
    }
  }