Exemplo n.º 1
0
func main() {

	good := 0
	bad := 0

	// open https
	// etcd.SetScheme(etcd.HTTPS)

	// add client cert
	// success, err :=etcd.SetCertAndKey("key/me_cert.pem", "key/me_key.pem")

	// if !success {
	// 	fmt.Println(err)
	// }

	etcd.SyncCluster()
	c := make(chan bool, 10)
	// set up a lock
	etcd.Set("lock", "unlock", 0)
	for i := 0; i < 10; i++ {
		go t(i, c)
	}

	for i := 0; i < 10; i++ {
		if <-c {
			good++
		} else {
			bad++
		}
	}
	fmt.Println("good: ", good, "bad: ", bad)
}
Exemplo n.º 2
0
func NewClient(key string) *Client {
	etcd := etcd.NewClient([]string{etcdMachine})

	etcd.SyncCluster()

	return &Client{Key: key, etcd: etcd}
}
Exemplo n.º 3
0
// This test creates a single node and then set a value to it.
// Then this test kills the node and restart it and tries to get the value again.
func TestSingleNodeRecovery(t *testing.T) {
	procAttr := new(os.ProcAttr)
	procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
	args := []string{"etcd", "-d=/tmp/node1"}

	process, err := os.StartProcess("etcd", append(args, "-i"), procAttr)
	if err != nil {
		t.Fatal("start process failed:" + err.Error())
		return
	}

	time.Sleep(time.Second)

	etcd.SyncCluster()
	// Test Set
	result, err := etcd.Set("foo", "bar", 100)

	if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL != 99 {
		if err != nil {
			t.Fatal(err)
		}

		t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL)
	}

	time.Sleep(time.Second)

	process.Kill()

	process, err = os.StartProcess("etcd", args, procAttr)
	defer process.Kill()
	if err != nil {
		t.Fatal("start process failed:" + err.Error())
		return
	}

	time.Sleep(time.Second)

	results, err := etcd.Get("foo")
	if err != nil {
		t.Fatal("get fail: " + err.Error())
		return
	}

	result = results[0]

	if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL > 99 {
		if err != nil {
			t.Fatal(err)
		}
		t.Fatalf("Recovery Get failed with %s %s %v", result.Key, result.Value, result.TTL)
	}
}
Exemplo n.º 4
0
func main() {
	etcd.SyncCluster()
	c := make(chan bool, 10)
	// set up a lock
	for i := 0; i < 200; i++ {
		go t(i, c)
	}
	start := time.Now()
	for i := 0; i < 200; i++ {
		<-c
	}
	fmt.Println(time.Now().Sub(start), ": ", 200*10000, "commands")
}
Exemplo n.º 5
0
// Create a five nodes
// Randomly kill one of the node and keep on sending set command to the cluster
func TestMultiNodeRecovery(t *testing.T) {
	procAttr := new(os.ProcAttr)
	procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}

	clusterSize := 5
	argGroup, etcds, err := createCluster(clusterSize, procAttr)

	if err != nil {
		t.Fatal("cannot create cluster")
	}

	defer destroyCluster(etcds)

	time.Sleep(2 * time.Second)

	etcd.SyncCluster()

	stop := make(chan bool)
	// Test Set
	go set(t, stop)

	for i := 0; i < 10; i++ {
		num := rand.Int() % clusterSize
		fmt.Println("kill node", num+1)

		// kill
		etcds[num].Kill()
		etcds[num].Release()
		time.Sleep(time.Second)

		// restart
		etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
		if err != nil {
			panic(err)
		}
		time.Sleep(time.Second)
	}

	stop <- true
	<-stop
}
Exemplo n.º 6
0
// Create a three nodes and try to set value
func TestSimpleMultiNode(t *testing.T) {
	procAttr := new(os.ProcAttr)
	procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}

	clusterSize := 3

	_, etcds, err := createCluster(clusterSize, procAttr)

	if err != nil {
		t.Fatal("cannot create cluster")
	}

	defer destroyCluster(etcds)

	time.Sleep(time.Second)

	etcd.SyncCluster()

	// Test Set
	result, err := etcd.Set("foo", "bar", 100)

	if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL != 99 {
		if err != nil {
			t.Fatal(err)
		}

		t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL)
	}

	time.Sleep(time.Second)

	result, err = etcd.Set("foo", "bar", 100)

	if err != nil || result.Key != "/foo" || result.Value != "bar" || result.PrevValue != "bar" || result.TTL != 99 {
		if err != nil {
			t.Fatal(err)
		}
		t.Fatalf("Set 2 failed with %s %s %v", result.Key, result.Value, result.TTL)
	}

}
Exemplo n.º 7
0
func main() {

	_, err := flags.ParseArgs(&Options, os.Args[1:])
	if err != nil {
		os.Exit(1)
	}

	if Options.Debug {
		log_debug = true
		debugMsg("Debug mode enabled")
	}

	// Create an ETCD client
	etcd := etcd.NewClient(Options.EtcdHosts)
	if !etcd.SyncCluster() {
		logger.Printf("[WARNING] Failed to connect to etcd cluster at launch time")
	}

	// Register the metrics writer
	if len(Options.GraphiteServer) > 0 {
		addr, err := net.ResolveTCPAddr("tcp", Options.GraphiteServer)
		if err != nil {
			logger.Fatalf("Failed to parse graphite server: ", err)
		}

		prefix := "discodns"
		hostname, err := os.Hostname()
		if err != nil {
			logger.Fatalf("Unable to get hostname: ", err)
		}

		prefix = prefix + "." + strings.Replace(hostname, ".", "_", -1)

		go metrics.Graphite(metrics.DefaultRegistry, time.Duration(Options.GraphiteDuration)*time.Second, prefix, addr)
	} else if Options.MetricsDuration > 0 {
		go metrics.Log(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration)*time.Second, logger)

		// Register a bunch of debug metrics
		metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
		metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
		go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration))
		go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration))
	} else {
		logger.Printf("Metric logging disabled")
	}

	// Start up the DNS resolver server
	server := &Server{
		addr:       Options.ListenAddress,
		port:       Options.ListenPort,
		etcd:       etcd,
		rTimeout:   time.Duration(5) * time.Second,
		wTimeout:   time.Duration(5) * time.Second,
		defaultTtl: Options.DefaultTtl,
		queryFilterer: &QueryFilterer{acceptFilters: parseFilters(Options.Accept),
			rejectFilters: parseFilters(Options.Reject)}}

	server.Run()

	logger.Printf("Listening on %s:%d\n", Options.ListenAddress, Options.ListenPort)

	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt)

forever:
	for {
		select {
		case <-sig:
			logger.Printf("Bye bye :(\n")
			break forever
		}
	}
}