-
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 #68 from cockroachdb/spencerkimball/address-bens-c…
…omments Created new block ID allocator & put it into use.
- Loading branch information
Showing
9 changed files
with
382 additions
and
166 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// 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: Spencer Kimball ([email protected]) | ||
|
||
package storage | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/proto" | ||
"github.com/cockroachdb/cockroach/storage/engine" | ||
"github.com/cockroachdb/cockroach/util/log" | ||
) | ||
|
||
const ( | ||
// allocationTrigger is a special ID which if encountered, | ||
// causes allocation of the next block of IDs. | ||
allocationTrigger = 0 | ||
) | ||
|
||
type IDAllocator struct { | ||
idKey engine.Key | ||
db DB | ||
minID int64 // Minimum ID to return | ||
blockSize int64 // Block allocation size | ||
ids chan int64 // Channel of available IDs | ||
} | ||
|
||
// NewIDAllocator creates a new ID allocator which increments the | ||
// specified key in allocation blocks of size blockSize, with | ||
// allocated IDs starting at minID. Allocated IDs are positive | ||
// integers. | ||
func NewIDAllocator(idKey engine.Key, db DB, minID int64, blockSize int64) *IDAllocator { | ||
if minID <= allocationTrigger { | ||
log.Fatalf("minID must be > %d", allocationTrigger) | ||
} | ||
if blockSize < 1 { | ||
log.Fatalf("blockSize must be a positive integer: %d", blockSize) | ||
} | ||
ia := &IDAllocator{ | ||
idKey: idKey, | ||
db: db, | ||
minID: minID, | ||
blockSize: blockSize, | ||
ids: make(chan int64, blockSize+blockSize/2+1), | ||
} | ||
ia.ids <- allocationTrigger | ||
return ia | ||
} | ||
|
||
// Allocate allocates a new ID from the global KV DB. If multiple | ||
func (ia *IDAllocator) Allocate() int64 { | ||
for { | ||
id := <-ia.ids | ||
if id == allocationTrigger { | ||
go ia.allocateBlock(ia.blockSize) | ||
} else { | ||
return id | ||
} | ||
} | ||
} | ||
|
||
// allocateBlock allocates a block of IDs using db.Increment and | ||
// sends all IDs on the ids channel. Midway through the block, a | ||
// special allocationTrigger ID is inserted which causes allocation | ||
// to occur before IDs run out to hide Increment latency. | ||
func (ia *IDAllocator) allocateBlock(incr int64) { | ||
ir := <-ia.db.Increment(&proto.IncrementRequest{ | ||
RequestHeader: proto.RequestHeader{ | ||
Key: ia.idKey, | ||
User: UserRoot, | ||
}, | ||
Increment: ia.blockSize, | ||
}) | ||
if ir.Error != nil { | ||
log.Errorf("unable to allocate %d %q IDs: %v", ia.blockSize, ia.idKey, ir.Error) | ||
} | ||
if ir.NewValue <= ia.minID { | ||
log.Warningf("allocator key is currently set at %d; minID is %d; allocating again to skip %d IDs", | ||
ir.NewValue, ia.minID, ia.minID-ir.NewValue) | ||
ia.allocateBlock(ia.minID - ir.NewValue + ia.blockSize) | ||
return | ||
} | ||
|
||
// Add all new ids to the channel for consumption. | ||
start := ir.NewValue - ia.blockSize + 1 | ||
end := ir.NewValue + 1 | ||
if start < ia.minID { | ||
start = ia.minID | ||
} | ||
|
||
for i := start; i < end; i++ { | ||
ia.ids <- i | ||
if i == (start+end)/2 { | ||
ia.ids <- allocationTrigger | ||
} | ||
} | ||
} |
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,86 @@ | ||
// 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: Spencer Kimball ([email protected]) | ||
|
||
package storage | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/storage/engine" | ||
) | ||
|
||
// TestIDAllocator creates an ID allocator which allocates from | ||
// the Raft ID generator system key in blocks of 10 with a minimum | ||
// ID value of 2 and then starts up 10 goroutines each allocating | ||
// 10 IDs. All goroutines deposit the allocated IDs into a final | ||
// channel, which is queried at the end to ensure that all IDs | ||
// from 2 to 101 are present. | ||
func TestIDAllocator(t *testing.T) { | ||
store, _ := createTestStore(false, t) | ||
allocd := make(chan int, 100) | ||
idAlloc := NewIDAllocator(engine.KeyRaftIDGenerator, store.db, 2, 10) | ||
|
||
for i := 0; i < 10; i++ { | ||
go func() { | ||
for j := 0; j < 10; j++ { | ||
allocd <- int(idAlloc.Allocate()) | ||
} | ||
}() | ||
} | ||
|
||
// Verify all IDs accounted for. | ||
ids := make([]int, 100) | ||
for i := 0; i < 100; i++ { | ||
ids[i] = <-allocd | ||
} | ||
sort.Ints(ids) | ||
for i := 0; i < 100; i++ { | ||
if ids[i] != i+2 { | ||
t.Error("expected \"%d\"th ID to be %d; got %d", i, i+2, ids[i]) | ||
} | ||
} | ||
|
||
// Verify no leftover IDs. | ||
select { | ||
case id := <-allocd: | ||
t.Error("there appear to be leftover IDs, starting with %d", id) | ||
default: | ||
// Expected; noop. | ||
} | ||
} | ||
|
||
// TestIDAllocatorNegativeValue creates an ID allocator against an | ||
// increment key which is preset to a negative value. We verify that | ||
// the id allocator makes a double-alloc to make up the difference | ||
// and push the id allocation into positive integers. | ||
func TestIDAllocatorNegativeValue(t *testing.T) { | ||
store, _ := createTestStore(false, t) | ||
// Increment our key to a negative value. | ||
newValue, err := engine.Increment(store.engine, engine.KeyRaftIDGenerator, -1024) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if newValue != -1024 { | ||
t.Errorf("expected new value to be -1024; got %d", newValue) | ||
} | ||
idAlloc := NewIDAllocator(engine.KeyRaftIDGenerator, store.db, 2, 10) | ||
value := idAlloc.Allocate() | ||
if value != 2 { | ||
t.Errorf("expected id allocation to have value 2; got %d", value) | ||
} | ||
} |
Oops, something went wrong.