Search in sources :

Example 21 with ClusterDO

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

the class DatanodesControlScreen method handleRequestInternal.

/*
     * ��ÿ��cobar���ݽڵ��б���������ȡ��ÿ���б�Ķ�Ӧ�ڵ�ֵ���бȽ�
     */
@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;
    List<Map<String, Object>> dataList = null;
    //if some cobar has datanodeList which is not null
    boolean hasDatanode = false;
    //all cobar has same datanodeList
    boolean isUniform = true;
    boolean connectionFlag = true;
    if (null != cList && cList.size() > 0) {
        if (-1 == clusterId) {
            clusterId = cList.get(0).getId();
            cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId, ConstantDefine.ACTIVE);
        } else {
            cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId, ConstantDefine.ACTIVE);
        }
    }
    if (null != cobarList && cobarList.size() > 0) {
        List<String> cobarNameList = new ArrayList<String>();
        Map<String, List<DataNodesStatus>> datanodeListMap = new HashMap<String, List<DataNodesStatus>>();
        //datanodeList length
        int listLength = -1;
        int maxListLength = -1;
        /* ��ȡÿ��cobar�����ݽڵ��б�������Map<cobarName,List>�� */
        for (CobarDO cobar : cobarList) {
            CobarAdapterDAO control = cobarAccesser.getAccesser(cobar.getId());
            if (!control.checkConnection()) {
                connectionFlag = false;
                break;
            }
            List<DataNodesStatus> dList = control.listDataNodes();
            if (null != dList) {
                hasDatanode = true;
                ListSortUtil.sortDataNodesByPoolName(dList);
                // first time to init listLength
                if (listLength < 0) {
                    listLength = dList.size();
                }
                if (listLength != dList.size()) {
                    isUniform = false;
                }
                maxListLength = (maxListLength < dList.size()) ? dList.size() : maxListLength;
            } else {
                if (listLength < 0) {
                    listLength = 0;
                } else if (listLength > 0) {
                    isUniform = false;
                }
            }
            cobarNameList.add(cobar.getName());
            datanodeListMap.put(cobar.getName(), dList);
        }
        /* all cobar has same datanodeList */
        if (connectionFlag && hasDatanode && isUniform) {
            dataList = new ArrayList<Map<String, Object>>();
            for (int i = 0; isUniform && i < listLength; i++) {
                DataNodesStatus dnode = datanodeListMap.get(cobarNameList.get(0)).get(i);
                int indexlength = dnode.getDataSource().split(",").length;
                long recoveryTime = dnode.getRecoveryTime();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("poolName", dnode.getPoolName());
                map.put("dataSource", dnode.getDataSource());
                boolean statusFlag = true;
                StringBuilder sb = new StringBuilder();
                StringBuilder time = new StringBuilder();
                for (String name : cobarNameList) {
                    DataNodesStatus tmp = datanodeListMap.get(name).get(i);
                    if (0 != ListSortUtil.comparePoolName(tmp.getPoolName(), dnode.getPoolName())) {
                        isUniform = false;
                        break;
                    }
                    sb.append(CobarStringUtil.htmlEscapedString(name)).append(":").append(tmp.getIndex()).append(";");
                    time.append(CobarStringUtil.htmlEscapedString(name)).append(":").append(tmp.getRecoveryTime()).append(";");
                    if (tmp.getIndex() != dnode.getIndex()) {
                        statusFlag = false;
                    }
                    recoveryTime = (recoveryTime < tmp.getRecoveryTime()) ? tmp.getRecoveryTime() : recoveryTime;
                }
                if (recoveryTime == -1) {
                    map.put("recoveryTime", recoveryTime);
                } else {
                    map.put("recoveryTime", FormatUtil.formatTime(recoveryTime * 1000, 2));
                }
                /* index = -1 ˵���ڵ㲻һ�£�����Ϊ��Ӧ�ڵ�index�� */
                if (statusFlag) {
                    map.put("index", dnode.getIndex());
                } else {
                    map.put("index", -1);
                }
                map.put("status", sb.toString());
                map.put("indexlength", indexlength);
                map.put("time", time);
                dataList.add(map);
            }
            ListSortUtil.sortDataNodesMapByPoolName(dataList);
        }
        /*
             * all datanodeList is not the same O(3MN)
             */
        if (connectionFlag && !isUniform) {
            dataList = new ArrayList<Map<String, Object>>();
            // should choose for whatever reason
            boolean choose = false;
            // if all cobar has the same pool
            boolean allsame = true;
            // if there is null dataList
            boolean nullList = false;
            for (; ; ) {
                // all list has finish
                if (cobarNameList.size() <= 0) {
                    break;
                }
                // clear the null dataList
                nullList = false;
                for (String name : cobarNameList) {
                    List<DataNodesStatus> dList = datanodeListMap.get(name);
                    if (null == dList || dList.size() == 0) {
                        // then ever datanode should be choosen
                        choose = true;
                        // one dataList is null
                        nullList = true;
                        //remove
                        datanodeListMap.remove(name);
                        //remove
                        cobarNameList.remove(name);
                        break;
                    }
                }
                if (nullList) {
                    continue;
                }
                // get the mini datanode
                DataNodesStatus dnode = null;
                allsame = true;
                for (String name : cobarNameList) {
                    DataNodesStatus tmp = datanodeListMap.get(name).get(0);
                    if (null == dnode) {
                        dnode = tmp;
                    } else if (ListSortUtil.comparePoolName(dnode.getPoolName(), tmp.getPoolName()) < 0) {
                        // dnode.poolname < tmp.poolname
                        allsame = false;
                    } else if (ListSortUtil.comparePoolName(dnode.getPoolName(), tmp.getPoolName()) > 0) {
                        // dnode.poolname > tmp.poolname
                        dnode = tmp;
                        allsame = false;
                    }
                }
                //remove allsame datanode
                if (!choose && allsame) {
                    for (String name : cobarNameList) {
                        datanodeListMap.get(name).remove(0);
                    }
                    continue;
                }
                Map<String, Object> map = new HashMap<String, Object>();
                StringBuilder sb = new StringBuilder();
                StringBuilder time = new StringBuilder();
                // if every datanode index is the same
                boolean statusFlag = true;
                long recoveryTime = dnode.getRecoveryTime();
                for (String name : cobarNameList) {
                    List<DataNodesStatus> dList = datanodeListMap.get(name);
                    // get the first datanode
                    DataNodesStatus tmp = dList.get(0);
                    if (ListSortUtil.comparePoolName(dnode.getPoolName(), tmp.getPoolName()) < 0) {
                        continue;
                    } else {
                        sb.append(CobarStringUtil.htmlEscapedString(name)).append(":").append(tmp.getIndex()).append(";");
                        time.append(CobarStringUtil.htmlEscapedString(name)).append(":").append(tmp.getRecoveryTime()).append(";");
                        if (tmp.getIndex() != dnode.getIndex()) {
                            statusFlag = false;
                        }
                        recoveryTime = (recoveryTime < tmp.getRecoveryTime()) ? tmp.getRecoveryTime() : recoveryTime;
                        dList.remove(0);
                    }
                }
                if (dnode != null && (choose || !allsame)) {
                    map.put("poolName", dnode.getPoolName());
                    map.put("dataSource", dnode.getDataSource());
                    map.put("recoveryTime", recoveryTime);
                    if (statusFlag) {
                        map.put("index", dnode.getIndex());
                    } else {
                        map.put("index", -1);
                    }
                    map.put("status", sb.toString());
                    map.put("indexlength", -1);
                    map.put("time", time);
                    dataList.add(map);
                }
            }
            ListSortUtil.sortDataNodesMapByPoolName(dataList);
        }
    }
    return new ModelAndView("c_datanodes", new FluenceHashMap<String, Object>().putKeyValue("cList", clusterList).putKeyValue("clusterId", clusterId).putKeyValue("user", user).putKeyValue("datanodes", dataList).putKeyValue("uniform", isUniform).putKeyValue("connecitonFlag", connectionFlag));
}
Also used : HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) ArrayList(java.util.ArrayList) List(java.util.List) CobarAdapterDAO(com.alibaba.cobar.manager.dao.CobarAdapterDAO) DataNodesStatus(com.alibaba.cobar.manager.dataobject.cobarnode.DataNodesStatus) UserDO(com.alibaba.cobar.manager.dataobject.xml.UserDO) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) Map(java.util.Map)

