// initializes a new server that does not yet have an ID. func initializeServer(u *url.URL, s *influxdb.Server, b *influxdb.Broker, w io.Writer, initBroker bool) { // TODO: Create replica using the messaging client. if initBroker { // Create replica on broker. if err := b.CreateReplica(1, u); err != nil { log.Fatalf("replica creation error: %s", err) } } // Create messaging client. c := messaging.NewClient(1) c.SetLogOutput(w) 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) } if initBroker { // Initialize the server. if err := s.Initialize(b.URL()); err != nil { log.Fatalf("server initialization error: %s", err) } } }
// NewClient returns a new instance of Client. func NewClient(replicaID uint64) *Client { return &Client{ clientConfig: "", // Not all tests with NewClient require automatic temp file creation. Client: messaging.NewClient(replicaID), Server: NewServer(), } }
// opens the messaging client and attaches it to the server. func openServerClient(s *influxdb.Server, joinURLs []*url.URL) { c := messaging.NewClient(s.ID()) if err := c.Open(filepath.Join(s.Path(), messagingClientFile), joinURLs); err != nil { log.Fatalf("messaging client error: %s", err) } if err := s.SetClient(c); err != nil { log.Fatalf("set client error: %s", err) } }
// Ensure a client is properly redirected to the leader. func TestClient_LeaderRedirect(t *testing.T) { c := NewCluster(3) defer c.Close() // Explicity tell the client to send its writes to a follower. c.Leader().Broker().CreateReplica(100, &url.URL{Host: "localhost"}) client := messaging.NewClient(100) client.Open("", []*url.URL{c.Follower().Broker().URL()}) // Ensure after writing to leader, client has correct leader URL. _, err := client.Publish(&messaging.Message{Type: 0, TopicID: 1, Data: make([]byte, 50)}) if err != nil { t.Fatalf("unexpected error: %s", err) } if client.LeaderURL().String() != c.URL().String() { t.Fatalf("client not using expected leader, expected: %s, actual: %s", c.URL().String(), client.LeaderURL().String()) } }
// 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) } }
// Benchmarks a cluster of 3 brokers over HTTP. func BenchmarkCluster_Publish(b *testing.B) { c := NewCluster(3) defer c.Close() // Create replica and connect client. c.Leader().Broker().CreateReplica(100, &url.URL{Host: "localhost"}) client := messaging.NewClient(100) client.Open("", []*url.URL{c.URL()}) b.ResetTimer() var index uint64 for i := 0; i < b.N; i++ { var err error index, err = client.Publish(&messaging.Message{Type: 0, TopicID: 1, Data: make([]byte, 50)}) if err != nil { b.Fatalf("unexpected error: %s", err) } } // Wait for the broker to commit. c.MustSync(index) }
// Ensure a client's Leader URL can be set correctly. func TestClient_SetLeaderURL(t *testing.T) { c := messaging.NewClient(100) // Nil shouldn't blow up. var u *url.URL c.SetLeaderURL(u) tests := []struct { leader string expected string }{ { leader: "http://localhost", expected: "http://localhost", }, { leader: "https://localhost", expected: "https://localhost", }, { leader: "http://localhost:8045", expected: "http://localhost:8045", }, { leader: "http://127.0.0.1:46684/messaging/messages?replicaID=100", expected: "http://127.0.0.1:46684", }, } for _, tt := range tests { c.SetLeaderURL(MustParseURL(tt.leader)) if c.LeaderURL().String() != tt.expected { t.Errorf("Setting client leader URL failed, expected: %s, got: %s", tt.expected, c.LeaderURL().String()) } } }