func createRings() (torus.Ring, torus.Ring) {
	ftype, ok := ring.RingTypeFromString(*ringType)
	if !ok {
		fmt.Fprintf(os.Stderr, "unknown ring type: %s\n", *ringType)
		os.Exit(1)
	}
	from, err := ring.CreateRing(&models.Ring{
		Type:              uint32(ftype),
		Version:           1,
		ReplicationFactor: uint32(*replication),
		Peers:             peers[:*nodes],
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "error creating from-ring: %s\n", err)
		os.Exit(1)
	}

	if v, ok := from.(torus.RingAdder); *delta > 0 && ok {
		to, err := v.AddPeers(peers[*nodes:])
		if err != nil {
			fmt.Fprintf(os.Stderr, "error adding peers to ring: %s\n", err)
			os.Exit(1)
		}
		return from, to
	}
	if v, ok := from.(torus.RingRemover); *delta <= 0 && ok {
		to, err := v.RemovePeers(peers[*nodes+*delta:].PeerList())
		if err != nil {
			fmt.Fprintf(os.Stderr, "error removing peers from ring: %s\n", err)
			os.Exit(1)
		}
		return from, to
	}

	ttype, ok := ring.RingTypeFromString(*ringType)
	if !ok {
		fmt.Fprintf(os.Stderr, "unknown ring type: %s\n", *ringType)
		os.Exit(1)
	}
	to, err := ring.CreateRing(&models.Ring{
		Type:              uint32(ttype),
		Version:           2,
		ReplicationFactor: uint32(*replicationEnd),
		Peers:             peers[:(*nodes + *delta)],
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "error creating from-ring: %s\n", err)
		os.Exit(1)
	}
	return from, to
}
func ringChangeAction(cmd *cobra.Command, args []string) {
	if mds == nil {
		mds = mustConnectToMDS()
	}
	currentRing, err := mds.GetRing()
	if err != nil {
		die("couldn't get ring: %v", err)
	}
	var newRing torus.Ring
	switch ringType {
	case "empty":
		newRing, err = ring.CreateRing(&models.Ring{
			Type:    uint32(ring.Empty),
			Version: uint32(currentRing.Version() + 1),
		})
	case "single":
		newRing, err = ring.CreateRing(&models.Ring{
			Type:    uint32(ring.Single),
			Peers:   peers,
			Version: uint32(currentRing.Version() + 1),
		})
	case "mod":
		newRing, err = ring.CreateRing(&models.Ring{
			Type:              uint32(ring.Mod),
			Peers:             peers,
			ReplicationFactor: uint32(repFactor),
			Version:           uint32(currentRing.Version() + 1),
		})
	case "ketama":
		newRing, err = ring.CreateRing(&models.Ring{
			Type:              uint32(ring.Ketama),
			Peers:             peers,
			ReplicationFactor: uint32(repFactor),
			Version:           uint32(currentRing.Version() + 1),
		})
	default:
		panic("still unknown ring type")
	}
	if err != nil {
		die("couldn't create new ring: %v", err)
	}
	cfg := torus.Config{
		MetadataAddress: etcdAddress,
	}
	err = torus.SetRing("etcd", cfg, newRing)
	if err != nil {
		die("couldn't set new ring: %v", err)
	}
}
Пример #3
0
func ringN(t testing.TB, n int) ([]*torus.Server, *temp.Server) {
	servers, mds := createN(t, n)
	var peers torus.PeerInfoList
	for _, s := range servers {
		peers = append(peers, &models.PeerInfo{
			UUID:        s.MDS.UUID(),
			TotalBlocks: StorageSize / BlockSize,
		})
	}

	rep := 2
	ringType := ring.Ketama
	if n == 1 {
		rep = 1
		ringType = ring.Single
	}

	newRing, err := ring.CreateRing(&models.Ring{
		Type:              uint32(ringType),
		Peers:             peers,
		ReplicationFactor: uint32(rep),
		Version:           uint32(2),
	})
	if err != nil {
		t.Fatal(err)
	}
	err = mds.SetRing(newRing)
	if err != nil {
		t.Fatal(err)
	}
	return servers, mds
}
Пример #4
0
func initEtcdMetadata(cfg torus.Config, gmd torus.GlobalMetadata, ringType torus.RingType) error {
	gmdbytes, err := json.Marshal(gmd)
	if err != nil {
		return err
	}
	emptyRing, err := ring.CreateRing(&models.Ring{
		Type:              uint32(ringType),
		Version:           1,
		ReplicationFactor: 2,
	})
	if err != nil {
		return err
	}
	ringb, err := emptyRing.Marshal()
	if err != nil {
		return err
	}

	client, err := etcdv3.New(etcdv3.Config{Endpoints: []string{cfg.MetadataAddress}, TLS: cfg.TLS})
	if err != nil {
		return err
	}
	defer client.Close()

	txn := client.Txn(context.Background())
	resp, err := txn.If(
		etcdv3.Compare(etcdv3.Version(MkKey("meta", "globalmetadata")), "=", 0),
	).Then(
		etcdv3.OpPut(MkKey("meta", "volumeminter"), string(Uint64ToBytes(1))),
		etcdv3.OpPut(MkKey("meta", "globalmetadata"), string(gmdbytes)),
	).Commit()
	if err != nil {
		return err
	}
	if !resp.Succeeded {
		return torus.ErrExists
	}
	_, err = client.Put(context.Background(), MkKey("meta", "the-one-ring"), string(ringb))
	if err != nil {
		return err
	}
	return nil
}
Пример #5
0
func NewServer() *Server {
	r, err := ring.CreateRing(&models.Ring{
		Type:    uint32(ring.Empty),
		Version: 1,
	})
	if err != nil {
		panic(err)
	}
	return &Server{
		volIndex: make(map[string]*models.Volume),
		global: torus.GlobalMetadata{
			BlockSize:        256,
			DefaultBlockSpec: blockset.MustParseBlockLayerSpec("crc,base"),
		},
		ring:  r,
		keys:  make(map[string]interface{}),
		inode: make(map[torus.VolumeID]torus.INodeID),
	}
}