// joins a broker to an existing cluster. func joinBroker(b *messaging.Broker, joinURLs []*url.URL) { // Attempts to join each server until successful. for _, u := range joinURLs { if err := b.Join(u); err != nil { log.Printf("join: failed to connect to broker: %s: %s", u, err) } else { log.Printf("join: connected broker to %s", u) return } } log.Fatalf("join: failed to connect broker to any specified server") }
// creates and initializes a server. func openServer(path string, u *url.URL, b *messaging.Broker, initializing, configExists bool, joinURLs []*url.URL) *influxdb.Server { // Ignore if there's no existing server and we're not initializing or joining. if !fileExists(path) && !initializing && len(joinURLs) == 0 { return nil } // Create and open the server. s := influxdb.NewServer() if err := s.Open(path); err != nil { log.Fatalf("failed to open data server: %v", err.Error()) } // If the server is uninitialized then initialize or join it. if initializing { if len(joinURLs) == 0 { initializeServer(s, b) } else { joinServer(s, u, joinURLs) openServerClient(s, joinURLs) } } else if !configExists { // We are spining up a server that has no config, // but already has an initialized data directory joinURLs = []*url.URL{b.URL()} openServerClient(s, joinURLs) } else { if len(joinURLs) == 0 { // If a config exists, but no joinUrls are specified, fall back to the broker URL // TODO: Make sure we have a leader, and then spin up the server joinURLs = []*url.URL{b.URL()} } openServerClient(s, joinURLs) } return s }
// initializes a new server that does not yet have an ID. func initializeServer(s *influxdb.Server, b *messaging.Broker) { // TODO: Create replica using the messaging client. // Create replica on broker. if err := b.CreateReplica(1); err != nil { log.Fatalf("replica creation error: %s", err) } // Create messaging client. c := messaging.NewClient(1) if err := c.Open(filepath.Join(s.Path(), messagingClientFile), []*url.URL{b.URL()}); err != nil { log.Fatalf("messaging client error: %s", err) } if err := s.SetClient(c); err != nil { log.Fatalf("set client error: %s", err) } // Initialize the server. if err := s.Initialize(b.URL()); err != nil { log.Fatalf("server initialization error: %s", err) } }
// initializes a new broker. func initializeBroker(b *messaging.Broker) { if err := b.Initialize(); err != nil { log.Fatalf("initialize: %s", err) } }