设为首页 收藏本站
查看: 1336|回复: 0

[经验分享] 实现hive proxy4-scratch目录权限问题解决

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-15 09:59:36 | 显示全部楼层 |阅读模式
  hive在hdfs中的job中间文件是根据当前登陆用户产生的,其默认值为/tmp/hive-${user.name},这就导致实现proxy的功能时会遇到临时文件的权限问题,比如在实现了proxy功能后,以超级用户hdfs proxy到普通用户user时,在hdfs中的临时文件在/tmp/hive-user目录中,而目录的属主是hdfs,这时再以普通用户user运行job时,对这个目录就会有权限问题,下面说下这里proxy的实现和解决权限问题的方法:
1.实现proxy功能
更改org.apache.hadoop.hive.ql.Context类的构造方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public Context(Configuration conf, String executionId)  {
  this.conf = conf;
  this.executionId = executionId;
  if(HiveConf.getBoolVar(conf,HiveConf.ConfVars.HIVE_USE_CUSTOM_PROXY)){
      String proxyUser = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_CUSTOM_PROXY_USER);
      LOG.warn("use custom proxy,gen Scratch path,proxy user is " + proxyUser);
      if(("").equals(proxyUser)||proxyUser == null||("hdfs").equals(proxyUser)){
          nonLocalScratchPath = new Path(HiveConf.getVar(conf, HiveConf.ConfVars.SCRATCHDIR),executionId);
          localScratchDir = new Path(HiveConf.getVar(conf, HiveConf.ConfVars.LOCALSCRATCHDIR),executionId).toUri().getPath();
      }else{
          localScratchDir = new Path((System.getProperty("java.io.tmpdir") + File.separator + proxyUser),executionId).toUri().getPath();
          nonLocalScratchPath = new Path(("/tmp/hive-" + proxyUser),executionId);
          }
  }else{
      nonLocalScratchPath = new Path(HiveConf.getVar(conf, HiveConf.ConfVars.SCRATCHDIR),executionId);
      localScratchDir = new Path(HiveConf.getVar(conf, HiveConf.ConfVars.LOCALSCRATCHDIR),executionId).toUri().getPath();
  }
  LOG.warn("in Context init function nonLocalScratchPath is " + nonLocalScratchPath);
  LOG.warn("in Context init function localScratchPath is " + localScratchDir);
  scratchDirPermission= HiveConf.getVar(conf, HiveConf.ConfVars.SCRATCHDIRPERMISSION);
}



2.权限问题的解决
在上面的代码中可以看到scratchDirPermission的设置,这个是指创建的目录的权限,默认是700,因为是中间文件,我们可以把权限设置的大一点,比如777,在设置了777之后,却发现目录的权限是755.
根据报错的堆栈可以看到方法在Context中调用的情况:

1
getExternalTmpPath--->getExternalScratchDir-->getScratchDir-->Utilities.createDirsWithPermission



(目录不存在时,根据HiveConf.ConfVars.SCRATCHDIRPERMISSION的设置创建hdfs tmp目录)
看下getScratchDir方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
private final Map<String, Path> fsScratchDirs = new HashMap<String, Path>();
.....
  private Path getScratchDir(String scheme, String authority,
                               boolean mkdir, String scratchDir) { // 如果是explain语句mkdir为false
    String fileSystem =  scheme + ":" + authority;
    Path dir = fsScratchDirs.get(fileSystem + "-" + TaskRunner.getTaskRunnerID());
    if (dir == null) {
      Path dirPath = new Path(scheme, authority,
          scratchDir + "-" + TaskRunner.getTaskRunnerID());
      if (mkdir) {
        try {
          FileSystem fs = dirPath.getFileSystem( conf);
          dirPath = new Path(fs.makeQualified(dirPath).toString());
          FsPermission fsPermission = new FsPermission(Short.parseShort(scratchDirPermission.trim(), 8));
// 目录权限由HiveConf.ConfVars.SCRATCHDIRPERMISSION 设置
          if (!Utilities.createDirsWithPermission(conf , dirPath, fsPermission)) {
            throw new RuntimeException("Cannot make directory: "
                                       + dirPath.toString());
          }
          if (isHDFSCleanup ) {
            fs.deleteOnExit(dirPath);
          }
        } catch (IOException e) {
          throw new RuntimeException (e);
        }
      }
      dir = dirPath;
      fsScratchDirs.put(fileSystem + "-" + TaskRunner.getTaskRunnerID(), dir);
    }
    return dir;
  }



