Skip to content

Commit 67cded1

Browse files
committed
Add a FormatVersionTestUtils.previous
This retrieves the format version previous to the given one, which is very useful for upgrade tests. Since this is only used for upgrade tests, I felt it was better to add as a test utils, rather than a method on FormatVersion itself
1 parent adf47d9 commit 67cded1

File tree

8 files changed

+68
-21
lines changed

8 files changed

+68
-21
lines changed

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/FDBRecordStoreFormatVersionTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public class FDBRecordStoreFormatVersionTest extends FDBRecordStoreTestBase {
4545

4646
@Test
4747
public void testFormatVersionUpgrade() {
48-
final List<FormatVersion> sortedVersions = Arrays.stream(FormatVersion.values()).sorted().collect(Collectors.toList());
49-
FormatVersion penultimateVersion = sortedVersions.get(sortedVersions.size() - 2);
48+
FormatVersion penultimateVersion = FormatVersionTestUtils.previous(FormatVersion.getMaximumSupportedVersion())
5049
assertTrue(penultimateVersion.compareTo(FormatVersion.getMaximumSupportedVersion()) < 0);
5150
try (FDBRecordContext context = openContext()) {
5251
recordStore = getStoreBuilder(context, simpleMetaData(NO_HOOK))
@@ -83,7 +82,7 @@ public void testAccessUserFieldAtOldFormatVersion() {
8382
final RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
8483
FDBRecordStore.Builder storeBuilder = FDBRecordStore.newBuilder()
8584
.setKeySpacePath(path).setMetaDataProvider(metaData)
86-
.setFormatVersion(FormatVersion.CACHEABLE_STATE);
85+
.setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.HEADER_USER_FIELDS));
8786
try (FDBRecordContext context = openContext()) {
8887
recordStore = storeBuilder.setContext(context).create();
8988
RecordCoreException err = assertThrows(RecordCoreException.class,
@@ -100,7 +99,8 @@ public void testAccessUserFieldAtOldFormatVersion() {
10099
}
101100
try (FDBRecordContext context = openContext()) {
102101
recordStore = storeBuilder.setFormatVersion(FormatVersion.INFO_ADDED).setContext(context).open();
103-
assertEquals(FormatVersion.CACHEABLE_STATE, recordStore.getFormatVersionEnum());
102+
assertEquals(FormatVersionTestUtils.previous(FormatVersion.HEADER_USER_FIELDS),
103+
recordStore.getFormatVersionEnum());
104104
RecordCoreException err = assertThrows(RecordCoreException.class,
105105
() -> recordStore.clearHeaderUserField("foo"));
106106
assertEquals(expectedErrMsg, err.getMessage());

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/FDBRecordStoreSplitRecordsTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ public void unsplitCompatibility(SplitRecordsTestConfig testConfig) throws Excep
134134
try (FDBRecordContext context = openContext()) {
135135
// Write a record using the old format
136136
recordStore = getStoreBuilder(context, simpleMetaData(NO_HOOK))
137-
.setFormatVersion(FormatVersion.FORMAT_CONTROL)
137+
.setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX))
138138
.create();
139-
assertEquals(FormatVersion.FORMAT_CONTROL, recordStore.getFormatVersionEnum());
139+
assertEquals(FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX), recordStore.getFormatVersionEnum());
140140
recordStore.saveRecord(rec1);
141141
final byte[] rec1Key = recordStore.getSubspace().pack(Tuple.from(FDBRecordStore.RECORD_KEY, 1415L));
142142
FDBStoredRecord<Message> readRec1 = recordStore.loadRecord(Tuple.from(1415L));
@@ -235,7 +235,7 @@ public void unsplitCompatibility(SplitRecordsTestConfig testConfig) throws Excep
235235
uncheckedOpenSimpleRecordStore(context, metaDataBuilder ->
236236
metaDataBuilder.addUniversalIndex(new Index("global$newCount", globalCountIndex().getRootExpression(), IndexTypes.COUNT))
237237
);
238-
recordStore = recordStore.asBuilder().setFormatVersion(FormatVersion.FORMAT_CONTROL).open();
238+
recordStore = recordStore.asBuilder().setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX)).open();
239239
assertEquals(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX, recordStore.getFormatVersionEnum());
240240

241241
final byte[] rawKey1 = recordStore.getSubspace().pack(Tuple.from(FDBRecordStore.RECORD_KEY, 1415L, SplitHelper.UNSPLIT_RECORD));
@@ -276,7 +276,7 @@ public void clearOmitUnsplitRecordSuffix(ClearOmitUnsplitRecordSuffixMode mode)
276276
FDBRecordStore.Builder builder = FDBRecordStore.newBuilder()
277277
.setKeySpacePath(path)
278278
.setMetaDataProvider(metaData)
279-
.setFormatVersion(FormatVersion.FORMAT_CONTROL);
279+
.setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX));
280280

