Search in sources :

Example 1 with CobarDO

use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.

the class CobarDAOImple method write.

@SuppressWarnings({ "rawtypes", "unchecked" })
private boolean write() {
    FileOutputStream os = null;
    lock.lock();
    try {
        if (!backup(xmlPath)) {
            logger.error("cobar backup fail!");
        }
        os = new FileOutputStream(xmlPath);
        xsl.setOutput(os, "UTF-8");
        xsl.startDocument("UTF-8", null);
        xsl.text("\n");
        xsl.startTag(null, "cobars");
        xsl.text("\n");
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Long, CobarDO> entry = (Entry<Long, CobarDO>) it.next();
            CobarDO cobar = entry.getValue();
            if (!write(cobar)) {
                throw new IOException("Cobar write error!");
            }
        }
        xsl.endTag(null, "cobars");
        xsl.endDocument();
        os.close();
        return true;
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        lock.unlock();
    }
    if (null != os) {
        try {
            os.close();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
Also used : Entry(java.util.Map.Entry) FileOutputStream(java.io.FileOutputStream) Iterator(java.util.Iterator) IOException(java.io.IOException) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with CobarDO

use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.

the class CobarDAOImple method getCobarList.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public List<CobarDO> getCobarList(long clusterId) {
    List<CobarDO> list = new ArrayList<CobarDO>();
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Long, CobarDO> entry = (Entry<Long, CobarDO>) it.next();
        CobarDO cobar = entry.getValue();
        if (clusterId == cobar.getClusterId()) {
            list.add(cobar);
        }
    }
    return list;
}
Also used : Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with CobarDO

use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.

the class StopHeartbeat method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
    UserDO user = (UserDO) request.getSession().getAttribute("user");
    String datanodes = request.getParameter("datanodes");
    int time = Integer.parseInt(request.getParameter("stoptime"));
    long clusterId = Long.parseLong(request.getParameter("clusterIdK"));
    ClusterDO cluster = xmlAccesser.getClusterDAO().getClusterById(clusterId);
    List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
    if (logger.isWarnEnabled()) {
        StringBuilder log = new StringBuilder();
        log.append(user.getUsername()).append(" | stop heartbeat | cluster:");
        log.append(cluster.getName()).append(" | ");
        log.append(datanodes).append(" | time:");
        log.append(time);
        logger.warn(log.toString());
    }
    List<CobarDO> cobarList = null;
    lock.lock();
    try {
        cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId, ConstantDefine.ACTIVE);
        for (CobarDO c : cobarList) {
            CobarAdapterDAO control = cobarAccesser.getAccesser(c.getId());
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
            if (control.checkConnection()) {
                int num = control.stopHeartbeat(datanodes, time);
                map.put("result", num + " rows");
            } else {
                map.put("result", "connction error!");
            }
            resultList.add(map);
        }
    } 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 stopping heartbeat");
        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) ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with CobarDO

use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.

the class SwitchDatanodes method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String datanodes = request.getParameter("datanodes");
    int index = Integer.parseInt(request.getParameter("index"));
    long clusterId = Long.parseLong(request.getParameter("clusterIdK"));
    UserDO user = (UserDO) request.getSession().getAttribute("user");
    ClusterDO cluster = xmlAccesser.getClusterDAO().getClusterById(clusterId);
    List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
    List<CobarDO> cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId, ConstantDefine.ACTIVE);
    if (logger.isWarnEnabled()) {
        StringBuilder log = new StringBuilder();
        log.append(user.getUsername()).append(" | switch datanodes | cluster:");
        log.append(cluster.getName()).append(" | ");
        log.append(datanodes).append(" | index:");
        log.append(index);
        logger.warn(log.toString());
    }
    lock.lock();
    try {
        for (CobarDO c : cobarList) {
            CobarAdapterDAO control = cobarAccesser.getAccesser(c.getId());
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
            if (control.checkConnection()) {
                int num = control.switchDataNode(datanodes, index);
                map.put("result", num + " rows");
            } else {
                map.put("result", "connction error!");
            }
            resultList.add(map);
        }
    } 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 switching");
        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) ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with CobarDO

use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.

