/** * Read project meta from zookeeper * * @param projectName name of the project * @return project meta * @throws KeeperException throws when write project meta to zookeeper * @throws InterruptedException if the zookeeper server transaction is interrupted */ private ProjectMeta constrcutProjectMeta(String projectName) throws KeeperException, InterruptedException { ProjectMeta projectMeta = null; String packageRoot = null; String masterAddr = null; String projectPath = Utils.getProjectRootPath(projectName); byte[] b = zk.getData(projectPath, false, null); // read the master meta MasterMeta maserMeta = JsonUtils.getObject(b, MasterMeta.class); packageRoot = maserMeta.getPackageRoot(); masterAddr = maserMeta.getMasterAddr(); String projectMetaPath = Utils.getProjectMetaRootPath(projectName); List<String> packageNameList = zk.getChildren(projectMetaPath, false); // read package list projectMeta = new ProjectMeta( projectName, masterAddr, packageRoot, readPackageInfo(projectMetaPath, packageNameList)); return projectMeta; }
/** * Read package info from zookeeper. * * @param packageRootPath the root path of the packages * @param packageNameList the list of package names * @return the list of package metas * @throws KeeperException throws when write project meta to zookeeper * @throws InterruptedException if the zookeeper server transaction is interrupted */ private List<PackageMeta> readPackageInfo(String packageRootPath, List<String> packageNameList) throws KeeperException, InterruptedException { List<PackageMeta> packageMetaList = new ArrayList<PackageMeta>(); for (String pkgName : packageNameList) { String packagePath = Utils.constructString(packageRootPath, Global.PATH_SEPARATOR, pkgName); // read package data and construct the meta object byte[] data = zk.getData(packagePath, false, null); PackageMeta packageMeta = JsonUtils.getObject(data, PackageMeta.class); packageMetaList.add(packageMeta); } return packageMetaList; }
/** * Parse project config from config file * * @param configFile filen name of the config file * @param localAddr address of the current master node * @return the project meta * @throws KeeperException throws when write project meta to zookeeper * @throws InterruptedException if the zookeeper server transaction is interrupted */ private ProjectMeta parseProjectConfig(String configFile, String localAddr) throws KeeperException, InterruptedException { ProjectMeta projectMeta = new ProjectMeta(configFile, localAddr); checkBasicNode(projectMeta); String projectPath = Utils.getProjectRootPath(projectMeta.getProjectName()); MasterMeta masterMeta = new MasterMeta(projectMeta.getMasterAddr(), projectMeta.getPackageRoot()); byte[] data = JsonUtils.getObjectData(masterMeta); zk.setData(projectPath, data, -1); writePackageInfo(projectMeta); return projectMeta; }
/** * Write packages info of project to zookeeper * * @param projectMeta project meta * @throws KeeperException throws when write project meta to zookeeper * @throws InterruptedException if the zookeeper server transaction is interrupted */ private void writePackageInfo(ProjectMeta projectMeta) throws KeeperException, InterruptedException { String projectMetaPath = Utils.getProjectMetaRootPath(projectMeta.getProjectName()); List<String> packageNameList = projectMeta.getTopoOrderPackageList(); for (String pkgName : packageNameList) { PackageMeta packageMeta = projectMeta.getPackageMeta(pkgName); String packagePath = Utils.constructString(projectMetaPath, Global.PATH_SEPARATOR, pkgName); byte[] packageInfo = JsonUtils.getObjectData(packageMeta); // write package info if (!Utils.checkNode(zk, packagePath)) { zk.create(packagePath, packageInfo, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { zk.setData(packagePath, packageInfo, -1); } } }