forked from minio/minio
-
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.
Save bootstrap trace events in a circular buffer (minio#16823)
- Loading branch information
Showing
5 changed files
with
189 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright (c) 2015-2023 MinIO, Inc. | ||
// | ||
// This file is part of MinIO Object Storage stack | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/minio/madmin-go/v2" | ||
"github.com/minio/minio/internal/pubsub" | ||
) | ||
|
||
const bootstrapMsgsLimit = 4 << 10 | ||
|
||
type bootstrapInfo struct { | ||
msg string | ||
ts time.Time | ||
source string | ||
} | ||
type bootstrapTracer struct { | ||
mu sync.RWMutex | ||
idx int | ||
info [bootstrapMsgsLimit]bootstrapInfo | ||
lastUpdate time.Time | ||
} | ||
|
||
var globalBootstrapTracer = &bootstrapTracer{} | ||
|
||
func (bs *bootstrapTracer) DropEvents() { | ||
bs.mu.Lock() | ||
defer bs.mu.Unlock() | ||
|
||
if time.Now().UTC().Sub(bs.lastUpdate) > 24*time.Hour { | ||
bs.info = [4096]bootstrapInfo{} | ||
bs.idx = 0 | ||
} | ||
} | ||
|
||
func (bs *bootstrapTracer) Empty() bool { | ||
var empty bool | ||
bs.mu.RLock() | ||
empty = bs.info[0].msg == "" | ||
bs.mu.RUnlock() | ||
|
||
return empty | ||
} | ||
|
||
func (bs *bootstrapTracer) Record(msg string) { | ||
source := getSource(2) | ||
bs.mu.Lock() | ||
now := time.Now().UTC() | ||
bs.info[bs.idx] = bootstrapInfo{ | ||
msg: msg, | ||
ts: now, | ||
source: source, | ||
} | ||
bs.lastUpdate = now | ||
bs.idx = (bs.idx + 1) % bootstrapMsgsLimit | ||
bs.mu.Unlock() | ||
} | ||
|
||
func (bs *bootstrapTracer) Events() []madmin.TraceInfo { | ||
traceInfo := make([]madmin.TraceInfo, 0, bootstrapMsgsLimit) | ||
|
||
// Add all messages in order | ||
addAll := func(info []bootstrapInfo) { | ||
for _, msg := range info { | ||
if msg.ts.IsZero() { | ||
continue // skip empty events | ||
} | ||
traceInfo = append(traceInfo, madmin.TraceInfo{ | ||
TraceType: madmin.TraceBootstrap, | ||
Time: msg.ts, | ||
NodeName: globalLocalNodeName, | ||
FuncName: "BOOTSTRAP", | ||
Message: fmt.Sprintf("%s %s", msg.source, msg.msg), | ||
}) | ||
} | ||
} | ||
|
||
bs.mu.RLock() | ||
addAll(bs.info[bs.idx:]) | ||
addAll(bs.info[:bs.idx]) | ||
bs.mu.RUnlock() | ||
return traceInfo | ||
} | ||
|
||
func (bs *bootstrapTracer) Publish(ctx context.Context, trace *pubsub.PubSub[madmin.TraceInfo, madmin.TraceType]) { | ||
if bs.Empty() { | ||
return | ||
} | ||
for _, bsEvent := range bs.Events() { | ||
select { | ||
case <-ctx.Done(): | ||
default: | ||
trace.Publish(bsEvent) | ||
} | ||
} | ||
} |
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,60 @@ | ||
// Copyright (c) 2015-2023 MinIO, Inc. | ||
// | ||
// This file is part of MinIO Object Storage stack | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBootstrap(t *testing.T) { | ||
// Bootstrap events exceed bootstrap messages limit | ||
bsTracer := &bootstrapTracer{} | ||
for i := 0; i < bootstrapMsgsLimit+10; i++ { | ||
bsTracer.Record(fmt.Sprintf("msg-%d", i)) | ||
} | ||
|
||
traceInfos := bsTracer.Events() | ||
if len(traceInfos) != bootstrapMsgsLimit { | ||
t.Fatalf("Expected length of events %d but got %d", bootstrapMsgsLimit, len(traceInfos)) | ||
} | ||
|
||
// Simulate the case where bootstrap events were updated a day ago | ||
bsTracer.lastUpdate = time.Now().UTC().Add(-25 * time.Hour) | ||
bsTracer.DropEvents() | ||
if !bsTracer.Empty() { | ||
t.Fatalf("Expected all bootstrap events to have been dropped, but found %d events", len(bsTracer.Events())) | ||
} | ||
|
||
// Fewer than 4K bootstrap events | ||
for i := 0; i < 10; i++ { | ||
bsTracer.Record(fmt.Sprintf("msg-%d", i)) | ||
} | ||
events := bsTracer.Events() | ||
if len(events) != 10 { | ||
t.Fatalf("Expected length of events %d but got %d", 10, len(events)) | ||
} | ||
for i, traceInfo := range bsTracer.Events() { | ||
msg := fmt.Sprintf("msg-%d", i) | ||
if !strings.HasSuffix(traceInfo.Message, msg) { | ||
t.Fatalf("Expected %s but got %s", msg, traceInfo.Message) | ||
} | ||
} | ||
} |
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