github.com/matrixorigin/matrixone@v1.2.0/pkg/queryservice/session_manager.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 queryservice 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/pb/status" 19 "sync" 20 ) 21 22 // Session is an interface which should have the following methods. 23 type Session interface { 24 // GetUUIDString returns the id of the session. 25 GetUUIDString() string 26 // GetTenantName returns the tenant name of the session. 27 GetTenantName() string 28 // StatusSession converts the session to status.Session. 29 StatusSession() *status.Session 30 // SetSessionRoutineStatus set the session Status 31 SetSessionRoutineStatus(status string) error 32 } 33 34 // SessionManager manages all sessions locally. 35 type SessionManager struct { 36 mu struct { 37 sync.RWMutex 38 sessionsByID map[string]Session 39 // Tenant name => []*Session 40 sessionsByTenant map[string]map[Session]struct{} 41 } 42 } 43 44 // NewSessionManager creates a new SessionManager instance. 45 func NewSessionManager() *SessionManager { 46 m := &SessionManager{} 47 m.mu.sessionsByID = make(map[string]Session) 48 m.mu.sessionsByTenant = make(map[string]map[Session]struct{}) 49 return m 50 } 51 52 // AddSession adds a new session to manager. 53 func (sm *SessionManager) AddSession(s Session) { 54 if sm == nil { 55 return 56 } 57 sm.mu.Lock() 58 defer sm.mu.Unlock() 59 sm.mu.sessionsByID[s.GetUUIDString()] = s 60 _, ok := sm.mu.sessionsByTenant[s.GetTenantName()] 61 if !ok { 62 sm.mu.sessionsByTenant[s.GetTenantName()] = make(map[Session]struct{}) 63 } 64 sm.mu.sessionsByTenant[s.GetTenantName()][s] = struct{}{} 65 } 66 67 // RemoveSession removes a session from manager. 68 func (sm *SessionManager) RemoveSession(s Session) { 69 if sm == nil { 70 return 71 } 72 sm.mu.Lock() 73 defer sm.mu.Unlock() 74 delete(sm.mu.sessionsByID, s.GetUUIDString()) 75 delete(sm.mu.sessionsByTenant[s.GetTenantName()], s) 76 } 77 78 // GetAllSessions returns all sessions in the manager. 79 func (sm *SessionManager) GetAllSessions() []Session { 80 if sm == nil { 81 return nil 82 } 83 sm.mu.RLock() 84 defer sm.mu.RUnlock() 85 sessions := make([]Session, 0, len(sm.mu.sessionsByID)) 86 for _, session := range sm.mu.sessionsByID { 87 sessions = append(sessions, session) 88 } 89 return sessions 90 } 91 92 // GetAllStatusSessions returns all status sessions in the manager. 93 func (sm *SessionManager) GetAllStatusSessions() []*status.Session { 94 if sm == nil { 95 return []*status.Session{} 96 } 97 sm.mu.RLock() 98 defer sm.mu.RUnlock() 99 sessions := make([]*status.Session, 0, len(sm.mu.sessionsByID)) 100 for _, session := range sm.mu.sessionsByID { 101 sessions = append(sessions, session.StatusSession()) 102 } 103 return sessions 104 } 105 106 // GetSessionsByTenant returns the sessions belongs to the tenant. 107 func (sm *SessionManager) GetSessionsByTenant(tenant string) []Session { 108 if sm == nil { 109 return nil 110 } 111 sm.mu.RLock() 112 defer sm.mu.RUnlock() 113 sessions := make([]Session, 0, len(sm.mu.sessionsByTenant[tenant])) 114 for session := range sm.mu.sessionsByTenant[tenant] { 115 sessions = append(sessions, session) 116 } 117 return sessions 118 } 119 120 // GetStatusSessionsByTenant returns the status sessions belongs to the tenant. 121 func (sm *SessionManager) GetStatusSessionsByTenant(tenant string) []*status.Session { 122 if sm == nil { 123 return []*status.Session{} 124 } 125 sm.mu.RLock() 126 defer sm.mu.RUnlock() 127 sessions := make([]*status.Session, 0, len(sm.mu.sessionsByTenant[tenant])) 128 for session := range sm.mu.sessionsByTenant[tenant] { 129 sessions = append(sessions, session.StatusSession()) 130 } 131 return sessions 132 }