Пример #1
0
// createTestNode creates an rpc server using the specified address,
// gossip instance, KV database and a node using the specified slice
// of engines. The server and node are returned. If gossipBS is not
// nil, the gossip bootstrap address is set to gossipBS.
func createTestNode(addr net.Addr, engines []engine.Engine, gossipBS net.Addr, t *testing.T) (
	*rpc.Server, *Node) {
	tlsConfig, err := rpc.LoadTestTLSConfig("..")
	if err != nil {
		t.Fatal(err)
	}

	rpcServer := rpc.NewServer(addr, tlsConfig)
	if err := rpcServer.Start(); err != nil {
		t.Fatal(err)
	}
	g := gossip.New(tlsConfig)
	if gossipBS != nil {
		// Handle possibility of a :0 port specification.
		if gossipBS == addr {
			gossipBS = rpcServer.Addr()
		}
		g.SetBootstrap([]net.Addr{gossipBS})
		g.Start(rpcServer)
	}
	clock := hlc.NewClock(hlc.UnixNano)
	db := kv.NewDB(kv.NewDistKV(g), clock)
	node := NewNode(db, g)
	if err := node.start(rpcServer, clock, engines, proto.Attributes{}); err != nil {
		t.Fatal(err)
	}
	return rpcServer, node
}
Пример #2
0
func newServer() (*server, error) {
	// Determine hostname in case it hasn't been specified in -rpc or -http.
	host, err := os.Hostname()
	if err != nil {
		host = "127.0.0.1"
	}

	// Resolve
	if strings.HasPrefix(*rpcAddr, ":") {
		*rpcAddr = host + *rpcAddr
	}
	_, err = net.ResolveTCPAddr("tcp", *rpcAddr)
	if err != nil {
		return nil, util.Errorf("unable to resolve RPC address %q: %v", *rpcAddr, err)
	}

	var tlsConfig *rpc.TLSConfig
	if *certDir == "" {
		tlsConfig = rpc.LoadInsecureTLSConfig()
	} else {
		var err error
		if tlsConfig, err = rpc.LoadTLSConfig(*certDir); err != nil {
			return nil, util.Errorf("unable to load TLS config: %v", err)
		}
	}

	s := &server{
		host:  host,
		mux:   http.NewServeMux(),
		clock: hlc.NewClock(hlc.UnixNano),
		rpc:   rpc.NewServer(util.MakeRawAddr("tcp", *rpcAddr), tlsConfig),
	}
	s.clock.SetMaxDrift(*maxDrift)

	s.gossip = gossip.New(tlsConfig)
	s.kvDB = kv.NewDB(kv.NewDistKV(s.gossip), s.clock)
	s.kvREST = rest.NewRESTServer(s.kvDB)
	s.node = NewNode(s.kvDB, s.gossip)
	s.admin = newAdminServer(s.kvDB)
	s.status = newStatusServer(s.kvDB)
	s.structuredDB = structured.NewDB(s.kvDB)
	s.structuredREST = structured.NewRESTServer(s.structuredDB)

	return s, nil
}