github.com/hernad/nomad@v1.6.112/nomad/state/state_store_restore.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package state 5 6 import ( 7 "fmt" 8 9 "github.com/hernad/nomad/nomad/structs" 10 ) 11 12 // StateRestore is used to optimize the performance when restoring state by 13 // only using a single large transaction instead of thousands of sub 14 // transactions. 15 type StateRestore struct { 16 txn *txn 17 } 18 19 // Abort is used to abort the restore operation 20 func (r *StateRestore) Abort() { 21 r.txn.Abort() 22 } 23 24 // Commit is used to commit the restore operation 25 func (r *StateRestore) Commit() error { 26 return r.txn.Commit() 27 } 28 29 // NodeRestore is used to restore a node 30 func (r *StateRestore) NodeRestore(node *structs.Node) error { 31 if err := r.txn.Insert("nodes", node); err != nil { 32 return fmt.Errorf("node insert failed: %v", err) 33 } 34 return nil 35 } 36 37 // NodePoolRestore is used to restore a node pool 38 func (r *StateRestore) NodePoolRestore(pool *structs.NodePool) error { 39 if err := r.txn.Insert(TableNodePools, pool); err != nil { 40 return fmt.Errorf("node pool insert failed: %v", err) 41 } 42 return nil 43 } 44 45 // JobRestore is used to restore a job 46 func (r *StateRestore) JobRestore(job *structs.Job) error { 47 48 // When upgrading a cluster pre to post 1.6, the existing jobs will not 49 // have a node pool set. Inserting this into the table will fail, as this 50 // is indexed and cannot be empty. 51 // 52 // This cannot happen within the job canonicalize function, as it would 53 // break the node pools and governance feature. 54 if job.NodePool == "" { 55 job.NodePool = structs.NodePoolDefault 56 } 57 58 if err := r.txn.Insert("jobs", job); err != nil { 59 return fmt.Errorf("job insert failed: %v", err) 60 } 61 return nil 62 } 63 64 // EvalRestore is used to restore an evaluation 65 func (r *StateRestore) EvalRestore(eval *structs.Evaluation) error { 66 if err := r.txn.Insert("evals", eval); err != nil { 67 return fmt.Errorf("eval insert failed: %v", err) 68 } 69 return nil 70 } 71 72 // AllocRestore is used to restore an allocation 73 func (r *StateRestore) AllocRestore(alloc *structs.Allocation) error { 74 if err := r.txn.Insert("allocs", alloc); err != nil { 75 return fmt.Errorf("alloc insert failed: %v", err) 76 } 77 return nil 78 } 79 80 // IndexRestore is used to restore an index 81 func (r *StateRestore) IndexRestore(idx *IndexEntry) error { 82 if err := r.txn.Insert("index", idx); err != nil { 83 return fmt.Errorf("index insert failed: %v", err) 84 } 85 return nil 86 } 87 88 // PeriodicLaunchRestore is used to restore a periodic launch. 89 func (r *StateRestore) PeriodicLaunchRestore(launch *structs.PeriodicLaunch) error { 90 if err := r.txn.Insert("periodic_launch", launch); err != nil { 91 return fmt.Errorf("periodic launch insert failed: %v", err) 92 } 93 return nil 94 } 95 96 // JobSummaryRestore is used to restore a job summary 97 func (r *StateRestore) JobSummaryRestore(jobSummary *structs.JobSummary) error { 98 if err := r.txn.Insert("job_summary", jobSummary); err != nil { 99 return fmt.Errorf("job summary insert failed: %v", err) 100 } 101 return nil 102 } 103 104 // JobVersionRestore is used to restore a job version 105 func (r *StateRestore) JobVersionRestore(version *structs.Job) error { 106 if err := r.txn.Insert("job_version", version); err != nil { 107 return fmt.Errorf("job version insert failed: %v", err) 108 } 109 return nil 110 } 111 112 // DeploymentRestore is used to restore a deployment 113 func (r *StateRestore) DeploymentRestore(deployment *structs.Deployment) error { 114 if err := r.txn.Insert("deployment", deployment); err != nil { 115 return fmt.Errorf("deployment insert failed: %v", err) 116 } 117 return nil 118 } 119 120 // VaultAccessorRestore is used to restore a vault accessor 121 func (r *StateRestore) VaultAccessorRestore(accessor *structs.VaultAccessor) error { 122 if err := r.txn.Insert("vault_accessors", accessor); err != nil { 123 return fmt.Errorf("vault accessor insert failed: %v", err) 124 } 125 return nil 126 } 127 128 // SITokenAccessorRestore is used to restore an SI token accessor 129 func (r *StateRestore) SITokenAccessorRestore(accessor *structs.SITokenAccessor) error { 130 if err := r.txn.Insert(siTokenAccessorTable, accessor); err != nil { 131 return fmt.Errorf("si token accessor insert failed: %w", err) 132 } 133 return nil 134 } 135 136 // ACLPolicyRestore is used to restore an ACL policy 137 func (r *StateRestore) ACLPolicyRestore(policy *structs.ACLPolicy) error { 138 if err := r.txn.Insert("acl_policy", policy); err != nil { 139 return fmt.Errorf("inserting acl policy failed: %v", err) 140 } 141 return nil 142 } 143 144 // ACLTokenRestore is used to restore an ACL token 145 func (r *StateRestore) ACLTokenRestore(token *structs.ACLToken) error { 146 if err := r.txn.Insert("acl_token", token); err != nil { 147 return fmt.Errorf("inserting acl token failed: %v", err) 148 } 149 return nil 150 } 151 152 // OneTimeTokenRestore is used to restore a one-time token 153 func (r *StateRestore) OneTimeTokenRestore(token *structs.OneTimeToken) error { 154 if err := r.txn.Insert("one_time_token", token); err != nil { 155 return fmt.Errorf("inserting one-time token failed: %v", err) 156 } 157 return nil 158 } 159 160 func (r *StateRestore) SchedulerConfigRestore(schedConfig *structs.SchedulerConfiguration) error { 161 if err := r.txn.Insert("scheduler_config", schedConfig); err != nil { 162 return fmt.Errorf("inserting scheduler config failed: %s", err) 163 } 164 return nil 165 } 166 167 func (r *StateRestore) ClusterMetadataRestore(meta *structs.ClusterMetadata) error { 168 if err := r.txn.Insert("cluster_meta", meta); err != nil { 169 return fmt.Errorf("inserting cluster meta failed: %v", err) 170 } 171 return nil 172 } 173 174 // ScalingPolicyRestore is used to restore a scaling policy 175 func (r *StateRestore) ScalingPolicyRestore(scalingPolicy *structs.ScalingPolicy) error { 176 if err := r.txn.Insert("scaling_policy", scalingPolicy); err != nil { 177 return fmt.Errorf("scaling policy insert failed: %v", err) 178 } 179 return nil 180 } 181 182 // CSIPluginRestore is used to restore a CSI plugin 183 func (r *StateRestore) CSIPluginRestore(plugin *structs.CSIPlugin) error { 184 if err := r.txn.Insert("csi_plugins", plugin); err != nil { 185 return fmt.Errorf("csi plugin insert failed: %v", err) 186 } 187 return nil 188 } 189 190 // CSIVolumeRestore is used to restore a CSI volume 191 func (r *StateRestore) CSIVolumeRestore(volume *structs.CSIVolume) error { 192 if err := r.txn.Insert("csi_volumes", volume); err != nil { 193 return fmt.Errorf("csi volume insert failed: %v", err) 194 } 195 return nil 196 } 197 198 // ScalingEventsRestore is used to restore scaling events for a job 199 func (r *StateRestore) ScalingEventsRestore(jobEvents *structs.JobScalingEvents) error { 200 if err := r.txn.Insert("scaling_event", jobEvents); err != nil { 201 return fmt.Errorf("scaling event insert failed: %v", err) 202 } 203 return nil 204 } 205 206 // NamespaceRestore is used to restore a namespace 207 func (r *StateRestore) NamespaceRestore(ns *structs.Namespace) error { 208 // Handle upgrade path. 209 ns.Canonicalize() 210 if err := r.txn.Insert(TableNamespaces, ns); err != nil { 211 return fmt.Errorf("namespace insert failed: %v", err) 212 } 213 return nil 214 } 215 216 // ServiceRegistrationRestore is used to restore a single service registration 217 // into the service_registrations table. 218 func (r *StateRestore) ServiceRegistrationRestore(service *structs.ServiceRegistration) error { 219 if err := r.txn.Insert(TableServiceRegistrations, service); err != nil { 220 return fmt.Errorf("service registration insert failed: %v", err) 221 } 222 return nil 223 } 224 225 // VariablesRestore is used to restore a single variable into the variables 226 // table. 227 func (r *StateRestore) VariablesRestore(variable *structs.VariableEncrypted) error { 228 if err := r.txn.Insert(TableVariables, variable); err != nil { 229 return fmt.Errorf("variable insert failed: %v", err) 230 } 231 return nil 232 } 233 234 // VariablesQuotaRestore is used to restore a single variable quota into the 235 // variables_quota table. 236 func (r *StateRestore) VariablesQuotaRestore(quota *structs.VariablesQuota) error { 237 if err := r.txn.Insert(TableVariablesQuotas, quota); err != nil { 238 return fmt.Errorf("variable quota insert failed: %v", err) 239 } 240 return nil 241 } 242 243 // RootKeyMetaQuotaRestore is used to restore a single root key meta into the 244 // root_key_meta table. 245 func (r *StateRestore) RootKeyMetaRestore(quota *structs.RootKeyMeta) error { 246 if err := r.txn.Insert(TableRootKeyMeta, quota); err != nil { 247 return fmt.Errorf("root key meta insert failed: %v", err) 248 } 249 return nil 250 } 251 252 // ACLRoleRestore is used to restore a single ACL role into the acl_roles 253 // table. 254 func (r *StateRestore) ACLRoleRestore(aclRole *structs.ACLRole) error { 255 if err := r.txn.Insert(TableACLRoles, aclRole); err != nil { 256 return fmt.Errorf("ACL role insert failed: %v", err) 257 } 258 return nil 259 } 260 261 // ACLAuthMethodRestore is used to restore a single ACL auth method into the 262 // acl_auth_methods table. 263 func (r *StateRestore) ACLAuthMethodRestore(aclAuthMethod *structs.ACLAuthMethod) error { 264 if err := r.txn.Insert(TableACLAuthMethods, aclAuthMethod); err != nil { 265 return fmt.Errorf("ACL auth method insert failed: %v", err) 266 } 267 return nil 268 } 269 270 // ACLBindingRuleRestore is used to restore a single ACL binding rule into the 271 // acl_binding_rules table. 272 func (r *StateRestore) ACLBindingRuleRestore(aclBindingRule *structs.ACLBindingRule) error { 273 if err := r.txn.Insert(TableACLBindingRules, aclBindingRule); err != nil { 274 return fmt.Errorf("ACL binding rule insert failed: %v", err) 275 } 276 return nil 277 }