-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from embark/embark/rebalancing
Simple StoreFinder and new capacity gossiping
- Loading branch information
Showing
9 changed files
with
236 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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 ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/cockroachdb/cockroach/gossip" | ||
"github.com/cockroachdb/cockroach/proto" | ||
) | ||
|
||
// FindStoreFunc finds the disks in a datacenter that have the requested | ||
// attributes. | ||
type FindStoreFunc func(proto.Attributes) ([]*StoreDescriptor, error) | ||
|
||
type stringSet map[string]struct{} | ||
|
||
// StoreFinder provides the data necessary to find stores with particular | ||
// attributes. | ||
type StoreFinder struct { | ||
finderMu sync.Mutex | ||
capacityKeys stringSet // Tracks gosisp keys used for capacity | ||
gossip *gossip.Gossip | ||
} | ||
|
||
// capacityGossipUpdate is a gossip callback triggered whenever capacity | ||
// information is gossiped. It just tracks keys used for capacity gossip. | ||
func (sf *StoreFinder) capacityGossipUpdate(key string, contentsChanged bool) { | ||
sf.finderMu.Lock() | ||
defer sf.finderMu.Unlock() | ||
|
||
if sf.capacityKeys == nil { | ||
sf.capacityKeys = stringSet{} | ||
} | ||
sf.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. | ||
// | ||
// TODO(embark, spencer): consider using a reverse index map from Attr->stores, | ||
// for efficiency. Ensure that entries in this map still have an opportunity | ||
// to be garbage collected. | ||
func (sf *StoreFinder) findStores(required proto.Attributes) ([]*StoreDescriptor, error) { | ||
sf.finderMu.Lock() | ||
defer sf.finderMu.Unlock() | ||
var stores []*StoreDescriptor | ||
for key := range sf.capacityKeys { | ||
storeDesc, err := storeDescFromGossip(key, sf.gossip) | ||
if err != nil { | ||
// We can no longer retrieve this key from the gossip store, | ||
// perhaps it expired. | ||
delete(sf.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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// 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) { | ||
sf := StoreFinder{} | ||
key := "testkey" | ||
|
||
// Order and value of contentsChanged shouldn't matter. | ||
sf.capacityGossipUpdate(key, true) | ||
sf.capacityGossipUpdate(key, false) | ||
|
||
expectedKeys := stringSet{key: struct{}{}} | ||
sf.finderMu.Lock() | ||
actualKeys := sf.capacityKeys | ||
sf.finderMu.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.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.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.capacityKeys) != 0 { | ||
t.Errorf("expected keys to be cleared, instead are %+v", | ||
s.capacityKeys) | ||
} | ||
} |