|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.store; |
| 10 | + |
| 11 | +import org.apache.lucene.store.FSDirectory; |
| 12 | +import org.apache.lucene.store.FilterDirectory; |
| 13 | +import org.apache.lucene.store.IOContext; |
| 14 | +import org.apache.lucene.store.IndexInput; |
| 15 | +import org.apache.lucene.store.IndexOutput; |
| 16 | +import org.apache.lucene.store.Lock; |
| 17 | +import org.opensearch.common.blobstore.BlobContainer; |
| 18 | +import org.opensearch.index.store.remote.filecache.CachedIndexInput; |
| 19 | +import org.opensearch.index.store.remote.filecache.FileCache; |
| 20 | +import org.opensearch.index.store.remote.utils.filetracker.FileState; |
| 21 | +import org.opensearch.index.store.remote.utils.filetracker.FileType; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +public class CompositeDirectory extends FilterDirectory { |
| 28 | + |
| 29 | + private final FSDirectory localDirectory; |
| 30 | + private final TransferManager transferManager; |
| 31 | + private final FileCache fileCache; |
| 32 | + |
| 33 | + public CompositeDirectory(FSDirectory localDirectory, BlobContainer blobContainer, FileCache fileCache) { |
| 34 | + super(localDirectory); |
| 35 | + this.localDirectory = localDirectory; |
| 36 | + this.fileCache = fileCache; |
| 37 | + this.transferManager = new CompositeDirectoryTransferManager(fileCache, blobContainer); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String[] listAll() throws IOException { |
| 42 | + return localDirectory.listAll(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void deleteFile(String name) throws IOException { |
| 47 | + super.deleteFile(name); |
| 48 | + transferManager.removeFileFromTracker(name); |
| 49 | + fileCache.remove(localDirectory.getDirectory().resolve(name)); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public long fileLength(String name) throws IOException { |
| 54 | + return localDirectory.fileLength(name); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public IndexOutput createOutput(String name, IOContext context) throws IOException { |
| 59 | + transferManager.trackFile(name, FileState.DISK, FileType.NON_BLOCK); |
| 60 | + return localDirectory.createOutput(name, context); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public IndexOutput createTempOutput(String prefix, String suffix, IOContext context) throws IOException { |
| 65 | + return localDirectory.createTempOutput(prefix, suffix, context); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void sync(Collection<String> names) throws IOException { |
| 70 | + localDirectory.sync(names); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void syncMetaData() throws IOException { |
| 75 | + localDirectory.syncMetaData(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void rename(String source, String dest) throws IOException { |
| 80 | + localDirectory.rename(source, dest); |
| 81 | + transferManager.trackFile(dest, transferManager.getFileState(source), transferManager.getFileType(source)); |
| 82 | + transferManager.removeFileFromTracker(source); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public IndexInput openInput(String name, IOContext context) throws IOException { |
| 87 | + if (!transferManager.isFilePresent(name)) { |
| 88 | + return localDirectory.openInput(name, context); |
| 89 | + } |
| 90 | + IndexInput indexInput = null; |
| 91 | + switch (transferManager.getFileState(name)) { |
| 92 | + case DISK: |
| 93 | + indexInput = localDirectory.openInput(name, context); |
| 94 | + break; |
| 95 | + |
| 96 | + case CACHE: |
| 97 | + indexInput = fileCache.get(localDirectory.getDirectory().resolve(name)).getIndexInput(); |
| 98 | + break; |
| 99 | + |
| 100 | + case REMOTE_ONLY: |
| 101 | + // TODO - return an implementation of OnDemandBlockIndexInput where the fetchBlock method is implemented |
| 102 | + break; |
| 103 | + } |
| 104 | + return indexInput; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Lock obtainLock(String name) throws IOException { |
| 109 | + return localDirectory.obtainLock(name); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void close() throws IOException { |
| 114 | + localDirectory.close(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public Set<String> getPendingDeletions() throws IOException { |
| 119 | + return localDirectory.getPendingDeletions(); |
| 120 | + } |
| 121 | + |
| 122 | + public void afterSyncToRemote(Collection<String> files) throws IOException { |
| 123 | + for (String fileName : files) { |
| 124 | + if (transferManager.isFilePresent(fileName) && !transferManager.getFileState(fileName).equals(FileState.CACHE)) { |
| 125 | + transferManager.updateFileState(fileName, FileState.CACHE); |
| 126 | + } |
| 127 | + fileCache.put(localDirectory.getDirectory().resolve(fileName), new CachedIndexInput() { |
| 128 | + @Override |
| 129 | + public IndexInput getIndexInput() throws IOException { |
| 130 | + return localDirectory.openInput(fileName, IOContext.READ); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public long length() { |
| 135 | + try { |
| 136 | + return localDirectory.fileLength(fileName); |
| 137 | + } catch (IOException e) { |
| 138 | + throw new RuntimeException(e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean isClosed() { |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void close() {} |
| 149 | + }); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments