Migrating cluster discovery store to sync by default (#10665)

* Migrating cluster discovery store to sync by default

* Addressing PR review comments
Этот коммит содержится в:
Jesús Espino
2019-04-24 12:36:56 +02:00
коммит произвёл GitHub
родитель 357065e202
Коммит 4ae38d00a8
5 изменённых файлов: 240 добавлений и 260 удалений

Просмотреть файл

@@ -28,145 +28,133 @@ func NewSqlClusterDiscoveryStore(sqlStore SqlStore) store.ClusterDiscoveryStore
return s
}
func (s sqlClusterDiscoveryStore) Save(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
ClusterDiscovery.PreSave()
if result.Err = ClusterDiscovery.IsValid(); result.Err != nil {
return
}
func (s sqlClusterDiscoveryStore) Save(ClusterDiscovery *model.ClusterDiscovery) *model.AppError {
ClusterDiscovery.PreSave()
if err := ClusterDiscovery.IsValid(); err != nil {
return err
}
if err := s.GetMaster().Insert(ClusterDiscovery); err != nil {
result.Err = model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.save.app_error", nil, err.Error(), http.StatusInternalServerError)
}
})
if err := s.GetMaster().Insert(ClusterDiscovery); err != nil {
return model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.save.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return nil
}
func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
result.Data = false
if count, err := s.GetMaster().SelectInt(
`
DELETE
FROM
ClusterDiscovery
WHERE
Type = :Type
AND ClusterName = :ClusterName
AND Hostname = :Hostname
`,
map[string]interface{}{
"Type": ClusterDiscovery.Type,
"ClusterName": ClusterDiscovery.ClusterName,
"Hostname": ClusterDiscovery.Hostname,
},
); err != nil {
result.Err = model.NewAppError("SqlClusterDiscoveryStore.Delete", "store.sql_cluster_discovery.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
if count > 0 {
result.Data = true
}
}
})
}
func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
result.Data = false
if count, err := s.GetMaster().SelectInt(
`
SELECT
COUNT(*)
FROM
ClusterDiscovery
WHERE
Type = :Type
AND ClusterName = :ClusterName
AND Hostname = :Hostname
`,
map[string]interface{}{
"Type": ClusterDiscovery.Type,
"ClusterName": ClusterDiscovery.ClusterName,
"Hostname": ClusterDiscovery.Hostname,
},
); err != nil {
result.Err = model.NewAppError("SqlClusterDiscoveryStore.Exists", "store.sql_cluster_discovery.exists.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
if count > 0 {
result.Data = true
}
}
})
}
func (s sqlClusterDiscoveryStore) GetAll(ClusterDiscoveryType, clusterName string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
lastPingAt := model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS
var list []*model.ClusterDiscovery
if _, err := s.GetMaster().Select(
&list,
`
SELECT
*
FROM
ClusterDiscovery
WHERE
Type = :ClusterDiscoveryType
AND ClusterName = :ClusterName
AND LastPingAt > :LastPingAt
`,
map[string]interface{}{
"ClusterDiscoveryType": ClusterDiscoveryType,
"ClusterName": clusterName,
"LastPingAt": lastPingAt,
},
); err != nil {
result.Err = model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = list
}
})
}
func (s sqlClusterDiscoveryStore) SetLastPingAt(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if _, err := s.GetMaster().Exec(
`
UPDATE ClusterDiscovery
SET
LastPingAt = :LastPingAt
WHERE
Type = :Type
func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscovery) (bool, *model.AppError) {
count, err := s.GetMaster().SelectInt(
`
DELETE
FROM
ClusterDiscovery
WHERE
Type = :Type
AND ClusterName = :ClusterName
AND Hostname = :Hostname
`,
map[string]interface{}{
"LastPingAt": model.GetMillis(),
"Type": ClusterDiscovery.Type,
"ClusterName": ClusterDiscovery.ClusterName,
"Hostname": ClusterDiscovery.Hostname,
},
); err != nil {
result.Err = model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.set_last_ping.app_error", nil, err.Error(), http.StatusInternalServerError)
}
})
`,
map[string]interface{}{
"Type": ClusterDiscovery.Type,
"ClusterName": ClusterDiscovery.ClusterName,
"Hostname": ClusterDiscovery.Hostname,
},
)
if err != nil {
return false, model.NewAppError("SqlClusterDiscoveryStore.Delete", "store.sql_cluster_discovery.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
}
if count == 0 {
return false, nil
}
return true, nil
}
func (s sqlClusterDiscoveryStore) Cleanup() store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if _, err := s.GetMaster().Exec(
`
DELETE FROM ClusterDiscovery
WHERE
LastPingAt < :LastPingAt
`,
map[string]interface{}{
"LastPingAt": model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS,
},
); err != nil {
result.Err = model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.cleanup.app_error", nil, err.Error(), http.StatusInternalServerError)
}
})
func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscovery) (bool, *model.AppError) {
count, err := s.GetMaster().SelectInt(
`
SELECT
COUNT(*)
FROM
ClusterDiscovery
WHERE
Type = :Type
AND ClusterName = :ClusterName
AND Hostname = :Hostname
`,
map[string]interface{}{
"Type": ClusterDiscovery.Type,
"ClusterName": ClusterDiscovery.ClusterName,
"Hostname": ClusterDiscovery.Hostname,
},
)
if err != nil {
return false, model.NewAppError("SqlClusterDiscoveryStore.Exists", "store.sql_cluster_discovery.exists.app_error", nil, err.Error(), http.StatusInternalServerError)
}
if count == 0 {
return false, nil
}
return true, nil
}
func (s sqlClusterDiscoveryStore) GetAll(ClusterDiscoveryType, clusterName string) ([]*model.ClusterDiscovery, *model.AppError) {
lastPingAt := model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS
var list []*model.ClusterDiscovery
if _, err := s.GetMaster().Select(
&list,
`
SELECT
*
FROM
ClusterDiscovery
WHERE
Type = :ClusterDiscoveryType
AND ClusterName = :ClusterName
AND LastPingAt > :LastPingAt
`,
map[string]interface{}{
"ClusterDiscoveryType": ClusterDiscoveryType,
"ClusterName": clusterName,
"LastPingAt": lastPingAt,
},
); err != nil {
return nil, model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return list, nil
}
func (s sqlClusterDiscoveryStore) SetLastPingAt(ClusterDiscovery *model.ClusterDiscovery) *model.AppError {
if _, err := s.GetMaster().Exec(
`
UPDATE ClusterDiscovery
SET
LastPingAt = :LastPingAt
WHERE
Type = :Type
AND ClusterName = :ClusterName
AND Hostname = :Hostname
`,
map[string]interface{}{
"LastPingAt": model.GetMillis(),
"Type": ClusterDiscovery.Type,
"ClusterName": ClusterDiscovery.ClusterName,
"Hostname": ClusterDiscovery.Hostname,
},
); err != nil {
return model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.set_last_ping.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return nil
}
func (s sqlClusterDiscoveryStore) Cleanup() *model.AppError {
if _, err := s.GetMaster().Exec(
`
DELETE FROM ClusterDiscovery
WHERE
LastPingAt < :LastPingAt
`,
map[string]interface{}{
"LastPingAt": model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS,
},
); err != nil {
return model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.cleanup.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return nil
}