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