the class ClusterInstantPerfValueAjax method getClusterInfo.

private AjaxResult getClusterInfo(AjaxParams params) {
    JSONArray array = params.getArray();
    long clusterId = params.getClusterId();
    JSONObject json = null;
    if (array.size() > 0) {
        json = array.getJSONObject(0);
    }
    AjaxResult rs = new AjaxResult();
    rs.setId(clusterId);
    List<CobarDO> nodes = xmlAccesser.getCobarDAO().getCobarList(clusterId);
    rs.setTotal(nodes.size());
    for (CobarDO cobar : nodes) {
        if (ConstantDefine.IN_ACTIVE.equals(cobar.getStatus())) {
            continue;
        }
        CobarAdapterDAO perfAccesser = cobarAccesser.getAccesser(cobar.getId());
        if (!perfAccesser.checkConnection()) {
            rs.addError(1);
            StringBuilder sb = new StringBuilder("getClusterInfo : cobar connect error for [ Name:");
            sb.append(cobar.getName()).append(" Host:").append(cobar.getHost()).append(" ]");
            logger.error(sb.toString());
            continue;
        }
        rs.addActive(1);
        rs.setSchema(perfAccesser.listDataBases().size());
        List<ProcessorStatus> list = perfAccesser.listProccessorStatus();
        rs.addNetIn(groupByPList(list, NET_IN));
        rs.addNetOut(groupByPList(list, NET_OUT));
        rs.addConnection(groupByPList(list, CONNECTION));
        rs.setTimestamp(list.get(list.size() - 1).getSampleTimeStamp());
        List<CommandStatus> commandList = perfAccesser.listCommandStatus();
        rs.addRequest(groupByCList(commandList, REQUEST_COUNT));
    }
    if (json != null && json.getLong("netIn") != -1) {
        long o_tiemstamp = json.getLong("timestamp");
        rs.setNetIn_deriv(FormatUtil.formatNetwork(Math.round(MathUtil.getDerivate(rs.getNetIn(), json.getLong("netIn"), rs.getTimestamp(), o_tiemstamp, 1000.0))));
        rs.setNetOut_deriv(FormatUtil.formatNetwork(Math.round(MathUtil.getDerivate(rs.getNetOut(), json.getLong("netOut"), rs.getTimestamp(), o_tiemstamp, 1000.0))));
        rs.setRequest_deriv(FormatUtil.formatNumber(Math.round(MathUtil.getDerivate(rs.getRequest(), json.getLong("reCount"), rs.getTimestamp(), o_tiemstamp, 1000.0))));
    }
    return rs;
}
Also used : CobarAdapterDAO(com.alibaba.cobar.manager.dao.CobarAdapterDAO) JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray) CommandStatus(com.alibaba.cobar.manager.dataobject.cobarnode.CommandStatus) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) ProcessorStatus(com.alibaba.cobar.manager.dataobject.cobarnode.ProcessorStatus)

Aggregations

CobarDO (com.alibaba.cobar.manager.dataobject.xml.CobarDO)35 CobarAdapterDAO (com.alibaba.cobar.manager.dao.CobarAdapterDAO)16 HashMap (java.util.HashMap)16 Map (java.util.Map)15 ArrayList (java.util.ArrayList)14 ModelAndView (org.springframework.web.servlet.ModelAndView)13 UserDO (com.alibaba.cobar.manager.dataobject.xml.UserDO)10 ClusterDO (com.alibaba.cobar.manager.dataobject.xml.ClusterDO)9 FluenceHashMap (com.alibaba.cobar.manager.util.FluenceHashMap)7 Test (org.junit.Test)7 PropertyUtilsBean (org.apache.commons.beanutils.PropertyUtilsBean)6 IOException (java.io.IOException)5 Iterator (java.util.Iterator)4 Entry (java.util.Map.Entry)4 JSONObject (net.sf.json.JSONObject)3 CommandStatus (com.alibaba.cobar.manager.dataobject.cobarnode.CommandStatus)2 ProcessorStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ProcessorStatus)2 ServerStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ServerStatus)2 Pair (com.alibaba.cobar.manager.util.Pair)2 JSONArray (net.sf.json.JSONArray)2