Create creates a new Node instance and initializes all resources necessary to be a member of the hash ring.
(config *NodeConfig)
| 55 | // Create creates a new Node instance and initializes all resources necessary to |
| 56 | // be a member of the hash ring. |
| 57 | func Create(config *NodeConfig) (*Node, error) { |
| 58 | if err := configValidator(config); err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | node := &Node{ |
| 63 | name: uuid.New(), |
| 64 | eventCh: make(chan serf.Event), |
| 65 | quitCh: make(chan bool), |
| 66 | replicas: config.Replicas, |
| 67 | nodes: make(map[string]*peerNode), |
| 68 | config: config, |
| 69 | } |
| 70 | |
| 71 | // RPC Endpoint |
| 72 | node.server = newRpcServer(node) |
| 73 | |
| 74 | // Initialize the ring. |
| 75 | node.ring = newConsistentRing(node.name, config.Replicas) |
| 76 | |
| 77 | // Set up logging. |
| 78 | if config.LogOutput == nil { |
| 79 | config.LogOutput = os.Stderr |
| 80 | } |
| 81 | node.logger = log.New(config.LogOutput, "", log.LstdFlags) |
| 82 | |
| 83 | // Initialize the data storage. |
| 84 | d, err := ttlstore.New(config.MaxMemoryUsage, node.logger) |
| 85 | if err != nil { |
| 86 | return nil, fmt.Errorf("DataStore initialization failed: %v", err) |
| 87 | } |
| 88 | node.data = d |
| 89 | |
| 90 | // Initialize replication |
| 91 | if config.Replicas == 1 { |
| 92 | node.replicator = &noOpReplicator{} |
| 93 | } else { |
| 94 | node.replicator = newReplicator(node) |
| 95 | } |
| 96 | |
| 97 | // Initialize garbage collection. |
| 98 | node.gc = time.AfterFunc(10*time.Minute, node.garbageCollect) |
| 99 | node.gc.Stop() |
| 100 | |
| 101 | // Initialize the RPC Server |
| 102 | if err := node.initRpcServer(config.RpcPort); err != nil { |
| 103 | return nil, fmt.Errorf("RPC server initialization failed: %v", err) |
| 104 | } |
| 105 | |
| 106 | if err := node.initSerf(config.SerfConfig); err != nil { |
| 107 | return nil, fmt.Errorf("serf initialization failed: %v", err) |
| 108 | } |
| 109 | |
| 110 | return node, nil |
| 111 | } |
| 112 | |
| 113 | // Start starts handling connections from other rings in the cluster as well as |
| 114 | // clients. |