-
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.
- Loading branch information
1 parent
a055dec
commit 7414be5
Showing
8 changed files
with
196 additions
and
21 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,122 @@ | ||
// Copyright 2024 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 roachtestutil | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/roachprod/logger" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/cockroach/pkg/util/log/logconfig" | ||
"github.com/cockroachdb/cockroach/pkg/util/log/logpb" | ||
"github.com/cockroachdb/cockroach/pkg/util/syncutil" | ||
"github.com/petermattis/goid" | ||
) | ||
|
||
type logInterceptor struct{} | ||
|
||
var logRedirect = struct { | ||
mu struct { | ||
syncutil.Mutex | ||
loggers map[int64][]*logger.Logger | ||
} | ||
interceptor logInterceptor | ||
}{ | ||
mu: struct { | ||
syncutil.Mutex | ||
loggers map[int64][]*logger.Logger | ||
}{ | ||
loggers: make(map[int64][]*logger.Logger), | ||
}, | ||
} | ||
|
||
// InitCRDBLogConfig sets up an interceptor for the CockroachDB logger in order to | ||
// redirect logs to the appropriate test log or stderr. This is necessary as the | ||
// CockroachDB logger is used in some test utilities shared between roachtest | ||
// and CockroachDB. Generally, CockroachDB logs should not be used explicitly | ||
// in roachtest. | ||
func InitCRDBLogConfig() { | ||
logConf := logconfig.DefaultStderrConfig() | ||
logConf.Sinks.Stderr.Filter = logpb.Severity_FATAL | ||
// Disable logging to a file. File sinks write the application arguments to | ||
// the log by default (see: log_entry.go), and it is best to avoid logging the | ||
// roachtest arguments g, as it may contain sensitive information. | ||
if err := logConf.Validate(nil); err != nil { | ||
panic(fmt.Errorf("internal error: could not validate CRDB log config: %w", err)) | ||
} | ||
if _, err := log.ApplyConfig(logConf, nil /* fileSinkMetricsForDir */, nil /* fatalOnLogStall */); err != nil { | ||
panic(fmt.Errorf("internal error: could not apply CRDB log config: %w", err)) | ||
} | ||
log.InterceptWith(context.Background(), &logRedirect.interceptor) | ||
} | ||
|
||
// Intercept intercepts the CockroachDB log entries and redirects them to the | ||
// appropriate roachtest test logger or stderr. | ||
func (i *logInterceptor) Intercept(logData []byte) { | ||
var entry logpb.Entry | ||
if err := json.Unmarshal(logData, &entry); err != nil { | ||
panic(fmt.Errorf("internal error: could not unmarshal CRDB log entry: %w", err)) | ||
} | ||
l := func() *logger.Logger { | ||
logRedirect.mu.Lock() | ||
defer logRedirect.mu.Unlock() | ||
if loggers, ok := logRedirect.mu.loggers[entry.Goroutine]; ok && len(loggers) > 0 { | ||
return loggers[len(loggers)-1] | ||
} | ||
return nil | ||
}() | ||
if l != nil && !l.Closed() { | ||
if entry.Severity == logpb.Severity_ERROR || entry.Severity == logpb.Severity_FATAL { | ||
writeLog(l.Stderr, entry) | ||
return | ||
} | ||
writeLog(l.Stdout, entry) | ||
return | ||
} | ||
// If no logger is found, dump to OS stderr | ||
writeLog(os.Stderr, entry) | ||
} | ||
|
||
func writeLog(dst io.Writer, entry logpb.Entry) { | ||
if err := log.FormatLegacyEntry(entry, dst); err != nil { | ||
panic(fmt.Errorf("internal error: could not format and write CRDB log entry: %w", err)) | ||
} | ||
} | ||
|
||
// BridgeCRDBLog bridges the CockroachDB logger to the provided test logger. It | ||
// uses the goroutine ID to determine which logger to bridge to. The returned | ||
// function should be called, in order, to remove the bridge. Multiple calls in | ||
// the same goroutine will bridge to the last logger, until the returned | ||
// function is called, which pops the last logger from the stack for that | ||
// goroutine. | ||
func BridgeCRDBLog(l *logger.Logger) func() { | ||
goID := goid.Get() | ||
|
||
logRedirect.mu.Lock() | ||
defer logRedirect.mu.Unlock() | ||
loggers := logRedirect.mu.loggers[goID] | ||
loggers = append(loggers, l) | ||
logRedirect.mu.loggers[goID] = loggers | ||
|
||
return func() { | ||
logRedirect.mu.Lock() | ||
defer logRedirect.mu.Unlock() | ||
loggers = logRedirect.mu.loggers[goID] | ||
if len(loggers) != 0 { | ||
loggers = loggers[:len(loggers)-1] | ||
logRedirect.mu.loggers[goID] = loggers | ||
} | ||
} | ||
} |
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
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,62 @@ | ||
// Copyright 2024 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 tests | ||
|
||
import ( | ||
"context" | ||
stdlog "log" | ||
"time" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/cluster" | ||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/option" | ||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry" | ||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/test" | ||
"github.com/cockroachdb/cockroach/pkg/roachprod/install" | ||
"github.com/cockroachdb/cockroach/pkg/testutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
func registerWIP(r registry.Registry) { | ||
r.Add(registry.TestSpec{ | ||
Name: "hbl/wip1", | ||
Owner: registry.OwnerTestEng, | ||
Cluster: r.MakeClusterSpec(1), | ||
CompatibleClouds: registry.CloudsWithServiceRegistration, | ||
Suites: registry.Suites(registry.Nightly), | ||
Leases: registry.MetamorphicLeases, | ||
Run: func(ctx context.Context, t test.Test, c cluster.Cluster) { | ||
c.Start(ctx, t.L(), option.DefaultStartOpts(), install.MakeClusterSettings()) | ||
WaitForReady(ctx, t, c, c.All()) | ||
stdlog.Printf("STDLOG PRINT") | ||
log.Infof(ctx, "CRDB LOG") | ||
//glog.Printf("USUAL PRINT") | ||
tn := time.Now() | ||
testutils.SucceedsSoon(t, func() error { | ||
if time.Since(tn) > 20*time.Second { | ||
return nil | ||
} | ||
return errors.Newf("not yet") | ||
}) | ||
}, | ||
}) | ||
//r.Add(registry.TestSpec{ | ||
// Name: "hbl/wip2", | ||
// Owner: registry.OwnerTestEng, | ||
// Cluster: r.MakeClusterSpec(1), | ||
// CompatibleClouds: registry.CloudsWithServiceRegistration, | ||
// Suites: registry.Suites(registry.Nightly), | ||
// Leases: registry.MetamorphicLeases, | ||
// Run: func(ctx context.Context, t test.Test, c cluster.Cluster) { | ||
// | ||
// }, | ||
//}) | ||
} |