Skip to content

Commit

Permalink
Enhance handle path for '/'
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Singh <[email protected]>
  • Loading branch information
ashking94 committed Nov 13, 2024
1 parent 59b03a5 commit 714e597
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.routing.RoutingTable;
import org.opensearch.common.blobstore.BlobPath;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
import org.opensearch.indices.RemoteStoreSettings;
Expand Down Expand Up @@ -580,4 +581,23 @@ public static boolean isPinnedTimestampStateStale() {
.millis();
return lastSuccessfulFetchTimestamp < (System.currentTimeMillis() - staleBufferInMillis);
}

public static BlobPath getShardPath(
final BlobPath basePath,
final String indexId,
final String shardId,
final boolean useTopLevelIndexLevelPath
) {
return getIndexPath(basePath, indexId, useTopLevelIndexLevelPath).add(shardId);
}

public static BlobPath getIndexPath(final BlobPath basePath, final String indexId, final boolean useTopLevelIndexPath) {
return new BlobPath().add(getIndexPathAsString(basePath.buildAsString(), indexId, useTopLevelIndexPath));
}

public static String getIndexPathAsString(final String basePath, final String indexId, final boolean useTopLevelIndexPath) {
String indexPath = basePath;
indexPath += "indices" + BlobPath.SEPARATOR + indexId;
return indexPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2826,7 +2826,7 @@ public BlobContainer shardContainer(IndexId indexId, int shardId) {
* @return A BlobPath object representing the full path to the shard in the blob store
* @throws IllegalArgumentException if the base path has more than one component
*/
private BlobPath shardPath(IndexId indexId, int shardId) {
public BlobPath shardPath(IndexId indexId, int shardId) {
PathType pathType = PathType.fromCode(indexId.getShardPathType());
BlobPath normalizedBasePath = normalizeBasePath(basePath());

Expand All @@ -2840,6 +2840,19 @@ private BlobPath shardPath(IndexId indexId, int shardId) {
return pathType.path(shardPathInput, pathHashAlgorithm);
}

public static BlobPath getShardPath(IndexId indexId, int shardId, BlobPath basePath) {
PathType pathType = PathType.fromCode(indexId.getShardPathType());
BlobPath normalizedBasePath = normalizeBasePath(basePath);

SnapshotShardPathInput shardPathInput = new SnapshotShardPathInput.Builder().basePath(normalizedBasePath)
.indexUUID(indexId.getId())
.shardId(String.valueOf(shardId))
.build();

PathHashAlgorithm pathHashAlgorithm = (pathType != PathType.FIXED) ? FNV_1A_COMPOSITE_1 : null;
return pathType.path(shardPathInput, pathHashAlgorithm);
}

/**
* Normalizes the base path by ensuring it has at most one component and
* removing any trailing separators.
Expand All @@ -2856,7 +2869,7 @@ private BlobPath shardPath(IndexId indexId, int shardId) {
* @return A normalized BlobPath
* @throws IllegalArgumentException if the base path has more than one component
*/
private BlobPath normalizeBasePath(BlobPath originalBasePath) {
private static BlobPath normalizeBasePath(BlobPath originalBasePath) {
String[] basePathComponents = originalBasePath.toArray();
if (basePathComponents.length > 1) {
throw new IllegalArgumentException("Base path can have maximum 1 component as BlobPath");
Expand All @@ -2867,11 +2880,15 @@ private BlobPath normalizeBasePath(BlobPath originalBasePath) {
}

String basePathStr = basePathComponents[0];
if (basePathStr.isEmpty()) {
return BlobPath.cleanPath();
}

if (basePathStr.endsWith(BlobPath.SEPARATOR)) {
basePathStr = basePathStr.substring(0, basePathStr.length() - 1);
}

return basePathStr.isEmpty() ? BlobPath.cleanPath() : BlobPath.cleanPath().add(basePathStr);
return BlobPath.cleanPath().add(basePathStr);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.opensearch.cluster.routing.TestShardRouting;
import org.opensearch.common.UUIDs;
import org.opensearch.common.blobstore.BlobMetadata;
import org.opensearch.common.blobstore.BlobPath;
import org.opensearch.common.blobstore.support.PlainBlobMetadata;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.ClusterSettings;
Expand All @@ -36,6 +37,8 @@
import org.opensearch.indices.RemoteStoreSettings;
import org.opensearch.indices.replication.common.ReplicationType;
import org.opensearch.node.remotestore.RemoteStoreNodeAttribute;
import org.opensearch.repositories.IndexId;
import org.opensearch.repositories.blobstore.BlobStoreRepository;
import org.opensearch.test.OpenSearchTestCase;

import java.math.BigInteger;
Expand All @@ -47,6 +50,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1180,4 +1184,43 @@ public void testGetPinnedTimestampLockedFilesWithCache() {
assertEquals(0, implicitLockedFiles.size());
assertEquals(0, metadataFilePinnedTimestampCache.size());
}

public void testGetShardPath() {
for (int i = 0; i < 1000000; i++) {
String path = generateRandomString();
// if (path.equals("/")) {
// continue;
// }
BlobPath basePath = BlobPath.cleanPath().add(path);
IndexId indexId = new IndexId("", "xyz", RemoteStoreEnums.PathType.FIXED.getCode());
BlobPath path1 = RemoteStoreUtils.getShardPath(basePath, indexId.getId(), "1", false);
BlobPath path2 = BlobStoreRepository.getShardPath(indexId, 1, basePath);
logger.info("{} equal is {}", path, path1.buildAsString().equals(path2.buildAsString()));
assertEquals(path1.buildAsString(), path2.buildAsString());
}
}

public static String generateRandomString() {
// Characters to be used in random string
String allowedChars = "abcdefghijklmnopqrstuvwxyz//////////////////////";

// Create Random object
Random random = new Random();

// Generate random length (less than 1024)
int length = random.nextInt(100);

// StringBuilder to store the random string
StringBuilder sb = new StringBuilder(length);

// Generate random string
for (int i = 0; i < length; i++) {
// Get random index from allowedChars
int randomIndex = random.nextInt(allowedChars.length());
// Append random character to StringBuilder
sb.append(allowedChars.charAt(randomIndex));
}
return sb.toString();
}

}

0 comments on commit 714e597

Please sign in to comment.