Skip to content

Commit 81e3a03

Browse files
committed
Update Shallow Snapshot flows to support remote path type & hash algo
Signed-off-by: Ashish Singh <ssashish@amazon.com>
1 parent a91ed4d commit 81e3a03

File tree

5 files changed

+129
-40
lines changed

5 files changed

+129
-40
lines changed

server/src/main/java/org/opensearch/index/remote/RemoteStoreEnums.java

+60-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
import org.opensearch.common.hash.FNV1a;
1414
import org.opensearch.index.remote.RemoteStorePathStrategy.PathInput;
1515

16+
import java.util.HashMap;
1617
import java.util.Locale;
18+
import java.util.Map;
1719
import java.util.Set;
1820

21+
import static java.util.Collections.unmodifiableMap;
1922
import static org.opensearch.index.remote.RemoteStoreEnums.DataType.DATA;
2023
import static org.opensearch.index.remote.RemoteStoreEnums.DataType.METADATA;
2124

@@ -78,7 +81,7 @@ public String getName() {
7881
*/
7982
@PublicApi(since = "2.14.0")
8083
public enum PathType {
81-
FIXED {
84+
FIXED(0) {
8285
@Override
8386
public BlobPath generatePath(PathInput pathInput, PathHashAlgorithm hashAlgorithm) {
8487
// Hash algorithm is not used in FIXED path type
@@ -94,7 +97,7 @@ boolean requiresHashAlgorithm() {
9497
return false;
9598
}
9699
},
97-
HASHED_PREFIX {
100+
HASHED_PREFIX(1) {
98101
@Override
99102
public BlobPath generatePath(PathInput pathInput, PathHashAlgorithm hashAlgorithm) {
100103
// TODO - We need to implement this, keeping the same path as Fixed for sake of multiple tests that can fail otherwise.
@@ -112,6 +115,33 @@ boolean requiresHashAlgorithm() {
112115
}
113116
};
114117

118+
private final int code;
119+
120+
PathType(int code) {
121+
this.code = code;
122+
}
123+
124+
public int getCode() {
125+
return code;
126+
}
127+
128+
private static final Map<Integer, PathType> CODE_TO_ENUM;
129+
static {
130+
PathType[] values = values();
131+
Map<Integer, PathType> codeToStatus = new HashMap<>(values.length);
132+
for (PathType value : values) {
133+
codeToStatus.put(value.code, value);
134+
}
135+
CODE_TO_ENUM = unmodifiableMap(codeToStatus);
136+
}
137+
138+
/**
139+
* Turn a status code into a {@link PathType}.
140+
*/
141+
public static PathType fromCode(int code) {
142+
return CODE_TO_ENUM.get(code);
143+
}
144+
115145
/**
116146
* This method generates the path for the given path input which constitutes multiple fields and characteristics
117147
* of the data.
@@ -158,7 +188,7 @@ public static PathType parseString(String pathType) {
158188
@PublicApi(since = "2.14.0")
159189
public enum PathHashAlgorithm {
160190

161-
FNV_1A {
191+
FNV_1A(0) {
162192
@Override
163193
long hash(PathInput pathInput) {
164194
String input = pathInput.indexUUID() + pathInput.shardId() + pathInput.dataCategory().getName() + pathInput.dataType()
@@ -167,6 +197,33 @@ long hash(PathInput pathInput) {
167197
}
168198
};
169199

200+
private final int code;
201+
202+
PathHashAlgorithm(int code) {
203+
this.code = code;
204+
}
205+
206+
public int getCode() {
207+
return code;
208+
}
209+
210+
private static final Map<Integer, PathHashAlgorithm> CODE_TO_ENUM;
211+
static {
212+
PathHashAlgorithm[] values = values();
213+
Map<Integer, PathHashAlgorithm> codeToStatus = new HashMap<>(values.length);
214+
for (PathHashAlgorithm value : values) {
215+
codeToStatus.put(value.code, value);
216+
}
217+
CODE_TO_ENUM = unmodifiableMap(codeToStatus);
218+
}
219+
220+
/**
221+
* Turn a status code into a {@link PathHashAlgorithm}.
222+
*/
223+
public static PathHashAlgorithm fromCode(int code) {
224+
return CODE_TO_ENUM.get(code);
225+
}
226+
170227
abstract long hash(PathInput pathInput);
171228

172229
public static PathHashAlgorithm parseString(String pathHashAlgorithm) {

server/src/main/java/org/opensearch/index/shard/StoreRecovery.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@
5858
import org.opensearch.index.engine.Engine;
5959
import org.opensearch.index.engine.EngineException;
6060
import org.opensearch.index.mapper.MapperService;
61-
import org.opensearch.index.remote.RemoteStoreEnums.PathType;
62-
import org.opensearch.index.remote.RemoteStorePathStrategy;
6361
import org.opensearch.index.seqno.SequenceNumbers;
6462
import org.opensearch.index.snapshots.IndexShardRestoreFailedException;
6563
import org.opensearch.index.snapshots.blobstore.RemoteStoreShardShallowCopySnapshot;
@@ -412,8 +410,7 @@ void recoverFromSnapshotAndRemoteStore(
412410
remoteStoreRepository,
413411
indexUUID,
414412
shardId,
415-
new RemoteStorePathStrategy(PathType.FIXED)
416-
// TODO - The path type needs to be obtained from RemoteStoreShardShallowCopySnapshot
413+
shallowCopyShardMetadata.getRemoteStorePathStrategy()
417414
);
418415
sourceRemoteDirectory.initializeToSpecificCommit(
419416
primaryTerm,

server/src/main/java/org/opensearch/index/snapshots/blobstore/RemoteStoreShardShallowCopySnapshot.java

+50-21
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
import org.opensearch.core.xcontent.ToXContentFragment;
1515
import org.opensearch.core.xcontent.XContentBuilder;
1616
import org.opensearch.core.xcontent.XContentParser;
17+
import org.opensearch.index.remote.RemoteStoreEnums.PathHashAlgorithm;
18+
import org.opensearch.index.remote.RemoteStoreEnums.PathType;
19+
import org.opensearch.index.remote.RemoteStorePathStrategy;
1720
import org.opensearch.index.snapshots.IndexShardSnapshotStatus;
1821

1922
import java.io.IOException;
2023
import java.util.ArrayList;
2124
import java.util.List;
25+
import java.util.Objects;
2226

2327
/**
2428
* Remote Store based Shard snapshot metadata
@@ -41,8 +45,10 @@ public class RemoteStoreShardShallowCopySnapshot implements ToXContentFragment,
4145
private final String repositoryBasePath;
4246
private final String indexUUID;
4347
private final List<String> fileNames;
48+
private final PathType pathType;
49+
private final PathHashAlgorithm pathHashAlgorithm;
4450

45-
static final String DEFAULT_VERSION = "1";
51+
static final String DEFAULT_VERSION = "2";
4652
static final String NAME = "name";
4753
static final String VERSION = "version";
4854
static final String INDEX_VERSION = "index_version";
@@ -61,6 +67,8 @@ public class RemoteStoreShardShallowCopySnapshot implements ToXContentFragment,
6167

6268
static final String TOTAL_FILE_COUNT = "number_of_files";
6369
static final String TOTAL_SIZE = "total_size";
70+
static final String PATH_TYPE = "path_type";
71+
static final String PATH_HASH_ALGORITHM = "path_hash_algorithm";
6472

6573
private static final ParseField PARSE_NAME = new ParseField(NAME);
6674
private static final ParseField PARSE_VERSION = new ParseField(VERSION);
@@ -75,6 +83,8 @@ public class RemoteStoreShardShallowCopySnapshot implements ToXContentFragment,
7583
private static final ParseField PARSE_REMOTE_STORE_REPOSITORY = new ParseField(REMOTE_STORE_REPOSITORY);
7684
private static final ParseField PARSE_REPOSITORY_BASE_PATH = new ParseField(REPOSITORY_BASE_PATH);
7785
private static final ParseField PARSE_FILE_NAMES = new ParseField(FILE_NAMES);
86+
private static final ParseField PARSE_PATH_TYPE = new ParseField(PATH_TYPE);
87+
private static final ParseField PARSE_PATH_HASH_ALGORITHM = new ParseField(PATH_HASH_ALGORITHM);
7888

7989
/**
8090
* Serializes shard snapshot metadata info into JSON
@@ -101,6 +111,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
101111
builder.value(fileName);
102112
}
103113
builder.endArray();
114+
builder.field(PATH_TYPE, pathType.getCode());
115+
builder.field(PATH_HASH_ALGORITHM, pathHashAlgorithm.getCode());
104116
return builder;
105117
}
106118

@@ -116,31 +128,27 @@ public RemoteStoreShardShallowCopySnapshot(
116128
String indexUUID,
117129
String remoteStoreRepository,
118130
String repositoryBasePath,
119-
List<String> fileNames
131+
List<String> fileNames,
132+
PathType pathType,
133+
PathHashAlgorithm pathHashAlgorithm
120134
) {
121-
this.version = DEFAULT_VERSION;
122-
verifyParameters(
123-
version,
135+
this(
136+
DEFAULT_VERSION,
124137
snapshot,
125138
indexVersion,
126139
primaryTerm,
127140
commitGeneration,
141+
startTime,
142+
time,
143+
totalFileCount,
144+
totalSize,
128145
indexUUID,
129146
remoteStoreRepository,
130-
repositoryBasePath
147+
repositoryBasePath,
148+
fileNames,
149+
pathType,
150+
pathHashAlgorithm
131151
);
132-
this.snapshot = snapshot;
133-
this.indexVersion = indexVersion;
134-
this.primaryTerm = primaryTerm;
135-
this.commitGeneration = commitGeneration;
136-
this.startTime = startTime;
137-
this.time = time;
138-
this.totalFileCount = totalFileCount;
139-
this.totalSize = totalSize;
140-
this.indexUUID = indexUUID;
141-
this.remoteStoreRepository = remoteStoreRepository;
142-
this.repositoryBasePath = repositoryBasePath;
143-
this.fileNames = fileNames;
144152
}
145153

146154
private RemoteStoreShardShallowCopySnapshot(
@@ -156,7 +164,9 @@ private RemoteStoreShardShallowCopySnapshot(
156164
String indexUUID,
157165
String remoteStoreRepository,
158166
String repositoryBasePath,
159-
List<String> fileNames
167+
List<String> fileNames,
168+
PathType pathType,
169+
PathHashAlgorithm pathHashAlgorithm
160170
) {
161171
verifyParameters(
162172
version,
@@ -181,6 +191,8 @@ private RemoteStoreShardShallowCopySnapshot(
181191
this.remoteStoreRepository = remoteStoreRepository;
182192
this.repositoryBasePath = repositoryBasePath;
183193
this.fileNames = fileNames;
194+
this.pathType = pathType;
195+
this.pathHashAlgorithm = pathHashAlgorithm;
184196
}
185197

186198
/**
@@ -203,6 +215,8 @@ public static RemoteStoreShardShallowCopySnapshot fromXContent(XContentParser pa
203215
long primaryTerm = -1;
204216
long commitGeneration = -1;
205217
List<String> fileNames = new ArrayList<>();
218+
PathType pathType = null;
219+
PathHashAlgorithm pathHashAlgorithm = null;
206220

207221
if (parser.currentToken() == null) { // fresh parser? move to the first token
208222
parser.nextToken();
@@ -237,6 +251,10 @@ public static RemoteStoreShardShallowCopySnapshot fromXContent(XContentParser pa
237251
remoteStoreRepository = parser.text();
238252
} else if (PARSE_REPOSITORY_BASE_PATH.match(currentFieldName, parser.getDeprecationHandler())) {
239253
repositoryBasePath = parser.text();
254+
} else if (PARSE_PATH_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
255+
pathType = PathType.fromCode(parser.intValue());
256+
} else if (PARSE_PATH_HASH_ALGORITHM.match(currentFieldName, parser.getDeprecationHandler())) {
257+
pathHashAlgorithm = PathHashAlgorithm.fromCode(parser.intValue());
240258
} else {
241259
throw new OpenSearchParseException("unknown parameter [{}]", currentFieldName);
242260
}
@@ -266,7 +284,9 @@ public static RemoteStoreShardShallowCopySnapshot fromXContent(XContentParser pa
266284
indexUUID,
267285
remoteStoreRepository,
268286
repositoryBasePath,
269-
fileNames
287+
fileNames,
288+
pathType,
289+
pathHashAlgorithm
270290
);
271291
}
272292

@@ -433,7 +453,9 @@ public RemoteStoreShardShallowCopySnapshot asClone(String targetSnapshotName, lo
433453
indexUUID,
434454
remoteStoreRepository,
435455
repositoryBasePath,
436-
fileNames
456+
fileNames,
457+
pathType,
458+
pathHashAlgorithm
437459
);
438460
}
439461

@@ -449,4 +471,11 @@ public IndexShardSnapshotStatus getIndexShardSnapshotStatus() {
449471
null
450472
); // Not adding a real generation here as it doesn't matter to callers
451473
}
474+
475+
public RemoteStorePathStrategy getRemoteStorePathStrategy() {
476+
if (Objects.nonNull(pathType) && Objects.nonNull(pathHashAlgorithm)) {
477+
return new RemoteStorePathStrategy(pathType, pathHashAlgorithm);
478+
}
479+
return new RemoteStorePathStrategy(PathType.FIXED);
480+
}
452481
}

server/src/main/java/org/opensearch/repositories/blobstore/BlobStoreRepository.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
import org.opensearch.core.xcontent.NamedXContentRegistry;
109109
import org.opensearch.core.xcontent.XContentParser;
110110
import org.opensearch.index.mapper.MapperService;
111-
import org.opensearch.index.remote.RemoteStoreEnums.PathType;
112111
import org.opensearch.index.remote.RemoteStorePathStrategy;
113112
import org.opensearch.index.snapshots.IndexShardRestoreFailedException;
114113
import org.opensearch.index.snapshots.IndexShardSnapshotStatus;
@@ -672,8 +671,7 @@ public void cloneRemoteStoreIndexShardSnapshot(
672671
remoteStoreRepository,
673672
indexUUID,
674673
String.valueOf(shardId.shardId()),
675-
new RemoteStorePathStrategy(PathType.FIXED)
676-
// TODO - The path type needs to be obtained from RemoteStoreShardShallowCopySnapshot
674+
remStoreBasedShardMetadata.getRemoteStorePathStrategy()
677675
);
678676
remoteStoreMetadataLockManger.cloneLock(
679677
FileLockInfo.getLockInfoBuilder().withAcquirerId(source.getUUID()).build(),
@@ -1154,8 +1152,7 @@ protected void releaseRemoteStoreLockAndCleanup(
11541152
remoteStoreRepoForIndex,
11551153
indexUUID,
11561154
shardId,
1157-
new RemoteStorePathStrategy(PathType.FIXED)
1158-
// TODO - The path type needs to be obtained from RemoteStoreShardShallowCopySnapshot
1155+
remoteStoreShardShallowCopySnapshot.getRemoteStorePathStrategy()
11591156
);
11601157
remoteStoreMetadataLockManager.release(FileLockInfo.getLockInfoBuilder().withAcquirerId(shallowSnapshotUUID).build());
11611158
logger.debug("Successfully released lock for shard {} of index with uuid {}", shardId, indexUUID);
@@ -1178,8 +1175,7 @@ protected void releaseRemoteStoreLockAndCleanup(
11781175
indexUUID,
11791176
new ShardId(Index.UNKNOWN_INDEX_NAME, indexUUID, Integer.parseInt(shardId)),
11801177
ThreadPool.Names.REMOTE_PURGE,
1181-
new RemoteStorePathStrategy(PathType.FIXED)
1182-
// TODO - The path type needs to be obtained from RemoteStoreShardShallowCopySnapshot
1178+
remoteStoreShardShallowCopySnapshot.getRemoteStorePathStrategy()
11831179
);
11841180
}
11851181
}
@@ -2694,6 +2690,7 @@ public void snapshotRemoteStoreIndexShard(
26942690
// now create and write the commit point
26952691
logger.trace("[{}] [{}] writing shard snapshot file", shardId, snapshotId);
26962692
try {
2693+
RemoteStorePathStrategy pathStrategy = store.indexSettings().getRemoteStorePathStrategy();
26972694
REMOTE_STORE_SHARD_SHALLOW_COPY_SNAPSHOT_FORMAT.write(
26982695
new RemoteStoreShardShallowCopySnapshot(
26992696
snapshotId.getName(),
@@ -2707,7 +2704,9 @@ public void snapshotRemoteStoreIndexShard(
27072704
store.indexSettings().getUUID(),
27082705
store.indexSettings().getRemoteStoreRepository(),
27092706
this.basePath().toString(),
2710-
fileNames
2707+
fileNames,
2708+
pathStrategy.getType(),
2709+
pathStrategy.getHashAlgorithm()
27112710
),
27122711
shardContainer,
27132712
snapshotId.getUUID(),

0 commit comments

Comments
 (0)