feat(snapshots): add support for transactions EE-5329 (#8947)

pull/8961/head
andres-portainer 2 years ago committed by GitHub
parent 5a04338087
commit dbd476008b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,7 @@ import (
"github.com/portainer/portainer/api/agent" "github.com/portainer/portainer/api/agent"
"github.com/portainer/portainer/api/crypto" "github.com/portainer/portainer/api/crypto"
"github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/dataservices"
"github.com/portainer/portainer/pkg/featureflags"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -49,15 +50,18 @@ func parseSnapshotFrequency(snapshotInterval string, dataStore dataservices.Data
if err != nil { if err != nil {
return 0, err return 0, err
} }
snapshotInterval = settings.SnapshotInterval snapshotInterval = settings.SnapshotInterval
if snapshotInterval == "" { if snapshotInterval == "" {
snapshotInterval = portainer.DefaultSnapshotInterval snapshotInterval = portainer.DefaultSnapshotInterval
} }
} }
snapshotFrequency, err := time.ParseDuration(snapshotInterval) snapshotFrequency, err := time.ParseDuration(snapshotInterval)
if err != nil { if err != nil {
return 0, err return 0, err
} }
return snapshotFrequency.Seconds(), nil return snapshotFrequency.Seconds(), nil
} }
@ -85,6 +89,7 @@ func SupportDirectSnapshot(endpoint *portainer.Endpoint) bool {
case portainer.EdgeAgentOnDockerEnvironment, portainer.EdgeAgentOnKubernetesEnvironment, portainer.AzureEnvironment: case portainer.EdgeAgentOnDockerEnvironment, portainer.EdgeAgentOnKubernetesEnvironment, portainer.AzureEnvironment:
return false return false
} }
return true return true
} }
@ -127,9 +132,9 @@ func (service *Service) FillSnapshotData(endpoint *portainer.Endpoint) error {
return FillSnapshotData(service.dataStore, endpoint) return FillSnapshotData(service.dataStore, endpoint)
} }
func FillSnapshotData(dataStore dataservices.DataStore, endpoint *portainer.Endpoint) error { func FillSnapshotData(tx dataservices.DataStoreTx, endpoint *portainer.Endpoint) error {
snapshot, err := dataStore.Snapshot().Snapshot(endpoint.ID) snapshot, err := tx.Snapshot().Snapshot(endpoint.ID)
if dataStore.IsErrObjectNotFound(err) { if tx.IsErrObjectNotFound(err) {
endpoint.Snapshots = []portainer.DockerSnapshot{} endpoint.Snapshots = []portainer.DockerSnapshot{}
endpoint.Kubernetes.Snapshots = []portainer.KubernetesSnapshot{} endpoint.Kubernetes.Snapshots = []portainer.KubernetesSnapshot{}
@ -213,50 +218,55 @@ func (service *Service) snapshotEndpoints() error {
} }
for _, endpoint := range endpoints { for _, endpoint := range endpoints {
if !SupportDirectSnapshot(&endpoint) { if !SupportDirectSnapshot(&endpoint) || endpoint.URL == "" {
continue
}
if endpoint.URL == "" {
continue continue
} }
snapshotError := service.SnapshotEndpoint(&endpoint) snapshotError := service.SnapshotEndpoint(&endpoint)
latestEndpointReference, err := service.dataStore.Endpoint().Endpoint(endpoint.ID) if featureflags.IsEnabled(portainer.FeatureNoTx) {
if latestEndpointReference == nil { updateEndpointStatus(service.dataStore, &endpoint, snapshotError)
log.Debug(). } else {
Str("endpoint", endpoint.Name). service.dataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
Str("URL", endpoint.URL).Err(err). updateEndpointStatus(tx, &endpoint, snapshotError)
Msg("background schedule error (environment snapshot), environment not found inside the database anymore") return nil
})
continue
} }
}
latestEndpointReference.Status = portainer.EndpointStatusUp return nil
if snapshotError != nil { }
log.Debug().
Str("endpoint", endpoint.Name).
Str("URL", endpoint.URL).Err(err).
Msg("background schedule error (environment snapshot), unable to create snapshot")
latestEndpointReference.Status = portainer.EndpointStatusDown func updateEndpointStatus(tx dataservices.DataStoreTx, endpoint *portainer.Endpoint, snapshotError error) {
} latestEndpointReference, err := tx.Endpoint().Endpoint(endpoint.ID)
if latestEndpointReference == nil {
log.Debug().
Str("endpoint", endpoint.Name).
Str("URL", endpoint.URL).Err(err).
Msg("background schedule error (environment snapshot), environment not found inside the database anymore")
latestEndpointReference.Agent.Version = endpoint.Agent.Version return
}
err = service.dataStore.Endpoint().UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) latestEndpointReference.Status = portainer.EndpointStatusUp
if err != nil { if snapshotError != nil {
log.Debug(). log.Debug().
Str("endpoint", endpoint.Name). Str("endpoint", endpoint.Name).
Str("URL", endpoint.URL).Err(err). Str("URL", endpoint.URL).Err(err).
Msg("background schedule error (environment snapshot), unable to update environment") Msg("background schedule error (environment snapshot), unable to create snapshot")
continue latestEndpointReference.Status = portainer.EndpointStatusDown
}
} }
return nil latestEndpointReference.Agent.Version = endpoint.Agent.Version
err = tx.Endpoint().UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference)
if err != nil {
log.Debug().
Str("endpoint", endpoint.Name).
Str("URL", endpoint.URL).Err(err).
Msg("background schedule error (environment snapshot), unable to update environment")
}
} }
// FetchDockerID fetches info.Swarm.Cluster.ID if environment(endpoint) is swarm and info.ID otherwise // FetchDockerID fetches info.Swarm.Cluster.ID if environment(endpoint) is swarm and info.ID otherwise

Loading…
Cancel
Save