-
Notifications
You must be signed in to change notification settings - Fork 28
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
Showing
27 changed files
with
701 additions
and
303 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
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,40 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// 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 logger | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type testingLog struct { | ||
*testing.T | ||
} | ||
|
||
func (t *testingLog) Write(b []byte) (int, error) { | ||
t.Logf("%s", b) | ||
return len(b), nil | ||
} | ||
|
||
// CreateLoggerForTest creates a logger for unit tests. | ||
func CreateLoggerForTest(t *testing.T) *zap.Logger { | ||
return zap.New(zapcore.NewCore( | ||
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), | ||
zapcore.AddSync(&testingLog{t}), | ||
zap.InfoLevel, | ||
)).Named(t.Name()) | ||
} |
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 2017 PingCAP, Inc. | ||
// | ||
// 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 systimemon | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// StartMonitor calls systimeErrHandler if system time jump backward. | ||
func StartMonitor(ctx context.Context, logger *zap.Logger, now func() time.Time, systimeErrHandler func(), successCallback func()) { | ||
logger.Info("start system time monitor") | ||
tick := time.NewTicker(100 * time.Millisecond) | ||
defer tick.Stop() | ||
tickCount := 0 | ||
for { | ||
last := now().UnixNano() | ||
select { | ||
case <-tick.C: | ||
case <-ctx.Done(): | ||
return | ||
} | ||
if now().UnixNano() < last { | ||
logger.Error("system time jump backward", zap.Int64("last", last)) | ||
systimeErrHandler() | ||
} | ||
// call successCallback per second. | ||
tickCount++ | ||
if tickCount >= 10 { | ||
tickCount = 0 | ||
successCallback() | ||
} | ||
} | ||
} |
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,50 @@ | ||
// Copyright 2017 PingCAP, Inc. | ||
// | ||
// 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 systimemon | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pingcap/TiProxy/lib/util/logger" | ||
"github.com/pingcap/TiProxy/lib/util/waitgroup" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func TestSystimeMonitor(t *testing.T) { | ||
errTriggered := atomic.NewBool(false) | ||
nowTriggered := atomic.NewBool(false) | ||
log := logger.CreateLoggerForTest(t) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
var wg waitgroup.WaitGroup | ||
wg.Run(func() { | ||
StartMonitor(ctx, log, | ||
func() time.Time { | ||
if !nowTriggered.Load() { | ||
nowTriggered.Store(true) | ||
return time.Now() | ||
} | ||
return time.Now().Add(-2 * time.Second) | ||
}, func() { | ||
errTriggered.Store(true) | ||
}, func() {}) | ||
}) | ||
|
||
require.Eventually(t, errTriggered.Load, time.Second, 10*time.Millisecond) | ||
cancel() | ||
wg.Wait() | ||
} |
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,65 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// 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 router | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pingcap/TiProxy/pkg/metrics" | ||
) | ||
|
||
func updateBackendStatusMetrics(addr string, prevStatus, curStatus BackendStatus) { | ||
metrics.BackendStatusGauge.WithLabelValues(addr, prevStatus.String()).Set(0) | ||
metrics.BackendStatusGauge.WithLabelValues(addr, curStatus.String()).Set(1) | ||
} | ||
|
||
func checkBackendStatusMetrics(addr string, status BackendStatus) bool { | ||
val, err := metrics.ReadGauge(metrics.BackendStatusGauge.WithLabelValues(addr, status.String())) | ||
if err != nil { | ||
return false | ||
} | ||
return val == 1 | ||
} | ||
|
||
func addBackendConnMetrics(addr string) { | ||
metrics.BackendConnGauge.WithLabelValues(addr).Add(1) | ||
} | ||
|
||
func subBackendConnMetrics(addr string) { | ||
metrics.BackendConnGauge.WithLabelValues(addr).Sub(1) | ||
} | ||
|
||
func readBackendConnMetrics(addr string) (int, error) { | ||
return metrics.ReadGauge(metrics.BackendConnGauge.WithLabelValues(addr)) | ||
} | ||
|
||
func succeedToLabel(succeed bool) string { | ||
if succeed { | ||
return "succeed" | ||
} | ||
return "fail" | ||
} | ||
|
||
func addMigrateMetrics(from, to string, succeed bool, startTime time.Time) { | ||
resLabel := succeedToLabel(succeed) | ||
metrics.MigrateCounter.WithLabelValues(from, to, resLabel).Inc() | ||
|
||
cost := time.Since(startTime) | ||
metrics.MigrateDurationHistogram.WithLabelValues(from, to, resLabel).Observe(float64(cost.Milliseconds())) | ||
} | ||
|
||
func readMigrateCounter(from, to string, succeed bool) (int, error) { | ||
return metrics.ReadCounter(metrics.MigrateCounter.WithLabelValues(from, to, succeedToLabel(succeed))) | ||
} |
Oops, something went wrong.