Example 22 with ClusterDO

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

the class EditClusterPage method handleRequestInternal.

@SuppressWarnings("unchecked")
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    long clusterId = 0;
    try {
        clusterId = Long.parseLong(request.getParameter("clusterId").trim());
    } catch (Exception e) {
        throw new IllegalArgumentException("parameter 'clusterId' is invalid:" + request.getParameter("clusterId"));
    }
    ClusterDO cluster = xmlAccesser.getClusterDAO().getClusterById(clusterId);
    Map<String, Object> clusterMap = new PropertyUtilsBean().describe(cluster);
    clusterMap.remove("class");
    clusterMap.remove("name");
    clusterMap.remove("deployDesc");
    clusterMap.put("name", CobarStringUtil.htmlEscapedString(cluster.getName()));
    clusterMap.put("deployDesc", CobarStringUtil.htmlEscapedString(cluster.getDeployDesc()));
    return new ModelAndView("m_editCluster", new FluenceHashMap<String, Object>().putKeyValue("cluster", clusterMap));
}
Also used : ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) ModelAndView(org.springframework.web.servlet.ModelAndView) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap)

Example 23 with ClusterDO

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

the class MCobarListScreen method handleRequestInternal.

@SuppressWarnings({ "unchecked" })
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    UserDO user = (UserDO) request.getSession().getAttribute("user");
    long clusterId = Long.parseLong(request.getParameter("clusterId"));
    ClusterDO cluster = xmlAccesser.getClusterDAO().getClusterById(clusterId);
    List<CobarDO> cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId);
    List<Map<String, Object>> cobarViewList = null;
    if (null != cobarList) {
        ListSortUtil.sortCobarByName(cobarList);
        cobarViewList = new ArrayList<Map<String, Object>>();
        PropertyUtilsBean util = new PropertyUtilsBean();
        for (CobarDO c : cobarList) {
            Map<String, Object> map = util.describe(c);
            map.remove("class");
            map.remove("name");
            map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
            cobarViewList.add(map);
        }
    }
    Map<String, Object> clusterView = new HashMap<String, Object>();
    clusterView.put("id", cluster.getId());
    clusterView.put("name", CobarStringUtil.htmlEscapedString(cluster.getName()));
    return new ModelAndView("m_cobarList", new FluenceHashMap<String, Object>().putKeyValue("cobarList", cobarViewList).putKeyValue("user", user).putKeyValue("cluster", clusterView));
}
Also used : PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) UserDO(com.alibaba.cobar.manager.dataobject.xml.UserDO) ModelAndView(org.springframework.web.servlet.ModelAndView) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) Map(java.util.Map)

