代码拉取完成,页面将自动刷新
/*
Copyright 2014 Outbrain Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package inst
import (
"fmt"
"github.com/outbrain/golib/log"
"github.com/outbrain/golib/sqlutils"
"github.com/outbrain/orchestrator/go/config"
"github.com/outbrain/orchestrator/go/db"
)
// ReadActiveMaintenance returns the list of currently active maintenance entries
func ReadActiveMaintenance() ([]Maintenance, error) {
res := []Maintenance{}
query := fmt.Sprintf(`
select
database_instance_maintenance_id,
hostname,
port,
begin_timestamp,
timestampdiff(second, begin_timestamp, now()) as seconds_elapsed,
maintenance_active,
owner,
reason
from
database_instance_maintenance
where
maintenance_active = 1
order by
database_instance_maintenance_id
`)
db, err := db.OpenOrchestrator()
if err != nil {
goto Cleanup
}
err = sqlutils.QueryRowsMap(db, query, func(m sqlutils.RowMap) error {
maintenance := Maintenance{}
maintenance.MaintenanceId = m.GetUint("database_instance_maintenance_id")
maintenance.Key.Hostname = m.GetString("hostname")
maintenance.Key.Port = m.GetInt("port")
maintenance.BeginTimestamp = m.GetString("begin_timestamp")
maintenance.SecondsElapsed = m.GetUint("seconds_elapsed")
maintenance.IsActive = m.GetBool("maintenance_active")
maintenance.Owner = m.GetString("owner")
maintenance.Reason = m.GetString("reason")
res = append(res, maintenance)
return err
})
Cleanup:
if err != nil {
log.Errore(err)
}
return res, err
}
// BeginBoundedMaintenance will make new maintenance entry for given instanceKey.
func BeginBoundedMaintenance(instanceKey *InstanceKey, owner string, reason string, durationSeconds uint) (int64, error) {
var maintenanceToken int64 = 0
if durationSeconds == 0 {
durationSeconds = config.Config.MaintenanceExpireMinutes * 60
}
res, err := db.ExecOrchestrator(`
insert ignore
into database_instance_maintenance (
hostname, port, maintenance_active, begin_timestamp, end_timestamp, owner, reason
) VALUES (
?, ?, 1, NOW(), NOW() + INTERVAL ? SECOND, ?, ?
)
`,
instanceKey.Hostname,
instanceKey.Port,
durationSeconds,
owner,
reason,
)
if err != nil {
return maintenanceToken, log.Errore(err)
}
if affected, _ := res.RowsAffected(); affected == 0 {
err = fmt.Errorf("Cannot begin maintenance for instance: %+v; maintenance reason: %+v", instanceKey, reason)
} else {
// success
maintenanceToken, _ = res.LastInsertId()
AuditOperation("begin-maintenance", instanceKey, fmt.Sprintf("maintenanceToken: %d, owner: %s, reason: %s", maintenanceToken, owner, reason))
}
return maintenanceToken, err
}
// BeginMaintenance will make new maintenance entry for given instanceKey. Maintenance time is unbounded
func BeginMaintenance(instanceKey *InstanceKey, owner string, reason string) (int64, error) {
return BeginBoundedMaintenance(instanceKey, owner, reason, 0)
}
// EndMaintenanceByInstanceKey will terminate an active maintenance using given instanceKey as hint
func EndMaintenanceByInstanceKey(instanceKey *InstanceKey) error {
res, err := db.ExecOrchestrator(`
update
database_instance_maintenance
set
maintenance_active = NULL,
end_timestamp = NOW()
where
hostname = ?
and port = ?
and maintenance_active = 1
`,
instanceKey.Hostname,
instanceKey.Port,
)
if err != nil {
return log.Errore(err)
}
if affected, _ := res.RowsAffected(); affected == 0 {
err = fmt.Errorf("Instance is not in maintenance mode: %+v", instanceKey)
} else {
// success
AuditOperation("end-maintenance", instanceKey, "")
}
return err
}
// ReadMaintenanceInstanceKey will return the instanceKey for active maintenance by maintenanceToken
func ReadMaintenanceInstanceKey(maintenanceToken int64) (*InstanceKey, error) {
var res *InstanceKey
query := fmt.Sprintf(`
select
hostname, port
from
database_instance_maintenance
where
database_instance_maintenance_id = %d `,
maintenanceToken)
db, err := db.OpenOrchestrator()
if err != nil {
goto Cleanup
}
err = sqlutils.QueryRowsMap(db, query, func(m sqlutils.RowMap) error {
instanceKey, merr := NewInstanceKeyFromStrings(m.GetString("hostname"), m.GetString("port"))
if merr != nil {
return merr
}
res = instanceKey
return nil
})
Cleanup:
if err != nil {
log.Errore(err)
}
return res, err
}
// EndMaintenance will terminate an active maintenance via maintenanceToken
func EndMaintenance(maintenanceToken int64) error {
res, err := db.ExecOrchestrator(`
update
database_instance_maintenance
set
maintenance_active = NULL,
end_timestamp = NOW()
where
database_instance_maintenance_id = ?
`,
maintenanceToken,
)
if err != nil {
return log.Errore(err)
}
if affected, _ := res.RowsAffected(); affected == 0 {
err = fmt.Errorf("Instance is not in maintenance mode; token = %+v", maintenanceToken)
} else {
// success
instanceKey, _ := ReadMaintenanceInstanceKey(maintenanceToken)
AuditOperation("end-maintenance", instanceKey, fmt.Sprintf("maintenanceToken: %d", maintenanceToken))
}
return err
}
// ExpireMaintenance will remove the maintenance flag on old maintenances and on bounded maintenances
func ExpireMaintenance() error {
{
res, err := db.ExecOrchestrator(`
delete from
database_instance_maintenance
where
maintenance_active is null
and end_timestamp < NOW() - INTERVAL ? DAY
`,
config.Config.MaintenancePurgeDays,
)
if err != nil {
return log.Errore(err)
}
if rowsAffected, _ := res.RowsAffected(); rowsAffected > 0 {
AuditOperation("expire-maintenance", nil, fmt.Sprintf("Purged historical entries: %d", rowsAffected))
}
}
{
res, err := db.ExecOrchestrator(`
update
database_instance_maintenance
set
maintenance_active = NULL
where
maintenance_active = 1
and end_timestamp < NOW()
`,
)
if err != nil {
return log.Errore(err)
}
if rowsAffected, _ := res.RowsAffected(); rowsAffected > 0 {
AuditOperation("expire-maintenance", nil, fmt.Sprintf("Expired bounded: %d", rowsAffected))
}
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。