vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/maintenance_dao.go (about)

     1  /*
     2     Copyright 2014 Outbrain Inc.
     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  	"fmt"
    21  
    22  	"vitess.io/vitess/go/vt/log"
    23  
    24  	"vitess.io/vitess/go/vt/vtorc/config"
    25  	"vitess.io/vitess/go/vt/vtorc/db"
    26  )
    27  
    28  // ExpireMaintenance will remove the maintenance flag on old maintenances and on bounded maintenances
    29  func ExpireMaintenance() error {
    30  	{
    31  		res, err := db.ExecVTOrc(`
    32  			delete from
    33  				database_instance_maintenance
    34  			where
    35  				maintenance_active is null
    36  				and end_timestamp < NOW() - INTERVAL ? DAY
    37  			`,
    38  			config.MaintenancePurgeDays,
    39  		)
    40  		if err != nil {
    41  			log.Error(err)
    42  			return err
    43  		}
    44  		if rowsAffected, _ := res.RowsAffected(); rowsAffected > 0 {
    45  			_ = AuditOperation("expire-maintenance", nil, fmt.Sprintf("Purged historical entries: %d", rowsAffected))
    46  		}
    47  	}
    48  	{
    49  		res, err := db.ExecVTOrc(`
    50  			delete from
    51  				database_instance_maintenance
    52  			where
    53  				maintenance_active = 1
    54  				and end_timestamp < NOW()
    55  			`,
    56  		)
    57  		if err != nil {
    58  			log.Error(err)
    59  			return err
    60  		}
    61  		if rowsAffected, _ := res.RowsAffected(); rowsAffected > 0 {
    62  			_ = AuditOperation("expire-maintenance", nil, fmt.Sprintf("Expired bounded: %d", rowsAffected))
    63  		}
    64  	}
    65  	{
    66  		res, err := db.ExecVTOrc(`
    67  			delete from
    68  				database_instance_maintenance
    69  			where
    70  				explicitly_bounded = 0
    71  				and concat(processing_node_hostname, ':', processing_node_token) not in (
    72  					select concat(hostname, ':', token) from node_health
    73  				)
    74  			`,
    75  		)
    76  		if err != nil {
    77  			log.Error(err)
    78  			return err
    79  		}
    80  		if rowsAffected, _ := res.RowsAffected(); rowsAffected > 0 {
    81  			_ = AuditOperation("expire-maintenance", nil, fmt.Sprintf("Expired dead: %d", rowsAffected))
    82  		}
    83  	}
    84  
    85  	return nil
    86  }