Example #1
0
func statsHandler(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
	switch req.Method {
	case "GET":
		globalCache.mu.Lock()
		endpoints := globalCache.endpoints
		globalCache.mu.Unlock()

		endpointToStats, nameToEndpoint, err := etcdproc.GetStats(endpoints...)
		if err != nil {
			return err
		}

		names := []string{}
		for k := range nameToEndpoint {
			names = append(names, k)
		}
		sort.Strings(names)
		copied := make([]string, 5)
		copy(copied, names)
		names = copied

		name1, endpoint1 := names[0], nameToEndpoint[names[0]]
		name2, endpoint2 := names[1], nameToEndpoint[names[1]]
		name3, endpoint3 := names[2], nameToEndpoint[names[2]]
		name4, endpoint4 := names[3], nameToEndpoint[names[3]]
		name5, endpoint5 := names[4], nameToEndpoint[names[4]]
		etcd1ID := ""
		etcd1State := ""
		etcd1StartTime := ""
		etcd1LeaderUptime := ""
		etcd1RecvAppendRequestCnt := ""
		etcd1RecvingBandwidthRate := ""
		etcd1SendAppendRequestCnt := ""
		etcd1SendingBandwidthRate := ""
		if v, ok := endpointToStats[endpoint1]; ok {
			etcd1ID = v.ID
			etcd1State = v.State
			etcd1StartTime = v.StartTime.String()[:19]
			etcd1LeaderUptime = v.LeaderInfo.Uptime
			etcd1RecvAppendRequestCnt = fmt.Sprintf("%d", v.RecvAppendRequestCnt)
			etcd1RecvingBandwidthRate = fmt.Sprintf("%f", v.RecvingBandwidthRate)
			etcd1SendAppendRequestCnt = fmt.Sprintf("%d", v.SendAppendRequestCnt)
			etcd1SendingBandwidthRate = fmt.Sprintf("%f", v.SendingBandwidthRate)
		}
		etcd2ID := ""
		etcd2State := ""
		etcd2StartTime := ""
		etcd2LeaderUptime := ""
		etcd2RecvAppendRequestCnt := ""
		etcd2RecvingBandwidthRate := ""
		etcd2SendAppendRequestCnt := ""
		etcd2SendingBandwidthRate := ""
		if v, ok := endpointToStats[endpoint2]; ok {
			etcd2ID = v.ID
			etcd2State = v.State
			etcd2StartTime = v.StartTime.String()[:19]
			etcd2LeaderUptime = v.LeaderInfo.Uptime
			etcd2RecvAppendRequestCnt = fmt.Sprintf("%d", v.RecvAppendRequestCnt)
			etcd2RecvingBandwidthRate = fmt.Sprintf("%f", v.RecvingBandwidthRate)
			etcd2SendAppendRequestCnt = fmt.Sprintf("%d", v.SendAppendRequestCnt)
			etcd2SendingBandwidthRate = fmt.Sprintf("%f", v.SendingBandwidthRate)
		}
		etcd3ID := ""
		etcd3State := ""
		etcd3StartTime := ""
		etcd3LeaderUptime := ""
		etcd3RecvAppendRequestCnt := ""
		etcd3RecvingBandwidthRate := ""
		etcd3SendAppendRequestCnt := ""
		etcd3SendingBandwidthRate := ""
		if v, ok := endpointToStats[endpoint3]; ok {
			etcd3ID = v.ID
			etcd3State = v.State
			etcd3StartTime = v.StartTime.String()[:19]
			etcd3LeaderUptime = v.LeaderInfo.Uptime
			etcd3RecvAppendRequestCnt = fmt.Sprintf("%d", v.RecvAppendRequestCnt)
			etcd3RecvingBandwidthRate = fmt.Sprintf("%f", v.RecvingBandwidthRate)
			etcd3SendAppendRequestCnt = fmt.Sprintf("%d", v.SendAppendRequestCnt)
			etcd3SendingBandwidthRate = fmt.Sprintf("%f", v.SendingBandwidthRate)

		}
		etcd4ID := ""
		etcd4State := ""
		etcd4StartTime := ""
		etcd4LeaderUptime := ""
		etcd4RecvAppendRequestCnt := ""
		etcd4RecvingBandwidthRate := ""
		etcd4SendAppendRequestCnt := ""
		etcd4SendingBandwidthRate := ""
		if v, ok := endpointToStats[endpoint4]; ok {
			etcd4ID = v.ID
			etcd4State = v.State
			etcd4StartTime = v.StartTime.String()[:19]
			etcd4LeaderUptime = v.LeaderInfo.Uptime
			etcd4RecvAppendRequestCnt = fmt.Sprintf("%d", v.RecvAppendRequestCnt)
			etcd4RecvingBandwidthRate = fmt.Sprintf("%f", v.RecvingBandwidthRate)
			etcd4SendAppendRequestCnt = fmt.Sprintf("%d", v.SendAppendRequestCnt)
			etcd4SendingBandwidthRate = fmt.Sprintf("%f", v.SendingBandwidthRate)
		}
		etcd5ID := ""
		etcd5State := ""
		etcd5StartTime := ""
		etcd5LeaderUptime := ""
		etcd5RecvAppendRequestCnt := ""
		etcd5RecvingBandwidthRate := ""
		etcd5SendAppendRequestCnt := ""
		etcd5SendingBandwidthRate := ""
		if v, ok := endpointToStats[endpoint5]; ok {
			etcd5ID = v.ID
			etcd5State = v.State
			etcd5StartTime = v.StartTime.String()[:19]
			etcd5LeaderUptime = v.LeaderInfo.Uptime
			etcd5RecvAppendRequestCnt = fmt.Sprintf("%d", v.RecvAppendRequestCnt)
			etcd5RecvingBandwidthRate = fmt.Sprintf("%f", v.RecvingBandwidthRate)
			etcd5SendAppendRequestCnt = fmt.Sprintf("%d", v.SendAppendRequestCnt)
			etcd5SendingBandwidthRate = fmt.Sprintf("%f", v.SendingBandwidthRate)
		}
		resp := struct {
			Etcd1Name                 string
			Etcd1Endpoint             string
			Etcd1ID                   string
			Etcd1State                string
			Etcd1StartTime            string
			Etcd1LeaderUptime         string
			Etcd1RecvAppendRequestCnt string
			Etcd1RecvingBandwidthRate string
			Etcd1SendAppendRequestCnt string
			Etcd1SendingBandwidthRate string

			Etcd2Name                 string
			Etcd2Endpoint             string
			Etcd2ID                   string
			Etcd2State                string
			Etcd2StartTime            string
			Etcd2LeaderUptime         string
			Etcd2RecvAppendRequestCnt string
			Etcd2RecvingBandwidthRate string
			Etcd2SendAppendRequestCnt string
			Etcd2SendingBandwidthRate string

			Etcd3Name                 string
			Etcd3Endpoint             string
			Etcd3ID                   string
			Etcd3State                string
			Etcd3StartTime            string
			Etcd3LeaderUptime         string
			Etcd3RecvAppendRequestCnt string
			Etcd3RecvingBandwidthRate string
			Etcd3SendAppendRequestCnt string
			Etcd3SendingBandwidthRate string

			Etcd4Name                 string
			Etcd4Endpoint             string
			Etcd4ID                   string
			Etcd4State                string
			Etcd4StartTime            string
			Etcd4LeaderUptime         string
			Etcd4RecvAppendRequestCnt string
			Etcd4RecvingBandwidthRate string
			Etcd4SendAppendRequestCnt string
			Etcd4SendingBandwidthRate string

			Etcd5Name                 string
			Etcd5Endpoint             string
			Etcd5ID                   string
			Etcd5State                string
			Etcd5StartTime            string
			Etcd5LeaderUptime         string
			Etcd5RecvAppendRequestCnt string
			Etcd5RecvingBandwidthRate string
			Etcd5SendAppendRequestCnt string
			Etcd5SendingBandwidthRate string
		}{
			name1,
			endpoint1,
			etcd1ID,
			etcd1State,
			etcd1StartTime,
			etcd1LeaderUptime,
			etcd1RecvAppendRequestCnt,
			etcd1RecvingBandwidthRate,
			etcd1SendAppendRequestCnt,
			etcd1SendingBandwidthRate,

			name2,
			endpoint2,
			etcd2ID,
			etcd2State,
			etcd2StartTime,
			etcd2LeaderUptime,
			etcd2RecvAppendRequestCnt,
			etcd2RecvingBandwidthRate,
			etcd2SendAppendRequestCnt,
			etcd2SendingBandwidthRate,

			name3,
			endpoint3,
			etcd3ID,
			etcd3State,
			etcd3StartTime,
			etcd3LeaderUptime,
			etcd3RecvAppendRequestCnt,
			etcd3RecvingBandwidthRate,
			etcd3SendAppendRequestCnt,
			etcd3SendingBandwidthRate,

			name4,
			endpoint4,
			etcd4ID,
			etcd4State,
			etcd4StartTime,
			etcd4LeaderUptime,
			etcd4RecvAppendRequestCnt,
			etcd4RecvingBandwidthRate,
			etcd4SendAppendRequestCnt,
			etcd4SendingBandwidthRate,

			name5,
			endpoint5,
			etcd5ID,
			etcd5State,
			etcd5StartTime,
			etcd5LeaderUptime,
			etcd5RecvAppendRequestCnt,
			etcd5RecvingBandwidthRate,
			etcd5SendAppendRequestCnt,
			etcd5SendingBandwidthRate,
		}
		if err := json.NewEncoder(w).Encode(resp); err != nil {
			return err
		}

	default:
		http.Error(w, "Method Not Allowed", 405)
	}

	return nil
}
Example #2
0
func CommandFunc(cmd *cobra.Command, args []string) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Fprintln(os.Stdout, "[demo.CommandFunc - panic]", err)
			os.Exit(0)
		}
	}()

	fs := make([]*etcdproc.Flags, cmdFlag.ClusterSize)
	for i := range fs {
		df, err := etcdproc.NewFlags(fmt.Sprintf("etcd%d", i+1), globalPorts, 11+i, "etcd-cluster-token", "new", uuid.NewV4().String(), cmdFlag.IsClientTLS, cmdFlag.IsPeerTLS, cmdFlag.CertPath, cmdFlag.PrivateKeyPath, cmdFlag.CAPath)
		if err != nil {
			fmt.Fprintln(os.Stdout, "exiting with:", err)
			return
		}
		fs[i] = df
	}

	c, err := etcdproc.CreateCluster(os.Stdout, nil, etcdproc.ToTerminal, cmdFlag.EtcdBinary, fs...)
	if err != nil {
		fmt.Fprintln(os.Stdout, "exiting with:", err)
		return
	}

	if cmdFlag.ProcSave {
		f, err := openToOverwrite(cmdFlag.ProcPath)
		if err != nil {
			fmt.Fprintln(os.Stdout, "exiting with:", err)
			return
		}
		c.WriteProc(f)
		f.Close()
	}

	// this does not run with the program exits with os.Exit(0)
	defer c.RemoveAllDataDirs()

	fmt.Fprintf(os.Stdout, "\n")
	fmt.Fprintln(os.Stdout, "####### Starting all of those 3 nodes in default cluster group")
	clusterDone := make(chan struct{})
	go func() {
		defer func() {
			clusterDone <- struct{}{}
		}()
		if err := c.StartAll(); err != nil {
			fmt.Fprintln(os.Stdout, "exiting with:", err)
			return
		}
	}()

	operationDone := make(chan struct{})

	if cmdFlag.IsSimpleSimulation {

		go func() {
			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### Stress")
			if err := c.Stress(cmdFlag.ConnectionNumber, cmdFlag.ClientNumber, cmdFlag.StressNumber, 15, 15); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}

			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### SimpleStress")
			if err := c.SimpleStress(); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}
		}()

	} else if !cmdFlag.IsSimple {

		go func() {
			defer func() {
				operationDone <- struct{}{}
			}()

			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### Terminate")
			if err := c.Terminate(nameToTerminate); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}

			// Stress here to trigger log compaction
			// (make terminated node fall behind)

			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### Restart")
			if err := c.Restart(nameToTerminate); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}

			key, val := []byte("sample_key"), []byte("sample_value")
			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### Put")
			if err := c.Put(key, val); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}

			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### Range")
			if err := c.Range(key); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}

			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### Stress")
			if err := c.Stress(cmdFlag.ConnectionNumber, cmdFlag.ClientNumber, cmdFlag.StressNumber, 15, 15); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}

			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### SimpleStress")
			if err := c.SimpleStress(); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}

			time.Sleep(cmdFlag.Pause)
			fmt.Fprintf(os.Stdout, "\n")
			fmt.Fprintln(os.Stdout, "####### WatchAndPut")
			if err := c.WatchAndPut(cmdFlag.ConnectionNumber, cmdFlag.ClientNumber, cmdFlag.StressNumber); err != nil {
				fmt.Fprintln(os.Stdout, "exiting with:", err)
				return
			}

			if !cmdFlag.IsClientTLS { // TODO: not working for now
				time.Sleep(cmdFlag.Pause)
				fmt.Fprintf(os.Stdout, "\n")
				fmt.Fprintln(os.Stdout, "####### GetStats #1")
				vm, ne, err := c.GetStats()
				if err != nil {
					fmt.Fprintln(os.Stdout, "exiting with:", err)
					return
				} else {
					fmt.Fprintf(os.Stdout, "Endpoint To Stats: %+v\n", vm)
					fmt.Fprintf(os.Stdout, "Name To Endpoint : %+v\n", ne)

					fmt.Fprintf(os.Stdout, "\n")
					fmt.Fprintln(os.Stdout, "####### GetStats #2")
					endpoints := []string{}
					for _, endpoint := range ne {
						endpoints = append(endpoints, endpoint)
					}
					sort.Strings(endpoints)
					vm2, ne2, err := etcdproc.GetStats(endpoints...)
					if err != nil {
						fmt.Fprintln(os.Stdout, "exiting with:", err)
						return
					}
					fmt.Fprintf(os.Stdout, "Endpoint To Stats: %+v\n", vm2)
					fmt.Fprintf(os.Stdout, "Name To Endpoint : %+v\n", ne2)
				}

				time.Sleep(cmdFlag.Pause)
				fmt.Fprintf(os.Stdout, "\n")
				fmt.Fprintln(os.Stdout, "####### GetMetrics #1")
				{
					vm, ne, err := c.GetMetrics()
					if err != nil {
						fmt.Fprintln(os.Stdout, "exiting with:", err)
						return
					} else {
						for n, mm := range vm {
							var fb uint64
							if fv, ok := mm["etcd_storage_db_total_size_in_bytes"]; ok {
								fb = uint64(fv)
							}
							fmt.Fprintf(os.Stdout, "%s: etcd_storage_keys_total             = %f\n", n, mm["etcd_storage_keys_total"])
							fmt.Fprintf(os.Stdout, "%s: etcd_storage_db_total_size_in_bytes = %s\n", n, humanize.Bytes(fb))
						}
						fmt.Fprintf(os.Stdout, "Name To Endpoint: %+v\n", ne)

						fmt.Fprintf(os.Stdout, "\n")
						fmt.Fprintln(os.Stdout, "####### GetMetrics #2")
						endpoints := []string{}
						for _, endpoint := range ne {
							endpoints = append(endpoints, endpoint)
						}
						sort.Strings(endpoints)
						vm2, ne2, err := etcdproc.GetMetrics(endpoints...)
						if err != nil {
							fmt.Fprintln(os.Stdout, "exiting with:", err)
							return
						}
						for n, mm := range vm2 {
							var fb uint64
							if fv, ok := mm["etcd_storage_db_total_size_in_bytes"]; ok {
								fb = uint64(fv)
							}
							fmt.Fprintf(os.Stdout, "%s: etcd_storage_keys_total             = %f\n", n, mm["etcd_storage_keys_total"])
							fmt.Fprintf(os.Stdout, "%s: etcd_storage_db_total_size_in_bytes = %s\n", n, humanize.Bytes(fb))
						}
						fmt.Fprintf(os.Stdout, "Name To Endpoint : %+v\n", ne2)
					}
				}

				fmt.Println()
			}
		}()

	}

	select {
	case <-clusterDone:
		fmt.Fprintln(os.Stdout, "[demo.CommandFunc END] etcd cluster terminated!")
		return
	case <-operationDone:
		fmt.Fprintln(os.Stdout, "[demo.CommandFunc END] operation terminated!")
		return
	case <-time.After(cmdFlag.Timeout):
		fmt.Fprintln(os.Stdout, "[demo.CommandFunc END] timed out!")
		return
	}
}