Example #1
0
func newDistributor(srv *torus.Server, addr *url.URL) (*Distributor, error) {
	var err error
	d := &Distributor{
		blocks: srv.Blocks,
		srv:    srv,
	}
	gmd := d.srv.MDS.GlobalMetadata()
	if addr != nil {
		d.rpcSrv, err = protocols.ListenRPC(addr, d, gmd)
		if err != nil {
			return nil, err
		}
	}
	if srv.Cfg.ReadCacheSize != 0 {
		size := srv.Cfg.ReadCacheSize / gmd.BlockSize
		if size < 100 {
			size = 100
		}
		d.readCache = newCache(int(size))
	}

	// Set up the rebalancer
	d.ring, err = d.srv.MDS.GetRing()
	if err != nil {
		return nil, err
	}
	d.ringWatcherChan = make(chan struct{})
	go d.ringWatcher(d.rebalancerChan)
	d.client = newDistClient(d)
	g := gc.NewGCController(d.srv, torus.NewINodeStore(d))
	d.rebalancer = rebalance.NewRebalancer(d, d.blocks, d.client, g)
	d.rebalancerChan = make(chan struct{})
	go d.rebalanceTicker(d.rebalancerChan)
	return d, nil
}
func openReplication(s *torus.Server, addr *url.URL) error {
	var err error
	if s.ReplicationOpen {
		return torus.ErrExists
	}
	dist, err := newDistributor(s, addr)
	if err != nil {
		return err
	}
	s.Blocks = dist
	s.INodes = torus.NewINodeStore(dist)
	err = s.BeginHeartbeat(addr)
	if err != nil {
		return err
	}
	s.ReplicationOpen = true
	return nil
}