3 Star 2 Fork 0

Gitee 极速下载 / orchestrator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/outbrain/orchestrator/
克隆/下载
election_dao.go 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright 2015 Shlomi Noach, courtesy Booking.com
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 process
import (
"github.com/outbrain/golib/log"
"github.com/outbrain/golib/sqlutils"
"github.com/outbrain/orchestrator/go/config"
"github.com/outbrain/orchestrator/go/db"
)
// AttemptElection tries to grab leadership (become active node)
func AttemptElection() (bool, error) {
sqlResult, err := db.ExecOrchestrator(`
insert ignore into active_node (
anchor, hostname, token, last_seen_active
) values (
1, ?, ?, now()
) on duplicate key update
hostname = if(last_seen_active < now() - interval ? second, values(hostname), hostname),
token = if(last_seen_active < now() - interval ? second, values(token), token),
last_seen_active = if(hostname = values(hostname) and token = values(token), values(last_seen_active), last_seen_active)
`,
ThisHostname, ProcessToken.Hash, config.Config.ActiveNodeExpireSeconds, config.Config.ActiveNodeExpireSeconds,
)
if err != nil {
return false, log.Errore(err)
}
rows, err := sqlResult.RowsAffected()
return (rows > 0), log.Errore(err)
}
// GrabElection forcibly grabs leadership. Use with care!!
func GrabElection() error {
_, err := db.ExecOrchestrator(`
replace into active_node (
anchor, hostname, token, last_seen_active
) values (
1, ?, ?, NOW()
)
`,
ThisHostname, ProcessToken.Hash,
)
return log.Errore(err)
}
// Reelect clears the way for re-elections. Active node is immediately demoted.
func Reelect() error {
_, err := db.ExecOrchestrator(`delete from active_node where anchor = 1`)
return log.Errore(err)
}
// ElectedNode returns the details of the elected node, as well as answering the question "is this process the elected one"?
func ElectedNode() (hostname string, token string, isElected bool, err error) {
query := `
select
hostname,
token
from
active_node
where
anchor = 1
`
err = db.QueryOrchestratorRowsMap(query, func(m sqlutils.RowMap) error {
hostname = m.GetString("hostname")
token = m.GetString("token")
return nil
})
if err != nil {
log.Errore(err)
}
isElected = (hostname == ThisHostname && token == ProcessToken.Hash)
return hostname, token, isElected, err
}
1
https://gitee.com/mirrors/orchestrator.git
git@gitee.com:mirrors/orchestrator.git
mirrors
orchestrator
orchestrator
v1.5.7

搜索帮助

53164aa7 5694891 3bd8fe86 5694891