Package com.taobao.common.stable

Source Code of com.taobao.common.stable.Console

package com.taobao.common.stable;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.taobao.common.stable.command.SetParameter;

/**
* xiaoxie 2010-11-14
*/
public class Console {
    public static String command(String command, String args) {
        if (SphCons.COMMAND_1.equals(command)) {
            Map<String, Sph> ctsphMap = SphDRuntime.getAllSphs();
            Set<Entry<String, Sph>> sets = ctsphMap.entrySet();
            StringBuffer result = new StringBuffer();
            Formatter formatter = new Formatter(result);
            formatter.format("%-15s %-11s %-6s %-8s %-8s %-8s %-9s %-6s %-6s\n",
                    "key", "COUNTVALVE", "COUNT", "PASS", "QPS", "BLOCK", "AVGVALVE", "AVG", "TYPE");
            for (Entry<String, Sph> entry : sets) {
               
                formatter.format("%-15s ", entry.getKey());
                CtSph sph = (CtSph) entry.getValue();
                int[] avgQps = sph.getDetailElapsed().getQpsAndAvg();
                formatter.format("%-11s ", sph.getCountValve());
                formatter.format("%-6s ", sph.getCount());
                formatter.format("%-8s ", sph.getPassCount());
                formatter.format("%-8s ", avgQps[1]);
                formatter.format("%-8s ", sph.getEffectCount());
                formatter.format("%-9s ", sph.getAvgElpasedValve());
                formatter.format("%-6s ", avgQps[0]);
                formatter.format("%-6s ", sph.getValveType().getIntValue());
                formatter.format("\n");
            }
            return result.toString();
        } else if (SphCons.COMMAND_2.equals(command)) {
            // comand="set" args="k.cv=12&k.t=1&k.av=2300"
            List<SetParameter> setParameters = parseSetParameter(args);
            StringBuffer result = new StringBuffer();
            for (SetParameter setParameter : setParameters) {
                CtSph sph = (CtSph) SphDRuntime.get(setParameter.getKey());
                if (sph != null) {
                    if ("cv".equals(setParameter.getValve())) {
                        sph.setDefaultCountValve(setParameter.getValue());
                    } else if ("t".equals(setParameter.getValve())) {
                        sph.setValveType(ValveType.getValveType(setParameter.getValue()));
                    } else if ("av".equals(setParameter.getValve())) {
                        sph.setAvgElapsedValve(setParameter.getValue());
                    } else if ("b".equals(setParameter.getValve())) {
                        sph.setEffectCount((setParameter.getValue()));
                    }
                    result.append(setParameter.getKey() + "." + setParameter.getValve() + "=" + setParameter.getValue());
                    result.append("\n");
                } else {
                    result.append("error command");
                    result.append("\n");
                }
            }
            return result.toString();
        } else if (SphCons.COMMAND_3.equals(command)) {
            // command=rcf args="path=/home/admin/bin/sswitch.conf"
            Map<String, String> parameters = string2Parameters(args);
            String path = parameters.get("path");
            SphDRuntime.init(path);
            return "load conf from " + path;
        } else if (SphCons.COMMAND_4.equals(command)) {
            // command=wcf args="path=/home/admin/bin/sswitch.conf"
            Map<String, String> paremeters = string2Parameters(args);
            String path = paremeters.get("path");
            SphDRuntime.write(path);
            return "write conf to " + path;
        else if (SphCons.COMMAND_12.equals(command)) {
            // �赲ip
            Map<String, String> parameters = string2Parameters(args);
            String bip = parameters.get("bip");
            if (bip != null) {
                String[] bips = bip.split(",");
                SphCons.BIPLIST = new ArrayList<String>();
                for (String ip : bips) {
                    SphCons.BIPLIST.add(ip);
                }
            }
            return bip;
        }
       
        else  {
       
            StringBuffer result = new StringBuffer();
            result.append("show command=show (display stable-switch status)\n");
            result.append("set  command=set&k.cv=10&k.av=200&k.b=0 ...(set stable-switch status)\n");
            result.append("     tip: cv=?(reset count_valve),av=?(reset responsetime_valve),b=?(reset block count)\n");
            result.append("wcf  command=wcf&path=...(write stable-switch status to specified path)\n");
            result.append("     example:command=wcf&path=/home/admin/bin/sw.conf(write stable-switch conf to /home/admin/bin/sw.conf)\n");
            result.append("rcf  command=rcf&path=...(load  stable-switch status from specified path)\n");
            return result.toString();
        }

    }
    private static List<SetParameter> parseSetParameter(String args) {
        List<SetParameter> setParameters = new ArrayList<SetParameter> ();
        if (args != null && args.length() > 0) {
            String[] pairs = args.split("&");
            if (pairs != null && pairs.length > 0) {
                for (String pair : pairs) {
                    String[] para = pair.split("=");
                    if (para != null && para.length == 2) {
                        String prefix = para[0];
                        String suffix = para[1];
                        String[] keyset = prefix.split("\\.");
                        if (keyset != null && keyset.length == 2) {
                            SetParameter sp = new SetParameter(keyset[0],keyset[1],Integer.parseInt(suffix));
                            setParameters.add(sp);
                        }
                    }
                }
            }
        }
        return setParameters;
    }
    private static Map<String, String> string2Parameters(String args) {
        Map<String, String> parameters = new HashMap<String, String> ();
        if (args != null && args.length() > 0) {
            String[] pairs = args.split("&");
            if (pairs != null && pairs.length > 0) {
                for (String pair : pairs) {
                    String[] para = pair.split("=");
                    if (para != null && para.length == 2) {
                        parameters.put(para[0], para[1]);
                    }
                }
            }
        }
        return parameters;
    }
    public static void main(String[] args) {
       
//        System.out.println(command("help",""));
//        List<SetParameter> sps = parseSetParameter("k.ct=1&k.cv=1&k.type=0&k.k.l=3");
//        for (SetParameter sp : sps) {
//            System.out.println("key=" + sp.getKey() + " valve=" + sp.getValve() + " value=" + sp.getValue());
//        }
//        Map<String, String> maps = string2Parameters("k.ct=1&k.cv=1&k.type=0&k.k.l=3");
//        
        CtSph sph1 = new CtSph(10, 200, ValveType.COUNT_VALVE_TYPE);
        CtSph sph2 = new CtSph(11, 210, ValveType.COUNT_VALVE_TYPE);
        CtSph sph3 = new CtSph(12, 220, ValveType.COUNT_VALVE_TYPE);
        CtSph sph4 = new CtSph(13, 230, ValveType.COUNT_VALVE_TYPE);
        SphDRuntime.put("s1", sph1);
        SphDRuntime.put("s2", sph2);
        SphDRuntime.put("s3", sph3);
        SphDRuntime.put("s4sdfsdfsdfsdfsdfsdfsdfee", sph4);
       
        String result = Console.command("show", null);
        System.out.println(result);
    }
}
TOP

Related Classes of com.taobao.common.stable.Console

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.