@@ -14,10 +14,12 @@ import (
14
14
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
15
15
"github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree"
16
16
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
17
+ "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
17
18
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
18
19
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
19
20
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
20
21
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
22
+ "github.com/cockroachdb/cockroach/pkg/util/buildutil"
21
23
"github.com/cockroachdb/cockroach/pkg/util/intsets"
22
24
"github.com/cockroachdb/cockroach/pkg/util/tracing"
23
25
"github.com/cockroachdb/errors"
@@ -67,7 +69,7 @@ func (tc *Collection) hydrateDescriptors(
67
69
68
70
// hydrate mutable hydratable descriptors of the slice in-place.
69
71
if ! hydratableMutableIndexes .Empty () {
70
- typeFn := makeMutableTypeLookupFunc (tc , txn , descs )
72
+ typeFn := makeMutableTypeLookupFunc (tc , txn , flags , descs )
71
73
for _ , i := range hydratableMutableIndexes .Ordered () {
72
74
if err := hydrate (ctx , descs [i ], typeFn ); err != nil {
73
75
return err
@@ -108,7 +110,7 @@ func (tc *Collection) hydrateDescriptors(
108
110
}
109
111
110
112
func makeMutableTypeLookupFunc (
111
- tc * Collection , txn * kv.Txn , descs []catalog.Descriptor ,
113
+ tc * Collection , txn * kv.Txn , flags getterFlags , descs []catalog.Descriptor ,
112
114
) typedesc.TypeLookupFunc {
113
115
var mc nstree.MutableCatalog
114
116
for _ , desc := range descs {
@@ -131,7 +133,7 @@ func makeMutableTypeLookupFunc(
131
133
if id == catconstants .PublicSchemaID {
132
134
return schemadesc .GetPublicSchema (), nil
133
135
}
134
- flags := getterFlags {
136
+ f := getterFlags {
135
137
contextFlags : contextFlags {
136
138
isMutable : true ,
137
139
},
@@ -140,9 +142,35 @@ func makeMutableTypeLookupFunc(
140
142
withoutLeased : true ,
141
143
withoutHydration : skipHydration ,
142
144
},
145
+ descFilters : descFilters {
146
+ // For hydration, we will allow offline descriptors for lookups
147
+ // of type descriptors. A descriptor can be offline either due to:
148
+ // 1) IMPORT in which case we are looking at an implicit record type
149
+ // 2) RESTORE which will always pick a new object to restore into, so
150
+ // only the restore code paths can enter here, which will include
151
+ // offline descriptors explicitly.
152
+ withoutOffline : false ,
153
+ },
154
+ }
155
+ g := ByIDGetter (makeGetterBase (txn , tc , f ))
156
+ desc , err := g .Desc (ctx , id )
157
+ if err != nil {
158
+ return nil , err
143
159
}
144
- g := ByIDGetter (makeGetterBase (txn , tc , flags ))
145
- return g .Desc (ctx , id )
160
+ // Sanity: If offline descriptors were asked to be ignored, then we should
161
+ // only observe ones caused by IMPORT.
162
+ if flags .descFilters .withoutOffline &&
163
+ (desc .GetOfflineReason () != "" && desc .GetOfflineReason () != tabledesc .OfflineReasonImporting ) {
164
+ if buildutil .CrdbTestBuild {
165
+ return nil , errors .AssertionFailedf ("unexpected offline descriptor %s(%d): %s" ,
166
+ desc .GetName (),
167
+ desc .GetID (),
168
+ desc .GetOfflineReason ())
169
+ }
170
+ // For release builds surface an offline descriptor error.
171
+ return nil , catalog .FilterOfflineDescriptor (desc )
172
+ }
173
+ return desc , nil
146
174
}
147
175
return makeTypeLookupFuncForHydration (mc , mutableLookupFunc )
148
176
}
@@ -169,11 +197,34 @@ func makeImmutableTypeLookupFunc(
169
197
},
170
198
descFilters : descFilters {
171
199
withoutDropped : true ,
172
- withoutOffline : flags .descFilters .withoutOffline ,
200
+ // For hydration, we will allow offline descriptors for lookups
201
+ // of type descriptors. A descriptor can be offline either due to:
202
+ // 1) IMPORT in which case we are looking at an implicit record type
203
+ // 2) RESTORE which will always pick a new object to restore into, so
204
+ // only the restore code paths can enter here, which will include
205
+ // offline descriptors explicitly.
206
+ withoutOffline : false ,
173
207
},
174
208
}
175
209
g := ByIDGetter (makeGetterBase (txn , tc , f ))
176
- return g .Desc (ctx , id )
210
+ desc , err := g .Desc (ctx , id )
211
+ if err != nil {
212
+ return nil , err
213
+ }
214
+ // Sanity: If offline descriptors were asked to be ignored, then we should
215
+ // only observe ones caused by IMPORT.
216
+ if flags .descFilters .withoutOffline &&
217
+ (desc .GetOfflineReason () != "" && desc .GetOfflineReason () != tabledesc .OfflineReasonImporting ) {
218
+ if buildutil .CrdbTestBuild {
219
+ return nil , errors .AssertionFailedf ("unexpected offline descriptor %s(%d): %s" ,
220
+ desc .GetName (),
221
+ desc .GetID (),
222
+ desc .GetOfflineReason ())
223
+ }
224
+ // For release builds surface an offline descriptor error.
225
+ return nil , catalog .FilterOfflineDescriptor (desc )
226
+ }
227
+ return desc , nil
177
228
}
178
229
return makeTypeLookupFuncForHydration (mc , immutableLookupFunc )
179
230
}
0 commit comments