调用Utilities.createDirsWithPermission方法时,传入的目录的权限(由HiveConf.ConfVars.SCRATCHDIRPERMISSION 设置)默认是700
org.apache.hadoop.hive.ql.exec.Utilities类的createDirsWithPermission方法内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public static boolean createDirsWithPermission(Configuration conf, Path mkdir,
    FsPermission fsPermission) throws IOException {
  boolean recursive = false;
  if (SessionState.get() != null) {
    recursive = SessionState.get().isHiveServerQuery() &&
        conf.getBoolean(HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS.varname,
            HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS.defaultBoolVal);
            //如果是来自hiverserver的请求,并且开启了doas,recursive为true,权限设置为777,umask 000
    fsPermission = new FsPermission((short)00777);
  }
  // if we made it so far without exception we are good!
  return createDirsWithPermission(conf, mkdir, fsPermission, recursive); //默认recursive为false
}
.....
public static boolean createDirsWithPermission(Configuration conf, Path mkdirPath,
    FsPermission fsPermission, boolean recursive) throws IOException {
  String origUmask = null;
  LOG.warn("Create dirs " + mkdirPath + " with permission " + fsPermission + " recursive " +
      recursive);
      
  if (recursive) { //如果recursive为true,origUmask 为000,否则为null
    origUmask = conf.get("fs.permissions.umask-mode");
    // this umask is required because by default the hdfs mask is 022 resulting in
    // all parents getting the fsPermission & !(022) permission instead of fsPermission
    conf.set("fs.permissions.umask-mode", "000");
  }
  FileSystem fs = ShimLoader.getHadoopShims().getNonCachedFileSystem(mkdirPath.toUri(), conf);
  LOG.warn("fs.permissions.umask-mode is " + conf.get("fs.permissions.umask-mode"));  //默认为022
  boolean retval = false;
  try {
    retval = fs.mkdirs(mkdirPath, fsPermission);
    resetConfAndCloseFS(conf, recursive, origUmask, fs);
    //这里因为recursive为false,导致不会重置fs.permissions.umask-mode的配置,
    即fs.permissions.umask-mode为022,因此导致即使设置了权限为777,创建的目录权限最终还是为755
  } catch (IOException ioe) {
    try {
      resetConfAndCloseFS(conf, recursive, origUmask, fs);
    }
    catch (IOException e) {
      // do nothing - double failure
    }
  }
  return retval;
}



hdfs中关于fs.permissions.umask-mode的配置,默认是002

1
2
public static final String  FS_PERMISSIONS_UMASK_KEY = "fs.permissions.umask-mode";
public static final int     FS_PERMISSIONS_UMASK_DEFAULT = 0022;



为了实现可以创建777权限的临时文件目录,更改createDirsWithPermission方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
public static boolean createDirsWithPermission(Configuration conf, Path mkdir,
    FsPermission fsPermission) throws IOException {
  boolean recursive = false;
  if (SessionState.get() != null) {
    recursive = (SessionState.get().isHiveServerQuery() &&
        conf.getBoolean(HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS.varname,
            HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS.defaultBoolVal))||(HiveConf.getBoolVar(conf,HiveConf.ConfVars.HIVE_USE_CUSTOM_PROXY));
    fsPermission = new FsPermission((short)00777);
  }
  // if we made it so far without exception we are good!
  return createDirsWithPermission(conf, mkdir, fsPermission, recursive);
}



这样,就可以创建出777的hdfs目录了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-38038-1-1.html 上篇帖子: 实现hive proxy2-hive操作hadoop时使用用户的地方 下篇帖子: win7+eclipse配置Hadoop开发环境注意事项
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表