Beispiel #1
0
/*
 * delete a given job log directory
 * This function takes jobid and deletes the related logs.
 */
int delete_log_directory(const char *subdir, const char * good_local_dirs) {
  char* job_log_dir = get_job_log_directory(subdir);
  
  int ret = -1;
  if (job_log_dir == NULL) return ret;

  //delete the job log directory in <hadoop.log.dir>/userlogs/jobid
  delete_path(job_log_dir, true);

  char **local_dir = get_mapred_local_dirs(good_local_dirs);

  char **local_dir_ptr;
  for(local_dir_ptr = local_dir; *local_dir_ptr != NULL; ++local_dir_ptr) {
     char *mapred_local_log_dir = concatenate("%s/userlogs/%s", 
				      "mapred local job log dir", 
			      	      2, *local_dir_ptr, subdir);
     if (mapred_local_log_dir != NULL) {
        //delete the job log directory in <mapred.local.dir>/userlogs/jobid
        delete_path(mapred_local_log_dir, true);
	free(mapred_local_log_dir);
     }
     else
        fprintf(LOGFILE, "Failed to delete mapred local log dir for jobid %s\n",
            subdir);
  }
  free(job_log_dir);
  free_values(local_dir);
  return 0;
}
void test_get_task_log_dir() {
  char *logdir = get_job_log_directory("job_5/task_4");
  char *expected = TEST_ROOT "/logs/userlogs/job_5/task_4";
  if (strcmp(logdir, expected) != 0) {
    printf("FAIL: get_task_log_dir expected %s got %s\n", logdir, expected);
  }
  free(logdir);
}
Beispiel #3
0
/**
 * delete a given log directory
 */
int delete_log_directory(const char *subdir) {
  char* log_subdir = get_job_log_directory(subdir);
  int ret = -1;
  if (log_subdir != NULL) {
    ret = delete_path(log_subdir, strchr(subdir, '/') == NULL);
  }
  free(log_subdir);
  return ret;
}
void test_get_job_log_dir() {
  char *expected = TEST_ROOT "/logs/userlogs/job_200906101234_0001";
  char *logdir = get_job_log_directory(TEST_ROOT "/logs/userlogs","job_200906101234_0001");
  if (strcmp(logdir, expected) != 0) {
    printf("Fail get_job_log_dir got %s expected %s\n", logdir, expected);
    exit(1);
  }
  free(logdir);
}
void test_create_log_directory() {
    printf("\nTesting test_create_log_directory\n");
    create_userlogs_dir();
    char *job_log_dir = get_job_log_directory("job_7");
    if (job_log_dir == NULL) {
        exit(1);
    }
    if (create_directory_for_user(job_log_dir) != 0) {
        exit(1);
    }
    free(job_log_dir);
    char* good_local_dirs = get_value("mapred.local.dir");
    if (good_local_dirs == NULL) {
        fprintf(LOGFILE, "Mapred local directories could not be obtained.\n");
        exit(1);
    }
    create_attempt_directories(username, good_local_dirs, "job_7", "task_1");

    //check if symlink got created
    struct stat file;
    int status;
    char actualpath [PATH_MAX+1];
    char *res;
    char *filepath = TEST_ROOT "/logs/userlogs/job_7/task_1";

    status = lstat(filepath, &file);
    if (!S_ISLNK(file.st_mode)) {
        fprintf(LOGFILE, "Symlink creation failed\n");
        exit(1);
    }

    //Check if symlink path exists
    res = realpath(filepath, actualpath);
    if(!res) {
        fprintf(LOGFILE, "Failed to get target for the symlink\n");
        exit(1);
    }

    char local_job_dir[PATH_MAX+1];
    int i;
    bool found = false;
    for(i=1; i<5; i++) {
        sprintf(local_job_dir, TEST_ROOT "/local-%d/userlogs/job_7/task_1", i);
        if (strcmp(local_job_dir, actualpath) == 0) {
            found = true;
            break;
        }
    }

    if(!found) {
        printf("FAIL: symlink path and target path mismatch\n");
        exit(1);
    }

    free(good_local_dirs);
}
void test_delete_log_directory() {
  printf("\nTesting delete_log_directory\n");
  char *job_log_dir = get_job_log_directory("job_1");
  if (job_log_dir == NULL) {
    exit(1);
  }
  if (create_directory_for_user(job_log_dir) != 0) {
    exit(1);
  }
  free(job_log_dir);
  char *task_log_dir = get_job_log_directory("job_1/task_2");
  if (task_log_dir == NULL) {
    exit(1);
  }
  if (mkdirs(task_log_dir, 0700) != 0) {
    exit(1);
  }
  if (access(TEST_ROOT "/logs/userlogs/job_1/task_2", R_OK) != 0) {
    printf("FAIL: can't access task directory - %s\n", strerror(errno));
    exit(1);
  }
  if (delete_log_directory("job_1/task_2") != 0) {
    printf("FAIL: can't delete task directory\n");
    exit(1);
  }
  if (access(TEST_ROOT "/logs/userlogs/job_1/task_2", R_OK) == 0) {
    printf("FAIL: task directory not deleted\n");
    exit(1);
  }
  if (access(TEST_ROOT "/logs/userlogs/job_1", R_OK) != 0) {
    printf("FAIL: job directory not deleted - %s\n", strerror(errno));
    exit(1);
  }
  if (delete_log_directory("job_1") != 0) {
    printf("FAIL: can't delete task directory\n");
    exit(1);
  }
  if (access(TEST_ROOT "/logs/userlogs/job_1", R_OK) == 0) {
    printf("FAIL: job directory not deleted\n");
    exit(1);
  }
  free(task_log_dir);
}
Beispiel #7
0
/**
 * Function to prepare the attempt directories for the task JVM.
 * It creates the task work and log directories.
 */
