예제 #1
0
파일: node.go 프로젝트: GavinHwa/cockroach
// NewNode returns a new instance of Node, interpreting command line
// flags to initialize the appropriate Store or set of
// Stores. Registers the storage instance for the RPC service "Node".
func NewNode(distDB kv.DB, gossip *gossip.Gossip) *Node {
	n := &Node{
		gossip:  gossip,
		distDB:  distDB,
		localDB: kv.NewLocalDB(),
		closer:  make(chan struct{}),
	}
	return n
}
예제 #2
0
파일: node.go 프로젝트: Zemnmez/cockroach
// BootstrapCluster bootstraps a store using the provided engine and
// cluster ID. The bootstrapped store contains a single range spanning
// all keys. Initial range lookup metadata is populated for the range.
// Returns a direct-access kv.LocalDB for unittest purposes only.
func BootstrapCluster(clusterID string, engine storage.Engine) (*kv.LocalDB, error) {
	sIdent := storage.StoreIdent{
		ClusterID: clusterID,
		NodeID:    1,
		StoreID:   1,
	}
	s := storage.NewStore(engine, nil)

	// Verify the store isn't already part of a cluster.
	if s.Ident.ClusterID != "" {
		return nil, util.Errorf("storage engine already belongs to a cluster (%s)", s.Ident.ClusterID)
	}

	// Bootstrap store to persist the store ident.
	if err := s.Bootstrap(sIdent); err != nil {
		return nil, err
	}

	if err := s.Init(); err != nil {
		return nil, err
	}

	// Create first range.
	rng, err := s.CreateRange(storage.KeyMin, storage.KeyMax)
	if err != nil {
		return nil, err
	}
	if rng.Meta.RangeID != 1 {
		return nil, util.Errorf("expected range id of 1, got %d", rng.Meta.RangeID)
	}

	// Create a local DB to directly modify the new range.
	localDB := kv.NewLocalDB(rng)

	// Initialize meta1 and meta2 range addressing records.
	replica := storage.Replica{
		NodeID:     1,
		StoreID:    1,
		RangeID:    1,
		Datacenter: getDatacenter(),
		DiskType:   engine.Type(),
	}
	kv.BootstrapRangeLocations(localDB, replica)

	// Initialize node and store ids after the fact to account
	// for use of node ID = 1 and store ID = 1.
	if nodeID, err := allocateNodeID(localDB); nodeID != sIdent.NodeID || err != nil {
		return nil, util.Errorf("expected to intialize node id allocator to %d, got %d: %v",
			sIdent.NodeID, nodeID, err)
	}
	if storeID, err := allocateStoreIDs(sIdent.NodeID, 1, localDB); storeID != sIdent.StoreID || err != nil {
		return nil, util.Errorf("expected to intialize store id allocator to %d, got %d: %v",
			sIdent.StoreID, storeID, err)
	}

	return localDB, nil
}
예제 #3
0
파일: node.go 프로젝트: Joinhack/cockroach
// BootstrapCluster bootstraps a store using the provided engine and
// cluster ID. The bootstrapped store contains a single range spanning
// all keys. Initial range lookup metadata is populated for the range.
//
// Returns a direct-access kv.LocalDB for unittest purposes only.
func BootstrapCluster(clusterID string, engine storage.Engine) (*kv.LocalDB, error) {
	sIdent := storage.StoreIdent{
		ClusterID: clusterID,
		NodeID:    1,
		StoreID:   1,
	}
	s := storage.NewStore(engine, nil)
	defer s.Close()

	// Verify the store isn't already part of a cluster.
	if s.Ident.ClusterID != "" {
		return nil, util.Errorf("storage engine already belongs to a cluster (%s)", s.Ident.ClusterID)
	}

	// Bootstrap store to persist the store ident.
	if err := s.Bootstrap(sIdent); err != nil {
		return nil, err
	}

	if err := s.Init(); err != nil {
		return nil, err
	}

	// Create first range.
	replica := storage.Replica{
		NodeID:  1,
		StoreID: 1,
		RangeID: 1,
		Attrs:   storage.Attributes{},
	}
	rng, err := s.CreateRange(storage.KeyMin, storage.KeyMax, []storage.Replica{replica})
	if err != nil {
		return nil, err
	}
	if rng.Meta.RangeID != 1 {
		return nil, util.Errorf("expected range id of 1, got %d", rng.Meta.RangeID)
	}

	// Create a local DB to directly modify the new range.
	localDB := kv.NewLocalDB(rng)

	// Initialize range addressing records and default administrative configs.
	if err := kv.BootstrapRangeDescriptor(localDB, replica); err != nil {
		return nil, err
	}
	if err := kv.BootstrapConfigs(localDB); err != nil {
		return nil, err
	}

	// Initialize node and store ids after the fact to account
	// for use of node ID = 1 and store ID = 1.
	if nodeID, err := allocateNodeID(localDB); nodeID != sIdent.NodeID || err != nil {
		return nil, util.Errorf("expected to intialize node id allocator to %d, got %d: %v",
			sIdent.NodeID, nodeID, err)
	}
	if storeID, err := allocateStoreIDs(sIdent.NodeID, 1, localDB); storeID != sIdent.StoreID || err != nil {
		return nil, util.Errorf("expected to intialize store id allocator to %d, got %d: %v",
			sIdent.StoreID, storeID, err)
	}

	return localDB, nil
}