Ejemplo n.º 1
0
func Acquire(cli *etcd.Client, lock string, timeout uint64) (goChan chan int, stopChan chan int) {
	me := uuid.NewV1().String()
	goChan = make(chan int)
	stopChan = make(chan int)

	go func() {
		log.Println("Hello, I am:", me)

		for {
			resp, acq, err := cli.TestAndSet(lock, "", me, timeout)

			log.Println("Lock Resp:", acq, resp, err)

			if !acq {
				// We want to watch for a change in the lock, and we'll repeat
				var watcherCh = make(chan *store.Response)
				var endCh = make(chan bool)

				go cli.Watch(lock, 0, watcherCh, endCh)
				<-watcherCh

				// Now, we'll try to acquire the lock, again
			} else {

				// We got a lock, we want to keep it
				go func() {
					for {
						resp, acq, err := cli.TestAndSet("/fiddler/watcher", me, me, timeout) // Keep the lock alive
						log.Println("Reset Resp:", acq, resp, err)

						if !acq {
							log.Println("Demoted:", me)
							stopChan <- 1 // Let's boot ourselves, we're no longer the leader
						}

						time.Sleep(time.Duration(timeout*500) * time.Millisecond) // We'll re-up after 50% fo the lock period
					}
				}()
				log.Println("King:", me)
				goChan <- 1
			}
		}
	}()

	return
}
Ejemplo n.º 2
0
/*
  Fiddler knows how to spawn itself into CoreOS
  and start docker containers.  Thus, Fiddler is
  quickly able to coordinate replicating itself
  as load dictates.  It's also able to detect drops
  in load, where it will kill excess servers.
*/

/*
  Fiddler uses a Fidderfile for all configuration settings.
  ./fiddler -c fiddler.conf
*/

var cli = etcd.NewClient()
var myid = uuid.NewV1().String()

func printUsage() {
	fmt.Println("Fiddler is an app to auto-scale containers.")
	fmt.Println("")
	fmt.Println("Usage:")
	fmt.Println("fiddler --config=<conf> install")
	fmt.Println("\tInstalls Fiddler into systemd")
	fmt.Println("fiddler --config=<conf> launch")
	fmt.Println("\tLaunches a new container within Fiddler")
	fmt.Println("fiddler --config=<conf> spawn")
	fmt.Println("\tSpawns a new fiddler environment in the cloud")
	fmt.Println("fiddler --config=<conf> daemon")
	fmt.Println("\tRuns Fiddler like a daemon to monitor machine")
}