Esempio n. 1
0
func TestServer(t *testing.T) {

	logger, _ := log_resolver.NewLogger("golog")
	if logger == nil {
		t.Fatalf("Invalid logger")
	}

	logging.SetLogger(logger)

	site, err := NewDatastore(TEST_URL)
	if err != nil {
		t.Skipf("SKIPPING TEST: %v", err)
	}

	namespaceNames, err := site.NamespaceNames()
	if err != nil {
		t.Fatalf("Failed to get Namespace names . error %v", err)
	}

	fmt.Printf("Namespaces in this instance %v", namespaceNames)

	namespace, err := site.NamespaceByName("default")
	if err != nil {
		t.Fatalf("Namespace default not found, error %v", err)
	}

	keyspaceNames, err := namespace.KeyspaceNames()
	if err != nil {
		t.Fatalf(" Cannot fetch keyspaces names. error %v", err)
	}

	fmt.Printf("Keyspaces in this namespace %v", keyspaceNames)

	//connect to beer-sample
	ks, err := namespace.KeyspaceByName("beer-sample")
	if err != nil {
		t.Fatalf(" Cannot connect to beer-sample. Error %v", err)
		return
	}

	indexer, err := ks.Indexer(datastore.VIEW)
	if err != nil {
		fmt.Printf("No indexers found")
		return
	}

	// try create a primary index
	index, err := indexer.CreatePrimaryIndex("#primary", nil)
	if err != nil {
		// keep going. maybe index already exists
		fmt.Printf(" Cannot create a primary index on bucket. Error %v", err)
	} else {

		fmt.Printf("primary index created %v", index)
	}

	pair, err := ks.Fetch([]string{"357", "aass_brewery"})
	if err != nil {
		t.Fatalf(" Cannot fetch keys error %v", err)

	}

	fmt.Printf("Keys fetched %v", pair)
	insertKey := datastore.Pair{Key: "testBeerKey", Value: value.NewValue(("This is a random test key-value"))}

	_, err = ks.Insert([]datastore.Pair{insertKey})
	if err != nil {
		t.Fatalf("Cannot insert key %v", insertKey)
	}

	deleted, err := ks.Delete([]string{insertKey.Key})
	if err != nil || (len(deleted) != 1 && deleted[0] != insertKey.Key) {
		t.Fatalf("Failed to delete %v", err)
	}

	pi, err := indexer.PrimaryIndexes()
	if err != nil || len(pi) < 1 {
		fmt.Printf("No primary index found")
		return
	}

	//fmt.Printf(" got primary index %s", pi.name)
	conn := datastore.NewIndexConnection(nil)
	go pi[0].ScanEntries(math.MaxInt64, datastore.UNBOUNDED, nil, conn)

	var entry *datastore.IndexEntry

	ok := true
	for ok {

		select {
		case entry, ok = <-conn.EntryChannel():
			if ok {
				fmt.Printf("\n primary key %v", entry.PrimaryKey)
			}
		}
	}
}
Esempio n. 2
0
func main() {
	flag.Parse()

	var f *os.File
	if *MEM_PROFILE != "" {
		var err error
		f, err = os.Create(*MEM_PROFILE)
		if err != nil {
			fmt.Printf("Cannot start mem profiler %v\n", err)
		}
	}

	if *CPU_PROFILE != "" {
		f, err := os.Create(*CPU_PROFILE)
		if err != nil {
			fmt.Printf("Cannot start cpu profiler %v\n", err)
		} else {
			pprof.StartCPUProfile(f)
		}
	}

	if *LOGGER != "" {
		logger, _ := log_resolver.NewLogger(*LOGGER)
		if logger == nil {
			fmt.Printf("Invalid logger: %s\n", *LOGGER)
			os.Exit(1)
		}

		logging.SetLogger(logger)
	}

	if *DEBUG {
		logging.SetLevel(logging.Debug)
		logging.Debugp("Debug mode enabled")
	} else {
		logging.SetLevel(logging.Info)
	}

	datastore, err := resolver.NewDatastore(*DATASTORE)
	if err != nil {
		logging.Errorp(err.Error())
		os.Exit(1)
	}
	datastore_package.SetDatastore(datastore)

	configstore, err := config_resolver.NewConfigstore(*CONFIGSTORE)
	if err != nil {
		logging.Errorp("Could not connect to configstore",
			logging.Pair{"error", err},
		)
	}
	acctstore, err := acct_resolver.NewAcctstore(*ACCTSTORE)
	if err != nil {
		logging.Errorp("Could not connect to acctstore",
			logging.Pair{"error", err},
		)
	} else {
		// Create the metrics we are interested in
		accounting.RegisterMetrics(acctstore)
		// Make metrics available
		acctstore.MetricReporter().Start(1, 1)
	}

	keep_alive_length, e := util.ParseQuantity(*KEEP_ALIVE_LENGTH)

	if e != nil {
		logging.Errorp("Error parsing keep alive length; reverting to default",
			logging.Pair{"keep alive length", *KEEP_ALIVE_LENGTH},
			logging.Pair{"error", e},
			logging.Pair{"default", server.KEEP_ALIVE_DEFAULT},
		)
	}

	if e == nil && keep_alive_length < 1 {
		logging.Infop("Negative or zero keep alive length; reverting to default",
			logging.Pair{"keep alive length", *KEEP_ALIVE_LENGTH},
			logging.Pair{"default", server.KEEP_ALIVE_DEFAULT},
		)
	}

	channel := make(server.RequestChannel, *REQUEST_CAP)
	server, err := server.NewServer(datastore, configstore, acctstore, *NAMESPACE, *READONLY, channel,
		*THREAD_COUNT, *TIMEOUT, *SIGNATURE, *METRICS, keep_alive_length)
	if err != nil {
		logging.Errorp(err.Error())
		os.Exit(1)
	}

	go server.Serve()

	logging.Infop("cbq-engine started",
		logging.Pair{"version", VERSION},
		logging.Pair{"datastore", *DATASTORE},
	)
	// Create http endpoint
	endpoint := http.NewServiceEndpoint(server, *STATIC_PATH, *METRICS)
	er := endpoint.Listen(*HTTP_ADDR)
	if er != nil {
		logging.Errorp("cbq-engine exiting with error",
			logging.Pair{"error", er},
			logging.Pair{"HTTP_ADDR", *HTTP_ADDR},
		)
		os.Exit(1)
	}
	if *CERT_FILE != "" && *KEY_FILE != "" {
		er := endpoint.ListenTLS(*HTTPS_ADDR, *CERT_FILE, *KEY_FILE)
		if er != nil {
			logging.Errorp("cbq-engine exiting with error",
				logging.Pair{"error", er},
				logging.Pair{"HTTP_ADDR", *HTTP_ADDR},
			)
			os.Exit(1)
		}
	}
	signalCatcher(server, endpoint, *CPU_PROFILE != "", f)
}
Esempio n. 3
0
func init() {
	logger, _ := log_resolver.NewLogger("golog")
	logging.SetLogger(logger)
}