Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to write debug code without affecting production performance #16452

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ endif
CGO_ENABLED=0 go build \
-trimpath $(EXTRA_BUILD_FLAGS) $(VT_GO_PARALLEL) \
-ldflags "$(EXTRA_BUILD_LDFLAGS) $(shell tools/build_version_flags.sh)" \
-tags "$(EXTRA_BUILD_TAGS)" \
-o ${VTROOTBIN} ./go/...
ifndef NOVTADMINBUILD
echo "Building VTAdmin Web, disable VTAdmin build by setting 'NOVTADMINBUILD'"
Expand Down
23 changes: 23 additions & 0 deletions go/testing/debug2PC.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build debug2PC

/*
Copyright 2024 The Vitess 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 testing

func init() {
Mode = Debug2PC
}
42 changes: 42 additions & 0 deletions go/testing/modes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2024 The Vitess 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 testing

type TestingMode int

const (
Production TestingMode = iota
Debug2PC
)

// Mode is used to define whether we are in Production mode or testing mode.
// We use go build directives to include files that change the testing mode when certain tags are provided
// while building binaries. This allows to have debugging code written in normal code flow without affecting
// production performance.
// Usage - To add debug code, put it behind testing.Mode != Production etc checks.
var Mode TestingMode = Production

// String is an unused function, but can be used in debugging to see what the testing mode is.
func (tm TestingMode) String() string {
switch tm {
case Production:
return "Production"
case Debug2PC:
return "Debug2PC"
}
return "Unknown"
}
6 changes: 6 additions & 0 deletions go/vt/vtgate/tx_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"fmt"
"strings"
"sync"
"time"

"vitess.io/vitess/go/mysql/sqlerror"
"vitess.io/vitess/go/testing"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/dtids"
"vitess.io/vitess/go/vt/log"
Expand Down Expand Up @@ -99,6 +101,10 @@ func (txc *TxConn) Commit(ctx context.Context, session *SafeSession) error {
}

if twopc {
if testing.Mode == testing.Debug2PC {
// Custom debug code
time.Sleep(3 * time.Second)
}
return txc.commit2PC(ctx, session)
}
return txc.commitNormal(ctx, session)
Expand Down
Loading