static int create_attempt_directories(const char* user, const char *job_id, 
					const char *task_id) {
  // create dirs as 0750
  const mode_t perms = S_IRWXU | S_IRGRP | S_IXGRP;
  if (job_id == NULL || task_id == NULL || user == NULL) {
    fprintf(LOGFILE, 
            "Either task_id is null or the user passed is null.\n");
    return -1;
  }
  int result = 0;

  char **local_dir = get_values(TT_SYS_DIR_KEY);

  if (local_dir == NULL) {
    fprintf(LOGFILE, "%s is not configured.\n", TT_SYS_DIR_KEY);
    return -1;
  }

  char **local_dir_ptr;
  for(local_dir_ptr = local_dir; *local_dir_ptr != NULL; ++local_dir_ptr) {
    char *task_dir = get_attempt_work_directory(*local_dir_ptr, user, job_id, 
                                                task_id);
    if (task_dir == NULL) {
      free_values(local_dir);
      return -1;
    }
    if (mkdirs(task_dir, perms) != 0) {
      // continue on to create other task directories
      free(task_dir);
    } else {
      free(task_dir);
    }
  }
  free_values(local_dir);

  // also make the directory for the task logs
  char *job_task_name = malloc(strlen(job_id) + strlen(task_id) + 2);
  if (job_task_name == NULL) {
    fprintf(LOGFILE, "Malloc of job task name failed\n");
    result = -1;
  } else {
    sprintf(job_task_name, "%s/%s", job_id, task_id);
    char *log_dir = get_job_log_directory(job_task_name);
    free(job_task_name);
    if (log_dir == NULL) {
      result = -1;
    } else if (mkdirs(log_dir, perms) != 0) {
      result = -1;
    }
    free(log_dir);
  }
  return result;
}
Beispiel #8
0
/**
 * Function to prepare the job directories for the task JVM.
 */
