示例#1
0
// 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(db *kv.DB, gossip *gossip.Gossip) *Node {
	n := &Node{
		gossip:  gossip,
		db:      db,
		localKV: kv.NewLocalKV(),
		closer:  make(chan struct{}),
	}
	return n
}
示例#2
0
文件: node.go 项目: bdotdub/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 kv.DB for unittest purposes only.
func BootstrapCluster(clusterID string, eng engine.Engine) (*kv.DB, error) {
	sIdent := proto.StoreIdent{
		ClusterID: clusterID,
		NodeID:    1,
		StoreID:   1,
	}
	clock := hlc.NewClock(hlc.UnixNano)
	now := clock.Now()
	s := storage.NewStore(clock, eng, nil, nil)

	// Verify the store isn't already part of a cluster.
	if len(s.Ident.ClusterID) > 0 {
		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
	}

	// Create first range.
	rng, err := s.CreateRange(s.BootstrapRangeMetadata())
	if err != nil {
		return nil, err
	}

	// Create a KV DB with a local KV to directly modify the new range.
	localKV := kv.NewLocalKV()
	localKV.AddStore(s)
	localDB := kv.NewDB(localKV, clock)

	// Initialize range addressing records and default administrative configs.
	desc := &rng.Meta.RangeDescriptor
	if err := storage.BootstrapRangeDescriptor(localDB, desc, now); err != nil {
		return nil, err
	}

	// Write default configs to local DB.
	if err := storage.BootstrapConfigs(localDB, now); 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
}