github.com/matrixorigin/matrixone@v0.7.0/pkg/dnservice/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 dnservice 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/frontend" 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 *store) initTaskHolder() { 30 s.task.Lock() 31 defer s.task.Unlock() 32 if s.task.serviceHolder != nil { 33 return 34 } 35 36 addressFunc := func(ctx context.Context) (string, error) { 37 ctx, cancel := context.WithTimeout(ctx, time.Second*5) 38 defer cancel() 39 details, err := s.hakeeperClient.GetClusterDetails(ctx) 40 if err != nil { 41 return "", err 42 } 43 if len(details.CNStores) == 0 { 44 return "", moerr.NewInvalidState(ctx, "no cn in the cluster") 45 } 46 47 n := rand.Intn(len(details.CNStores)) 48 return details.CNStores[n].SQLAddress, nil 49 } 50 51 if s.task.storageFactory != nil { 52 s.task.serviceHolder = taskservice.NewTaskServiceHolderWithTaskStorageFactorySelector( 53 s.rt, 54 addressFunc, 55 func(_, _, _ string) taskservice.TaskStorageFactory { 56 return s.task.storageFactory 57 }) 58 return 59 } 60 61 s.task.serviceHolder = taskservice.NewTaskServiceHolder(s.rt, addressFunc) 62 } 63 64 func (s *store) createTaskService(command *logservicepb.CreateTaskService) { 65 s.task.Lock() 66 defer s.task.Unlock() 67 if s.task.serviceCreated { 68 return 69 } 70 71 // Notify frontend to set up the special account used to task framework create and query async tasks. 72 // The account is always in the memory. 73 frontend.SetSpecialUser(command.User.Username, []byte(command.User.Password)) 74 if err := s.task.serviceHolder.Create(*command); err != nil { 75 s.rt.Logger().Error("create task service failed", 76 zap.Error(err)) 77 return 78 } 79 s.task.serviceCreated = true 80 } 81 82 func (s *store) taskServiceCreated() bool { 83 s.task.RLock() 84 defer s.task.RUnlock() 85 return s.task.serviceCreated 86 } 87 88 func (s *store) GetTaskService() (taskservice.TaskService, bool) { 89 s.task.RLock() 90 defer s.task.RUnlock() 91 if s.task.serviceHolder == nil { 92 return nil, false 93 } 94 return s.task.serviceHolder.Get() 95 }