int initialize_job(const char *user, const char *jobid, 
		   const char* credentials, const char* job_xml,
                   char* const* args) {
  if (jobid == NULL || user == NULL) {
    fprintf(LOGFILE, "Either jobid is null or the user passed is null.\n");
    return INVALID_ARGUMENT_NUMBER;
  }

  // create the user directory
  int result = initialize_user(user);
  if (result != 0) {
    return result;
  }

  // create the log directory for the job
  char *job_log_dir = get_job_log_directory(jobid);
  if (job_log_dir == NULL) {
    return -1;
  }
  result = create_directory_for_user(job_log_dir);
  free(job_log_dir);
  if (result != 0) {
    return -1;
  }

  // open up the credentials file
  int cred_file = open_file_as_task_tracker(credentials);
  if (cred_file == -1) {
    return -1;
  }

  int job_file = open_file_as_task_tracker(job_xml);
  if (job_file == -1) {
    return -1;
  }

  // give up root privs
  if (change_user(user_detail->pw_uid, user_detail->pw_gid) != 0) {
    return -1;
  }

  // 750
  mode_t permissions = S_IRWXU | S_IRGRP | S_IXGRP;
  char **tt_roots = get_values(TT_SYS_DIR_KEY);

  if (tt_roots == NULL) {
    return INVALID_CONFIG_FILE;
  }

  char **tt_root;
  char *primary_job_dir = NULL;
  for(tt_root=tt_roots; *tt_root != NULL; ++tt_root) {
    char *job_dir = get_job_directory(*tt_root, user, jobid);
    if (job_dir == NULL) {
      // try the next one
    } else if (mkdirs(job_dir, permissions) != 0) {
      free(job_dir);
    } else if (primary_job_dir == NULL) {
      primary_job_dir = job_dir;
    } else {
      free(job_dir);
    }
  }
  free_values(tt_roots);
  if (primary_job_dir == NULL) {
    fprintf(LOGFILE, "Did not create any job directories\n");
    return -1;
  }

  char *cred_file_name = concatenate("%s/%s", "cred file", 2,
				     primary_job_dir, CREDENTIALS_FILENAME);
  if (cred_file_name == NULL) {
    return -1;
  }
  if (copy_file(cred_file, credentials, cred_file_name, S_IRUSR|S_IWUSR) != 0){
    return -1;
  }
  char *job_file_name = concatenate("%s/%s", "job file", 2,
				     primary_job_dir, JOB_FILENAME);
  if (job_file_name == NULL) {
    return -1;
  }
  if (copy_file(job_file, job_xml, job_file_name,
        S_IRUSR|S_IWUSR|S_IRGRP) != 0) {
    return -1;
  }
  fclose(stdin);
  fflush(LOGFILE);
  if (LOGFILE != stdout) {
    fclose(stdout);
  }
  fclose(stderr);
  if (chdir(primary_job_dir)) {
    fprintf(LOGFILE, "Failure to chdir to job dir - %s\n",
      strerror(errno));
    return -1;
  }

  execvp(args[0], args);
  fprintf(LOGFILE, "Failure to exec job initialization process - %s\n",
	  strerror(errno));
  return -1;
}
void test_run_task() {
  printf("\nTesting run task\n");
  if (seteuid(0) != 0) {
    printf("FAIL: seteuid to root failed - %s\n", strerror(errno));
    exit(1);
  }
  FILE* creds = fopen(TEST_ROOT "/creds.txt", "w");
  if (creds == NULL) {
    printf("FAIL: failed to create credentials file - %s\n", strerror(errno));
    exit(1);
  }
  if (fprintf(creds, "secret key\n") < 0) {
    printf("FAIL: fprintf failed - %s\n", strerror(errno));
    exit(1);
  }
  if (fclose(creds) != 0) {
    printf("FAIL: fclose failed - %s\n", strerror(errno));
    exit(1);
  }

  const char* script_name = TEST_ROOT "/task-script";
  FILE* script = fopen(script_name, "w");
  if (script == NULL) {
    printf("FAIL: failed to create script file - %s\n", strerror(errno));
    exit(1);
  }
  if (seteuid(user_detail->pw_uid) != 0) {
    printf("FAIL: failed to seteuid back to user - %s\n", strerror(errno));
    exit(1);
  }
  if (fprintf(script, "#!/bin/bash\n"
                     "touch foobar\n"
                     "exit 0") < 0) {
    printf("FAIL: fprintf failed - %s\n", strerror(errno));
    exit(1);
  }
  if (fclose(script) != 0) {
    printf("FAIL: fclose failed - %s\n", strerror(errno));
    exit(1);
  }
  fflush(stdout);
  fflush(stderr);
  char* task_dir = get_attempt_work_directory(TEST_ROOT "/local-1", 
					      username, "job_4", "task_1");
  pid_t child = fork();
  if (child == -1) {
    printf("FAIL: failed to fork process for init_job - %s\n", 
	   strerror(errno));
    exit(1);
  } else if (child == 0) {
    if (run_task_as_user(username, "job_4", "task_1", 
                         task_dir, script_name, TEST_ROOT "creds.txt") != 0) {
      printf("FAIL: failed in child\n");
      exit(42);
    }
    // should never return
    exit(1);
  }
  int status = 0;
  if (waitpid(child, &status, 0) <= 0) {
    printf("FAIL: failed waiting for process %d - %s\n", child, 
	   strerror(errno));
    exit(1);
  }
  if (access(TEST_ROOT "/logs/userlogs/job_4/task_1", R_OK) != 0) {
    printf("FAIL: failed to create task log directory\n");
    exit(1);
  }
  if (access(task_dir, R_OK) != 0) {
    printf("FAIL: failed to create task directory %s\n", task_dir);
    exit(1);
  }
  char buffer[100000];
  sprintf(buffer, "%s/foobar", task_dir);
  if (access(buffer, R_OK) != 0) {
    printf("FAIL: failed to create touch file %s\n", buffer);
    exit(1);
  }
  free(task_dir);
  task_dir = get_job_log_directory("logs", "job_4/task_1");
  if (access(task_dir, R_OK) != 0) {
    printf("FAIL: failed to create job log directory %s\n", task_dir);
    exit(1);
  }
  free(task_dir);
}
void test_init_job() {
  printf("\nTesting init job\n");
  if (seteuid(0) != 0) {
    printf("FAIL: seteuid to root failed - %s\n", strerror(errno));
    exit(1);
  }
  FILE* creds = fopen(TEST_ROOT "/creds.txt", "w");
  if (creds == NULL) {
    printf("FAIL: failed to create credentials file - %s\n", strerror(errno));
    exit(1);
  }
  if (fprintf(creds, "secret key\n") < 0) {
    printf("FAIL: fprintf failed - %s\n", strerror(errno));
    exit(1);
  }
  if (fclose(creds) != 0) {
    printf("FAIL: fclose failed - %s\n", strerror(errno));
    exit(1);
  }
  FILE* job_xml = fopen(TEST_ROOT "/job.xml", "w");
  if (job_xml == NULL) {
    printf("FAIL: failed to create job file - %s\n", strerror(errno));
    exit(1);
  }
  if (fprintf(job_xml, "<jobconf/>\n") < 0) {
    printf("FAIL: fprintf failed - %s\n", strerror(errno));
    exit(1);
  }
  if (fclose(job_xml) != 0) {
    printf("FAIL: fclose failed - %s\n", strerror(errno));
    exit(1);
  }
  if (seteuid(user_detail->pw_uid) != 0) {
    printf("FAIL: failed to seteuid back to user - %s\n", strerror(errno));
    exit(1);
  }
  fflush(stdout);
  fflush(stderr);
  pid_t child = fork();
  if (child == -1) {
    printf("FAIL: failed to fork process for init_job - %s\n", 
	   strerror(errno));
    exit(1);
  } else if (child == 0) {
    char *final_pgm[] = {"touch", "my-touch-file", 0};
    if (initialize_job(username, "job_4", TEST_ROOT "/creds.txt", final_pgm) != 0) {
      printf("FAIL: failed in child\n");
      exit(42);
    }
    // should never return
    exit(1);
  }
  int status = 0;
  if (waitpid(child, &status, 0) <= 0) {
    printf("FAIL: failed waiting for process %d - %s\n", child, 
	   strerror(errno));
    exit(1);
  }
  if (access(TEST_ROOT "/logs/userlogs/job_4", R_OK) != 0) {
    printf("FAIL: failed to create job log directory\n");
    exit(1);
  }
  char* job_dir = get_job_directory(TEST_ROOT "/local-1", username, "job_4");
  if (access(job_dir, R_OK) != 0) {
    printf("FAIL: failed to create job directory %s\n", job_dir);
    exit(1);
  }
  char buffer[100000];
  sprintf(buffer, "%s/jobToken", job_dir);
  if (access(buffer, R_OK) != 0) {
    printf("FAIL: failed to create credentials %s\n", buffer);
    exit(1);
  }
  sprintf(buffer, "%s/my-touch-file", job_dir);
  if (access(buffer, R_OK) != 0) {
    printf("FAIL: failed to create touch file %s\n", buffer);
    exit(1);
  }
  free(job_dir);
  job_dir = get_job_log_directory("logs","job_4");
  if (access(job_dir, R_OK) != 0) {
    printf("FAIL: failed to create job log directory %s\n", job_dir);
    exit(1);
  }
  free(job_dir);
}
void test_delete_log_directory() {
    printf("\nTesting delete_log_directory\n");
    char* local_dirs = get_value("mapred.local.dir");
    char *job_log_dir = get_job_log_directory("job_1");
    if (job_log_dir == NULL) {
        exit(1);
    }
    if (create_directory_for_user(job_log_dir) != 0) {
        exit(1);
    }
    free(job_log_dir);
    char *task_log_dir = get_job_log_directory("job_1/task_2");
    if (task_log_dir == NULL) {
        exit(1);
    }
    if (mkdirs(task_log_dir, 0700) != 0) {
        exit(1);
    }
    if (access(TEST_ROOT "/logs/userlogs/job_1/task_2", R_OK) != 0) {
        printf("FAIL: can't access task directory - %s\n", strerror(errno));
        exit(1);
    }
    if (delete_log_directory("job_1/task_2", local_dirs) != 0) {
        printf("FAIL: can't delete task directory\n");
        exit(1);
    }
    if (access(TEST_ROOT "/logs/userlogs/job_1/task_2", R_OK) == 0) {
        printf("FAIL: task directory not deleted\n");
        exit(1);
    }
    if (access(TEST_ROOT "/logs/userlogs/job_1", R_OK) != 0) {
        printf("FAIL: job directory not deleted - %s\n", strerror(errno));
        exit(1);
    }
    if (delete_log_directory("job_1", local_dirs) != 0) {
        printf("FAIL: can't delete task directory\n");
        exit(1);
    }
    if (access(TEST_ROOT "/logs/userlogs/job_1", R_OK) == 0) {
        printf("FAIL: job directory not deleted\n");
        exit(1);
    }
    if (delete_log_directory("job_7", local_dirs) != 0) {
        printf("FAIL: can't delete job directory\n");
        exit(1);
    }
    if (access(TEST_ROOT "/logs/userlogs/job_7", R_OK) == 0) {
        printf("FAIL: job log directory not deleted\n");
        exit(1);
    }
    char local_job_dir[PATH_MAX+1];
    int i;
    for(i=1; i<5; i++) {
        sprintf(local_job_dir, TEST_ROOT "/local-%d/userlogs/job_7", i);
        if (access(local_job_dir, R_OK) == 0) {
            printf("FAIL: job log directory in mapred local not deleted\n");
            exit(1);
        }
    }
    free(task_log_dir);
    free(local_dirs);
}
Beispiel #12
0
/**
 * Function to prepare the attempt directories for the task JVM.
 * It creates the task work and log directories.
 */
