Skip to content

Commit

Permalink
fix(conn): #307 query units from db to decide which node to connect t…
Browse files Browse the repository at this point in the history
…he tenant
  • Loading branch information
powerfooI committed Apr 18, 2024
1 parent 316909e commit 7125ac3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
38 changes: 13 additions & 25 deletions internal/dashboard/handler/conn_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand All @@ -31,7 +30,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"

"github.com/oceanbase/ob-operator/api/v1alpha1"
"github.com/oceanbase/ob-operator/internal/clients"
oceanbaseconst "github.com/oceanbase/ob-operator/internal/const/oceanbase"
"github.com/oceanbase/ob-operator/internal/dashboard/business/k8s"
Expand Down Expand Up @@ -196,35 +194,25 @@ func CreateOBTenantConnTerminal(c *gin.Context) (*response.OBConnection, error)
}
passwd = string(secret.Data["password"])
}

obzoneList := &v1alpha1.OBZoneList{}
err = clients.ZoneClient.List(c, nn.Namespace, obzoneList, metav1.ListOptions{
LabelSelector: oceanbaseconst.LabelRefOBCluster + "=" + obtenant.Spec.ClusterName,
})
conn := &response.OBConnection{}
obcluster, err := clients.GetOBCluster(c, nn.Namespace, obtenant.Spec.ClusterName)
if err != nil {
return nil, httpErr.NewInternal(err.Error())
return nil, httpErr.NewBadRequest(err.Error())
}

if len(obzoneList.Items) == 0 {
return nil, httpErr.NewBadRequest("no obzone found in obcluster " + obtenant.Spec.ClusterName)
// Select unit information from the oceanbase cluster
db, err := getSysClient(c, obcluster, oceanbaseconst.RootUser, oceanbaseconst.SysTenant, obtenant.Spec.Credentials.Root)
if err != nil {
return nil, httpErr.NewInternal(err.Error())
}

// get full replica observer
fullReplicaMap := make(map[string]struct{})
for _, pool := range obtenant.Spec.Pools {
if pool.Type != nil && strings.EqualFold(pool.Type.Name, "Full") {
fullReplicaMap[pool.Zone] = struct{}{}
}
units, err := db.ListUnitsWithTenantId(int64(obtenant.Status.TenantRecordInfo.TenantID))
if err != nil {
return nil, httpErr.NewInternal(err.Error())
}
conn := &response.OBConnection{}
for _, zone := range obzoneList.Items {
if _, ok := fullReplicaMap[zone.Spec.Topology.Zone]; ok {
if len(zone.Status.OBServerStatus) > 0 {
conn.Host = zone.Status.OBServerStatus[0].Server
break
}
}
if len(units) == 0 {
return nil, httpErr.NewInternal("no unit found in obtenant " + obtenant.Name)
}
conn.Host = units[0].SvrIp

if conn.Host == "" {
return nil, httpErr.NewBadRequest("no full replica observer found in obtenant")
Expand Down
44 changes: 44 additions & 0 deletions internal/dashboard/handler/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ See the Mulan PSL v2 for more details.
package handler

import (
"context"

"github.com/go-logr/logr"
"github.com/pkg/errors"
logger "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/oceanbase/ob-operator/api/v1alpha1"
"github.com/oceanbase/ob-operator/internal/clients"
oceanbaseconst "github.com/oceanbase/ob-operator/internal/const/oceanbase"
"github.com/oceanbase/ob-operator/internal/dashboard/model/param"
"github.com/oceanbase/ob-operator/pkg/k8s/client"
"github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/connector"
"github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/operation"
)

func loggingCreateOBClusterParam(param *param.CreateOBClusterParam) {
Expand Down Expand Up @@ -47,3 +58,36 @@ func loggingCreateOBTenantParam(param *param.CreateOBTenantParam) {
WithField("Source", param.Source).
Infof("Create OBTenant param")
}

func getSysClient(ctx context.Context, obcluster *v1alpha1.OBCluster, userName, tenantName, secretName string) (*operation.OceanbaseOperationManager, error) {
observerList := &v1alpha1.OBServerList{}
err := clients.ServerClient.List(ctx, obcluster.Namespace, observerList, metav1.ListOptions{
LabelSelector: oceanbaseconst.LabelRefOBCluster + "=" + obcluster.Name,
})
if err != nil {
return nil, errors.Wrap(err, "Get observer list")
}
if len(observerList.Items) == 0 {
return nil, errors.Errorf("No observer belongs to cluster %s", obcluster.Name)
}

var s *connector.OceanBaseDataSource
secret, err := client.GetClient().ClientSet.CoreV1().Secrets(obcluster.Namespace).Get(ctx, secretName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrapf(err, "Get secret %s", secretName)
}

password := string(secret.Data["password"])
for _, observer := range observerList.Items {
address := observer.Status.GetConnectAddr()
s = connector.NewOceanBaseDataSource(address, oceanbaseconst.SqlPort, userName, tenantName, password, oceanbaseconst.DefaultDatabase)
// if err is nil, db connection is already checked available
sysClient, err := operation.GetOceanbaseOperationManager(s)
if err == nil && sysClient != nil {
dummy := logr.Discard()
sysClient.Logger = &dummy
return sysClient, nil
}
}
return nil, errors.Errorf("Can not get oceanbase operation manager of obcluster %s after checked all server", obcluster.Name)
}

0 comments on commit 7125ac3

Please sign in to comment.