github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/jobs/jobspb/wrap.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package jobspb 12 13 import ( 14 "fmt" 15 "strings" 16 17 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 18 "github.com/cockroachdb/cockroach/pkg/sql/stats" 19 ) 20 21 // Details is a marker interface for job details proto structs. 22 type Details interface{} 23 24 var _ Details = BackupDetails{} 25 var _ Details = RestoreDetails{} 26 var _ Details = SchemaChangeDetails{} 27 var _ Details = ChangefeedDetails{} 28 var _ Details = CreateStatsDetails{} 29 var _ Details = SchemaChangeGCDetails{} 30 31 // ProgressDetails is a marker interface for job progress details proto structs. 32 type ProgressDetails interface{} 33 34 var _ ProgressDetails = BackupProgress{} 35 var _ ProgressDetails = RestoreProgress{} 36 var _ ProgressDetails = SchemaChangeProgress{} 37 var _ ProgressDetails = ChangefeedProgress{} 38 var _ ProgressDetails = CreateStatsProgress{} 39 var _ ProgressDetails = SchemaChangeGCProgress{} 40 41 // Type returns the payload's job type. 42 func (p *Payload) Type() Type { 43 return DetailsType(p.Details) 44 } 45 46 // DetailsType returns the type for a payload detail. 47 func DetailsType(d isPayload_Details) Type { 48 switch d := d.(type) { 49 case *Payload_Backup: 50 return TypeBackup 51 case *Payload_Restore: 52 return TypeRestore 53 case *Payload_SchemaChange: 54 return TypeSchemaChange 55 case *Payload_Import: 56 return TypeImport 57 case *Payload_Changefeed: 58 return TypeChangefeed 59 case *Payload_CreateStats: 60 createStatsName := d.CreateStats.Name 61 if createStatsName == stats.AutoStatsName { 62 return TypeAutoCreateStats 63 } 64 return TypeCreateStats 65 case *Payload_SchemaChangeGC: 66 return TypeSchemaChangeGC 67 default: 68 panic(fmt.Sprintf("Payload.Type called on a payload with an unknown details type: %T", d)) 69 } 70 } 71 72 // WrapProgressDetails wraps a ProgressDetails object in the protobuf wrapper 73 // struct necessary to make it usable as the Details field of a Progress. 74 // 75 // Providing an unknown details type indicates programmer error and so causes a 76 // panic. 77 func WrapProgressDetails(details ProgressDetails) interface { 78 isProgress_Details 79 } { 80 switch d := details.(type) { 81 case BackupProgress: 82 return &Progress_Backup{Backup: &d} 83 case RestoreProgress: 84 return &Progress_Restore{Restore: &d} 85 case SchemaChangeProgress: 86 return &Progress_SchemaChange{SchemaChange: &d} 87 case ImportProgress: 88 return &Progress_Import{Import: &d} 89 case ChangefeedProgress: 90 return &Progress_Changefeed{Changefeed: &d} 91 case CreateStatsProgress: 92 return &Progress_CreateStats{CreateStats: &d} 93 case SchemaChangeGCProgress: 94 return &Progress_SchemaChangeGC{SchemaChangeGC: &d} 95 default: 96 panic(fmt.Sprintf("WrapProgressDetails: unknown details type %T", d)) 97 } 98 } 99 100 // UnwrapDetails returns the details object stored within the payload's Details 101 // field, discarding the protobuf wrapper struct. 102 func (p *Payload) UnwrapDetails() Details { 103 switch d := p.Details.(type) { 104 case *Payload_Backup: 105 return *d.Backup 106 case *Payload_Restore: 107 return *d.Restore 108 case *Payload_SchemaChange: 109 return *d.SchemaChange 110 case *Payload_Import: 111 return *d.Import 112 case *Payload_Changefeed: 113 return *d.Changefeed 114 case *Payload_CreateStats: 115 return *d.CreateStats 116 case *Payload_SchemaChangeGC: 117 return *d.SchemaChangeGC 118 default: 119 return nil 120 } 121 } 122 123 // UnwrapDetails returns the details object stored within the progress' Details 124 // field, discarding the protobuf wrapper struct. 125 func (p *Progress) UnwrapDetails() ProgressDetails { 126 switch d := p.Details.(type) { 127 case *Progress_Backup: 128 return *d.Backup 129 case *Progress_Restore: 130 return *d.Restore 131 case *Progress_SchemaChange: 132 return *d.SchemaChange 133 case *Progress_Import: 134 return *d.Import 135 case *Progress_Changefeed: 136 return *d.Changefeed 137 case *Progress_CreateStats: 138 return *d.CreateStats 139 case *Progress_SchemaChangeGC: 140 return *d.SchemaChangeGC 141 default: 142 return nil 143 } 144 } 145 146 func (t Type) String() string { 147 // Protobufs, by convention, use CAPITAL_SNAKE_CASE for enum identifiers. 148 // Since Type's string representation is used as a SHOW JOBS output column, we 149 // simply swap underscores for spaces in the identifier for very SQL-esque 150 // names, like "BACKUP" and "SCHEMA CHANGE". 151 return strings.Replace(Type_name[int32(t)], "_", " ", -1) 152 } 153 154 // WrapPayloadDetails wraps a Details object in the protobuf wrapper struct 155 // necessary to make it usable as the Details field of a Payload. 156 // 157 // Providing an unknown details type indicates programmer error and so causes a 158 // panic. 159 func WrapPayloadDetails(details Details) interface { 160 isPayload_Details 161 } { 162 switch d := details.(type) { 163 case BackupDetails: 164 return &Payload_Backup{Backup: &d} 165 case RestoreDetails: 166 return &Payload_Restore{Restore: &d} 167 case SchemaChangeDetails: 168 return &Payload_SchemaChange{SchemaChange: &d} 169 case ImportDetails: 170 return &Payload_Import{Import: &d} 171 case ChangefeedDetails: 172 return &Payload_Changefeed{Changefeed: &d} 173 case CreateStatsDetails: 174 return &Payload_CreateStats{CreateStats: &d} 175 case SchemaChangeGCDetails: 176 return &Payload_SchemaChangeGC{SchemaChangeGC: &d} 177 default: 178 panic(fmt.Sprintf("jobs.WrapPayloadDetails: unknown details type %T", d)) 179 } 180 } 181 182 // ChangefeedTargets is a set of id targets with metadata. 183 type ChangefeedTargets map[sqlbase.ID]ChangefeedTarget 184 185 // SchemaChangeDetailsFormatVersion is the format version for 186 // SchemaChangeDetails. 187 type SchemaChangeDetailsFormatVersion uint32 188 189 const ( 190 // BaseFormatVersion corresponds to the initial version of 191 // SchemaChangeDetails, intended for the original version of schema change 192 // jobs which were meant to be updated by a SchemaChanger instead of being run 193 // as jobs by the job registry. 194 BaseFormatVersion SchemaChangeDetailsFormatVersion = iota 195 // JobResumerFormatVersion corresponds to the introduction of the schema 196 // change job resumer. This version introduces the TableID and MutationID 197 // fields, and, more generally, flags the job as being suitable for the job 198 // registry to adopt. 199 JobResumerFormatVersion 200 201 // Silence unused warning. 202 _ = BaseFormatVersion 203 )