github.com/matrixorigin/matrixone@v0.7.0/pkg/tests/service/fileservice.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 service 16 17 import ( 18 "path/filepath" 19 "sync" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 25 "github.com/matrixorigin/matrixone/pkg/defines" 26 "github.com/matrixorigin/matrixone/pkg/fileservice" 27 ) 28 29 // fileServices contains all FileService instances. 30 type fileServices struct { 31 sync.RWMutex 32 33 t *testing.T 34 dnServiceNum int 35 cnServiceNum int 36 37 dnLocalFSs []fileservice.FileService 38 cnLocalFSs []fileservice.FileService 39 s3FS fileservice.FileService 40 } 41 42 // newFileServices constructs an instance of fileServices. 43 func (c *testCluster) buildFileServices() *fileServices { 44 dnServiceNum := c.opt.initial.dnServiceNum 45 cnServiceNum := c.opt.initial.cnServiceNum 46 47 factory := func(dir string, name string) fileservice.FileService { 48 fs, err := fileservice.NewMemoryFS(name) 49 require.NoError(c.t, err) 50 return fs 51 } 52 if c.opt.keepData { 53 factory = func(dir string, name string) fileservice.FileService { 54 fs, err := fileservice.NewLocalFS(name, filepath.Join(dir, name), 0) 55 require.NoError(c.t, err) 56 return fs 57 } 58 } 59 60 dnLocals := make([]fileservice.FileService, 0, dnServiceNum) 61 for i := 0; i < dnServiceNum; i++ { 62 dnLocals = append(dnLocals, factory(c.dn.cfgs[i].DataDir, defines.LocalFileServiceName)) 63 } 64 65 cnLocals := make([]fileservice.FileService, 0, cnServiceNum) 66 for i := 0; i < cnServiceNum; i++ { 67 cnLocals = append(cnLocals, factory(filepath.Join(c.opt.rootDataDir, c.cn.cfgs[i].UUID), defines.LocalFileServiceName)) 68 } 69 70 return &fileServices{ 71 t: c.t, 72 dnServiceNum: dnServiceNum, 73 cnServiceNum: cnServiceNum, 74 dnLocalFSs: dnLocals, 75 cnLocalFSs: cnLocals, 76 s3FS: factory(c.opt.rootDataDir, defines.SharedFileServiceName), 77 } 78 } 79 80 // assertFileServiceLocked asserts constructed file services. 81 func (f *fileServices) assertFileServiceLocked() { 82 assert.Equal(f.t, f.dnServiceNum, len(f.dnLocalFSs)) 83 assert.Equal(f.t, f.cnServiceNum, len(f.cnLocalFSs)) 84 } 85 86 // getDNLocalFileService gets local FileService for DN service. 87 func (f *fileServices) getDNLocalFileService(index int) fileservice.FileService { 88 f.RLock() 89 defer f.RUnlock() 90 91 f.assertFileServiceLocked() 92 93 if index >= len(f.dnLocalFSs) { 94 return nil 95 } 96 return f.dnLocalFSs[index] 97 } 98 99 func (f *fileServices) getCNLocalFileService(index int) fileservice.FileService { 100 f.RLock() 101 defer f.RUnlock() 102 103 f.assertFileServiceLocked() 104 105 if index >= len(f.cnLocalFSs) { 106 return nil 107 } 108 return f.cnLocalFSs[index] 109 } 110 111 // getS3FileService gets S3 FileService for all DN services. 112 func (f *fileServices) getS3FileService() fileservice.FileService { 113 f.RLock() 114 defer f.RUnlock() 115 f.assertFileServiceLocked() 116 return f.s3FS 117 }