github.com/matrixorigin/matrixone@v0.7.0/pkg/logservice/service_task.go (about)

     1  // Copyright 2021 - 2022 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 logservice
    16  
    17  import (
    18  	"context"
    19  	"math/rand"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    24  	logservicepb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    25  	"github.com/matrixorigin/matrixone/pkg/taskservice"
    26  	"go.uber.org/zap"
    27  )
    28  
    29  func (s *Service) initTaskHolder() {
    30  	s.task.Lock()
    31  	defer s.task.Unlock()
    32  	if s.task.holder != nil {
    33  		return
    34  	}
    35  
    36  	addressFunc := func(ctx context.Context) (string, error) {
    37  		ctx, cancel := context.WithTimeout(ctx,
    38  			time.Second*5)
    39  		defer cancel()
    40  		if s.haClient == nil {
    41  			return "", nil
    42  		}
    43  		details, err := s.haClient.GetClusterDetails(ctx)
    44  		if err != nil {
    45  			return "", err
    46  		}
    47  		if len(details.CNStores) == 0 {
    48  			return "", moerr.NewInvalidState(ctx, "no cn in the cluster")
    49  		}
    50  
    51  		n := rand.Intn(len(details.CNStores))
    52  		return details.CNStores[n].SQLAddress, nil
    53  	}
    54  
    55  	if s.task.storageFactory != nil {
    56  		s.task.holder = taskservice.NewTaskServiceHolderWithTaskStorageFactorySelector(
    57  			runtime.ProcessLevelRuntime(),
    58  			addressFunc,
    59  			func(_, _, _ string) taskservice.TaskStorageFactory {
    60  				return s.task.storageFactory
    61  			})
    62  		return
    63  	}
    64  	s.task.holder = taskservice.NewTaskServiceHolder(runtime.ProcessLevelRuntime(), addressFunc)
    65  }
    66  
    67  func (s *Service) createTaskService(command *logservicepb.CreateTaskService) {
    68  	s.task.Lock()
    69  	defer s.task.Unlock()
    70  	if s.task.created {
    71  		return
    72  	}
    73  	if err := s.task.holder.Create(*command); err != nil {
    74  		s.runtime.Logger().Error("create task service failed",
    75  			zap.Error(err))
    76  		return
    77  	}
    78  	s.task.created = true
    79  }
    80  
    81  func (s *Service) taskServiceCreated() bool {
    82  	s.task.RLock()
    83  	defer s.task.RUnlock()
    84  	return s.task.created
    85  }
    86  
    87  func (s *Service) getTaskService() taskservice.TaskService {
    88  	s.task.RLock()
    89  	defer s.task.RUnlock()
    90  	if s.task.holder == nil {
    91  		return nil
    92  	}
    93  
    94  	ts, ok := s.task.holder.Get()
    95  	if !ok {
    96  		return nil
    97  	}
    98  	return ts
    99  }