forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: introduce a hook for short-running migrations
TODO: see if this migration is actually "short-running". That is, in a sufficiently large cluster, does this cause significant load? ---- This is a baby version of cockroachdb#39182 that handles only short-running migrations but is useful in itself because it allows us to migrate us fully into the following two KV below-Raft migrations: 1. use the RangeAppliedState on all ranges 2. use the unreplicated TruncatedState on all ranges These two migrations have been around for a while and it has been getting in the way of things. While ideally we introduce cockroachdb#39182 in the near future and use it to address a larger class of migration concerns, this PR serves as a starting point and work done on cockroachdb#39182 should be able to absorb this PR with relative ease. With this PR, legacy code related to 1) and 2) above can be removed from `master` once the 20.1 branch is cut, i.e. roughly in April 2020. The main ingredients in this PR are a) introduce a hook that is called during `SET CLUSTER SETTING version`, after the change has been validated but before it is made. b) introduce a KV-level `Migrate` ranged write command that triggers the migrations for 1) and 2) above. It is called from the hook for all of the keyspace. c) before returning to the client, `Migrate` waits for the command to be durably applied on all followers. Trying to get this 100% correct has proven tricky, perhaps foreshadowing similar issues that expect us once we try cockroachdb#39182 in earnest. For one, the mechanism only reaches replicas that members of the raft group, that is, it won't touch replicas which are gc'able. For the migrations at hand this means that in 20.2 there may - in theory - still be replicas that have a replicated truncated state. I believe that our recent efforts around not re-using replicas across replicaIDs has made sure that this isn't an issue for this particular migration, but in general it will have to remain on the radar. Similarly, it seems hard to prove conclusively that no snapshot is in-flight that would set up a new follower with a state predating the explicit migration, though it would be exceptionally rare in practice. Release note (general change): version upgrades now perform maintenance duties that may slightly delay the `SET CLUSTER SETTING version` command and may cause a small amount of additional load on the cluster while an upgrade is being finalized.
- Loading branch information
1 parent
a415689
commit 11da41b
Showing
39 changed files
with
3,128 additions
and
1,098 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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
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
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,78 @@ | ||
// Copyright 2014 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/clusterversion" | ||
"github.com/cockroachdb/cockroach/pkg/keys" | ||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval/result" | ||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanset" | ||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/storagepb" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/storage" | ||
"github.com/cockroachdb/cockroach/pkg/util/hlc" | ||
) | ||
|
||
func init() { | ||
RegisterReadWriteCommand(roachpb.Migrate, declareKeysMigrate, Migrate) | ||
} | ||
|
||
func declareKeysMigrate( | ||
_ *roachpb.RangeDescriptor, header roachpb.Header, req roachpb.Request, latchSpans, _ *spanset.SpanSet, | ||
) { | ||
latchSpans.AddNonMVCC(spanset.SpanReadWrite, roachpb.Span{Key: keys.RaftTruncatedStateLegacyKey(header.RangeID)}) | ||
} | ||
|
||
// Migrate ensures that the range proactively carries out any outstanding | ||
// below-Raft migrations. When this command returns, all of the below-Raft | ||
// features known to be available are enabled. | ||
func Migrate( | ||
ctx context.Context, batch storage.ReadWriter, cArgs CommandArgs, resp roachpb.Response, | ||
) (result.Result, error) { | ||
newV := cArgs.Args.(*roachpb.MigrateRequest).NewVersion | ||
if newV.Less(clusterversion.VersionByKey(clusterversion.VersionNoLegacyTruncatedAndAppliedState)) { | ||
// This is only hit during testing or when upgrading an alpha to an unstable below | ||
// the above version. In production, newV will be a major release and more precisely, | ||
// it will equal the BinaryServerVersion of any binary in the cluster (they are all | ||
// equal when the cluster version is bumped, mod pathological races due to user error). | ||
return result.Result{}, nil | ||
} | ||
|
||
var legacyTruncatedState roachpb.RaftTruncatedState | ||
legacyKeyFound, err := storage.MVCCGetProto( | ||
ctx, batch, keys.RaftTruncatedStateLegacyKey(cArgs.EvalCtx.GetRangeID()), | ||
hlc.Timestamp{}, &legacyTruncatedState, storage.MVCCGetOptions{}, | ||
) | ||
if err != nil { | ||
return result.Result{}, err | ||
} | ||
|
||
var pd result.Result | ||
if legacyKeyFound { | ||
// Time to migrate by deleting the legacy key. The downstream-of-Raft | ||
// code will atomically rewrite the truncated state (supplied via the | ||
// side effect) into the new unreplicated key. | ||
if err := storage.MVCCDelete( | ||
ctx, batch, cArgs.Stats, keys.RaftTruncatedStateLegacyKey(cArgs.EvalCtx.GetRangeID()), | ||
hlc.Timestamp{}, nil, /* txn */ | ||
); err != nil { | ||
return result.Result{}, err | ||
} | ||
pd.Replicated.State = &storagepb.ReplicaState{ | ||
// We need to pass in a truncated state to enable the migration. | ||
// Passing the same one is the easiest thing to do. | ||
TruncatedState: &legacyTruncatedState, | ||
} | ||
} | ||
return pd, 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
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.