github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mysql/get_replica_role.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package mysql
    21  
    22  import (
    23  	"context"
    24  
    25  	"github.com/pkg/errors"
    26  
    27  	"github.com/1aal/kubeblocks/pkg/lorry/dcs"
    28  	"github.com/1aal/kubeblocks/pkg/lorry/engines/models"
    29  )
    30  
    31  func (mgr *Manager) GetReplicaRole(ctx context.Context, cluster *dcs.Cluster) (string, error) {
    32  	if cluster == nil {
    33  		return "", errors.New("cluster not found")
    34  	}
    35  
    36  	if !cluster.IsLocked() {
    37  		return "", errors.New("cluster has no leader lease")
    38  	}
    39  
    40  	if cluster.Leader.Name != mgr.CurrentMemberName {
    41  		return models.SECONDARY, nil
    42  	}
    43  
    44  	return models.PRIMARY, nil
    45  	// slaveRunning, err := mgr.isSlaveRunning(ctx)
    46  	// if err != nil {
    47  	// 	return "", err
    48  	// }
    49  	// if slaveRunning {
    50  	// 	return SECONDARY, nil
    51  	// }
    52  
    53  	// hasSlave, err := mgr.hasSlaveHosts(ctx)
    54  	// if err != nil {
    55  	// 	return "", err
    56  	// }
    57  	// if hasSlave {
    58  	// 	return PRIMARY, nil
    59  	// }
    60  
    61  	// isReadonly, err := mgr.IsReadonly(ctx, nil, nil)
    62  	// if err != nil {
    63  	// 	return "", err
    64  	// }
    65  	// if isReadonly {
    66  	// 	// TODO: in case of diskfull lock, dababase will be set readonly,
    67  	// 	// how to deal with this situation
    68  	// 	return SECONDARY, nil
    69  	// }
    70  
    71  	// return PRIMARY, nil
    72  }
    73  
    74  // func (mgr *Manager) isSlaveRunning(ctx context.Context) (bool, error) {
    75  // 	var rowMap = mgr.slaveStatus
    76  //
    77  // 	if len(rowMap) == 0 {
    78  // 		return false, nil
    79  // 	}
    80  // 	ioRunning := rowMap.GetString("Slave_IO_Running")
    81  // 	sqlRunning := rowMap.GetString("Slave_SQL_Running")
    82  // 	if ioRunning == "Yes" || sqlRunning == "Yes" {
    83  // 		return true, nil
    84  // 	}
    85  // 	return false, nil
    86  // }
    87  //
    88  // func (mgr *Manager) hasSlaveHosts(ctx context.Context) (bool, error) {
    89  // 	sql := "show slave hosts"
    90  // 	var rowMap RowMap
    91  //
    92  // 	err := QueryRowsMap(mgr.DB, sql, func(rMap RowMap) error {
    93  // 		rowMap = rMap
    94  // 		return nil
    95  // 	})
    96  // 	if err != nil {
    97  // 		mgr.Logger.Error(err, fmt.Sprintf("error executing %s", sql))
    98  // 		return false, err
    99  // 	}
   100  //
   101  // 	if len(rowMap) == 0 {
   102  // 		return false, nil
   103  // 	}
   104  //
   105  // 	return true, nil
   106  // }