Example 24 with ClusterDO

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

the class ClusterDAOImple method write.

@SuppressWarnings("rawtypes")
private boolean write() {
    FileOutputStream os = null;
    lock.lock();
    try {
        if (!backup(xmlPath)) {
            logger.error("cluster backup fail!");
        }
        os = new FileOutputStream(xmlPath);
        xsl.setOutput(os, "UTF-8");
        xsl.startDocument("UTF-8", null);
        xsl.text("\n");
        xsl.startTag(null, "clusters");
        xsl.text("\n");
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            @SuppressWarnings("unchecked") Map.Entry<Long, ClusterDO> entry = (Entry<Long, ClusterDO>) it.next();
            ClusterDO cluster = entry.getValue();
            if (!write(cluster)) {
                throw new IOException("cluster write error!");
            }
        }
        xsl.endTag(null, "clusters");
        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) ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) FileOutputStream(java.io.FileOutputStream) Iterator(java.util.Iterator) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with ClusterDO

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

the class ClusterDAOImple method read.

private ClusterDO read(XmlPullParser xpp) {
    ClusterDO cluster = new ClusterDO();
    cluster.setSortId(0);
    try {
        while (!(xpp.getEventType() == XmlPullParser.END_TAG && "cluster".equals(xpp.getName()))) {
            if (xpp.getEventType() == XmlPullParser.START_TAG && "property".equals(xpp.getName())) {
                int type = typeMap.get(xpp.getAttributeValue(0).trim());
                switch(type) {
                    case ID:
                        cluster.setId(Long.parseLong(xpp.nextText().trim()));
                        break;
                    case SORT_ID:
                        cluster.setSortId(Integer.parseInt(xpp.nextText().trim()));
                        break;
                    case NAME:
                        cluster.setName(xpp.nextText().trim());
                        break;
                    case DEPLOY_CONTACT:
                        cluster.setDeployContact(xpp.nextText().trim());
                        break;
                    case MAINT_CONTACT:
                        cluster.setMaintContact(xpp.nextText().trim());
                        break;
                    case DEPLOY_DESC:
                        cluster.setDeployDesc(xpp.nextText().trim());
                        break;
                    case ONLINE_TIME:
                        cluster.setOnlineTime(xpp.nextText().trim());
                        break;
                    default:
                        break;
                }
            }
            xpp.next();
        }
        return cluster;
    } catch (NumberFormatException e) {
        logger.error(e.getMessage(), e);
    } catch (XmlPullParserException e) {
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
    return null;
}
Also used : ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

ClusterDO (com.alibaba.cobar.manager.dataobject.xml.ClusterDO)30 ModelAndView (org.springframework.web.servlet.ModelAndView)16 HashMap (java.util.HashMap)15 Map (java.util.Map)15 FluenceHashMap (com.alibaba.cobar.manager.util.FluenceHashMap)12 UserDO (com.alibaba.cobar.manager.dataobject.xml.UserDO)11 ArrayList (java.util.ArrayList)11 CobarDO (com.alibaba.cobar.manager.dataobject.xml.CobarDO)9 PropertyUtilsBean (org.apache.commons.beanutils.PropertyUtilsBean)7 Test (org.junit.Test)7 CobarAdapterDAO (com.alibaba.cobar.manager.dao.CobarAdapterDAO)5 IOException (java.io.IOException)5 Iterator (java.util.Iterator)3 Entry (java.util.Map.Entry)3 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 ClusterDAOImple (com.alibaba.cobar.manager.dao.xml.ClusterDAOImple)1 ConnectionStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ConnectionStatus)1 DataNodesStatus (com.alibaba.cobar.manager.dataobject.cobarnode.DataNodesStatus)1 ServerStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ServerStatus)1 FileInputStream (java.io.FileInputStream)1