|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package hostpath |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 25 | + "golang.org/x/net/context" |
| 26 | + "k8s.io/klog/v2" |
| 27 | +) |
| 28 | + |
| 29 | +// NOTE: This implementation of SnapshotMetadata service is used for demo and CI testing purpose only. |
| 30 | +// This should not be used in production or as an example about how to write a real driver. |
| 31 | + |
| 32 | +type fileBlockReader struct { |
| 33 | + base *os.File |
| 34 | + target *os.File |
| 35 | + offset int64 |
| 36 | + blockSize int64 |
| 37 | + blockMetadataType csi.BlockMetadataType |
| 38 | + maxResult int32 |
| 39 | +} |
| 40 | + |
| 41 | +func newFileBlockReader( |
| 42 | + basePath, |
| 43 | + targetPath string, |
| 44 | + startingOffset int64, |
| 45 | + blockSize int64, |
| 46 | + blockMetadataType csi.BlockMetadataType, |
| 47 | + maxResult int32, |
| 48 | +) (*fileBlockReader, error) { |
| 49 | + base, target, err := openFiles(basePath, targetPath) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + return &fileBlockReader{ |
| 55 | + base: base, |
| 56 | + target: target, |
| 57 | + offset: startingOffset, |
| 58 | + blockSize: blockSize, |
| 59 | + blockMetadataType: blockMetadataType, |
| 60 | + maxResult: maxResult, |
| 61 | + }, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (cb *fileBlockReader) seekToStartingOffset() error { |
| 65 | + if _, err := cb.target.Seek(cb.offset, io.SeekStart); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + if cb.base == nil { |
| 69 | + return nil |
| 70 | + } |
| 71 | + if _, err := cb.base.Seek(cb.offset, io.SeekStart); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func (cb *fileBlockReader) Close() error { |
| 78 | + if cb.base != nil { |
| 79 | + if err := cb.base.Close(); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + } |
| 83 | + if cb.target != nil { |
| 84 | + if err := cb.target.Close(); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func openFiles(basePath, targetPath string) (base, target *os.File, err error) { |
| 92 | + target, err = os.Open(targetPath) |
| 93 | + if err != nil { |
| 94 | + return nil, nil, err |
| 95 | + } |
| 96 | + if basePath == "" { |
| 97 | + return nil, target, nil |
| 98 | + } |
| 99 | + base, err = os.Open(basePath) |
| 100 | + if err != nil { |
| 101 | + target.Close() |
| 102 | + return nil, nil, err |
| 103 | + } |
| 104 | + |
| 105 | + return base, target, nil |
| 106 | +} |
| 107 | + |
| 108 | +// getChangedBlockMetadata reads base and target files, compare block differences between them |
| 109 | +// and returns list of changed block metadata. It reads all the blocks till it reaches EOF or size of changed block |
| 110 | +// metadata list <= maxSize. |
| 111 | +func (cb *fileBlockReader) getChangedBlockMetadata(ctx context.Context) ([]*csi.BlockMetadata, error) { |
| 112 | + if cb.base == nil { |
| 113 | + klog.V(4).Infof("finding allocated blocks by file: %s", cb.target.Name()) |
| 114 | + } else { |
| 115 | + klog.V(4).Infof("finding changed blocks between two files: %s, %s", cb.base.Name(), cb.target.Name()) |
| 116 | + } |
| 117 | + |
| 118 | + blockIndex := cb.offset / cb.blockSize |
| 119 | + sBuffer := make([]byte, cb.blockSize) |
| 120 | + tBuffer := make([]byte, cb.blockSize) |
| 121 | + eofBaseFile, eofTargetFile := false, false |
| 122 | + |
| 123 | + changedBlocks := []*csi.BlockMetadata{} |
| 124 | + |
| 125 | + // Read blocks and compare them. Create the list of changed blocks metadata. |
| 126 | + // Once the number of blocks reaches to maxResult, return the result and |
| 127 | + // compute next batch of blocks. |
| 128 | + for int32(len(changedBlocks)) < cb.maxResult { |
| 129 | + select { |
| 130 | + case <-ctx.Done(): |
| 131 | + klog.V(4).Infof("handling cancellation signal, closing goroutine") |
| 132 | + return nil, ctx.Err() |
| 133 | + default: |
| 134 | + } |
| 135 | + targetReadBytes, eofTarget, err := readFileBlock(cb.target, tBuffer, eofTargetFile) |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + eofTargetFile = eofTarget |
| 140 | + |
| 141 | + // If base is nil, return blocks allocated by target file. |
| 142 | + if cb.base == nil { |
| 143 | + if eofTargetFile { |
| 144 | + return changedBlocks, io.EOF |
| 145 | + } |
| 146 | + // if VARIABLE_LENGTH type is enabled, return blocks extend instead of individual blocks. |
| 147 | + blockMetadata := createBlockMetadata(blockIndex, cb.blockSize) |
| 148 | + if extendBlock(changedBlocks, csi.BlockMetadataType(cb.blockMetadataType), blockIndex, cb.blockSize) { |
| 149 | + changedBlocks[len(changedBlocks)-1].SizeBytes += cb.blockSize |
| 150 | + cb.offset += cb.blockSize |
| 151 | + blockIndex++ |
| 152 | + continue |
| 153 | + } |
| 154 | + changedBlocks = append(changedBlocks, blockMetadata) |
| 155 | + cb.offset += cb.blockSize |
| 156 | + blockIndex++ |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + baseReadBytes, eofBase, err := readFileBlock(cb.base, sBuffer, eofBaseFile) |
| 161 | + if err != nil { |
| 162 | + return nil, err |
| 163 | + } |
| 164 | + eofBaseFile = eofBase |
| 165 | + |
| 166 | + // If both files have reached EOF, exit the loop. |
| 167 | + if eofBaseFile && eofTargetFile { |
| 168 | + klog.V(4).Infof("reached end of the files") |
| 169 | + return changedBlocks, io.EOF |
| 170 | + } |
| 171 | + |
| 172 | + // Compare the two blocks and add result. |
| 173 | + // Even if one of the file reaches to end, continue to add block metadata of other file. |
| 174 | + if blockChanged(sBuffer[:baseReadBytes], tBuffer[:targetReadBytes]) { |
| 175 | + blockMetadata := createBlockMetadata(blockIndex, cb.blockSize) |
| 176 | + // if VARIABLE_LEGTH type is enabled, check if blocks are adjacent, |
| 177 | + // extend the previous block if adjacent blocks found instead of adding new entry. |
| 178 | + if extendBlock(changedBlocks, csi.BlockMetadataType(cb.blockMetadataType), blockIndex, cb.blockSize) { |
| 179 | + changedBlocks[len(changedBlocks)-1].SizeBytes += cb.blockSize |
| 180 | + cb.offset += cb.blockSize |
| 181 | + blockIndex++ |
| 182 | + continue |
| 183 | + } |
| 184 | + changedBlocks = append(changedBlocks, blockMetadata) |
| 185 | + } |
| 186 | + |
| 187 | + cb.offset += cb.blockSize |
| 188 | + blockIndex++ |
| 189 | + } |
| 190 | + return changedBlocks, nil |
| 191 | +} |
| 192 | + |
| 193 | +// readFileBlock reads blocks from a file. |
| 194 | +func readFileBlock(file *os.File, buffer []byte, eof bool) (int, bool, error) { |
| 195 | + if eof { |
| 196 | + return 0, true, nil |
| 197 | + } |
| 198 | + |
| 199 | + bytesRead, err := file.Read(buffer) |
| 200 | + if err != nil && err != io.EOF { |
| 201 | + return 0, false, err |
| 202 | + } |
| 203 | + |
| 204 | + return bytesRead, err == io.EOF, nil |
| 205 | +} |
| 206 | + |
| 207 | +func blockChanged(baseBlock, targetBlock []byte) bool { |
| 208 | + return !bytes.Equal(baseBlock, targetBlock) |
| 209 | +} |
| 210 | + |
| 211 | +func createBlockMetadata(blockIndex, blockSize int64) *csi.BlockMetadata { |
| 212 | + return &csi.BlockMetadata{ |
| 213 | + ByteOffset: blockIndex * blockSize, |
| 214 | + SizeBytes: blockSize, |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +func extendBlock(changedBlocks []*csi.BlockMetadata, metadataType csi.BlockMetadataType, blockIndex, blockSize int64) bool { |
| 219 | + blockMetadata := createBlockMetadata(blockIndex, blockSize) |
| 220 | + // if VARIABLE_LEGTH type is enabled, check if blocks are adjacent, |
| 221 | + // extend the previous block if adjacent blocks found instead of adding new entry. |
| 222 | + if len(changedBlocks) < 1 { |
| 223 | + return false |
| 224 | + } |
| 225 | + lastBlock := changedBlocks[len(changedBlocks)-1] |
| 226 | + if blockMetadata.ByteOffset == lastBlock.ByteOffset+lastBlock.SizeBytes && |
| 227 | + metadataType == csi.BlockMetadataType_VARIABLE_LENGTH { |
| 228 | + return true |
| 229 | + } |
| 230 | + return false |
| 231 | +} |
0 commit comments