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