Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

add some method for cluster config and global config in metastore #869

Merged
merged 2 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@
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;
import org.smartdata.model.CachedFileStatus;
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<ClusterConfig> 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);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ClusterConfig> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}