Search in sources :

Example 21 with CobarAdapterDAO

use of com.alibaba.cobar.manager.dao.CobarAdapterDAO in project cobar by alibaba.

the class CobarAccesser method getAccesser.

public CobarAdapterDAO getAccesser(long cobarId) {
    final CobarDO cobar = xmlAccesser.getCobarDAO().getCobarById(cobarId);
    if (cobar == null) {
        logger.error(new StringBuilder("Fail to get cobar information which id = ").append(cobarId).toString());
    }
    CobarAdapterDAO accesser = cobarAdapterDelegate.getCobarNodeAccesser(cobar.getHost(), cobar.getPort(), cobar.getUser(), cobar.getPassword());
    return accesser;
}
Also used : CobarAdapterDAO(com.alibaba.cobar.manager.dao.CobarAdapterDAO) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO)

Example 22 with CobarAdapterDAO

use of com.alibaba.cobar.manager.dao.CobarAdapterDAO in project cobar by alibaba.

the class PropertyReload method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    UserDO user = (UserDO) request.getSession().getAttribute("user");
    String types = request.getParameter("type");
    int type = typeMap.get(types);
    String list = request.getParameter("list");
    String[] temp = list.split(",");
    int[] index = new int[temp.length];
    for (int i = 0; i < temp.length; i++) {
        index[i] = Integer.parseInt(temp[i]);
    }
    if (logger.isWarnEnabled()) {
        StringBuilder log = new StringBuilder(user.getUsername()).append(" | do ").append(types).append(" | cobar:");
        for (int i = 0; i < index.length; i++) {
            log.append(xmlAccesser.getCobarDAO().getCobarById(index[i])).append(" ");
        }
        logger.warn(log.toString());
    }
    List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
    lock.lock();
    try {
        switch(type) {
            case CONFIG_RELOAD:
                for (int i = 0; i < index.length; i++) {
                    CobarDO c = xmlAccesser.getCobarDAO().getCobarById(index[i]);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
                    if (c.getStatus().equals(ConstantDefine.ACTIVE)) {
                        CobarAdapterDAO perf = cobarAccesser.getAccesser(index[i]);
                        if (perf.checkConnection()) {
                            if (perf.reloadConfig()) {
                                map.put("result", "success");
                            } else {
                                map.put("result", "config reload error");
                            }
                        } else {
                            map.put("result", "connection error");
                        }
                    } else {
                        map.put("result", "cobar InActive");
                    }
                    resultList.add(map);
                }
                break;
            case CONFIG_ROLLBACK:
                for (int i = 0; i < index.length; i++) {
                    CobarDO c = xmlAccesser.getCobarDAO().getCobarById(index[i]);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
                    if (c.getStatus().equals(ConstantDefine.ACTIVE)) {
                        CobarAdapterDAO perf = cobarAccesser.getAccesser(index[i]);
                        if (perf.checkConnection()) {
                            if (perf.rollbackConfig()) {
                                map.put("result", "success");
                            } else {
                                map.put("result", "config reload error");
                            }
                        } else {
                            map.put("result", "connection error");
                        }
                    } else {
                        map.put("result", "cobar InActive");
                    }
                    resultList.add(map);
                }
                break;
            default:
                logger.error("Wrong property control type!");
                break;
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "UNKNOWN ERROR");
        map.put("result", "unknown exception occurs when reloading");
        resultList.clear();
        resultList.add(map);
    } finally {
        lock.unlock();
    }
    return new ModelAndView("c_result", "resultList", resultList);
}
Also used : CobarAdapterDAO(com.alibaba.cobar.manager.dao.CobarAdapterDAO) HashMap(java.util.HashMap) UserDO(com.alibaba.cobar.manager.dataobject.xml.UserDO) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with CobarAdapterDAO

use of com.alibaba.cobar.manager.dao.CobarAdapterDAO in project cobar by alibaba.

the class PropertyReloadScreen method handleRequestInternal.

@SuppressWarnings("unchecked")
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    UserDO user = (UserDO) request.getSession().getAttribute("user");
    String id = request.getParameter("clusterId");
    long clusterId = -1;
    if (null != id) {
        clusterId = Long.parseLong(id);
    }
    List<ClusterDO> cList = xmlAccesser.getClusterDAO().listAllCluster();
    List<Map<String, Object>> clusterList = new ArrayList<Map<String, Object>>();
    ListSortUtil.sortClusterByName(cList);
    for (ClusterDO e : cList) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", e.getId());
        map.put("name", CobarStringUtil.htmlEscapedString(e.getName()));
        clusterList.add(map);
    }
    List<CobarDO> cobarList = null;
    if (null != cList && cList.size() > 0) {
        if (-1 == clusterId) {
            clusterId = cList.get(0).getId();
            cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId);
        } else {
            cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId);
        }
    }
    List<Map<String, Object>> cobarListMap = new ArrayList<Map<String, Object>>();
    PropertyUtilsBean util = new PropertyUtilsBean();
    if (null != cobarList) {
        ListSortUtil.sortCobarByName(cobarList);
        for (CobarDO c : cobarList) {
            CobarAdapterDAO perf = cobarAccesser.getAccesser(c.getId());
            Map<String, Object> map;
            try {
                map = util.describe(c);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
            map.remove("class");
            map.remove("name");
            map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
            if (ConstantDefine.ACTIVE.equals(c.getStatus())) {
                if (!perf.checkConnection()) {
                    map.remove("status");
                    map.put("status", ConstantDefine.ERROR);
                    map.put("reloadTime", "");
                    map.put("rollbackTime", "");
                } else {
                    ServerStatus ss = perf.getServerStatus();
                    String rollbackTime = "NO";
                    String reloadTime = FormatUtil.fromMilliseconds2String(ss.getReloadTime());
                    if (ss.getRollbackTime() != -1) {
                        rollbackTime = FormatUtil.fromMilliseconds2String(ss.getRollbackTime());
                    }
                    map.put("reloadTime", reloadTime);
                    map.put("rollbackTime", rollbackTime);
                }
            } else {
                map.put("reloadTime", "");
                map.put("rollbackTime", "");
            }
            cobarListMap.add(map);
        }
    }
    return new ModelAndView("c_propertyReload", new FluenceHashMap<String, Object>().putKeyValue("cList", clusterList).putKeyValue("cobarList", cobarListMap).putKeyValue("clusterId", clusterId).putKeyValue("user", user));
}
Also used : CobarAdapterDAO(com.alibaba.cobar.manager.dao.CobarAdapterDAO) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) UserDO(com.alibaba.cobar.manager.dataobject.xml.UserDO) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) ServerStatus(com.alibaba.cobar.manager.dataobject.cobarnode.ServerStatus) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) Map(java.util.Map)