int create_attempt_directories(const char* user,
    const char * good_local_dirs, const char *job_id, const char *task_id) {
  // create dirs as 0750
  const mode_t perms = S_IRWXU | S_IRGRP | S_IXGRP;
  if (job_id == NULL || task_id == NULL || user == NULL) {
    fprintf(LOGFILE, 
            "Either task_id is null or the user passed is null.\n");
    return -1;
  }
  int result = 0;

  char **local_dir = get_mapred_local_dirs(good_local_dirs);

  if (local_dir == NULL) {
    fprintf(LOGFILE, "Good mapred local directories could not be obtained.\n");
    return INVALID_TT_ROOT;
  }

  char **local_dir_ptr;
  for(local_dir_ptr = local_dir; *local_dir_ptr != NULL; ++local_dir_ptr) {
    char *task_dir = get_attempt_work_directory(*local_dir_ptr, user, job_id, 
                                                task_id);
    if (task_dir == NULL) {
      free_values(local_dir);
      return -1;
    }
    if (mkdirs(task_dir, perms) != 0) {
      // continue on to create other task directories
      free(task_dir);
    } else {
      free(task_dir);
    }
  }

  // also make the directory for the task logs
  char *job_task_name = malloc(strlen(job_id) + strlen(task_id) + 2);
  char *real_task_dir = NULL; // target of symlink
  char *real_job_dir = NULL;  // parent dir of target of symlink
  char *random_local_dir = NULL;
  char *link_task_log_dir = NULL; // symlink
  if (job_task_name == NULL) {
    fprintf(LOGFILE, "Malloc of job task name failed\n");
    result = -1;
  } else {
    sprintf(job_task_name, "%s/%s", job_id, task_id);
    link_task_log_dir = get_job_log_directory(job_task_name);
    random_local_dir = get_random_local_dir(local_dir);
    if(random_local_dir == NULL) {
      result = -1;
      goto cleanup;
    }
    real_job_dir = malloc(strlen(random_local_dir) + strlen(USERLOGS) + 
                          strlen(job_id) + 3);
    if (real_job_dir == NULL) {
      fprintf(LOGFILE, "Malloc of real job directory failed\n");
      result = -1;
      goto cleanup;
    } 
    real_task_dir = malloc(strlen(random_local_dir) + strlen(USERLOGS) + 
                           strlen(job_id) + strlen(task_id) + 4);
    if (real_task_dir == NULL) {
      fprintf(LOGFILE, "Malloc of real task directory failed\n");
      result = -1;
      goto cleanup;
    }
    sprintf(real_job_dir, "%s/userlogs/%s", random_local_dir, job_id);
    result = create_directory_for_user(real_job_dir);
    if( result != 0) {
      result = -1;
      goto cleanup;
    }
    sprintf(real_task_dir, "%s/userlogs/%s/%s",
            random_local_dir, job_id, task_id);
    result = mkdirs(real_task_dir, perms); 
    if( result != 0) {
      result = -1; 
      goto cleanup;
    }
    result = symlink(real_task_dir, link_task_log_dir);
    if( result != 0) {
      fprintf(LOGFILE, "Failed to create symlink %s to %s - %s\n",
              link_task_log_dir, real_task_dir, strerror(errno));
      result = -1;
    }
  }

 cleanup:
  free(random_local_dir);
  free(job_task_name);
  free(link_task_log_dir);
  free(real_job_dir);
  free(real_task_dir);
  free_values(local_dir);

  return result;
}