Ejemplo n.º 1
0
func (s *CmdCtrlClient) ringUpdateNodeCmd(filename string) error {
	ctx, _ := context.WithTimeout(context.Background(), 60*time.Second)
	r, _, err := ring.RingOrBuilder(filename)
	if err != nil {
		return err
	}
	if r == nil {
		return fmt.Errorf("Provided builder file rather than ring file")
	}
	ru := &cc.Ring{}
	ru.Version = r.Version()
	ru.Ring, err = ioutil.ReadFile(filename)
	if err != nil {
		return err
	}
	status, err := s.client.RingUpdate(ctx, ru)
	if err != nil {
		return err
	}
	if status.Newversion != ru.Version {
		return fmt.Errorf("Ring update seems to have failed. Expected: %d, but remote host reports: %d\n", ru.Version, status.Newversion)
	}
	fmt.Println("Remote version is now", status.Newversion)
	return nil
}
Ejemplo n.º 2
0
func (o *Server) ObtainConfig() (err error) {
	e := NewEnvGetter(fmt.Sprintf("OORT_%s", strings.ToUpper(o.serviceName)), "_")
	envSkipSRV := e.Get("SKIP_SRV")
	// Check whether we're supposed to skip loading via srv method
	if strings.ToLower(envSkipSRV) != "true" {
		s := &srvconf.SRVLoader{
			SyndicateURL: e.Get("SYNDICATE_OVERRIDE"),
		}
		s.Record, err = GenServiceID(o.serviceName, "syndicate", "tcp")
		if err != nil {
			if e.Get("SYNDICATE_OVERRIDE") == "" {
				log.Println(err)
			} else {
				log.Fatalln("No SYNDICATE_OVERRIDE provided and", err)
			}
		}
		if e.Get("SYNDICATE_OVERRIDE") != "" {
			log.Println("Over wrote syndicate url with url from env!", e.Get("SYNDICATE_OVERRIDE"))
		}
		nc, err := s.Load()
		if err != nil {
			return err
		}
		o.ring, err = ring.LoadRing(bytes.NewReader(nc.Ring))
		if err != nil {
			return fmt.Errorf("Error while loading ring for config get via srv lookup: %s", err)
		}
		err = ring.PersistRingOrBuilder(o.ring, nil, fmt.Sprintf("%s/ring/%d-%s.ring", o.cwd, o.ring.Version(), o.serviceName))
		if err != nil {
			return err
		}
		o.localID = nc.Localid
		o.ring.SetLocalNode(o.localID)
		o.ringFile = fmt.Sprintf("%s/ring/%d-%s.ring", o.cwd, o.ring.Version(), o.serviceName)
		err = o.loadCmdCtrlConfig()
		if err != nil {
			return err
		}
	} else {
		// if you skip the srv load you have to provide all of the info in env vars!
		log.Println("Skipped SRV Config attempting to load from env")
		s, err := strconv.ParseUint(e.Get("LOCALID"), 10, 64)
		if err != nil {
			return fmt.Errorf("Unable to load env specified local id")
		}
		o.localID = s
		o.ringFile = e.Get("RING_FILE")
		o.ring, _, err = ring.RingOrBuilder(o.ringFile)
		if err != nil {
			return fmt.Errorf("Unable to road env specified ring: %s", err)
		}
		o.ring.SetLocalNode(o.localID)
		err = o.loadCmdCtrlConfig()
		if err != nil {
			return err
		}
	}
	return nil
}
func (s *ringslave) Store(c context.Context, r *pb.RingMsg) (*pb.StoreResult, error) {
	log.Println("Got store request:", r.Version, r.Deadline, r.Rollback)
	s.Lock()
	defer s.Unlock()

	s.last = time.Now()
	bs, rs, err := s.saveRingAndBuilderBytes(&r.Ring, &r.Builder, r.Version)
	if err != nil {
		return &pb.StoreResult{
			Version: r.Version,
			Ring:    rs,
			Builder: bs,
			ErrMsg:  fmt.Sprintf("Encountered error during save: %s", err),
		}, nil
	}
	s.version = r.Version

	_, builder, err := ring.RingOrBuilder(path.Join(s.spath, fmt.Sprintf("%d.oort.builder", r.Version)))
	if err != nil || builder == nil {
		return &pb.StoreResult{
			Version: r.Version,
			Ring:    false,
			Builder: false,
			ErrMsg:  fmt.Sprintf("Encountered error during builder load: %s", err),
		}, nil

	}
	oldbuilder := s.b
	s.b = builder

	ring, _, err := ring.RingOrBuilder(path.Join(s.spath, fmt.Sprintf("%d.oort.ring.gz", r.Version)))
	if err != nil || ring == nil || ring.Version() != r.Version {
		//restore builder
		s.b = oldbuilder
		return &pb.StoreResult{
			Version: r.Version,
			Ring:    false,
			Builder: false,
			ErrMsg:  fmt.Sprintf("Encountered error during ring load: %s", err),
		}, nil

	}
	s.r = ring
	s.version = r.Version
	return &pb.StoreResult{Version: r.Version, Ring: true, Builder: true, ErrMsg: ""}, nil
}
Ejemplo n.º 4
0
//getRing loads a ring from disk
func (s *Server) getRing(path string) (ring.Ring, error) {
	r, _, err := ring.RingOrBuilder(path)
	return r, err
}
Ejemplo n.º 5
0
//getBuilder loads a builder from disk
func (s *Server) getBuilder(path string) (*ring.Builder, error) {
	_, b, err := ring.RingOrBuilder(path)
	return b, err
}
Ejemplo n.º 6
0
//NewServer returns a new instance of an up and running syndicate mangement node
func NewServer(cfg *Config, servicename string, opts ...MockOpt) (*Server, error) {
	var err error
	s := new(Server)
	s.cfg = cfg
	s.servicename = servicename
	log.SetFormatter(&log.TextFormatter{})
	if s.cfg.Debug {
		log.SetLevel(log.DebugLevel)
	}
	s.ctxlog = log.WithField("service", s.servicename)
	s.metrics = metricsInit(s.servicename)

	s.parseConfig()

	for _, opt := range opts {
		opt(s)
	}
	if s.rbPersistFn == nil {
		s.rbPersistFn = s.ringBuilderPersisterFn
	}
	if s.rbLoaderFn == nil {
		s.rbLoaderFn = func(path string) ([]byte, error) {
			return ioutil.ReadFile(path)
		}
	}
	if s.getBuilderFn == nil {
		s.getBuilderFn = s.getBuilder
	}

	bfile, rfile, err := getRingPaths(cfg, s.servicename)
	if err != nil {
		panic(err)
	}

	_, s.b, err = ring.RingOrBuilder(bfile)
	FatalIf(err, fmt.Sprintf("Builder file (%s) load failed:", bfile))
	s.r, _, err = ring.RingOrBuilder(rfile)
	FatalIf(err, fmt.Sprintf("Ring file (%s) load failed:", rfile))
	//TODO: verify ring version in bytes matches what we expect
	s.rb, s.bb, err = s.loadRingBuilderBytes(s.r.Version())
	FatalIf(err, "Attempting to load ring/builder bytes")

	for _, v := range cfg.NetFilter {
		_, n, err := net.ParseCIDR(v)
		if err != nil {
			FatalIf(err, "Invalid network range provided")
		}
		s.netlimits = append(s.netlimits, n)
	}
	s.tierlimits = cfg.TierFilter
	s.managedNodes = bootstrapManagedNodes(s.r, s.cfg.CmdCtrlPort, s.ctxlog)
	s.metrics.managedNodes.Set(float64(len(s.managedNodes)))
	s.changeChan = make(chan *changeMsg, 1)
	s.subsChangeChan = make(chan *changeMsg, 1)
	go s.RingChangeManager()
	s.ringSubs = &RingSubscribers{
		subs: make(map[string]chan *pb.Ring),
	}
	go s.ringSubscribersNotify()
	s.slaves = parseSlaveAddrs(cfg.Slaves)
	if len(s.slaves) == 0 {
		s.ctxlog.Debug("running without slaves")
		return s, nil
	}

	failcount := 0
	for _, slave := range s.slaves {
		if err = s.RegisterSlave(slave); err != nil {
			s.ctxlog.WithFields(
				log.Fields{
					"slave":  slave.addr,
					"status": slave.status,
					"err":    err,
				}).Warning("Error registering slave")
			failcount++
		}
	}
	if failcount > (len(s.slaves) / 2) {
		return s, fmt.Errorf("More than half of the ring slaves failed to respond. Exiting.")
	}
	return s, nil
}