github.com/matrixorigin/matrixone@v1.2.0/pkg/tnservice/store_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 tnservice
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/frontend"
    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 *store) initSqlWriterFactory() {
    27  	getClient := func() util.HAKeeperClient {
    28  		return s.hakeeperClient
    29  	}
    30  	db_holder.SetSQLWriterDBAddressFunc(util.AddressFunc(getClient))
    31  }
    32  
    33  func (s *store) createSQLLogger(command *logservicepb.CreateTaskService) {
    34  	// convert username to "mo_logger"
    35  	frontend.SetSpecialUser(db_holder.MOLoggerUser, []byte(command.User.Password))
    36  	db_holder.SetSQLWriterDBUser(db_holder.MOLoggerUser, command.User.Password)
    37  }
    38  
    39  func (s *store) initTaskHolder() {
    40  	s.task.Lock()
    41  	defer s.task.Unlock()
    42  	if s.task.serviceHolder != nil {
    43  		return
    44  	}
    45  
    46  	getClient := func() util.HAKeeperClient {
    47  		return s.hakeeperClient
    48  	}
    49  	if s.task.storageFactory != nil {
    50  		s.task.serviceHolder = taskservice.NewTaskServiceHolderWithTaskStorageFactorySelector(
    51  			s.rt,
    52  			util.AddressFunc(getClient),
    53  			func(_, _, _ string) taskservice.TaskStorageFactory {
    54  				return s.task.storageFactory
    55  			})
    56  		return
    57  	}
    58  
    59  	s.task.serviceHolder = taskservice.NewTaskServiceHolder(s.rt, util.AddressFunc(getClient))
    60  }
    61  
    62  func (s *store) createTaskService(command *logservicepb.CreateTaskService) {
    63  	s.task.Lock()
    64  	defer s.task.Unlock()
    65  	if s.task.serviceCreated {
    66  		return
    67  	}
    68  
    69  	// Notify frontend to set up the special account used to task framework create and query async tasks.
    70  	// The account is always in the memory.
    71  	frontend.SetSpecialUser(command.User.Username, []byte(command.User.Password))
    72  	if err := s.task.serviceHolder.Create(*command); err != nil {
    73  		s.rt.Logger().Error("create task service failed",
    74  			zap.Error(err))
    75  		return
    76  	}
    77  	s.task.serviceCreated = true
    78  }
    79  
    80  func (s *store) taskServiceCreated() bool {
    81  	s.task.RLock()
    82  	defer s.task.RUnlock()
    83  	return s.task.serviceCreated
    84  }
    85  
    86  func (s *store) GetTaskService() (taskservice.TaskService, bool) {
    87  	s.task.RLock()
    88  	defer s.task.RUnlock()
    89  	if s.task.serviceHolder == nil {
    90  		return nil, false
    91  	}
    92  	return s.task.serviceHolder.Get()
    93  }