|
| 1 | +// Copyright 2025 Matrix Origin |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fileservice |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/matrixorigin/matrixone/pkg/common/moerr" |
| 23 | + metric "github.com/matrixorigin/matrixone/pkg/util/metric/v2" |
| 24 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestObjectStorageMetricsWriteMultipartParallel(t *testing.T) { |
| 29 | + name := t.Name() |
| 30 | + upstream := &mockParallelObjectStorage{supports: true} |
| 31 | + wrapped := newObjectStorageMetrics(upstream, name) |
| 32 | + gauge := metric.FSObjectStorageOperations.WithLabelValues(name, "write") |
| 33 | + before := testutil.ToFloat64(gauge) |
| 34 | + |
| 35 | + err := wrapped.WriteMultipartParallel(context.Background(), "key", strings.NewReader("data"), nil, nil) |
| 36 | + require.NoError(t, err) |
| 37 | + require.Equal(t, before+1, testutil.ToFloat64(gauge)) |
| 38 | + require.Equal(t, "key", upstream.key) |
| 39 | +} |
| 40 | + |
| 41 | +func TestObjectStorageMetricsWriteMultipartParallelNotSupported(t *testing.T) { |
| 42 | + name := t.Name() |
| 43 | + wrapped := newObjectStorageMetrics(dummyObjectStorage{}, name) |
| 44 | + gauge := metric.FSObjectStorageOperations.WithLabelValues(name, "write") |
| 45 | + before := testutil.ToFloat64(gauge) |
| 46 | + |
| 47 | + err := wrapped.WriteMultipartParallel(context.Background(), "key", strings.NewReader("data"), nil, nil) |
| 48 | + require.Error(t, err) |
| 49 | + require.True(t, moerr.IsMoErrCode(err, moerr.ErrNotSupported)) |
| 50 | + require.Equal(t, before+1, testutil.ToFloat64(gauge)) |
| 51 | +} |
| 52 | + |
| 53 | +func TestObjectStorageMetricsDelegates(t *testing.T) { |
| 54 | + name := t.Name() |
| 55 | + upstream := &recordingObjectStorage{} |
| 56 | + wrapped := newObjectStorageMetrics(upstream, name) |
| 57 | + |
| 58 | + require.NoError(t, wrapped.Delete(context.Background(), "a")) |
| 59 | + _, _ = wrapped.Exists(context.Background(), "b") |
| 60 | + seq := wrapped.List(context.Background(), "c") |
| 61 | + seq(func(_ *DirEntry, _ error) bool { return true }) |
| 62 | + rc, err := wrapped.Read(context.Background(), "d", nil, nil) |
| 63 | + require.NoError(t, err) |
| 64 | + require.NoError(t, rc.Close()) |
| 65 | + _, _ = wrapped.Stat(context.Background(), "e") |
| 66 | + require.NoError(t, wrapped.Write(context.Background(), "f", strings.NewReader("x"), nil, nil)) |
| 67 | + |
| 68 | + require.ElementsMatch(t, []string{ |
| 69 | + "delete", "exists", "list", "read", "stat", "write", |
| 70 | + }, upstream.calls) |
| 71 | + |
| 72 | + // gauges incremented |
| 73 | + require.True(t, testutil.ToFloat64(metric.FSObjectStorageOperations.WithLabelValues(name, "delete")) >= 1) |
| 74 | + require.True(t, testutil.ToFloat64(metric.FSObjectStorageOperations.WithLabelValues(name, "exists")) >= 1) |
| 75 | + require.True(t, testutil.ToFloat64(metric.FSObjectStorageOperations.WithLabelValues(name, "list")) >= 1) |
| 76 | + require.True(t, testutil.ToFloat64(metric.FSObjectStorageOperations.WithLabelValues(name, "read")) >= 1) |
| 77 | + require.True(t, testutil.ToFloat64(metric.FSObjectStorageOperations.WithLabelValues(name, "stat")) >= 1) |
| 78 | + require.True(t, testutil.ToFloat64(metric.FSObjectStorageOperations.WithLabelValues(name, "write")) >= 1) |
| 79 | +} |
| 80 | + |
| 81 | +func TestObjectStorageMetricsReadCloseDecrementsActive(t *testing.T) { |
| 82 | + name := t.Name() |
| 83 | + upstream := &recordingObjectStorage{} |
| 84 | + wrapped := newObjectStorageMetrics(upstream, name) |
| 85 | + active := metric.FSObjectStorageOperations.WithLabelValues(name, "active-read") |
| 86 | + before := testutil.ToFloat64(active) |
| 87 | + |
| 88 | + r, err := wrapped.Read(context.Background(), "key", nil, nil) |
| 89 | + require.NoError(t, err) |
| 90 | + require.Equal(t, before+1, testutil.ToFloat64(active)) |
| 91 | + require.NoError(t, r.Close()) |
| 92 | + require.Equal(t, before, testutil.ToFloat64(active)) |
| 93 | +} |
0 commit comments