vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/candidate_database_instance_dao.go (about) 1 /* 2 Copyright 2016 Simon J Mudd 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package inst 18 19 import ( 20 "vitess.io/vitess/go/vt/external/golib/sqlutils" 21 "vitess.io/vitess/go/vt/log" 22 "vitess.io/vitess/go/vt/vtorc/config" 23 "vitess.io/vitess/go/vt/vtorc/db" 24 ) 25 26 // RegisterCandidateInstance markes a given instance as suggested for succeeding a primary in the event of failover. 27 func RegisterCandidateInstance(candidate *CandidateDatabaseInstance) error { 28 if candidate.LastSuggestedString == "" { 29 candidate = candidate.WithCurrentTime() 30 } 31 args := sqlutils.Args(candidate.Hostname, candidate.Port, string(candidate.PromotionRule), candidate.LastSuggestedString) 32 33 query := ` 34 insert into candidate_database_instance ( 35 hostname, 36 port, 37 promotion_rule, 38 last_suggested 39 ) values ( 40 ?, ?, ?, ? 41 ) on duplicate key update 42 last_suggested=values(last_suggested), 43 promotion_rule=values(promotion_rule) 44 ` 45 writeFunc := func() error { 46 _, err := db.ExecVTOrc(query, args...) 47 if err != nil { 48 log.Error(err) 49 } 50 return err 51 } 52 return ExecDBWriteFunc(writeFunc) 53 } 54 55 // ExpireCandidateInstances removes stale primary candidate suggestions. 56 func ExpireCandidateInstances() error { 57 writeFunc := func() error { 58 _, err := db.ExecVTOrc(` 59 delete from candidate_database_instance 60 where last_suggested < NOW() - INTERVAL ? MINUTE 61 `, config.CandidateInstanceExpireMinutes, 62 ) 63 if err != nil { 64 log.Error(err) 65 } 66 return err 67 } 68 return ExecDBWriteFunc(writeFunc) 69 }