|
| 1 | +// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. |
| 2 | + |
| 3 | +package restore |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "sync" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/jarcoal/httpmock" |
| 13 | + "github.com/pingcap/kvproto/pkg/import_sstpb" |
| 14 | + "github.com/pingcap/kvproto/pkg/kvrpcpb" |
| 15 | + "github.com/pingcap/kvproto/pkg/metapb" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + pd "github.com/tikv/pd/client" |
| 18 | + "google.golang.org/grpc/keepalive" |
| 19 | +) |
| 20 | + |
| 21 | +var defaultKeepaliveCfg = keepalive.ClientParameters{ |
| 22 | + Time: 3 * time.Second, |
| 23 | + Timeout: 10 * time.Second, |
| 24 | +} |
| 25 | + |
| 26 | +type fakePDClient struct { |
| 27 | + pd.Client |
| 28 | + stores []*metapb.Store |
| 29 | +} |
| 30 | + |
| 31 | +func (fpdc fakePDClient) GetAllStores(context.Context, ...pd.GetStoreOption) ([]*metapb.Store, error) { |
| 32 | + return append([]*metapb.Store{}, fpdc.stores...), nil |
| 33 | +} |
| 34 | + |
| 35 | +// Mock ImporterClient interface |
| 36 | +type FakeImporterClient struct { |
| 37 | + ImporterClient |
| 38 | +} |
| 39 | + |
| 40 | +// Record the stores that have communicated |
| 41 | +type RecordStores struct { |
| 42 | + mu sync.Mutex |
| 43 | + stores map[uint64]uint64 |
| 44 | +} |
| 45 | + |
| 46 | +func NewRecordStores() RecordStores { |
| 47 | + return RecordStores{stores: make(map[uint64]uint64, 0)} |
| 48 | +} |
| 49 | + |
| 50 | +func (r *RecordStores) put(id uint64, rateLimit uint64) { |
| 51 | + r.mu.Lock() |
| 52 | + defer r.mu.Unlock() |
| 53 | + r.stores[id] = rateLimit |
| 54 | +} |
| 55 | + |
| 56 | +func (r *RecordStores) len() int { |
| 57 | + r.mu.Lock() |
| 58 | + defer r.mu.Unlock() |
| 59 | + return len(r.stores) |
| 60 | +} |
| 61 | + |
| 62 | +func (r *RecordStores) get(id uint64) uint64 { |
| 63 | + r.mu.Lock() |
| 64 | + defer r.mu.Unlock() |
| 65 | + return r.stores[id] |
| 66 | +} |
| 67 | + |
| 68 | +func (r *RecordStores) toString() string { |
| 69 | + r.mu.Lock() |
| 70 | + defer r.mu.Unlock() |
| 71 | + return fmt.Sprintf("%v", r.stores) |
| 72 | +} |
| 73 | + |
| 74 | +var recordStores RecordStores |
| 75 | + |
| 76 | +const ( |
| 77 | + WORKING_TIME = 10 |
| 78 | +) |
| 79 | + |
| 80 | +func (fakeImportCli FakeImporterClient) SetDownloadSpeedLimit( |
| 81 | + ctx context.Context, |
| 82 | + storeID uint64, |
| 83 | + req *import_sstpb.SetDownloadSpeedLimitRequest, |
| 84 | +) (*import_sstpb.SetDownloadSpeedLimitResponse, error) { |
| 85 | + time.Sleep(WORKING_TIME * time.Millisecond) // simulate doing 100 ms work |
| 86 | + recordStores.put(storeID, req.SpeedLimit) |
| 87 | + return nil, nil |
| 88 | +} |
| 89 | + |
| 90 | +func TestSetSpeedLimit(t *testing.T) { |
| 91 | + mockStores := []*metapb.Store{ |
| 92 | + {Id: 1}, |
| 93 | + {Id: 2}, |
| 94 | + {Id: 3}, |
| 95 | + {Id: 4}, |
| 96 | + {Id: 5}, |
| 97 | + {Id: 6}, |
| 98 | + {Id: 7}, |
| 99 | + {Id: 8}, |
| 100 | + {Id: 9}, |
| 101 | + {Id: 10}, |
| 102 | + } |
| 103 | + httpmock.Activate() |
| 104 | + defer httpmock.DeactivateAndReset() |
| 105 | + // Exact URL match |
| 106 | + httpmock.RegisterResponder("GET", `=~^/config`, |
| 107 | + httpmock.NewStringResponder(200, `{"storage":{"api-version":2, "enable-ttl":true}}`)) |
| 108 | + // 1. The cost of concurrent communication is expected to be less than the cost of serial communication. |
| 109 | + client, err := NewRestoreClient(fakePDClient{ |
| 110 | + stores: mockStores, |
| 111 | + }, nil, defaultKeepaliveCfg, true) |
| 112 | + require.NoError(t, err) |
| 113 | + client.fileImporter = NewFileImporter(nil, FakeImporterClient{}, nil, true, kvrpcpb.APIVersion_V2) |
| 114 | + ctx := context.Background() |
| 115 | + |
| 116 | + rateLimit := uint64(10) |
| 117 | + recordStores = NewRecordStores() |
| 118 | + start := time.Now() |
| 119 | + err = client.setSpeedLimit(ctx, rateLimit) |
| 120 | + cost := time.Since(start) |
| 121 | + require.NoError(t, err) |
| 122 | + |
| 123 | + t.Logf("Total Cost: %v\n", cost) |
| 124 | + t.Logf("Has Communicated: %v\n", recordStores.toString()) |
| 125 | + |
| 126 | + serialCost := time.Duration(len(mockStores)*WORKING_TIME) * time.Millisecond |
| 127 | + require.LessOrEqual(t, serialCost, cost) |
| 128 | + require.Equal(t, len(mockStores), recordStores.len()) |
| 129 | + for i := 0; i < len(mockStores); i++ { |
| 130 | + require.Equal(t, rateLimit, recordStores.get(mockStores[i].Id)) |
| 131 | + } |
| 132 | + |
| 133 | + recordStores = NewRecordStores() |
| 134 | + start = time.Now() |
| 135 | + err = client.resetSpeedLimit(ctx) |
| 136 | + cost = time.Since(start) |
| 137 | + require.NoError(t, err) |
| 138 | + require.LessOrEqual(t, serialCost, cost) |
| 139 | + require.Equal(t, len(mockStores), recordStores.len()) |
| 140 | + for i := 0; i < len(mockStores); i++ { |
| 141 | + require.Equal(t, uint64(0), recordStores.get(mockStores[i].Id)) |
| 142 | + } |
| 143 | +} |
0 commit comments