github.com/matrixorigin/matrixone@v1.2.0/pkg/proxy/bootstrap.go (about)

     1  // Copyright 2021 - 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proxy
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    22  	"github.com/matrixorigin/matrixone/pkg/util"
    23  	db_holder "github.com/matrixorigin/matrixone/pkg/util/export/etl/db"
    24  )
    25  
    26  const (
    27  	BootstrapInterval = time.Millisecond * 200
    28  	BootstrapTimeout  = time.Minute * 5
    29  )
    30  
    31  func (h *handler) bootstrap(ctx context.Context) {
    32  	ticker := time.NewTicker(time.Millisecond * 200)
    33  	defer ticker.Stop()
    34  	retry := 0
    35  	getClient := func() util.HAKeeperClient {
    36  		return h.haKeeperClient
    37  	}
    38  	var state pb.CheckerState
    39  	var err error
    40  	for retry < int(BootstrapTimeout/BootstrapInterval) {
    41  		select {
    42  		case <-ticker.C:
    43  			func(ctx context.Context) {
    44  				ctx, cancel := context.WithTimeout(ctx, time.Second*3)
    45  				defer cancel()
    46  				state, err = h.haKeeperClient.GetClusterState(ctx)
    47  				if err != nil {
    48  					panic(err)
    49  				}
    50  			}(ctx)
    51  			if state.TaskTableUser.GetUsername() != "" && state.TaskTableUser.GetPassword() != "" {
    52  				db_holder.SetSQLWriterDBUser(db_holder.MOLoggerUser, state.TaskTableUser.GetPassword())
    53  				db_holder.SetSQLWriterDBAddressFunc(util.AddressFunc(getClient))
    54  				return
    55  			}
    56  		case <-ctx.Done():
    57  			return
    58  		}
    59  		retry += 1
    60  	}
    61  	panic("proxy bootstrap failed")
    62  }