-
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.
kvserver,roachpb,storage: add IsSpanEmptyRequest to check for any data
This commit adds a new request, `IsSpanEmptyRequest`, which checks to see if there is any data in a key span of any kind. It ignore the GC threshold, and operates across all versions. Fixes #85726 Release note: None
- Loading branch information
Showing
13 changed files
with
311 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package batcheval | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval/result" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/storage" | ||
"github.com/cockroachdb/cockroach/pkg/util/hlc" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
func init() { | ||
RegisterReadOnlyCommand(roachpb.IsSpanEmpty, DefaultDeclareKeys, IsSpanEmpty) | ||
} | ||
|
||
// IsSpanEmpty determines whether there are any keys in the key span requested | ||
// at any time. If there are any keys, the response header will have a NumKeys | ||
// value of 1. | ||
func IsSpanEmpty( | ||
ctx context.Context, reader storage.Reader, cArgs CommandArgs, resp roachpb.Response, | ||
) (result.Result, error) { | ||
args := cArgs.Args.(*roachpb.IsSpanEmptyRequest) | ||
reply := resp.(*roachpb.IsSpanEmptyResponse) | ||
isEmpty, err := storage.MVCCIsSpanEmpty(ctx, reader, storage.MVCCIsSpanEmptyOptions{ | ||
StartKey: args.Key, | ||
EndKey: args.EndKey, | ||
StartTS: hlc.MinTimestamp, // beginning of time | ||
EndTS: hlc.MaxTimestamp, // end of time | ||
}) | ||
if err != nil { | ||
return result.Result{}, errors.Wrap(err, "IsSpanEmpty") | ||
} | ||
if !isEmpty { | ||
reply.NumKeys++ | ||
} | ||
return result.Result{}, 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,97 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package batcheval_test | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/kv" | ||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIsSpanEmpty(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
ctx := context.Background() | ||
var sentIsSpanEmptyRequests int64 | ||
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{ | ||
ServerArgs: base.TestServerArgs{ | ||
Knobs: base.TestingKnobs{ | ||
Store: &kvserver.StoreTestingKnobs{ | ||
TestingRequestFilter: func(ctx context.Context, request roachpb.BatchRequest) *roachpb.Error { | ||
if _, exists := request.GetArg(roachpb.IsSpanEmpty); exists { | ||
atomic.AddInt64(&sentIsSpanEmptyRequests, 1) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
defer tc.Stopper().Stop(ctx) | ||
|
||
kvDB := tc.Server(0).DB() | ||
scratchKey := tc.ScratchRange(t) | ||
mkKey := func(suffix string) roachpb.Key { | ||
return append(scratchKey[:len(scratchKey):len(scratchKey)], suffix...) | ||
} | ||
|
||
checkIsEmpty := func(t *testing.T, exp bool, from, to roachpb.Key) { | ||
var ba kv.Batch | ||
ba.Header.MaxSpanRequestKeys = 1 | ||
ba.AddRawRequest(&roachpb.IsSpanEmptyRequest{ | ||
RequestHeader: roachpb.RequestHeader{ | ||
Key: from, EndKey: to, | ||
}, | ||
}) | ||
require.NoError(t, kvDB.Run(ctx, &ba)) | ||
require.Equal(t, exp, ba.RawResponse().Responses[0].GetIsSpanEmpty().IsEmpty()) | ||
} | ||
|
||
requireEmpty := func(t *testing.T, from, to roachpb.Key) { | ||
checkIsEmpty(t, true, from, to) | ||
} | ||
requireNotEmpty := func(t *testing.T, from, to roachpb.Key) { | ||
checkIsEmpty(t, false, from, to) | ||
} | ||
|
||
requireEmpty(t, mkKey(""), mkKey("").PrefixEnd()) | ||
|
||
tc.SplitRangeOrFatal(t, mkKey("c")) | ||
requireEmpty(t, mkKey(""), mkKey("").PrefixEnd()) | ||
|
||
require.NoError(t, kvDB.Put(ctx, mkKey("x"), "foo")) | ||
requireEmpty(t, mkKey(""), mkKey("x")) | ||
requireNotEmpty(t, mkKey(""), mkKey("").PrefixEnd()) | ||
|
||
require.NoError(t, kvDB.Del(ctx, mkKey("x"))) | ||
requireEmpty(t, mkKey(""), mkKey("x")) | ||
requireNotEmpty(t, mkKey(""), mkKey("").PrefixEnd()) | ||
|
||
// We want to make sure that the DistSender stops iterating ranges once | ||
// the first range with any keys is found. | ||
checkIsCalled := func(t *testing.T, expEmpty bool, delta int64, from, to roachpb.Key) { | ||
before := atomic.LoadInt64(&sentIsSpanEmptyRequests) | ||
checkIsEmpty(t, expEmpty, from, to) | ||
require.Equal(t, delta, atomic.LoadInt64(&sentIsSpanEmptyRequests)-before) | ||
} | ||
checkIsCalled(t, false, 2, mkKey(""), mkKey("").PrefixEnd()) | ||
tc.SplitRangeOrFatal(t, mkKey("a")) | ||
tc.SplitRangeOrFatal(t, mkKey("b")) | ||
checkIsCalled(t, false, 4, mkKey(""), mkKey("").PrefixEnd()) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.