@@ -10,6 +10,7 @@ import (
10
10
"time"
11
11
12
12
"github.com/pkg/errors"
13
+ "github.com/scylladb/gocqlx/v2/qb"
13
14
"github.com/scylladb/scylla-manager/v3/pkg/metrics"
14
15
"github.com/scylladb/scylla-manager/v3/pkg/scyllaclient"
15
16
"github.com/scylladb/scylla-manager/v3/pkg/util/query"
@@ -71,71 +72,35 @@ func (w *worker) dropView(ctx context.Context, view View) error {
71
72
}
72
73
73
74
func (w * worker ) getViews (ctx context.Context , workload []hostWorkload ) ([]View , error ) {
74
- keyspaces , err := w .client .Keyspaces (ctx )
75
- if err != nil {
76
- return nil , errors .Wrap (err , "get keyspaces" )
77
- }
78
-
79
- describedViews , err := w .viewsSchemaByName (ctx , workload )
75
+ tablesToRestore := getTablesToRestore (workload )
76
+ views , err := w .viewsFromSchema (ctx , workload )
80
77
if err != nil {
81
- return nil , err
78
+ return nil , errors . Wrap ( err , "get views from schema" )
82
79
}
83
-
84
- tablesToRestore := getTablesToRestore (workload )
85
-
86
- var views []View
87
- for _ , ks := range keyspaces {
88
- meta , err := w .clusterSession .KeyspaceMetadata (ks )
89
- if err != nil {
90
- return nil , errors .Wrapf (err , "get keyspace %s metadata" , ks )
91
- }
92
-
93
- for _ , index := range meta .Indexes {
94
- if _ , ok := tablesToRestore [scyllaTable {keyspace : index .KeyspaceName , table : index .TableName }]; ! ok {
95
- continue
96
- }
97
- stmt , ok := describedViews [scyllaTable {keyspace : index .KeyspaceName , table : index .Name + "_index" }]
98
- if ! ok {
99
- continue
100
- }
101
- stmt , err = addIfNotExists (stmt , SecondaryIndex )
102
- if err != nil {
103
- return nil , err
104
- }
105
-
106
- views = append (views , View {
107
- Keyspace : index .KeyspaceName ,
108
- View : index .Name ,
109
- Type : SecondaryIndex ,
110
- BaseTable : index .TableName ,
111
- CreateStmt : stmt ,
112
- })
80
+ result := make ([]View , 0 , len (views ))
81
+ for _ , view := range views {
82
+ if _ , ok := tablesToRestore [scyllaTable {keyspace : view .Keyspace , table : view .BaseTable }]; ! ok {
83
+ continue
113
84
}
85
+ result = append (result , view )
86
+ }
114
87
115
- for _ , view := range meta .Views {
116
- if _ , ok := tablesToRestore [scyllaTable {keyspace : view .KeyspaceName , table : view .BaseTableName }]; ! ok {
117
- continue
118
- }
119
- stmt , ok := describedViews [scyllaTable {keyspace : view .KeyspaceName , table : view .ViewName }]
120
- if ! ok {
121
- continue
122
- }
123
- stmt , err = addIfNotExists (stmt , MaterializedView )
124
- if err != nil {
125
- return nil , err
126
- }
88
+ return result , nil
89
+ }
127
90
128
- views = append (views , View {
129
- Keyspace : view .KeyspaceName ,
130
- View : view .ViewName ,
131
- Type : MaterializedView ,
132
- BaseTable : view .BaseTableName ,
133
- CreateStmt : stmt ,
134
- })
135
- }
91
+ func (w * worker ) getBaseTableName (keyspace , name string ) (string , error ) {
92
+ q := qb .Select ("system_schema.views" ).
93
+ Columns ("base_table_name" ).
94
+ Where (qb .Eq ("keyspace_name" ), qb .Eq ("view_name" )).
95
+ Query (w .clusterSession ).
96
+ Bind (keyspace , name )
97
+ defer q .Release ()
98
+ var baseTableName string
99
+ err := q .Scan (& baseTableName )
100
+ if err != nil {
101
+ return "" , errors .Wrapf (err , "scan base table name for view %s.%s" , keyspace , name )
136
102
}
137
-
138
- return views , nil
103
+ return baseTableName , nil
139
104
}
140
105
141
106
var (
@@ -206,24 +171,19 @@ func (w *worker) createView(ctx context.Context, view View) error {
206
171
// Scylla operation might take a really long (and difficult to estimate) time.
207
172
// This func exits ONLY on: success, context cancel or error.
208
173
func (w * worker ) waitForViewBuilding (ctx context.Context , view View , pr * RunViewProgress ) error {
209
- viewTableName := view .View
210
- if view .Type == SecondaryIndex {
211
- viewTableName += "_index"
212
- }
213
-
214
- status , err := w .client .ViewBuildStatus (ctx , view .Keyspace , viewTableName )
174
+ status , err := w .client .ViewBuildStatus (ctx , view .Keyspace , view .View )
215
175
if err != nil {
216
176
return err
217
177
}
218
178
219
179
w .updateReCreateViewProgress (ctx , pr , status )
220
180
switch status {
221
181
case scyllaclient .StatusUnknown :
222
- w .metrics .SetViewBuildStatus (w .runInfo .ClusterID , view .Keyspace , viewTableName , metrics .BuildStatusUnknown )
182
+ w .metrics .SetViewBuildStatus (w .runInfo .ClusterID , view .Keyspace , view . View , metrics .BuildStatusUnknown )
223
183
case scyllaclient .StatusStarted :
224
- w .metrics .SetViewBuildStatus (w .runInfo .ClusterID , view .Keyspace , viewTableName , metrics .BuildStatusStarted )
184
+ w .metrics .SetViewBuildStatus (w .runInfo .ClusterID , view .Keyspace , view . View , metrics .BuildStatusStarted )
225
185
case scyllaclient .StatusSuccess :
226
- w .metrics .SetViewBuildStatus (w .runInfo .ClusterID , view .Keyspace , viewTableName , metrics .BuildStatusSuccess )
186
+ w .metrics .SetViewBuildStatus (w .runInfo .ClusterID , view .Keyspace , view . View , metrics .BuildStatusSuccess )
227
187
return nil
228
188
}
229
189
@@ -245,7 +205,7 @@ func (w *worker) waitForViewBuilding(ctx context.Context, view View, pr *RunView
245
205
return nil
246
206
}
247
207
248
- func (w * worker ) viewsSchemaByName (ctx context.Context , workload []hostWorkload ) (map [ scyllaTable ] string , error ) {
208
+ func (w * worker ) viewsFromSchema (ctx context.Context , workload []hostWorkload ) ([] View , error ) {
249
209
// In order to ensure schema consistency, we need to call raft read barrier on a host
250
210
// from which we are going to read the schema.
251
211
host := workload [0 ].host
@@ -272,7 +232,7 @@ func (w *worker) viewsSchemaByName(ctx context.Context, workload []hostWorkload)
272
232
if err != nil {
273
233
return nil , errors .Wrap (err , "describe schema" )
274
234
}
275
- result := map [ scyllaTable ] string {}
235
+ var result [] View
276
236
for _ , stmt := range describedSchema {
277
237
if stmt .Keyspace == "" {
278
238
continue
@@ -281,7 +241,26 @@ func (w *worker) viewsSchemaByName(ctx context.Context, workload []hostWorkload)
281
241
continue
282
242
}
283
243
284
- result [scyllaTable {keyspace : stmt .Keyspace , table : stmt .Name }] = stmt .CQLStmt
244
+ viewType := MaterializedView
245
+ if stmt .Type == "index" {
246
+ viewType = SecondaryIndex
247
+ }
248
+ baseTableName , err := w .getBaseTableName (stmt .Keyspace , stmt .Name )
249
+ if err != nil {
250
+ return nil , errors .Wrapf (err , "get base table name for view %s.%s" , stmt .Keyspace , stmt .Name )
251
+ }
252
+ createStmt , err := addIfNotExists (stmt .CQLStmt , viewType )
253
+ if err != nil {
254
+ return nil , err
255
+ }
256
+ result = append (result , View {
257
+ Keyspace : stmt .Keyspace ,
258
+ View : stmt .Name ,
259
+ Type : viewType ,
260
+ BaseTable : baseTableName ,
261
+ CreateStmt : createStmt ,
262
+ BuildStatus : scyllaclient .StatusUnknown , // We don't know the build status yet.
263
+ })
285
264
}
286
265
return result , nil
287
266
}
0 commit comments