Example #1
0
// Creates a new router entry from the dynmap passed in
func ToRouterEntry(mp *dynmap.DynMap) (*RouterEntry, error) {
	e := &RouterEntry{
		Self:          false,
		PartitionsMap: make(map[int]bool),
	}
	var ok bool
	e.Address, ok = mp.GetString("address")
	if !ok {
		return nil, fmt.Errorf("No Address in Entry: %s", mp)
	}

	e.JsonPort = mp.MustInt("ports.json", 0)
	e.HttpPort = mp.MustInt("ports.http", 0)

	e.Partitions, ok = mp.GetIntSlice("partitions")
	if !ok {
		return nil, fmt.Errorf("No Partitions in Entry: %s", mp)
	}
	for _, p := range e.Partitions {
		e.PartitionsMap[p] = true
	}
	e.DynMap = e.ToDynMap()
	return e, nil
}
Example #2
0
// Creates a new router table from the dynmap passed in
func ToRouterTable(mp *dynmap.DynMap) (*RouterTable, error) {
	t := &RouterTable{}

	var ok bool
	t.Service, ok = mp.GetString("service")
	if !ok {
		return nil, fmt.Errorf("No Service in the table %s", mp)
	}

	t.Revision, ok = mp.GetInt64("revision")
	if !ok {
		return nil, fmt.Errorf("No Revision in the table %s", mp)
	}

	t.TotalPartitions, ok = mp.GetInt("total_partitions")
	if !ok {
		return nil, fmt.Errorf("No total_partitions in the table %s", mp)
	}

	t.ReplicationFactor, ok = mp.GetInt("replication_factor")
	if !ok {
		return nil, fmt.Errorf("No replication_factor in the table %s", mp)
	}

	//fill the entries
	t.Entries = make([]*RouterEntry, 0)
	entryMaps, ok := mp.GetDynMapSlice("entries")
	if !ok {
		return nil, fmt.Errorf("Bad entries in the table %s", mp)
	}
	for _, em := range entryMaps {
		routerEntry, err := ToRouterEntry(em)
		if err != nil {
			return nil, err
		}
		t.Entries = append(t.Entries, routerEntry)
	}

	// set up the partition to entry mapping
	partitionCount := 0
	entriesPartition := make([]*RouterEntry, t.TotalPartitions)
	for _, e := range t.Entries {
		for _, p := range e.Partitions {
			if p >= t.TotalPartitions {
				return nil, fmt.Errorf("Bad Partition entry (greater then total partitions): %s", e)
			}
			entriesPartition[p] = e
			partitionCount++
		}
	}

	if partitionCount != t.TotalPartitions {
		return nil, fmt.Errorf("Bad table, some partitions un accounted for")
	}

	t.DynMap = t.ToDynMap()

	t.EntriesPartition = make([][]*RouterEntry, t.TotalPartitions)
	//Now setup the replication partitions.
	for _, e := range t.Entries {

		for _, p := range e.Partitions {
			pRep, err := t.repPartitions(p)
			if err != nil {
				return nil, fmt.Errorf("Bad table (%s)", err)
			}
			entries := make([]*RouterEntry, len(pRep)+1)
			entries[0] = e
			for i := 1; i < len(entries); i++ {
				entries[i] = entriesPartition[pRep[i-1]]
				e.PartitionsMap[pRep[i-1]] = false
			}
			t.EntriesPartition[p] = entries
		}
	}

	return t, nil
}