Example 24 with CobarAdapterDAO

use of com.alibaba.cobar.manager.dao.CobarAdapterDAO in project cobar by alibaba.

the class CobarAccessTest method main.

public static void main(String[] args) throws Exception {
    String xmlpath = "./src/main/resources/";
    CobarAccesser accesser = new CobarAccesser();
    XmlAccesser xmlAccesser = new XmlAccesser();
    XMLFileLoaderPath xmlFileLoader = new XMLFileLoaderPath();
    xmlFileLoader.setXmlPath(xmlpath);
    ClusterDAOImple cluster = new ClusterDAOImple();
    cluster.setXmlFileLoader(xmlFileLoader);
    cluster.afterPropertiesSet();
    CobarDAOImple cobar = new CobarDAOImple();
    cobar.setXmlFileLoader(xmlFileLoader);
    cobar.afterPropertiesSet();
    xmlAccesser.setClusterDAO(cluster);
    xmlAccesser.setCobarDAO(cobar);
    accesser.setXmlAccesser(xmlAccesser);
    AdapterDelegate res = new AdapterDelegate();
    accesser.setCobarAdapterDelegate(res);
    CobarAdapterDAO perf = accesser.getAccesser(1L);
    String version = perf.getVersion();
    System.out.println(version);
    ServerStatus ss = perf.getServerStatus();
    System.out.println(ss.getMaxMemory());
    System.out.println(ss.getStatus());
    System.out.println(ss.getTotalMemory());
    System.out.println(ss.getUptime());
    System.out.println(ss.getUsedMemory());
    System.out.println(ss.getReloadTime());
    System.out.println(ss.getRollbackTime());
    List<String> databases = perf.listDataBases();
    for (String s : databases) {
        System.out.println(s);
    }
//        perf.reloadConfig();
//        perf.rollbackConfig();
//        int num = perf.switchDataNode("circe,cndb,dubbo,napoli", 0);
//        System.out.println("num: " + num);
//        int num = perf.stopHeartbeat("circe,cndb,dubbo,napoli", -10);
//        System.out.println("num: " + num);
}
Also used : CobarAdapterDAO(com.alibaba.cobar.manager.dao.CobarAdapterDAO) ClusterDAOImple(com.alibaba.cobar.manager.dao.xml.ClusterDAOImple) XmlAccesser(com.alibaba.cobar.manager.service.XmlAccesser) ServerStatus(com.alibaba.cobar.manager.dataobject.cobarnode.ServerStatus) XMLFileLoaderPath(com.alibaba.cobar.manager.dao.xml.XMLFileLoaderPath) CobarDAOImple(com.alibaba.cobar.manager.dao.xml.CobarDAOImple) CobarAccesser(com.alibaba.cobar.manager.service.CobarAccesser) AdapterDelegate(com.alibaba.cobar.manager.dao.delegate.AdapterDelegate)

Aggregations

CobarAdapterDAO (com.alibaba.cobar.manager.dao.CobarAdapterDAO)24 CobarDO (com.alibaba.cobar.manager.dataobject.xml.CobarDO)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)15 Map (java.util.Map)14 JSONObject (net.sf.json.JSONObject)9 ModelAndView (org.springframework.web.servlet.ModelAndView)9 UserDO (com.alibaba.cobar.manager.dataobject.xml.UserDO)7 ClusterDO (com.alibaba.cobar.manager.dataobject.xml.ClusterDO)5 CommandStatus (com.alibaba.cobar.manager.dataobject.cobarnode.CommandStatus)4 ProcessorStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ProcessorStatus)4 ServerStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ServerStatus)4 JSONArray (net.sf.json.JSONArray)4 FluenceHashMap (com.alibaba.cobar.manager.util.FluenceHashMap)3 PropertyUtilsBean (org.apache.commons.beanutils.PropertyUtilsBean)3 ConnectionStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ConnectionStatus)2 DataNodesStatus (com.alibaba.cobar.manager.dataobject.cobarnode.DataNodesStatus)2 Pair (com.alibaba.cobar.manager.util.Pair)2 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2