func monitorETCD(etcdAdapter *etcdstoreadapter.ETCDStoreAdapter) { out, err := os.Create(filepath.Join(outDir, "etcdstats.log")) if err != nil { logger.Fatal("etcd.log.creation.failure", err) } cleanup.Register(func() { out.Sync() }) go func() { ticker := time.NewTicker(time.Second) for { <-ticker.C t := time.Now() logger.Info("fetch.etcd.runonce.data") runOnceNodes, err := etcdAdapter.ListRecursively(Bbs.TaskSchemaRoot) if err != nil { logger.Info("fetch.etcd.runOnceNodes.error", err) } executorNode, err := etcdAdapter.ListRecursively(Bbs.ExecutorSchemaRoot) if err != nil { logger.Info("fetch.etcd.executorNode.error", err) } readTime := time.Since(t) d := etcdData{ Time: float64(time.Now().UnixNano()) / 1e9, RunningByExecutor: map[string]int{}, PresentExecutors: len(executorNode.ChildNodes), ReadTime: float64(readTime) / 1e9, } for _, node := range runOnceNodes.ChildNodes { runOnce, err := models.NewTaskFromJSON(node.Value) if err != nil { logger.Error("etcd.decode.runonce", err) continue } switch runOnce.State { case models.TaskStatePending: d.Pending++ case models.TaskStateClaimed: d.Claimed++ case models.TaskStateRunning: d.Running++ d.RunningByExecutor[runOnce.ExecutorID]++ case models.TaskStateCompleted: d.Completed++ } } logger.Info("fetched.etcd.runonce.data", time.Since(t), d.String()) out.Write(d.toJson()) out.Write([]byte("\n")) } }() }
func main() { flag.Parse() runtime.GOMAXPROCS(runtime.NumCPU()) //make the out dir logger.Component = "SIMULATOR" if outDir == "" { logger.Fatal("out.dir.unspecified") } err := os.MkdirAll(outDir, 0777) if err != nil { logger.Fatal("out.dir.creation.failed", err) } //set up logging outputFile, err := os.Create(filepath.Join(outDir, "simulator.log")) if err != nil { logger.Fatal("failed.to.create.simulator.log", err) } logger.Writer = io.MultiWriter(os.Stdout, outputFile) cleanup.Register(func() { outputFile.Sync() }) //start etcd natsClient := yagnats.NewClient() natsMembers := []yagnats.ConnectionProvider{} for _, addr := range strings.Split(*natsAddresses, ",") { natsMembers = append( natsMembers, &yagnats.ConnectionInfo{addr, *natsUsername, *natsPassword}, ) } natsInfo := &yagnats.ConnectionCluster{Members: natsMembers} err = natsClient.Connect(natsInfo) if err != nil { logger.Fatal("could not connect to nats:", err) } logger.Component = "simulator" etcdAdapter := etcdstoreadapter.NewETCDStoreAdapter( strings.Split(*etcdCluster, ","), workerpool.NewWorkerPool(10), ) err = etcdAdapter.Connect() if err != nil { logger.Fatal("etcd.connect-failed", map[string]interface{}{ "error": err.Error(), }) } //write info to the output dir writeInfo() //monitor etcd monitorETCD(etcdAdapter) //run the simulator runSimulation(natsClient) cleanup.Exit(0) }