281281
try (FDBRecordContext context = openContext()) {
282282
recordStore = builder.setContext(context).create();
@@ -318,7 +318,7 @@ public void clearOmitUnsplitRecordSuffixTyped() {
318318
FDBRecordStore.Builder builder = FDBRecordStore.newBuilder()
319319
.setKeySpacePath(path)
320320
.setMetaDataProvider(metaData)
321-
.setFormatVersion(FormatVersion.FORMAT_CONTROL);
321+
.setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX));
322322

323323
final FDBStoredRecord<Message> saved;
324324
try (FDBRecordContext context = openContext()) {
@@ -349,7 +349,7 @@ public void clearOmitUnsplitRecordSuffixOverlapping() {
349349
FDBRecordStore.Builder builder = FDBRecordStore.newBuilder()
350350
.setKeySpacePath(path)
351351
.setMetaDataProvider(metaData)
352-
.setFormatVersion(FormatVersion.FORMAT_CONTROL);
352+
.setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX));
353353

354354
try (FDBRecordContext context = openContext()) {
355355
recordStore = builder.setContext(context).create();

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/FDBRecordStoreTestBase.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@ public FDBRecordStoreTestBase(@Nullable KeySpacePath path) {
7676

7777
@Nonnull
7878
public static Stream<Arguments> formatVersionAndSplitArgs() {
79-
return Stream.of(FormatVersion.FORMAT_CONTROL, FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX, FormatVersion.getMaximumSupportedVersion())
80-
.flatMap(formatVersion -> Stream.of(Arguments.of(formatVersion, false), Arguments.of(formatVersion, true)));
79+
return Stream.of(
80+
FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX),
81+
FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX,
82+
FormatVersion.getMaximumSupportedVersion())
83+
.flatMap(formatVersion -> Stream.of(
84+
Arguments.of(formatVersion, false),
85+
Arguments.of(formatVersion, true)));
8186
}
8287

8388
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* FormatVersionTestUtils.java
3+
*
4+
* This source file is part of the FoundationDB open source project
5+
*
6+
* Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package com.apple.foundationdb.record.provider.foundationdb;
22+
23+
import java.util.Arrays;
24+
import java.util.Comparator;
25+
26+
/**
27+
* Utility class to support testing with various {@link FormatVersion FormatVersions}.
28+
*/
29+
public final class FormatVersionTestUtils {
30+
31+
private FormatVersionTestUtils() {
32+
}
33+
34+
public static FormatVersion previous(FormatVersion version) {
35+
return Arrays.stream(FormatVersion.values())
36+
.filter(other -> other.compareTo(version) < 0)
37+
.max(Comparator.naturalOrder())
38+
.orElseThrow(() -> new IllegalArgumentException("No versions previous to " + version));
39+
}
40+
}

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/OnlineIndexerIndexFromIndexTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void testCanBuildNonIdempotentIndexFromIndexOnNewStoreWithOldFormatVersionInInde
218218
openSimpleMetaData(hook);
219219
// Set a format version on the store that does not allow index-from-index builds
220220
// Because the store already has this format version, though it should be allowed
221-
this.formatVersion = FormatVersion.READABLE_UNIQUE_PENDING;
221+
this.formatVersion = FormatVersionTestUtils.previous(FormatVersion.CHECK_INDEX_BUILD_TYPE_DURING_UPDATE);
222222
try (OnlineIndexer indexBuilder = newIndexerBuilder(tgtIndex, timer)
223223
.setIndexingPolicy(OnlineIndexer.IndexingPolicy.newBuilder()
224224
.setSourceIndex("src_index")
@@ -237,7 +237,7 @@ void testCanBuildNonIdempotentIndexFromIndexOnNewStoreWithOldFormatVersionInInde
237237
@BooleanSource
238238
void testNonIdempotentIndexFromIndexOldFormatFallback(boolean reverseScan) {
239239
// Attempt to build a non-idempotent index at an older format version. This should fall back to a full record scan
240-
this.formatVersion = FormatVersion.READABLE_UNIQUE_PENDING;
240+
this.formatVersion = FormatVersionTestUtils.previous(FormatVersion.CHECK_INDEX_BUILD_TYPE_DURING_UPDATE);
241241
final FDBStoreTimer timer = new FDBStoreTimer();
242242
final long numRecords = 6;
243243
final long otherRecords = 5;
@@ -270,7 +270,7 @@ void testNonIdempotentIndexFromIndexOldFormatFallback(boolean reverseScan) {
270270
void testNonIdempotentIndexFromIndexOldFormatNoFallback() {
271271
// Attempt to build a non-idempotent index at old format version where this is not supported. This should
272272
// error as falling back to a record scan is not enabled
273-
this.formatVersion = FormatVersion.READABLE_UNIQUE_PENDING;
273+
this.formatVersion = FormatVersionTestUtils.previous(FormatVersion.CHECK_INDEX_BUILD_TYPE_DURING_UPDATE);
274274
final FDBStoreTimer timer = new FDBStoreTimer();
275275
final long numRecords = 7;
276276
final long otherRecords = 8;

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/RemoteFetchOldVersionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class RemoteFetchOldVersionTest extends RemoteFetchTestBase {
5656

5757
@BeforeEach
5858
void setup() throws Exception {
59-
complexQuerySetupWithVersion(simpleVersionHook, FormatVersion.FORMAT_CONTROL);
59+
complexQuerySetupWithVersion(simpleVersionHook, FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX));
6060
}
6161

6262
@ParameterizedTest
@@ -66,7 +66,7 @@ void oldVersionFormatTest(IndexFetchMethod useIndexPrefetch) throws Exception {
6666

6767
int count = 0;
6868
try (FDBRecordContext context = openContext()) {
69-
openStoreWithVersion(context, simpleVersionHook, FormatVersion.FORMAT_CONTROL);
69+
openStoreWithVersion(context, simpleVersionHook, FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX));
7070
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan, null, ExecuteProperties.SERIAL_EXECUTE).asIterator()) {
7171
while (cursor.hasNext()) {
7272
FDBQueriedRecord<Message> record = cursor.next();

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/indexes/VersionIndexTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer;
6565
import com.apple.foundationdb.record.provider.foundationdb.FDBStoredRecord;
6666
import com.apple.foundationdb.record.provider.foundationdb.FormatVersion;
67+
import com.apple.foundationdb.record.provider.foundationdb.FormatVersionTestUtils;
6768
import com.apple.foundationdb.record.provider.foundationdb.IndexOrphanBehavior;
6869
import com.apple.foundationdb.record.provider.foundationdb.IndexScanBounds;
6970
import com.apple.foundationdb.record.provider.foundationdb.IndexScanRange;
@@ -261,7 +262,7 @@ public void setUp() {
261262

262263
private static Stream<FormatVersion> formatVersionsOfInterest() {
263264
return Stream.of(
264-
FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX,
265+
FormatVersionTestUtils.previous(FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX),
265266
FormatVersion.SAVE_UNSPLIT_WITH_SUFFIX,
266267
FormatVersion.SAVE_VERSION_WITH_RECORD,
267268
FormatVersion.getMaximumSupportedVersion()

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/storestate/FDBRecordStoreStateCacheTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase;
4040
import com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer;
4141
import com.apple.foundationdb.record.provider.foundationdb.FormatVersion;
42+
import com.apple.foundationdb.record.provider.foundationdb.FormatVersionTestUtils;
4243
import com.apple.foundationdb.record.provider.foundationdb.RecordStoreAlreadyExistsException;
4344
import com.apple.foundationdb.record.provider.foundationdb.RecordStoreNoInfoAndNotEmptyException;
4445
import com.apple.foundationdb.record.provider.foundationdb.RecordStoreStaleMetaDataVersionException;
@@ -1126,7 +1127,7 @@ void doNotSetCacheabilityDuringCheckVersionOnOldFormatVersion() throws Exception
11261127
// do not commit
11271128
}
11281129

1129-
storeBuilder.setFormatVersion(FormatVersion.SAVE_VERSION_WITH_RECORD);
1130+
storeBuilder.setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.CACHEABLE_STATE));
11301131

11311132
try (FDBRecordContext context = openContext()) {
11321133
recordStore = storeBuilder
@@ -1166,7 +1167,7 @@ void doNotSetCacheabilityDuringCheckVersionOnOldFormatVersion() throws Exception
11661167
recordStore = storeBuilder
11671168
.setContext(context)
11681169
.setStateCacheabilityOnOpen(FDBRecordStore.StateCacheabilityOnOpen.CACHEABLE)
1169-
.setFormatVersion(FormatVersion.SAVE_VERSION_WITH_RECORD)
1170+
.setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.CACHEABLE_STATE))
11701171
.open();
11711172
assertCacheable();
11721173
commit(context);
@@ -1205,7 +1206,7 @@ public void setCacheableAtWrongFormatVersion() throws Exception {
12051206
FDBRecordStore.Builder storeBuilder = FDBRecordStore.newBuilder()
12061207
.setKeySpacePath(path)
12071208
.setMetaDataProvider(RecordMetaData.build(TestRecords1Proto.getDescriptor()))
1208-
.setFormatVersion(FormatVersion.SAVE_VERSION_WITH_RECORD);
1209+
.setFormatVersion(FormatVersionTestUtils.previous(FormatVersion.CACHEABLE_STATE));
12091210
try (FDBRecordContext context = openContext()) {
12101211
storeBuilder.copyBuilder().setContext(context).create();
12111212
commit(context);

0 commit comments

Comments
 (0)