// NewServer returns a new instance of Server built from a config. func NewServer(c *Config, version string) (*Server, error) { s := &Server{ version: version, err: make(chan error), closing: make(chan struct{}), Hostname: c.Meta.Hostname, BindAddress: c.Meta.BindAddress, MetaStore: meta.NewStore(c.Meta), DataStore: db.NewStore(c.Data.Dir), reportingDisabled: c.ReportingDisabled, } // Copy TSDB configuration. s.DataStore.MaxWALSize = c.Data.MaxWALSize s.DataStore.WALFlushInterval = time.Duration(c.Data.WALFlushInterval) s.DataStore.WALPartitionFlushDelay = time.Duration(c.Data.WALPartitionFlushDelay) // Set the shard mapper s.ShardMapper = cluster.NewShardMapper(time.Duration(c.Cluster.ShardMapperTimeout)) s.ShardMapper.ForceRemoteMapping = c.Cluster.ForceRemoteShardMapping s.ShardMapper.MetaStore = s.MetaStore s.ShardMapper.DataStore = s.DataStore // Initialize query executor. s.QueryExecutor = db.NewQueryExecutor(s.DataStore) s.QueryExecutor.MetaStore = s.MetaStore s.QueryExecutor.MetaStatementExecutor = &meta.StatementExecutor{Store: s.MetaStore} // Set the shard writer s.ShardWriter = cluster.NewShardWriter(time.Duration(c.Cluster.ShardWriterTimeout)) s.ShardWriter.MetaStore = s.MetaStore // Create the hinted handoff service s.HintedHandoff = hh.NewService(c.HintedHandoff, s.ShardWriter) // Initialize points writer. s.MessagesWriter = cluster.NewMessagesWriter() s.MessagesWriter.WriteTimeout = time.Duration(c.Cluster.WriteTimeout) s.MessagesWriter.MetaStore = s.MetaStore s.MessagesWriter.DataStore = s.DataStore s.MessagesWriter.ShardWriter = s.ShardWriter s.MessagesWriter.HintedHandoff = s.HintedHandoff // Append services. s.appendClusterService(c.Cluster) s.appendSnapshotterService() s.appendAdminService(c.Admin) s.appendHTTPDService(c.HTTPD) s.appendRetentionPolicyService(c.Retention) return s, nil }
// unpackMeta reads the metadata from the snapshot and initializes a raft // cluster and replaces the root metadata. func (cmd *Command) unpackMeta(mr *snapshot.MultiReader, sf snapshot.File, config *Config) error { // Read meta into buffer. var buf bytes.Buffer if _, err := io.CopyN(&buf, mr, sf.Size); err != nil { return fmt.Errorf("copy: %s", err) } // Unpack into metadata. var data meta.Data if err := data.UnmarshalBinary(buf.Bytes()); err != nil { return fmt.Errorf("unmarshal: %s", err) } // Copy meta config and remove peers so it starts in single mode. c := config.Meta c.Peers = nil // Initialize meta store. store := meta.NewStore(config.Meta) store.RaftListener = newNopListener() store.ExecListener = newNopListener() // Determine advertised address. _, port, err := net.SplitHostPort(config.Meta.BindAddress) if err != nil { return fmt.Errorf("split bind address: %s", err) } hostport := net.JoinHostPort(config.Meta.Hostname, port) // Resolve address. addr, err := net.ResolveTCPAddr("tcp", hostport) if err != nil { return fmt.Errorf("resolve tcp: addr=%s, err=%s", hostport, err) } store.Addr = addr // Open the meta store. if err := store.Open(); err != nil { return fmt.Errorf("open store: %s", err) } defer store.Close() // Wait for the store to be ready or error. select { case <-store.Ready(): case err := <-store.Err(): return err } // Force set the full metadata. if err := store.SetData(&data); err != nil { return fmt.Errorf("set data: %s", err) } return nil }