forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support scan-keys command to scan the key space starting a specific r…
…evision Signed-off-by: Benjamin Wang <[email protected]>
- Loading branch information
Showing
5 changed files
with
312 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2024 The etcd 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. | ||
|
||
package main | ||
|
||
import "unsafe" | ||
|
||
const magic uint32 = 0xED0CDAED | ||
|
||
type inBucket struct { | ||
root uint64 // page id of the bucket's root-level page | ||
sequence uint64 // monotonically incrementing, used by NextSequence() | ||
} | ||
|
||
type Meta struct { | ||
magic uint32 | ||
version uint32 | ||
pageSize uint32 | ||
flags uint32 | ||
root inBucket | ||
freelist uint64 | ||
pgid uint64 | ||
txid uint64 | ||
checksum uint64 | ||
} | ||
|
||
func loadPageMeta(buf []byte) *Meta { | ||
return (*Meta)(unsafe.Pointer(&buf[pageHeaderSize])) | ||
} |
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,68 @@ | ||
// Copyright 2024 The etcd 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. | ||
|
||
package main | ||
|
||
import "unsafe" | ||
|
||
const pageHeaderSize = unsafe.Sizeof(page{}) | ||
const leafPageElementSize = unsafe.Sizeof(leafPageElement{}) | ||
const pageMaxAllocSize = 0xFFFFFFF | ||
|
||
const ( | ||
leafPageFlag = 0x02 | ||
) | ||
|
||
type page struct { | ||
id uint64 | ||
flags uint16 | ||
count uint16 | ||
overflow uint32 | ||
} | ||
|
||
func (p *page) isLeafPage() bool { | ||
return p.flags == leafPageFlag | ||
} | ||
|
||
func loadPage(buf []byte) *page { | ||
return (*page)(unsafe.Pointer(&buf[0])) | ||
} | ||
|
||
// LeafPageElement retrieves the leaf node by index | ||
func (p *page) LeafPageElement(index uint16) *leafPageElement { | ||
return (*leafPageElement)(unsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p), | ||
leafPageElementSize, int(index))) | ||
} | ||
|
||
// leafPageElement represents a node on a leaf page. | ||
type leafPageElement struct { | ||
flags uint32 | ||
pos uint32 | ||
ksize uint32 | ||
vsize uint32 | ||
} | ||
|
||
// Key returns a byte slice of the node key. | ||
func (n *leafPageElement) key() []byte { | ||
i := int(n.pos) | ||
j := i + int(n.ksize) | ||
return unsafeByteSlice(unsafe.Pointer(n), 0, i, j) | ||
} | ||
|
||
// Value returns a byte slice of the node value. | ||
func (n *leafPageElement) value() []byte { | ||
i := int(n.pos) + int(n.ksize) | ||
j := i + int(n.vsize) | ||
return unsafeByteSlice(unsafe.Pointer(n), 0, i, j) | ||
} |
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,150 @@ | ||
// Copyright 2024 The etcd 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. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"go.etcd.io/etcd/server/v3/storage/mvcc" | ||
) | ||
|
||
func scanKeys(dbPath string, startRev int64) error { | ||
pgSize, hwm, err := readPageAndHWMSize(dbPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to read page and HWM size: %w", err) | ||
} | ||
|
||
for pageID := uint64(2); pageID < hwm; { | ||
p, _, err := readPage(dbPath, pgSize, pageID) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Reading page %d failed: %v. Continuting...\n", pageID, err) | ||
pageID++ | ||
continue | ||
} | ||
|
||
if !p.isLeafPage() { | ||
pageID++ | ||
continue | ||
} | ||
|
||
for i := uint16(0); i < p.count; i++ { | ||
e := p.LeafPageElement(i) | ||
|
||
rev, err := bytesToBucketKey(e.key()) | ||
if err != nil { | ||
if exceptionCheck(e.key()) { | ||
break | ||
} | ||
fmt.Fprintf(os.Stderr, "Decoding revision failed, pageID: %d, index: %d, key: %x, error: %v\n", pageID, i, string(e.key()), err) | ||
continue | ||
} | ||
|
||
if startRev != 0 && rev.Main < startRev { | ||
continue | ||
} | ||
|
||
fmt.Printf("pageID=%d, index=%d, ", pageID, i) | ||
keyDecoder(e.key(), e.value()) | ||
} | ||
|
||
pageID += uint64(p.overflow) + 1 | ||
} | ||
return nil | ||
} | ||
|
||
func bytesToBucketKey(key []byte) (rev mvcc.BucketKey, err error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("BytesToBucketKey failed: %v", r) | ||
} | ||
}() | ||
rev = mvcc.BytesToBucketKey(key) | ||
return rev, err | ||
} | ||
|
||
func readPageAndHWMSize(dbPath string) (uint64, uint64, error) { | ||
f, err := os.Open(dbPath) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
defer f.Close() | ||
|
||
// read 4KB chunk | ||
buf := make([]byte, 4096) | ||
if _, err := io.ReadFull(f, buf); err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
m := loadPageMeta(buf) | ||
if m.magic != magic { | ||
return 0, 0, fmt.Errorf("the Meta Page has wrong (unexpected) magic") | ||
} | ||
|
||
return uint64(m.pageSize), m.pgid, nil | ||
} | ||
|
||
func readPage(dbPath string, pageSize uint64, pageID uint64) (*page, []byte, error) { | ||
f, err := os.Open(dbPath) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer f.Close() | ||
|
||
buf := make([]byte, pageSize) | ||
if _, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
p := loadPage(buf) | ||
if p.id != pageID { | ||
return nil, nil, fmt.Errorf("unexpected page id: %d, wanted: %d", p.id, pageID) | ||
} | ||
|
||
if p.overflow == 0 { | ||
return p, buf, nil | ||
} | ||
|
||
buf = make([]byte, (uint64(p.overflow)+1)*pageSize) | ||
if _, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
p = loadPage(buf) | ||
if p.id != pageID { | ||
return nil, nil, fmt.Errorf("unexpected page id: %d, wanted: %d", p.id, pageID) | ||
} | ||
|
||
return p, buf, nil | ||
} | ||
|
||
func exceptionCheck(key []byte) bool { | ||
whiteKeyList := map[string]struct{}{ | ||
"alarm": {}, | ||
"auth": {}, | ||
"authRoles": {}, | ||
"authUsers": {}, | ||
"cluster": {}, | ||
"key": {}, | ||
"lease": {}, | ||
"members": {}, | ||
"members_removed": {}, | ||
"meta": {}, | ||
} | ||
|
||
_, ok := whiteKeyList[string(key)] | ||
return ok | ||
} |
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