From e5ddb5b65ac392c1c308ff190690f856def77f31 Mon Sep 17 00:00:00 2001 From: ZhenDu <454858191@qq.com> Date: Thu, 3 Aug 2017 17:32:35 +0800 Subject: [PATCH 1/2] add some method for cluster config and global config in metastore --- .../org/smartdata/metastore/MetaStore.java | 69 +++++++++++++++++++ .../metastore/dao/ClusterConfigDao.java | 2 +- .../metastore/dao/GlobalConfigDao.java | 5 ++ .../smartdata/metastore/TestMetaStore.java | 35 ++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/MetaStore.java b/smart-metastore/src/main/java/org/smartdata/metastore/MetaStore.java index ff85184de54..bbd1b46be84 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/MetaStore.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/MetaStore.java @@ -24,15 +24,18 @@ import org.smartdata.metastore.dao.AccessCountTable; import org.smartdata.metastore.dao.ActionDao; import org.smartdata.metastore.dao.CacheFileDao; +import org.smartdata.metastore.dao.ClusterConfigDao; import org.smartdata.metastore.dao.CmdletDao; import org.smartdata.metastore.dao.FileDiffDao; import org.smartdata.metastore.dao.FileInfoDao; +import org.smartdata.metastore.dao.GlobalConfigDao; import org.smartdata.metastore.dao.GroupsDao; import org.smartdata.metastore.dao.MetaStoreHelper; import org.smartdata.metastore.dao.RuleDao; import org.smartdata.metastore.dao.StorageDao; import org.smartdata.metastore.dao.UserDao; import org.smartdata.metastore.dao.XattrDao; +import org.smartdata.model.ClusterConfig; import org.smartdata.model.CmdletState; import org.smartdata.model.ActionInfo; import org.smartdata.model.CmdletInfo; @@ -40,6 +43,7 @@ import org.smartdata.model.FileAccessInfo; import org.smartdata.model.FileDiff; import org.smartdata.model.FileInfo; +import org.smartdata.model.GlobalConfig; import org.smartdata.model.RuleInfo; import org.smartdata.model.StorageCapacity; import org.smartdata.model.StoragePolicy; @@ -86,6 +90,8 @@ public class MetaStore { private FileDiffDao fileDiffDao; private AccessCountDao accessCountDao; private MetaStoreHelper metaStoreHelper; + private ClusterConfigDao clusterConfigDao; + private GlobalConfigDao globalConfigDao; public MetaStore(DBPool pool) throws MetaStoreException { this.pool = pool; @@ -101,6 +107,8 @@ public MetaStore(DBPool pool) throws MetaStoreException { accessCountDao = new AccessCountDao(pool.getDataSource()); fileDiffDao = new FileDiffDao(pool.getDataSource()); metaStoreHelper = new MetaStoreHelper(pool.getDataSource()); + clusterConfigDao = new ClusterConfigDao(pool.getDataSource()); + globalConfigDao = new GlobalConfigDao(pool.getDataSource()); } public Connection getConnection() throws MetaStoreException { @@ -837,4 +845,65 @@ public void aggregateTables(AccessCountTable destinationTable throw new MetaStoreException(e); } } + + public void setClusterConfig(ClusterConfig clusterConfig) throws MetaStoreException { + try { + + if (clusterConfigDao.getCountByName(clusterConfig.getNode_name()) == 0) { + //insert + clusterConfigDao.insert(clusterConfig); + } else { + //update + clusterConfigDao.updateByNodeName(clusterConfig.getNode_name(),clusterConfig.getConfig_path()); + } + } catch (Exception e) { + throw new MetaStoreException(e); + } + } + + public void delClusterConfig(ClusterConfig clusterConfig) throws MetaStoreException { + try { + if (clusterConfigDao.getCountByName(clusterConfig.getNode_name()) > 0){ + //insert + clusterConfigDao.delete(clusterConfig.getCid()); + } + } catch (Exception e) { + throw new MetaStoreException(e); + } + } + + public List listClusterConfig() throws MetaStoreException { + try { + return clusterConfigDao.getAll(); + } catch (Exception e) { + throw new MetaStoreException(e); + } + } + + public GlobalConfig getDefaultGlobalConfigByName(String config_name) throws MetaStoreException { + try { + if (globalConfigDao.getCountByName(config_name) > 0) { + //the property is existed + return globalConfigDao.getByPropertyName(config_name); + } else { + return null; + } + } catch (Exception e) { + throw new MetaStoreException(e); + } + } + + public void setGlobalConfig(GlobalConfig globalConfig) throws MetaStoreException { + try { + if (globalConfigDao.getCountByName(globalConfig.getProperty_name()) > 0) { + globalConfigDao.update(globalConfig.getProperty_name(), globalConfig.getProperty_value()); + } else { + globalConfigDao.insert(globalConfig); + } + } catch (Exception e) { + throw new MetaStoreException(e); + } + } + + } diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/dao/ClusterConfigDao.java b/smart-metastore/src/main/java/org/smartdata/metastore/dao/ClusterConfigDao.java index 2e2ecba38e7..cd68979390a 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/dao/ClusterConfigDao.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/dao/ClusterConfigDao.java @@ -70,7 +70,7 @@ public ClusterConfig getById(long cid) { public long getCountByName(String name) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - return jdbcTemplate.queryForObject("select COUNT(*) FROM cluster_config WHERE node_name = ?",Long.class); + return jdbcTemplate.queryForObject("select COUNT(*) FROM cluster_config WHERE node_name = ?",Long.class,name); } public ClusterConfig getByName(String name) { diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/dao/GlobalConfigDao.java b/smart-metastore/src/main/java/org/smartdata/metastore/dao/GlobalConfigDao.java index 759ea822113..4cd98f3acdc 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/dao/GlobalConfigDao.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/dao/GlobalConfigDao.java @@ -105,6 +105,11 @@ public void insert(GlobalConfig[] globalConfigs) { simpleJdbcInsert.executeBatch(maps); } + public long getCountByName(String name) { + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + return jdbcTemplate.queryForObject("select COUNT(*) FROM global_config WHERE property_name = ?",Long.class,name); + } + public int update(String property_name, String property_value) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String sql = "update global_config set property_value = ? WHERE property_name = ?"; diff --git a/smart-metastore/src/test/java/org/smartdata/metastore/TestMetaStore.java b/smart-metastore/src/test/java/org/smartdata/metastore/TestMetaStore.java index a51a3aa2f73..4b8948e76f8 100644 --- a/smart-metastore/src/test/java/org/smartdata/metastore/TestMetaStore.java +++ b/smart-metastore/src/test/java/org/smartdata/metastore/TestMetaStore.java @@ -25,9 +25,11 @@ import org.smartdata.metrics.FileAccessEvent; import org.smartdata.model.ActionInfo; import org.smartdata.model.CachedFileStatus; +import org.smartdata.model.ClusterConfig; import org.smartdata.model.CmdletInfo; import org.smartdata.model.CmdletState; import org.smartdata.model.FileInfo; +import org.smartdata.model.GlobalConfig; import org.smartdata.model.RuleInfo; import org.smartdata.model.RuleState; import org.smartdata.model.StorageCapacity; @@ -488,4 +490,37 @@ public void testInsertXattrTable() throws Exception { Assert.assertTrue(result.size() == attributes.size()); Assert.assertTrue(result.containsAll(attributes)); } + + @Test + public void testSetClusterConfig() throws MetaStoreException { + ClusterConfig clusterConfig = new ClusterConfig(1,"test" , "test1"); + metaStore.setClusterConfig(clusterConfig); + List list = new LinkedList<>(); + list.add(clusterConfig); + Assert.assertTrue(metaStore.listClusterConfig().equals(list)); + list.get(0).setConfig_path("test2"); + + metaStore.setClusterConfig(list.get(0)); + Assert.assertTrue(metaStore.listClusterConfig().equals(list)); + } + + @Test + public void testDelClusterConfig() throws MetaStoreException { + ClusterConfig clusterConfig = new ClusterConfig(1, "test", "test1"); + metaStore.setClusterConfig(clusterConfig); + metaStore.delClusterConfig(clusterConfig); + Assert.assertTrue(metaStore.listClusterConfig().size() == 0); + } + + @Test + public void testSetGlobalConfig() throws MetaStoreException { + GlobalConfig globalConfig = new GlobalConfig(1,"test" , "test1"); + metaStore.setGlobalConfig(globalConfig); + Assert.assertTrue(metaStore.getDefaultGlobalConfigByName("test").equals(globalConfig)); + globalConfig.setProperty_value("test2"); + + metaStore.setGlobalConfig(globalConfig); + Assert.assertTrue(metaStore.getDefaultGlobalConfigByName("test").equals(globalConfig)); + } + } From 683a7119fa3461a8d442f127a25a7106948478bb Mon Sep 17 00:00:00 2001 From: ZhenDu <454858191@qq.com> Date: Thu, 3 Aug 2017 22:25:12 +0800 Subject: [PATCH 2/2] fix some bug --- .../java/org/smartdata/metastore/dao/TestClusterConfigDao.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart-metastore/src/test/java/org/smartdata/metastore/dao/TestClusterConfigDao.java b/smart-metastore/src/test/java/org/smartdata/metastore/dao/TestClusterConfigDao.java index 2fe0712948f..a754463ddb2 100644 --- a/smart-metastore/src/test/java/org/smartdata/metastore/dao/TestClusterConfigDao.java +++ b/smart-metastore/src/test/java/org/smartdata/metastore/dao/TestClusterConfigDao.java @@ -62,6 +62,6 @@ public void testgetCountByName(){ Assert.assertTrue(clusterConfigDao.getCountByName("test") == 0); ClusterConfig clusterConfig = new ClusterConfig(1, "test", "test1"); clusterConfigDao.insert(clusterConfig); - Assert.assertTrue(clusterConfigDao.getCountByName("test") == 0); + Assert.assertTrue(clusterConfigDao.getCountByName("test") == 1); } }