Skip to content

Commit

Permalink
Merge pull request #2239 from cockroachdb/bram/gossip
Browse files Browse the repository at this point in the history
Convert gossip's `capacity` key to `store` key
  • Loading branch information
BramGruneir committed Aug 24, 2015
2 parents 92ffef5 + 9edd75f commit 67a29f1
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 98 deletions.
13 changes: 6 additions & 7 deletions gossip/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ const (
// KeyConfigZone is the zone configuration map.
KeyConfigZone = "zones"

// KeyCapacityPrefix is the key prefix for gossiping available
// store capacity. The suffix is composed of: <node ID>-<store ID>.
// The value is a storage.StoreDescriptor struct.
KeyCapacityPrefix = "capacity"
// KeyStorePrefix is the key prefix for gossiping stores in the network.
// The suffix is a store ID and the value is proto.StoreDescriptor.
KeyStorePrefix = "store"

// KeyNodeCount is the count of gossip nodes in the
// network. The value is an int64 containing the count of nodes in
Expand Down Expand Up @@ -99,7 +98,7 @@ func MakeNodeIDKey(nodeID proto.NodeID) string {
return MakeKey(KeyNodeIDPrefix, nodeID.String())
}

// MakeCapacityKey returns the gossip key for the given store's capacity.
func MakeCapacityKey(nodeID proto.NodeID, storeID proto.StoreID) string {
return MakeKey(KeyCapacityPrefix, nodeID.String(), "-", storeID.String())
// MakeStoreKey returns the gossip key for the given store.
func MakeStoreKey(storeID proto.StoreID) string {
return MakeKey(KeyStorePrefix, storeID.String())
}
13 changes: 6 additions & 7 deletions server/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (n *Node) bootstrapStores(bootstraps *list.List, stopper *stop.Stopper) {
log.Infof("bootstrapped store %s", s)
// Done regularly in Node.startGossip, but this cuts down the time
// until this store is used for range allocations.
s.GossipCapacity()
s.GossipStore()
}
}

Expand Down Expand Up @@ -427,24 +427,23 @@ func (n *Node) startGossip(stopper *stop.Stopper) {
stopper.RunWorker(func() {
ticker := time.NewTicker(gossipInterval)
defer ticker.Stop()
n.gossipCapacities() // one-off run before going to sleep
n.gossipStores() // one-off run before going to sleep
for {
select {
case <-ticker.C:
n.gossipCapacities()
n.gossipStores()
case <-stopper.ShouldStop():
return
}
}
})
}

// gossipCapacities calls capacity on each store and adds it to the
// gossip network.
func (n *Node) gossipCapacities() {
// gossipStores broadcasts each store to the gossip network.
func (n *Node) gossipStores() {
// will never error because `return nil` below
_ = n.lSender.VisitStores(func(s *storage.Store) error {
s.GossipCapacity()
s.GossipStore()
return nil
})
}
Expand Down
29 changes: 14 additions & 15 deletions storage/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type allocator struct {
gossip *gossip.Gossip
randGen *rand.Rand
deterministic bool // Set deterministic for unittests
capacityKeys map[string]struct{} // Tracks gossip keys used for capacity
storeKeys map[string]struct{} // Tracks gossip keys used for stores
storeLists map[string]*storeList // Cache from attributes to storeList
}

Expand All @@ -106,10 +106,10 @@ func newAllocator(g *gossip.Gossip) *allocator {
gossip: g,
randGen: rand.New(rand.NewSource(rand.Int63())),
}
// Callback triggers on any capacity gossip updates.
// Callback triggers on any store gossip updates.
if a.gossip != nil {
capacityRegex := gossip.MakePrefixPattern(gossip.KeyCapacityPrefix)
a.gossip.RegisterCallback(capacityRegex, a.capacityGossipUpdate)
storeRegex := gossip.MakePrefixPattern(gossip.KeyStorePrefix)
a.gossip.RegisterCallback(storeRegex, a.storeGossipUpdate)
}
return a
}
Expand All @@ -125,7 +125,7 @@ func getUsedNodes(existing []proto.Replica) map[proto.NodeID]struct{} {
}

// storeDescFromGossip retrieves a StoreDescriptor from the specified
// capacity gossip key. Returns an error if the gossip doesn't exist
// store gossip key. Returns an error if the gossip doesn't exist
// or is not a StoreDescriptor.
func storeDescFromGossip(key string, g *gossip.Gossip) (*proto.StoreDescriptor, error) {
info, err := g.GetInfo(key)
Expand All @@ -140,19 +140,18 @@ func storeDescFromGossip(key string, g *gossip.Gossip) (*proto.StoreDescriptor,
return &storeDesc, nil
}

// capacityGossipUpdate is a gossip callback triggered whenever capacity
// information is gossiped. It just tracks keys used for capacity
// gossip.
func (a *allocator) capacityGossipUpdate(key string, _ bool) {
// storeGossipUpdate is a gossip callback triggered whenever store information
// is gossiped. It just tracks the gossiped keys.
func (a *allocator) storeGossipUpdate(key string, _ bool) {
a.Lock()
defer a.Unlock()

// Clear the cached store lists on new gossip.
a.storeLists = nil
if a.capacityKeys == nil {
a.capacityKeys = map[string]struct{}{}
if a.storeKeys == nil {
a.storeKeys = map[string]struct{}{}
}
a.capacityKeys[key] = struct{}{}
a.storeKeys[key] = struct{}{}
}

// AllocateTarget returns a suitable store for a new allocation with
Expand Down Expand Up @@ -312,15 +311,15 @@ func (a *allocator) getStoreList(required proto.Attributes) *storeList {
if err != nil {
// We can no longer retrieve this key from the gossip store,
// perhaps it expired.
delete(a.capacityKeys, key)
delete(a.storeKeys, key)
} else if required.IsSubset(*storeDesc.CombinedAttrs()) {
sl.Add(storeDesc)
}
}

if a.deterministic {
var keys []string
for key := range a.capacityKeys {
for key := range a.storeKeys {
keys = append(keys, key)
}
sort.Strings(keys)
Expand All @@ -330,7 +329,7 @@ func (a *allocator) getStoreList(required proto.Attributes) *storeList {
return sl
}

for key := range a.capacityKeys {
for key := range a.storeKeys {
updateStoreList(key)
}
return sl
Expand Down
122 changes: 61 additions & 61 deletions storage/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ var multiDCConfig = config.ZoneConfig{
func gossipStores(g *gossip.Gossip, stores []*proto.StoreDescriptor, t *testing.T) {
var wg sync.WaitGroup
wg.Add(len(stores))
g.RegisterCallback(gossip.MakePrefixPattern(gossip.KeyCapacityPrefix), func(_ string, _ bool) { wg.Done() })
g.RegisterCallback(gossip.MakePrefixPattern(gossip.KeyStorePrefix), func(_ string, _ bool) { wg.Done() })

for _, s := range stores {
keyMaxCapacity := gossip.MakeCapacityKey(s.Node.NodeID, s.StoreID)
keyStoreGossip := gossip.MakeStoreKey(s.StoreID)
// Gossip store descriptor.
err := g.AddInfo(keyMaxCapacity, *s, 0)
err := g.AddInfo(keyStoreGossip, *s, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -538,12 +538,12 @@ func TestAllocatorCapacityGossipUpdate(t *testing.T) {

// Order and value of contentsChanged shouldn't matter.
key := "testkey"
s.allocator().capacityGossipUpdate(key, true)
s.allocator().capacityGossipUpdate(key, false)
s.allocator().storeGossipUpdate(key, true)
s.allocator().storeGossipUpdate(key, false)

expectedKeys := map[string]struct{}{key: {}}
s.allocator().Lock()
actualKeys := s.allocator().capacityKeys
actualKeys := s.allocator().storeKeys
s.allocator().Unlock()

if !reflect.DeepEqual(expectedKeys, actualKeys) {
Expand Down Expand Up @@ -621,7 +621,7 @@ func TestAllocatorGarbageCollection(t *testing.T) {
s, _, stopper := createTestStore(t)
defer stopper.Stop()

s.allocator().capacityKeys = map[string]struct{}{
s.allocator().storeKeys = map[string]struct{}{
"key0": {},
"key1": {},
}
Expand All @@ -632,8 +632,8 @@ func TestAllocatorGarbageCollection(t *testing.T) {
if len(sl.stores) != 0 {
t.Errorf("expected no stores found, instead %+v", sl.stores)
}
if len(s.allocator().capacityKeys) != 0 {
t.Errorf("expected keys to be cleared, instead are %+v", s.allocator().capacityKeys)
if len(s.allocator().storeKeys) != 0 {
t.Errorf("expected keys to be cleared, instead are %+v", s.allocator().storeKeys)
}
}

Expand Down Expand Up @@ -665,7 +665,7 @@ func Example_rebalancing() {
alloc.deterministic = true

var wg sync.WaitGroup
g.RegisterCallback(gossip.MakePrefixPattern(gossip.KeyCapacityPrefix), func(_ string, _ bool) { wg.Done() })
g.RegisterCallback(gossip.MakePrefixPattern(gossip.KeyStorePrefix), func(_ string, _ bool) { wg.Done() })

const generations = 100
const nodes = 20
Expand All @@ -688,7 +688,7 @@ func Example_rebalancing() {
if testStores[j].Capacity.RangeCount > 0 {
testStores[j].Add(alloc.randGen.Int63n(1 << 20))
}
key := gossip.MakeCapacityKey(proto.NodeID(j), proto.StoreID(j))
key := gossip.MakeStoreKey(proto.StoreID(j))
if err := g.AddInfo(key, testStores[j].StoreDescriptor, 0); err != nil {
panic(err)
}
Expand Down Expand Up @@ -739,54 +739,54 @@ func Example_rebalancing() {

// Output:
// 999 000 000 000 000 000 000 739 000 000 000 000 000 000 000 000 000 000 000 000
// 999 000 000 000 204 000 000 375 000 000 107 000 000 000 000 000 000 000 000 536
// 942 000 000 463 140 000 000 646 000 288 288 000 442 000 058 647 000 000 316 999
// 880 000 412 630 365 745 445 565 122 407 380 570 276 000 271 709 000 718 299 999
// 925 000 667 600 555 975 704 552 272 491 773 890 584 000 407 974 000 930 476 999
// 990 967 793 579 493 999 698 453 616 608 777 755 709 425 455 984 483 698 267 931
// 965 999 869 606 635 908 630 585 567 577 818 870 740 621 550 868 805 790 411 913
// 953 995 990 624 617 947 562 609 670 658 909 952 835 851 641 958 924 999 526 987
// 999 923 901 571 687 915 636 636 674 685 831 881 847 820 702 905 897 983 509 981
// 999 884 809 585 691 826 640 572 748 641 754 887 758 848 643 927 865 897 541 956
// 999 856 891 594 691 745 602 615 766 663 814 834 719 886 733 925 882 911 593 926
// 999 890 900 653 707 759 642 697 771 732 851 858 748 869 842 953 903 928 655 923
// 999 924 909 696 748 797 693 689 806 766 841 902 705 897 874 914 913 916 730 892
// 999 948 892 704 740 821 685 656 859 772 893 911 690 878 824 935 928 941 741 860
// 999 948 931 697 770 782 697 666 893 761 944 869 658 902 816 925 923 983 742 831
// 999 878 901 736 750 737 677 647 869 731 930 825 631 880 775 947 949 930 687 810
// 999 890 910 764 778 757 709 663 849 777 964 837 672 891 814 978 944 946 721 868
// 985 895 968 806 791 791 720 694 883 819 999 847 652 888 790 995 950 947 692 843
// 960 903 956 794 815 779 746 706 891 824 958 830 665 886 757 999 931 969 701 861
// 999 928 954 805 807 822 764 734 910 829 952 827 678 927 785 980 936 962 677 836
// 999 903 924 800 769 822 776 730 886 815 935 781 668 890 805 948 929 965 676 837
// 999 926 935 836 782 836 809 756 897 835 937 781 690 894 804 979 951 978 667 832
// 999 937 936 875 843 872 854 793 908 873 950 808 714 901 860 981 975 962 693 866
// 988 957 938 898 922 912 916 886 905 912 964 867 764 915 911 992 999 985 776 896
// 945 959 922 910 937 913 938 944 957 921 993 916 898 957 928 999 976 997 855 957
// 980 986 944 956 963 920 966 967 999 966 991 956 981 973 955 998 990 954 994 981
// 956 985 942 945 950 900 933 949 981 969 946 935 963 951 931 999 936 941 972 963
// 940 999 964 949 941 974 967 937 970 975 965 951 976 968 949 993 944 949 977 964
// 926 999 973 932 944 952 933 944 963 965 927 940 964 960 938 995 932 935 968 951
// 907 999 919 957 941 958 934 935 930 941 940 926 966 933 920 973 937 923 938 946
// 924 999 914 963 976 945 911 936 929 951 930 930 972 935 941 977 932 960 939 958
// 942 999 950 961 987 942 928 945 938 941 939 936 985 937 969 985 952 958 957 948
// 956 999 950 947 943 939 949 934 929 935 940 942 943 957 988 974 933 936 938 951
// 967 990 950 949 964 952 951 922 943 940 954 956 962 946 982 999 945 949 940 954
// 970 999 952 959 970 955 957 974 937 965 968 947 950 958 947 993 953 938 958 950
// 945 964 954 963 965 959 967 961 925 978 954 944 968 937 960 999 947 947 961 960
// 930 957 938 974 956 944 968 930 944 972 930 946 958 974 940 999 961 945 953 947
// 966 980 954 989 979 960 969 995 961 986 954 980 980 971 968 999 968 977 979 972
// 963 953 958 986 990 947 973 955 955 983 974 981 961 964 977 999 984 982 966 964
// 964 968 975 993 999 955 965 958 972 995 978 981 956 966 981 987 978 976 985 966
// 967 957 954 999 963 940 968 966 941 966 971 969 957 961 949 940 968 963 988 947
// 951 939 952 980 937 948 964 970 941 965 979 966 941 940 952 938 973 955 999 934
// 939 958 941 998 942 951 962 942 962 951 972 978 946 935 958 935 950 947 999 953
// 959 952 938 999 936 957 961 950 937 954 975 971 958 930 938 930 944 939 978 950
// 957 943 963 999 947 965 953 937 966 953 978 972 963 937 933 945 944 937 979 952
// 945 951 956 999 926 948 958 923 947 934 951 961 955 941 949 936 945 929 960 947
// 956 960 975 999 945 977 956 934 954 943 961 956 956 954 960 954 958 929 969 938
// 947 966 993 999 944 963 942 939 963 935 952 957 968 947 962 946 962 947 959 942
// 940 961 999 992 935 946 938 932 968 939 957 938 970 949 964 934 948 957 952 939
// 944 955 999 978 940 932 937 944 957 936 957 945 958 955 947 933 956 948 947 942
// Total bytes=1003302292, ranges=1899
// 999 107 000 000 204 000 000 375 000 000 000 000 000 000 000 000 000 000 536 000
// 999 310 000 262 872 000 000 208 000 705 000 526 000 000 439 000 000 607 933 000
// 812 258 000 220 999 673 402 480 000 430 516 374 000 431 318 000 551 714 917 000
// 582 625 185 334 720 589 647 619 000 300 483 352 279 502 208 665 816 684 999 374
// 751 617 771 542 738 676 665 525 309 435 612 449 457 616 306 837 993 754 999 445
// 759 659 828 478 693 622 594 591 349 458 630 538 526 613 462 827 879 787 999 550
// 861 658 828 559 801 660 681 560 487 529 652 686 642 716 575 999 989 875 989 581
// 775 647 724 557 779 662 670 494 535 502 681 676 624 695 561 961 999 772 888 592
// 856 712 753 661 767 658 717 606 529 615 755 699 672 700 576 955 999 755 861 671
// 882 735 776 685 844 643 740 578 610 688 787 741 661 767 587 999 955 809 803 731
// 958 716 789 719 861 689 821 608 634 724 800 782 694 799 619 994 999 851 812 818
// 949 726 788 664 873 633 749 599 680 714 790 728 663 842 628 999 978 816 823 791
// 923 698 792 712 816 605 774 651 661 728 802 718 670 819 714 999 966 801 829 791
// 962 779 847 737 900 675 811 691 745 778 835 812 680 894 790 999 989 872 923 799
// 967 812 826 772 891 685 828 683 761 808 864 820 643 873 783 969 999 873 910 781
// 923 813 837 739 867 672 792 664 773 772 879 803 610 845 740 957 999 867 912 732
// 952 803 866 759 881 655 765 668 803 772 929 762 601 844 751 973 999 892 864 731
// 970 777 867 800 859 639 774 662 787 760 906 751 595 854 732 989 999 853 859 762
// 943 776 872 787 861 686 780 663 789 793 926 784 612 832 733 999 968 868 827 767
// 914 801 912 802 878 704 800 685 818 808 939 759 627 844 717 999 976 872 828 757
// 935 806 911 797 887 710 798 711 826 824 938 775 614 870 716 999 986 886 803 767
// 991 851 898 856 872 795 828 782 826 852 963 797 710 868 775 994 999 923 896 794
// 999 924 866 877 884 883 886 836 846 869 953 851 762 887 858 985 949 900 917 836
// 999 910 887 878 897 890 906 868 906 903 983 947 801 895 913 976 924 890 904 898
// 955 884 888 916 886 879 901 872 898 883 999 874 829 888 892 937 918 889 891 862
// 974 952 957 990 950 976 945 946 980 961 999 975 942 926 957 994 965 946 960 960
// 949 929 952 999 929 961 943 946 993 918 984 961 952 919 953 950 952 941 949 934
// 907 999 916 935 903 903 909 907 960 939 973 912 901 885 916 910 941 911 906 913
// 939 999 948 948 945 962 951 954 952 964 996 942 975 962 962 956 971 969 975 969
// 940 974 964 947 971 975 949 954 953 970 992 971 981 973 948 962 999 969 978 975
// 950 971 953 938 962 967 930 964 953 978 999 945 974 972 951 950 998 951 949 962
// 934 946 943 936 942 949 929 956 928 970 989 944 945 923 987 927 999 942 931 944
// 939 957 942 958 951 970 937 946 930 950 940 959 963 937 973 943 999 931 949 940
// 933 935 945 929 933 960 937 935 919 918 930 931 950 924 969 935 999 943 949 926
// 959 941 948 952 948 957 936 937 943 930 955 962 953 949 980 948 999 934 980 942
// 950 973 954 962 949 964 935 949 925 936 951 962 979 962 999 942 990 948 969 959
// 937 993 958 949 960 960 942 954 969 950 951 952 974 970 999 927 979 964 975 944
// 981 986 971 968 964 984 954 959 985 979 966 963 994 963 999 970 991 971 988 965
// 967 997 961 957 959 985 956 940 955 955 957 955 970 952 979 964 999 951 960 968
// 937 969 931 950 945 954 932 925 954 946 944 926 955 938 957 949 999 934 947 938
// 958 967 954 955 971 973 946 934 979 947 944 958 954 954 960 948 999 936 960 951
// 950 948 940 958 937 955 928 927 953 923 935 939 934 921 934 934 999 922 940 938
// 960 960 929 962 955 955 926 935 957 928 939 941 938 926 941 924 999 923 957 942
// 979 958 947 987 980 972 945 943 984 939 951 943 944 946 942 942 999 928 970 943
// 981 941 931 961 969 962 927 935 985 925 964 945 946 939 946 938 999 933 964 928
// 980 944 929 970 973 955 942 937 977 920 955 929 937 946 935 933 999 947 956 926
// 980 948 926 981 938 939 936 936 963 949 965 935 943 946 933 933 999 947 955 943
// 968 959 945 941 929 926 924 941 970 951 959 941 924 952 931 943 999 941 951 950
// 961 946 930 923 933 932 953 937 954 940 964 944 931 952 939 935 999 936 945 948
// Total bytes=996294324, ranges=1897
}
4 changes: 2 additions & 2 deletions storage/client_raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,10 @@ func TestStoreRangeReplicate(t *testing.T) {
// Initialize the gossip network.
var wg sync.WaitGroup
wg.Add(len(mtc.stores))
key := gossip.MakePrefixPattern(gossip.KeyCapacityPrefix)
key := gossip.MakePrefixPattern(gossip.KeyStorePrefix)
mtc.stores[0].Gossip().RegisterCallback(key, func(_ string, _ bool) { wg.Done() })
for _, s := range mtc.stores {
s.GossipCapacity()
s.GossipStore()
}
wg.Wait()

Expand Down
12 changes: 6 additions & 6 deletions storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const (
defaultRaftTickInterval = 100 * time.Millisecond
defaultHeartbeatIntervalTicks = 3
defaultRaftElectionTimeoutTicks = 15
// ttlCapacityGossip is time-to-live for capacity-related info.
ttlCapacityGossip = 2 * time.Minute
// ttlStoreGossip is time-to-live for store-related info.
ttlStoreGossip = 2 * time.Minute
)

var (
Expand Down Expand Up @@ -676,18 +676,18 @@ func (s *Store) configGossipUpdate(key string, contentsChanged bool) {
}
}

// GossipCapacity broadcasts the node's capacity on the gossip network.
func (s *Store) GossipCapacity() {
// GossipStore broadcasts the store on the gossip network.
func (s *Store) GossipStore() {
storeDesc, err := s.Descriptor()
ctx := s.Context(nil)
if err != nil {
log.Warningc(ctx, "problem getting store descriptor for store %+v: %v", s.Ident, err)
return
}
// Unique gossip key per store.
keyMaxCapacity := gossip.MakeCapacityKey(storeDesc.Node.NodeID, storeDesc.StoreID)
gossipStoreKey := gossip.MakeStoreKey(storeDesc.StoreID)
// Gossip store descriptor.
err = s.ctx.Gossip.AddInfo(keyMaxCapacity, *storeDesc, ttlCapacityGossip)
err = s.ctx.Gossip.AddInfo(gossipStoreKey, *storeDesc, ttlStoreGossip)
if err != nil {
log.Warningc(ctx, "%s", err)
}
Expand Down

0 comments on commit 67a29f1

Please sign in to comment.