Skip to content

Commit

Permalink
feat: add ability to write debug code without affecting production st…
Browse files Browse the repository at this point in the history
…atus

Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
GuptaManan100 committed Jul 23, 2024
1 parent efd8292 commit 9aef7de
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
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

0 comments on commit 9aef7de

Please sign in to comment.