Exemplo n.º 1
0
Arquivo: main.go Projeto: mdmarek/grid
func main() {
	g := grid.New("firstgrid", []string{"http://localhost:2379"}, []string{"nats://localhost:4222"}, &maker{})

	// Start the grid.
	exit, err := g.Start()
	if err != nil {
		log.Fatalf("error: failed to start grid: %v", err)
	}

	// Start the leader actor.
	err = g.StartActor(grid.NewActorDef(Leader))
	if err != nil {
		log.Fatalf("error: failed to start actor: %v", err)
	}
	// Start the follower actor.
	err = g.StartActor(grid.NewActorDef(Follower))
	if err != nil {
		log.Fatalf("error: failed to start actor: %v", err)
	}

	// Wait for a signal from the user to exit, or
	// exit if exit channel is closed by grid.
	go func() {
		sig := make(chan os.Signal, 1)
		signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
		select {
		case <-sig:
			log.Printf("shutting down")
			g.Stop()
		case <-exit:
		}
	}()

	<-exit
	log.Printf("shutdown complete")
}
Exemplo n.º 2
0
Arquivo: ring.go Projeto: mdmarek/grid
func (r *ring) actorDef(i int) *grid.ActorDef {
	a := grid.NewActorDef(r.actorName(i))
	a.DefineType(r.name)
	return a
}
Exemplo n.º 3
0
Arquivo: main.go Projeto: mdmarek/grid
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.Parse()

	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()

	etcdservers := strings.Split(*etcdconnect, ",")
	natsservers := strings.Split(*natsconnect, ",")

	conf := &Conf{
		GridName:    "flowgrid",
		MsgSize:     *msgsize,
		MsgCount:    *msgcount,
		NrProducers: *producers,
		NrConsumers: *consumers,
	}

	hostname, err := os.Hostname()
	if err != nil {
		log.Fatalf("error: failed to discover hostname: %v", err)
	}

	m, err := newActorMaker(conf)
	if err != nil {
		log.Fatalf("error: failed to make actor maker: %v", err)
	}

	g := grid.New(conf.GridName, etcdservers, natsservers, m)

	exit, err := g.Start()
	if err != nil {
		log.Fatalf("error: failed to start grid: %v", err)
	}

	j := condition.NewJoin(g.Etcd(), 30*time.Second, g.Name(), "hosts", hostname)
	err = j.Join()
	if err != nil {
		log.Fatalf("error: failed to regester: %v", err)
	}
	defer j.Exit()
	go func() {
		ticker := time.NewTicker(15 * time.Second)
		defer ticker.Stop()
		for {
			select {
			case <-exit:
				return
			case <-ticker.C:
				err := j.Alive()
				if err != nil {
					log.Fatalf("error: failed to report liveness: %v", err)
				}
			}
		}
	}()

	w := condition.NewCountWatch(g.Etcd(), g.Name(), "hosts")
	defer w.Stop()

	started := w.WatchUntil(*nodes)
	select {
	case <-exit:
		log.Printf("Shutting down, grid exited")
		return
	case <-w.WatchError():
		log.Fatalf("error: failed to watch other hosts join: %v", err)
	case <-started:
	}

	for i := 0; i < *flows; i++ {
		flow := NewFlow(i)

		rp := ring.New(flow.NewContextualName("producer"), conf.NrProducers)
		for _, def := range rp.ActorDefs() {
			def.DefineType("producer")
			def.Define("flow", flow.Name())
			err := g.StartActor(def)
			if err != nil {
				log.Fatalf("error: failed to start: %v, due to: %v", def, err)
			}
		}

		rc := ring.New(flow.NewContextualName("consumer"), conf.NrConsumers)
		for _, def := range rc.ActorDefs() {
			def.DefineType("consumer")
			def.Define("flow", flow.Name())
			err := g.StartActor(def)
			if err != nil {
				log.Fatalf("error: failed to start: %v, due to: %v", def, err)
			}
		}

		def := grid.NewActorDef(flow.NewContextualName("leader"))
		def.DefineType("leader")
		def.Define("flow", flow.Name())
		err = g.StartActor(def)
		if err != nil {
			log.Fatalf("error: failed to start: %v, due to: %v", "leader", err)
		}

		time.Sleep(5 * time.Second)
	}

	go func() {
		sig := make(chan os.Signal, 1)
		signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
		select {
		case <-sig:
			log.Printf("shutting down")
			g.Stop()
		case <-exit:
		}
	}()

	<-exit
	log.Println("shutdown complete")
}