Skip to content

Commit

Permalink
[Remote Store] Add implementation for path types and change default p…
Browse files Browse the repository at this point in the history
…ath type

Signed-off-by: Ashish Singh <[email protected]>
  • Loading branch information
ashking94 committed Apr 5, 2024
1 parent e713175 commit 326459d
Show file tree
Hide file tree
Showing 6 changed files with 481 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,24 @@ boolean requiresHashAlgorithm() {
HASHED_PREFIX(1) {
@Override
public BlobPath generatePath(PathInput pathInput, PathHashAlgorithm hashAlgorithm) {
// TODO - We need to implement this, keeping the same path as Fixed for sake of multiple tests that can fail otherwise.
// throw new UnsupportedOperationException("Not implemented"); --> Not using this for unblocking couple of tests.
assert Objects.nonNull(hashAlgorithm) : "hashAlgorithm is expected to be non-null";
return addPrefix(pathInput.basePath(), hashAlgorithm.hash(pathInput)).add(pathInput.indexUUID())
.add(pathInput.shardId())
.add(pathInput.dataCategory().getName())
.add(pathInput.dataType().getName());
}

@Override
boolean requiresHashAlgorithm() {
return true;
}
},
HASHED_INFIX(2) {
@Override
public BlobPath generatePath(PathInput pathInput, PathHashAlgorithm hashAlgorithm) {
assert Objects.nonNull(hashAlgorithm) : "hashAlgorithm is expected to be non-null";
return pathInput.basePath()
.add(hashAlgorithm.hash(pathInput))
.add(pathInput.indexUUID())
.add(pathInput.shardId())
.add(pathInput.dataCategory().getName())
Expand Down Expand Up @@ -200,10 +215,11 @@ public enum PathHashAlgorithm {

FNV_1A(0) {
@Override
long hash(PathInput pathInput) {
String hash(PathInput pathInput) {
String input = pathInput.indexUUID() + pathInput.shardId() + pathInput.dataCategory().getName() + pathInput.dataType()
.getName();
return FNV1a.hash32(input);
long hash = FNV1a.hash64(input);
return RemoteStoreUtils.longToBase64(hash);
}
};

Expand All @@ -218,6 +234,7 @@ public int getCode() {
}

private static final Map<Integer, PathHashAlgorithm> CODE_TO_ENUM;

static {
PathHashAlgorithm[] values = values();
Map<Integer, PathHashAlgorithm> codeToStatus = new HashMap<>(values.length);
Expand All @@ -240,7 +257,7 @@ public static PathHashAlgorithm fromCode(int code) {
return CODE_TO_ENUM.get(code);
}

abstract long hash(PathInput pathInput);
abstract String hash(PathInput pathInput);

public static PathHashAlgorithm parseString(String pathHashAlgorithm) {
try {
Expand All @@ -257,4 +274,13 @@ public static PathHashAlgorithm parseString(String pathHashAlgorithm) {
*/
public static final String NAME = "path_hash_algorithm";
}

static BlobPath addPrefix(BlobPath blobPath, String prefix) {
BlobPath updatedPath = BlobPath.cleanPath();
updatedPath = updatedPath.add(prefix);
for (String path : blobPath) {
updatedPath = updatedPath.add(path);
}
return updatedPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

import org.opensearch.common.collect.Tuple;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -101,4 +103,26 @@ public static void verifyNoMultipleWriters(List<String> mdFiles, Function<String
});
}

/**
* Converts an input hash which occupies 64 bits of space into Base64 (6 bits per character) String. This must not
* be changed as it is used for creating path for storing remote store data on the remote store.
*/
static String longToBase64(long value) {
byte[] hashBytes = ByteBuffer.allocate(Long.BYTES).putLong(value).array();
return base64(hashBytes);
}

/**
* This converts the byte array to base 64 string. `/` is replaced with `_`, `+` is replaced with `-` and `=`
* which is padded at the last is also removed. These characters are either used as delimiter or special character
* requiring special handling in some vendors. The characters present in this base64 version are [A-Za-z0-9_-].
* This must not be changed as it is used for creating path for storing remote store data on the remote store.
*
* @param bytes byte array
* @return base 64 string.
*/
private static String base64(byte[] bytes) {
String base64 = Base64.getEncoder().encodeToString(bytes);
return base64.substring(0, base64.length() - 1).replace('/', '_').replace('+', '-');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public class IndicesService extends AbstractLifecycleComponent
*/
public static final Setting<PathType> CLUSTER_REMOTE_STORE_PATH_PREFIX_TYPE_SETTING = new Setting<>(
"cluster.remote_store.index.path.prefix.type",
PathType.FIXED.toString(),
PathType.HASHED_PREFIX.toString(),
PathType::parseString,
Property.NodeScope,
Property.Dynamic
Expand Down
Loading

0 comments on commit 326459d

Please sign in to comment.