-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple StoreFinder and new capacity gossiping #187
Changes from 3 commits
2d6f305
0740147
32fa1a8
adb6600
e142aa5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2014 The Cockroach Authors. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of this could be done in storage/store.go, I just felt that file was getting pretty long. Willing to change back at request! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not combine the code in this file which adds methods to the Store struct into a StoreFinder struct and then embed that in Store? I dislike having methods defined in separate files for the same struct. Currently "StoreFinder" is a func type. We could consider just renaming that FindStoreFunc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or don't embed it...just keep it as a constituent object. That's fine too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you suggesting making the below methods defined on the finderContext or new StoreFinder struct instead of methods defined directly on Store? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, my instinct would be to create a StoreFinder struct and add these methods to it. Then just create an instance of StoreFinder when creating a new Store. Embedding might make sense too, but I don't think it matters in this context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, I tried the embedding method. Looks reasonable -- PTAL |
||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. See the AUTHORS file | ||
// for names of contributors. | ||
// | ||
// Author: Kathy Spradlin ([email protected]) | ||
|
||
package storage | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/cockroachdb/cockroach/gossip" | ||
"github.com/cockroachdb/cockroach/proto" | ||
) | ||
|
||
// StoreFinder finds the disks in a datacenter that have the requested | ||
// attributes. | ||
type StoreFinder func(proto.Attributes) ([]*StoreDescriptor, error) | ||
|
||
type stringSet map[string]struct{} | ||
|
||
type storeFinderContext struct { | ||
sync.Mutex | ||
capacityKeys stringSet | ||
} | ||
|
||
// capacityGossipUpdate is a gossip callback triggered whenever capacity | ||
// information is gossiped. It just tracks keys used for capacity gossip. | ||
func (s *Store) capacityGossipUpdate(key string, contentsChanged bool) { | ||
s.finderContext.Lock() | ||
defer s.finderContext.Unlock() | ||
|
||
if s.finderContext.capacityKeys == nil { | ||
s.finderContext.capacityKeys = stringSet{} | ||
} | ||
s.finderContext.capacityKeys[key] = struct{}{} | ||
} | ||
|
||
// findStores is the Store's implementation of a StoreFinder. It returns a list | ||
// of stores with attributes that are a superset of the required attributes. It | ||
// never returns an error. | ||
// | ||
// If it cannot retrieve a StoreDescriptor from the Store's gossip, it garbage | ||
// collects the failed key. | ||
func (s *Store) findStores(required proto.Attributes) ([]*StoreDescriptor, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a TODO to consider using reverse indexes (e.g. map[AttrStr]map[StoreID]*StoreDescriptor) in order to quickly get the sets for each required attribute and then intersect them for final set of matching stores. Only worth doing when the list of stores grows > 100 I'd imagine. But that's still a pretty small cluster, so I suspect we'll need to make this optimization. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this TODO. I was on that track, second guessed myself, and now I'm triple guessing myself. I'll probably get around to it soon. Though this method might still work out well if we are dealing with gossip in groups. |
||
s.finderContext.Lock() | ||
defer s.finderContext.Unlock() | ||
var stores []*StoreDescriptor | ||
for key := range s.finderContext.capacityKeys { | ||
storeDesc, err := storeDescFromGossip(key, s.gossip) | ||
if err != nil { | ||
// We can no longer retrieve this key from the gossip store, | ||
// perhaps it expired. | ||
delete(s.finderContext.capacityKeys, key) | ||
} else if required.IsSubset(storeDesc.Attrs) { | ||
stores = append(stores, storeDesc) | ||
} | ||
} | ||
return stores, nil | ||
} | ||
|
||
// storeDescFromGossip retrieves a StoreDescriptor from the specified capacity | ||
// gossip key. Returns an error if the gossip doesn't exist or is not | ||
// a StoreDescriptor. | ||
func storeDescFromGossip(key string, g *gossip.Gossip) (*StoreDescriptor, error) { | ||
info, err := g.GetInfo(key) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
storeDesc, ok := info.(StoreDescriptor) | ||
if !ok { | ||
return nil, fmt.Errorf("gossiped info is not a StoreDescriptor: %+v", info) | ||
} | ||
return &storeDesc, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2014 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. See the AUTHORS file | ||
// for names of contributors. | ||
// | ||
// Author: Kathy Spradlin ([email protected]) | ||
|
||
package storage | ||
|
||
import ( | ||
"reflect" | ||
"sort" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cockroachdb/cockroach/proto" | ||
) | ||
|
||
func TestCapacityGossipUpdate(t *testing.T) { | ||
s, _ := createTestStore(t) | ||
defer s.Stop() | ||
key := "testkey" | ||
|
||
// Order and value of contentsChanged shouldn't matter. | ||
s.capacityGossipUpdate(key, true) | ||
s.capacityGossipUpdate(key, false) | ||
|
||
expectedKeys := stringSet{key: struct{}{}} | ||
s.finderContext.Lock() | ||
actualKeys := s.finderContext.capacityKeys | ||
s.finderContext.Unlock() | ||
|
||
if !reflect.DeepEqual(expectedKeys, actualKeys) { | ||
t.Errorf("expected to fetch %+v, instead %+v", expectedKeys, actualKeys) | ||
} | ||
} | ||
|
||
func TestStoreFinder(t *testing.T) { | ||
s, _ := createTestStore(t) | ||
defer s.Stop() | ||
required := []string{"ssd", "dc"} | ||
// Nothing yet. | ||
if stores, _ := s.findStores(proto.Attributes{Attrs: required}); stores != nil { | ||
t.Errorf("expected no stores, instead %+v", stores) | ||
} | ||
|
||
matchingStore := StoreDescriptor{ | ||
Attrs: proto.Attributes{Attrs: required}, | ||
} | ||
supersetStore := StoreDescriptor{ | ||
Attrs: proto.Attributes{Attrs: append(required, "db")}, | ||
} | ||
unmatchingStore := StoreDescriptor{ | ||
Attrs: proto.Attributes{Attrs: []string{"ssd", "otherdc"}}, | ||
} | ||
emptyStore := StoreDescriptor{Attrs: proto.Attributes{}} | ||
|
||
// Explicitly add keys rather than registering a gossip callback to avoid | ||
// waiting for the goroutine callback to finish. | ||
s.finderContext.capacityKeys = stringSet{ | ||
"k1": struct{}{}, | ||
"k2": struct{}{}, | ||
"k3": struct{}{}, | ||
"k4": struct{}{}, | ||
} | ||
s.gossip.AddInfo("k1", matchingStore, time.Hour) | ||
s.gossip.AddInfo("k2", supersetStore, time.Hour) | ||
s.gossip.AddInfo("k3", unmatchingStore, time.Hour) | ||
s.gossip.AddInfo("k4", emptyStore, time.Hour) | ||
|
||
expected := []string{matchingStore.Attrs.SortedString(), supersetStore.Attrs.SortedString()} | ||
stores, err := s.findStores(proto.Attributes{Attrs: required}) | ||
if err != nil { | ||
t.Errorf("expected no err, got %s", err) | ||
} | ||
var actual []string | ||
for _, store := range stores { | ||
actual = append(actual, store.Attrs.SortedString()) | ||
} | ||
sort.Strings(expected) | ||
sort.Strings(actual) | ||
if !reflect.DeepEqual(expected, actual) { | ||
t.Errorf("expected %+v Attrs, instead %+v", expected, actual) | ||
} | ||
} | ||
|
||
// TestStoreFinderGarbageCollection ensures removal of capacity gossip keys in | ||
// the map, if their gossip does not exist when we try to retrieve them. | ||
func TestStoreFinderGarbageCollection(t *testing.T) { | ||
s, _ := createTestStore(t) | ||
defer s.Stop() | ||
|
||
s.finderContext.capacityKeys = stringSet{ | ||
"key0": struct{}{}, | ||
"key1": struct{}{}, | ||
} | ||
required := []string{} | ||
|
||
// No gossip added for either key, so they should be removed. | ||
stores, err := s.findStores(proto.Attributes{Attrs: required}) | ||
if err != nil { | ||
t.Errorf("unexpected error retrieving stores %s", err) | ||
} else if len(stores) != 0 { | ||
t.Errorf("expected no stores found, instead %+v", stores) | ||
} | ||
|
||
if len(s.finderContext.capacityKeys) != 0 { | ||
t.Errorf("expected keys to be cleared, instead are %+v", | ||
s.finderContext.capacityKeys) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually think it was a mistake to use hexidecimal format for the IDs. Mind changing this to just decimal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course! Done. I was wondering how obvious it would be that these were in hex.