Пример #1
0
func NewPeer(cfg *PeerConfig, debug bool) (*Peer, error) {

	cl, err := NewClient(cfg.MasterAddr)
	if err != nil {
		return nil, err
	}
	ret := &Peer{}
	ret.Cfg = cfg
	ret.Datanode, err = dataserver.NewDataServer(cfg.BindAddr, cfg.VolumeRoots, cl.Names, cl.Datas)
	if err != nil {
		return ret, err
	}
	dnStat, err := ret.Datanode.HeartBeat()
	if err != nil {
		return ret, err
	}
	fuseConnector, err := client.NewMaggieFuse(cl.Leases, cl.Names, cl.Datas, &dnStat.DnId)
	if err != nil {
		return ret, err
	}
	ret.Mfs = fuseConnector
	ret.Mountpoint, err = NewMount(fuseConnector, cfg.MountPoint, false)

	opMap := make(map[uint32]func(*net.TCPConn))
	opMap[dataserver.DIAL_READ] = ret.Datanode.ServeReadConn
	opMap[dataserver.DIAL_WRITE] = ret.Datanode.ServeWriteConn

	dataServ, err := mrpc.CloseableRPC(cfg.BindAddr, "Peer", maggiefs.NewPeerService(ret.Datanode), opMap)
	if err != nil {
		return ret, err
	}
	multiServ := NewMultiService()

	err = multiServ.AddService(dataServ)
	if err != nil {
		return ret, err
	}
	err = multiServ.AddService(ret.Mountpoint)
	if err != nil {
		return ret, err
	}
	// now that services are started, tell master to connect to us
	err = cl.Names.Join(dnStat.DnId, cfg.BindAddr)

	ret.svc = multiServ
	return ret, err
}
Пример #2
0
func NewSingleNodeCluster(startPort int, numDNs int, volsPerDn int, replicationFactor uint32, baseDir string, mountPoint string, debugMode bool) (*SingleNodeCluster, error) {
	cl := &SingleNodeCluster{}
	nncfg, ds, err := NewConfSet2(startPort, numDNs, volsPerDn, replicationFactor, baseDir)
	if err != nil {
		return nil, err
	}
	log.Printf("Creating singleNodeCluster with master conf: %s, ds confs: %s", nncfg, ds)
	nls, err := NewMaster(nncfg, true)
	if err != nil {
		return nil, err
	}
	cl.LeaseServer = nls.leaseServer
	cl.NameServer = nls.nameserver

	// make service wrapper
	multiServ := NewMultiService()
	cl.svc = multiServ
	multiServ.AddService(nls) // add nameLeaseServer
	masterAddr := fmt.Sprintf("127.0.0.1:%d", nls.port)
	log.Printf("Connecting client to started master at %s", masterAddr)
	cli, err := NewClient(masterAddr)
	if err != nil {
		return cl, err
	}
	cl.Names = cli.Names
	cl.Leases = cli.Leases
	cl.Datas = cli.Datas
	// start dataservers
	cl.DataNodes = make([]*dataserver.DataServer, len(ds))
	cl.DataNodeServs = make([]*mrpc.CloseableServer, len(ds))
	for idx, dscfg := range ds {
		log.Printf("Starting DS with cfg %+v", dscfg)
		// create and register with SingleNodeCluster struct
		ds, err := dataserver.NewDataServer(dscfg.BindAddr, dscfg.VolumeRoots, cl.Names, cl.Datas)
		if err != nil {
			return cl, err
		}
		cl.DataNodes[idx] = ds
		// start and register with MultiServ
		opMap := make(map[uint32]func(*net.TCPConn))
		opMap[dataserver.DIAL_READ] = ds.ServeReadConn
		opMap[dataserver.DIAL_WRITE] = ds.ServeWriteConn
		dataServ, err := mrpc.CloseableRPC(dscfg.BindAddr, "Peer", maggiefs.NewPeerService(ds), opMap)
		if err != nil {
			return cl, err
		}
		cl.DataNodeServs[idx] = dataServ
		err = multiServ.AddService(dataServ)
		if err != nil {
			return cl, err
		}
		dnInfo, _ := ds.HeartBeat()
		err = cl.Names.Join(dnInfo.DnId, dscfg.BindAddr)
		if err != nil {
			return cl, err
		}
	}

	// create mountpoint if necessary
	if mountPoint != "" {
		maggieFuse, err := client.NewMaggieFuse(cl.Leases, cl.Names, cl.Datas, nil)
		if err != nil {
			return cl, err
		}
		mount, err := NewMount(maggieFuse, mountPoint, debugMode)
		if err != nil {
			return cl, err
		}
		err = multiServ.AddService(mount)
		if err != nil {
			return cl, err
		}
	}
	time.Sleep(1 * time.Second)
	return cl, nil
}