Пример #1
0
func (s *server) start() {
	os.Stdin.Close()

	procs := runtime.NumCPU()
	if procs < 2 {
		procs = 2
	}
	runtime.GOMAXPROCS(procs)

	commandLineConfig, err := s.commandLineConfig()
	s.maybeShutdown(err)
	if commandLineConfig == nil {
		commandLineConfig = configuration.BlankTopology("").Configuration
	}

	nodeCertPrivKeyPair, err := certs.GenerateNodeCertificatePrivateKeyPair(s.certificate)
	for idx := range s.certificate {
		s.certificate[idx] = 0
	}
	s.certificate = nil
	s.maybeShutdown(err)

	disk, err := mdbs.NewMDBServer(s.dataDir, 0, 0600, goshawk.MDBInitialSize, procs/2, time.Millisecond, db.DB)
	s.maybeShutdown(err)
	db := disk.(*db.Databases)
	s.addOnShutdown(db.Shutdown)

	cm, transmogrifier := network.NewConnectionManager(s.rmId, s.bootCount, procs, db, nodeCertPrivKeyPair, s.port, s, commandLineConfig)
	s.addOnShutdown(func() { cm.Shutdown(paxos.Sync) })
	s.addOnShutdown(transmogrifier.Shutdown)
	s.connectionManager = cm
	s.transmogrifier = transmogrifier

	go s.signalHandler()

	listener, err := network.NewListener(s.port, cm)
	s.maybeShutdown(err)
	s.addOnShutdown(listener.Shutdown)

	defer s.shutdown(nil)
	<-s.shutdownChan
}
Пример #2
0
func (s *server) start() {
	procs := runtime.NumCPU()
	if procs < 2 {
		procs = 2
	}
	runtime.GOMAXPROCS(procs)

	disk, err := mdbs.NewMDBServer(s.dataDir, mdb.WRITEMAP, 0600, goshawk.OneTB, procs/2, time.Millisecond, db.DB)
	s.maybeShutdown(err)
	s.addOnShutdown(disk.Shutdown)

	cm, lc := network.NewConnectionManager(s.rmId, s.bootCount, procs, disk, s.passwordHash)
	s.connectionManager = cm
	s.addOnShutdown(cm.Shutdown)
	s.addOnShutdown(lc.Shutdown)

	s.Add(1)
	go s.signalHandler()

	topologyLocal, err := network.GetTopologyFromLocalDatabase(cm, cm.Dispatchers.VarDispatcher, lc)
	s.maybeShutdown(err)

	topology, err := s.chooseTopology(topologyLocal)
	s.maybeShutdown(err)

	if topologyLocal == nil {
		topologyTxnId, err := network.CreateTopologyZero(cm, topology, lc)
		s.maybeShutdown(err)
		topology.DBVersion = topologyTxnId
	}

	cm.SetTopology(topology)

	cm.Dispatchers.VarDispatcher.ApplyToVar(func(v *eng.Var, err error) {
		if err != nil {
			log.Println("Error trying to subscribe to topology:", err)
			return
		}
		emptyTxnId := common.MakeTxnId([]byte{})
		v.AddWriteSubscriber(emptyTxnId,
			func(v *eng.Var, value []byte, refs *msgs.VarIdPos_List, txn *eng.Txn) {
				var rootVarPosPtr *msgs.VarIdPos
				if refs.Len() == 1 {
					root := refs.At(0)
					rootVarPosPtr = &root
				}
				topology, err := goshawk.TopologyDeserialize(txn.Id, rootVarPosPtr, value)
				if err != nil {
					log.Println("Unable to deserialize new topology:", err)
				}
				cm.SetTopology(topology)
				disk.WithEnv(func(env *mdb.Env) (interface{}, error) {
					return nil, env.SetFlags(mdb.MAPASYNC, topology.AsyncFlush)
				})
			})
	}, false, goshawk.TopologyVarUUId)

	cm.AddSender(network.NewTopologyWriter(topology, lc, cm))

	localHost, remoteHosts, err := topology.LocalRemoteHosts(s.port)
	s.maybeShutdown(err)

	log.Printf(">==> We are %v (%v) <==<\n", localHost, s.rmId)

	listener, err := network.NewListener(s.port, cm)
	s.maybeShutdown(err)
	s.addOnShutdown(listener.Shutdown)

	cm.SetDesiredServers(localHost, remoteHosts)

	defer s.shutdown(nil)
	s.Wait()
}