|
| 1 | +/* |
| 2 | +Copyright 2025 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package topo_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + topodatapb "vitess.io/vitess/go/vt/proto/topodata" |
| 26 | + "vitess.io/vitess/go/vt/topo" |
| 27 | + "vitess.io/vitess/go/vt/topo/memorytopo" |
| 28 | +) |
| 29 | + |
| 30 | +func TestDeleteOrphanedKeyspaceFiles(t *testing.T) { |
| 31 | + cell := "zone-1" |
| 32 | + cell2 := "zone-2" |
| 33 | + keyspace := "ks" |
| 34 | + keyspace2 := "ks2" |
| 35 | + ctx := context.Background() |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + setup func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) |
| 39 | + verify func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "No orphaned files to delete", |
| 43 | + setup: func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) { |
| 44 | + // We don't create anything that needs to be cleared. |
| 45 | + }, |
| 46 | + verify: func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) { |
| 47 | + // We check that we only get one ListDir call. |
| 48 | + require.EqualValues(t, 1, mtf.GetCallStats().Counts()["ListDir"]) |
| 49 | + }, |
| 50 | + }, { |
| 51 | + name: "Single orphaned file to delete", |
| 52 | + setup: func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) { |
| 53 | + err := ts.UpdateShardReplicationFields(ctx, cell, keyspace, "0", func(sr *topodatapb.ShardReplication) error { |
| 54 | + sr.Nodes = append(sr.Nodes, &topodatapb.ShardReplication_Node{TabletAlias: &topodatapb.TabletAlias{Cell: cell, Uid: 0}}) |
| 55 | + return nil |
| 56 | + }) |
| 57 | + require.NoError(t, err) |
| 58 | + // Verify that getting srvKeyspaceNames now gives us this keyspace. |
| 59 | + keyspaces, err := ts.GetSrvKeyspaceNames(ctx, cell) |
| 60 | + require.NoError(t, err) |
| 61 | + require.EqualValues(t, 1, len(keyspaces)) |
| 62 | + require.EqualValues(t, keyspace, keyspaces[0]) |
| 63 | + // Also verify that we can't get the SrvKeyspace. |
| 64 | + _, err = ts.GetSrvKeyspace(ctx, cell, keyspace) |
| 65 | + require.Error(t, err) |
| 66 | + require.True(t, topo.IsErrType(err, topo.NoNode)) |
| 67 | + mtf.GetCallStats().ResetAll() |
| 68 | + }, |
| 69 | + verify: func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) { |
| 70 | + // We check that we only get 3 ListDir calls - |
| 71 | + // - keyspaces/ks |
| 72 | + // - keyspaces/ks/shards |
| 73 | + // - keyspaces/ks/shards/0 |
| 74 | + require.EqualValues(t, 3, mtf.GetCallStats().Counts()["ListDir"]) |
| 75 | + // We check that we delete the shard replication file |
| 76 | + // - keyspaces/ks/shards/0/ShardReplication |
| 77 | + require.EqualValues(t, 1, mtf.GetCallStats().Counts()["Delete"]) |
| 78 | + // Verify that getting srvKeyspaceNames is now empty. |
| 79 | + keyspaces, err := ts.GetSrvKeyspaceNames(ctx, cell) |
| 80 | + require.NoError(t, err) |
| 81 | + require.Len(t, keyspaces, 0) |
| 82 | + }, |
| 83 | + }, { |
| 84 | + name: "Ensure we don't delete extra files", |
| 85 | + setup: func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) { |
| 86 | + err := ts.UpdateShardReplicationFields(ctx, cell, keyspace, "0", func(sr *topodatapb.ShardReplication) error { |
| 87 | + sr.Nodes = append(sr.Nodes, &topodatapb.ShardReplication_Node{TabletAlias: &topodatapb.TabletAlias{Cell: cell, Uid: 0}}) |
| 88 | + return nil |
| 89 | + }) |
| 90 | + require.NoError(t, err) |
| 91 | + err = ts.CreateKeyspace(ctx, keyspace2, &topodatapb.Keyspace{KeyspaceType: topodatapb.KeyspaceType_NORMAL}) |
| 92 | + require.NoError(t, err) |
| 93 | + err = ts.UpdateShardReplicationFields(ctx, cell, keyspace2, "0", func(sr *topodatapb.ShardReplication) error { |
| 94 | + sr.Nodes = append(sr.Nodes, &topodatapb.ShardReplication_Node{TabletAlias: &topodatapb.TabletAlias{Cell: cell, Uid: 0}}) |
| 95 | + return nil |
| 96 | + }) |
| 97 | + require.NoError(t, err) |
| 98 | + }, |
| 99 | + verify: func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) { |
| 100 | + // Verify that we don't delete the files for the other keyspace. |
| 101 | + _, err := ts.GetKeyspace(ctx, keyspace2) |
| 102 | + require.NoError(t, err) |
| 103 | + _, err = ts.GetShardReplication(ctx, cell, keyspace2, "0") |
| 104 | + require.NoError(t, err) |
| 105 | + }, |
| 106 | + }, { |
| 107 | + name: "Multiple orphaned files to delete", |
| 108 | + setup: func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) { |
| 109 | + err := ts.UpdateShardReplicationFields(ctx, cell, keyspace, "0", func(sr *topodatapb.ShardReplication) error { |
| 110 | + sr.Nodes = append(sr.Nodes, &topodatapb.ShardReplication_Node{TabletAlias: &topodatapb.TabletAlias{Cell: cell, Uid: 0}}) |
| 111 | + return nil |
| 112 | + }) |
| 113 | + require.NoError(t, err) |
| 114 | + err = ts.UpdateSrvKeyspace(ctx, cell, keyspace, &topodatapb.SrvKeyspace{ |
| 115 | + Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{ |
| 116 | + { |
| 117 | + ServedType: topodatapb.TabletType_PRIMARY, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }) |
| 121 | + require.NoError(t, err) |
| 122 | + err = ts.UpdateSrvKeyspace(ctx, cell2, keyspace, &topodatapb.SrvKeyspace{ |
| 123 | + Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{ |
| 124 | + { |
| 125 | + ServedType: topodatapb.TabletType_PRIMARY, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }) |
| 129 | + require.NoError(t, err) |
| 130 | + // Verify that getting srvKeyspaceNames now gives us this keyspace. |
| 131 | + keyspaces, err := ts.GetSrvKeyspaceNames(ctx, cell) |
| 132 | + require.NoError(t, err) |
| 133 | + require.EqualValues(t, 1, len(keyspaces)) |
| 134 | + require.EqualValues(t, keyspace, keyspaces[0]) |
| 135 | + // Also verify that we can't get the SrvKeyspace. |
| 136 | + _, err = ts.GetSrvKeyspace(ctx, cell, keyspace) |
| 137 | + require.NoError(t, err) |
| 138 | + mtf.GetCallStats().ResetAll() |
| 139 | + }, |
| 140 | + verify: func(t *testing.T, ts *topo.Server, mtf *memorytopo.Factory) { |
| 141 | + // We check that we only get 3 ListDir calls - |
| 142 | + // - keyspaces/ks |
| 143 | + // - keyspaces/ks/shards |
| 144 | + // - keyspaces/ks/shards/0 |
| 145 | + require.EqualValues(t, 3, mtf.GetCallStats().Counts()["ListDir"]) |
| 146 | + // We check that we delete the shard replication file |
| 147 | + // - keyspaces/ks/shards/0/ShardReplication |
| 148 | + // - keyspaces/ks/SrvKeyspace |
| 149 | + require.EqualValues(t, 2, mtf.GetCallStats().Counts()["Delete"]) |
| 150 | + // Verify that getting srvKeyspaceNames is now empty. |
| 151 | + keyspaces, err := ts.GetSrvKeyspaceNames(ctx, cell) |
| 152 | + require.NoError(t, err) |
| 153 | + require.Len(t, keyspaces, 0) |
| 154 | + // Since we only clear orphaned files for one cell, we should still see |
| 155 | + // they srvKeyspace in the other cell. |
| 156 | + keyspaces, err = ts.GetSrvKeyspaceNames(ctx, cell2) |
| 157 | + require.NoError(t, err) |
| 158 | + require.Len(t, keyspaces, 1) |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + for _, tt := range tests { |
| 163 | + t.Run(tt.name, func(t *testing.T) { |
| 164 | + ts, mtf := memorytopo.NewServerAndFactory(ctx, cell, cell2) |
| 165 | + tt.setup(t, ts, mtf) |
| 166 | + err := ts.DeleteOrphanedKeyspaceFiles(ctx, cell, keyspace) |
| 167 | + require.NoError(t, err) |
| 168 | + tt.verify(t, ts, mtf) |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
0 commit comments