github.com/matrixorigin/matrixone@v1.2.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  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    19  	logservicepb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    20  	"github.com/matrixorigin/matrixone/pkg/taskservice"
    21  	"github.com/matrixorigin/matrixone/pkg/util"
    22  	"github.com/matrixorigin/matrixone/pkg/util/export/etl/db"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  func (s *Service) initSqlWriterFactory() {
    27  	getClient := func() util.HAKeeperClient {
    28  		return s.haClient
    29  	}
    30  	db_holder.SetSQLWriterDBAddressFunc(util.AddressFunc(getClient))
    31  }
    32  
    33  func (s *Service) createSQLLogger(command *logservicepb.CreateTaskService) {
    34  	db_holder.SetSQLWriterDBUser(db_holder.MOLoggerUser, command.User.Password)
    35  }
    36  
    37  func (s *Service) initTaskHolder() {
    38  	s.task.Lock()
    39  	defer s.task.Unlock()
    40  	if s.task.holder != nil {
    41  		return
    42  	}
    43  
    44  	getClient := func() util.HAKeeperClient {
    45  		return s.haClient
    46  	}
    47  
    48  	if s.task.storageFactory != nil {
    49  		s.task.holder = taskservice.NewTaskServiceHolderWithTaskStorageFactorySelector(
    50  			runtime.ProcessLevelRuntime(),
    51  			util.AddressFunc(getClient),
    52  			func(_, _, _ string) taskservice.TaskStorageFactory {
    53  				return s.task.storageFactory
    54  			})
    55  		return
    56  	}
    57  	s.task.holder = taskservice.NewTaskServiceHolder(runtime.ProcessLevelRuntime(), util.AddressFunc(getClient))
    58  }
    59  
    60  func (s *Service) createTaskService(command *logservicepb.CreateTaskService) {
    61  	s.task.Lock()
    62  	defer s.task.Unlock()
    63  	if s.task.created {
    64  		return
    65  	}
    66  	if err := s.task.holder.Create(*command); err != nil {
    67  		s.runtime.Logger().Error("create task service failed",
    68  			zap.Error(err))
    69  		return
    70  	}
    71  	s.task.created = true
    72  }
    73  
    74  func (s *Service) taskServiceCreated() bool {
    75  	s.task.RLock()
    76  	defer s.task.RUnlock()
    77  	return s.task.created
    78  }
    79  
    80  func (s *Service) getTaskService() taskservice.TaskService {
    81  	s.task.RLock()
    82  	defer s.task.RUnlock()
    83  	if s.task.holder == nil {
    84  		return nil
    85  	}
    86  
    87  	ts, ok := s.task.holder.Get()
    88  	if !ok {
    89  		return nil
    90  	}
    91  	return ts
    92  }