|
10 | 10 |
|
11 | 11 | import org.apache.logging.log4j.LogManager;
|
12 | 12 | import org.apache.logging.log4j.Logger;
|
| 13 | +import org.opensearch.cluster.DiffableUtils; |
| 14 | +import org.opensearch.cluster.routing.IndexRoutingTable; |
| 15 | +import org.opensearch.cluster.routing.RoutingTable; |
| 16 | +import org.opensearch.common.blobstore.BlobContainer; |
13 | 17 | import org.opensearch.common.lifecycle.AbstractLifecycleComponent;
|
| 18 | +import org.opensearch.common.settings.Setting; |
14 | 19 | import org.opensearch.common.settings.Settings;
|
15 | 20 | import org.opensearch.common.util.io.IOUtils;
|
| 21 | +import org.opensearch.core.action.ActionListener; |
| 22 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 23 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 24 | +import org.opensearch.core.index.Index; |
| 25 | +import org.opensearch.gateway.remote.ClusterMetadataManifest; |
| 26 | +import org.opensearch.gateway.remote.RemoteClusterStateService; |
| 27 | +import org.opensearch.gateway.remote.routingtable.RemoteIndexRoutingTable; |
16 | 28 | import org.opensearch.node.Node;
|
17 | 29 | import org.opensearch.node.remotestore.RemoteStoreNodeAttribute;
|
18 | 30 | import org.opensearch.repositories.RepositoriesService;
|
19 | 31 | import org.opensearch.repositories.Repository;
|
20 | 32 | import org.opensearch.repositories.blobstore.BlobStoreRepository;
|
| 33 | +import org.opensearch.threadpool.ThreadPool; |
21 | 34 |
|
22 | 35 | import java.io.IOException;
|
| 36 | +import java.io.InputStream; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Optional; |
| 41 | +import java.util.Set; |
| 42 | +import java.util.concurrent.ExecutorService; |
| 43 | +import java.util.function.Function; |
23 | 44 | import java.util.function.Supplier;
|
| 45 | +import java.util.stream.Collectors; |
24 | 46 |
|
25 | 47 | import static org.opensearch.node.remotestore.RemoteStoreNodeAttribute.isRemoteRoutingTableEnabled;
|
26 | 48 |
|
|
31 | 53 | */
|
32 | 54 | public class RemoteRoutingTableService extends AbstractLifecycleComponent {
|
33 | 55 |
|
| 56 | + /** |
| 57 | + * Cluster setting to specify if routing table should be published to remote store |
| 58 | + */ |
| 59 | + public static final Setting<Boolean> REMOTE_ROUTING_TABLE_ENABLED_SETTING = Setting.boolSetting( |
| 60 | + "cluster.remote_store.routing.enabled", |
| 61 | + true, |
| 62 | + Setting.Property.NodeScope, |
| 63 | + Setting.Property.Final |
| 64 | + ); |
| 65 | + public static final String INDEX_ROUTING_PATH_TOKEN = "index-routing"; |
| 66 | + public static final String ROUTING_TABLE = "routing-table"; |
| 67 | + public static final String INDEX_ROUTING_FILE_PREFIX = "index_routing"; |
| 68 | + public static final String DELIMITER = "__"; |
| 69 | + public static final String INDEX_ROUTING_METADATA_PREFIX = "indexRouting--"; |
34 | 70 | private static final Logger logger = LogManager.getLogger(RemoteRoutingTableService.class);
|
35 | 71 | private final Settings settings;
|
36 | 72 | private final Supplier<RepositoriesService> repositoriesService;
|
37 | 73 | private BlobStoreRepository blobStoreRepository;
|
| 74 | + private final ThreadPool threadPool; |
38 | 75 |
|
39 |
| - public RemoteRoutingTableService(Supplier<RepositoriesService> repositoriesService, Settings settings) { |
| 76 | + private static final DiffableUtils.NonDiffableValueSerializer<String, IndexRoutingTable> CUSTOM_ROUTING_TABLE_VALUE_SERIALIZER = new DiffableUtils.NonDiffableValueSerializer<String, IndexRoutingTable>() { |
| 77 | + @Override |
| 78 | + public void write(IndexRoutingTable value, StreamOutput out) throws IOException { |
| 79 | + value.writeTo(out); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public IndexRoutingTable read(StreamInput in, String key) throws IOException { |
| 84 | + return IndexRoutingTable.readFrom(in); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + |
| 89 | + public RemoteRoutingTableService(Supplier<RepositoriesService> repositoriesService, |
| 90 | + Settings settings, ThreadPool threadPool) { |
40 | 91 | assert isRemoteRoutingTableEnabled(settings) : "Remote routing table is not enabled";
|
41 | 92 | this.repositoriesService = repositoriesService;
|
42 | 93 | this.settings = settings;
|
| 94 | + this.threadPool = threadPool; |
| 95 | + } |
| 96 | + |
| 97 | + public List<ClusterMetadataManifest.UploadedIndexMetadata> getAllUploadedIndicesRouting(ClusterMetadataManifest previousManifest, List<ClusterMetadataManifest.UploadedIndexMetadata> indicesRoutingToUpload, Set<String> indicesRoutingToDelete) { |
| 98 | + final Map<String, ClusterMetadataManifest.UploadedIndexMetadata> allUploadedIndicesRouting = previousManifest.getIndicesRouting() |
| 99 | + .stream() |
| 100 | + .collect(Collectors.toMap(ClusterMetadataManifest.UploadedIndexMetadata::getIndexName, Function.identity())); |
| 101 | + |
| 102 | + indicesRoutingToUpload.forEach( |
| 103 | + uploadedIndexRouting -> allUploadedIndicesRouting.put(uploadedIndexRouting.getIndexName(), uploadedIndexRouting) |
| 104 | + ); |
| 105 | + |
| 106 | + indicesRoutingToDelete.forEach(allUploadedIndicesRouting::remove); |
| 107 | + |
| 108 | + logger.info("allUploadedIndicesRouting ROUTING {}", allUploadedIndicesRouting); |
| 109 | + |
| 110 | + return new ArrayList<>(allUploadedIndicesRouting.values()); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + private String getIndexRoutingFileName() { |
| 115 | + return String.join( |
| 116 | + DELIMITER, |
| 117 | + INDEX_ROUTING_FILE_PREFIX, |
| 118 | + RemoteStoreUtils.invertLong(System.currentTimeMillis()) |
| 119 | + ); |
| 120 | + |
43 | 121 | }
|
44 | 122 |
|
| 123 | + public CheckedRunnable<IOException> getAsyncIndexMetadataReadAction( |
| 124 | + String uploadedFilename, |
| 125 | + Index index, |
| 126 | + LatchedActionListener<IndexRoutingTable> latchedActionListener) { |
| 127 | + int idx = uploadedFilename.lastIndexOf("/"); |
| 128 | + String blobFileName = uploadedFilename.substring(idx+1); |
| 129 | + BlobContainer blobContainer = blobStoreRepository.blobStore().blobContainer( BlobPath.cleanPath().add(uploadedFilename.substring(0,idx))); |
| 130 | + |
| 131 | + return () -> readAsync( |
| 132 | + blobContainer, |
| 133 | + blobFileName, |
| 134 | + index, |
| 135 | + threadPool.executor(ThreadPool.Names.GENERIC), |
| 136 | + ActionListener.wrap(response -> latchedActionListener.onResponse(response.getIndexRoutingTable()), latchedActionListener::onFailure) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + public void readAsync(BlobContainer blobContainer, String name, Index index, ExecutorService executorService, ActionListener<RemoteIndexRoutingTable> listener) throws IOException { |
| 141 | + executorService.execute(() -> { |
| 142 | + try { |
| 143 | + listener.onResponse(read(blobContainer, name, index)); |
| 144 | + } catch (Exception e) { |
| 145 | + listener.onFailure(e); |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + public RemoteIndexRoutingTable read(BlobContainer blobContainer, String path, Index index) { |
| 151 | + try { |
| 152 | + return new RemoteIndexRoutingTable(blobContainer.readBlob(path), index); |
| 153 | + } catch (IOException | AssertionError e) { |
| 154 | + logger.info("RoutingTable read failed with error: {}", e.toString()); |
| 155 | + throw new RemoteClusterStateService.RemoteStateTransferException("Failed to read RemoteRoutingTable from Manifest with error ", e); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public List<ClusterMetadataManifest.UploadedIndexMetadata> getUpdatedIndexRoutingTableMetadata(List<String> updatedIndicesRouting, List<ClusterMetadataManifest.UploadedIndexMetadata> allIndicesRouting) { |
| 160 | + return updatedIndicesRouting.stream().map(idx -> { |
| 161 | + Optional<ClusterMetadataManifest.UploadedIndexMetadata> uploadedIndexMetadataOptional = allIndicesRouting.stream().filter(idx2 -> idx2.getIndexName().equals(idx)).findFirst(); |
| 162 | + assert uploadedIndexMetadataOptional.isPresent() == true; |
| 163 | + return uploadedIndexMetadataOptional.get(); |
| 164 | + }).collect(Collectors.toList()); |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + public static DiffableUtils.MapDiff<String, IndexRoutingTable, Map<String, IndexRoutingTable>> getIndicesRoutingMapDiff(RoutingTable before, RoutingTable after) { |
| 169 | + return DiffableUtils.diff( |
| 170 | + before.getIndicesRouting(), |
| 171 | + after.getIndicesRouting(), |
| 172 | + DiffableUtils.getStringKeySerializer(), |
| 173 | + CUSTOM_ROUTING_TABLE_VALUE_SERIALIZER |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + |
45 | 178 | @Override
|
46 |
| - protected void doClose() throws IOException { |
| 179 | + public void doClose() throws IOException { |
47 | 180 | if (blobStoreRepository != null) {
|
48 | 181 | IOUtils.close(blobStoreRepository);
|
49 | 182 | }
|
|
0 commit comments