rich7420 commented on code in PR #10845: URL: https://github.com/apache/ozone/pull/10845#discussion_r4758270319
########## hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestVersionIdGenerator.java: ########## @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.helpers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.stream.Stream; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.om.OMConfigKeys; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * Tests the generators that propose a versionId, and the configuration that + * selects one. + */ +public class TestVersionIdGenerator { + + /** Every generator, so the contract below is checked against all of them. */ + static Stream<VersionIdGenerator> generators() { + return Stream.of(new UniqueIdVersionIdGenerator(), + new PinnedFirstVersionIdGenerator()); + } + + /** + * The contract every generator owes: ids come out strictly increasing, and + * above the reserved unset value. A clock can still break this across OMs, + * which is what the allocator's floor is for. + */ + @ParameterizedTest + @MethodSource("generators") + void proposedIdsStrictlyIncrease(VersionIdGenerator generator) { + long previous = VersionIdGenerator.UNSET_VERSION_ID; + + for (int i = 0; i < 10000; i++) { Review Comment: With 200000 iterations this fails at call 65536, right when the `UniqueId` counter wraps. ########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/VersionIdAllocator.java: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om; + +import com.google.common.base.Preconditions; +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; +import org.apache.hadoop.ozone.om.helpers.VersionIdGenerator; + +/** + * Settles the versionId of a version as it is applied, from the id proposed for + * it before the request was replicated. + * + * <p>The versionedKeyTable orders a key's versions by + * {@code Long.MAX_VALUE - versionId}, so the ids of one key have to increase in + * the order the versions were written. A proposal is a clock reading and cannot + * promise that: ids proposed inside one millisecond can exhaust the counter + * separating them, and a leader change onto a lagging clock proposes a lower + * value. + * + * <p>So a proposal is a floor. The applied id is the later of it and the id + * after the key's current version, which the write path already holds — no read + * of its own, no global state, and identical on every OM. Under a clock + * regression an affected key's ids climb by one until proposals overtake them + * again: the versions stay ordered and only the id's reading as a time + * degrades. + */ +public class VersionIdAllocator { + + private final VersionIdGenerator generator; + + public VersionIdAllocator(ConfigurationSource conf) { + this(VersionIdGenerator.fromConfiguration(conf)); + } + + public VersionIdAllocator(VersionIdGenerator generator) { + this.generator = generator; + } + + public VersionIdGenerator getGenerator() { + return generator; + } + + /** Proposes an id, on the OM that received the request. */ + public long propose() { + final long versionId = generator.generateVersionId(); + Preconditions.checkState(versionId > VersionIdGenerator.UNSET_VERSION_ID, + "%s proposed the reserved versionId %s", + generator.getClass().getName(), versionId); + return versionId; + } + + /** + * @param proposedVersionId the id the request carried, which must be set: + * an unset field would otherwise be applied as the reserved unset value + * @param currentVersion the key's current version, or null when it has none. + * The write path holds it already, so no extra read is needed here. + * @return the versionId to freeze on the version being applied + */ + public long allocate(long proposedVersionId, OmKeyInfo currentVersion) { + Preconditions.checkArgument( + proposedVersionId > VersionIdGenerator.UNSET_VERSION_ID, + "no versionId was proposed for this version; every request that can " + + "create one has to propose it in preExecute"); + + final long versionId = + generator.versionIdFor(proposedVersionId, currentVersion != null); Review Comment: Won't replicas apply the same request with different ids during a rolling switch to the pinned generator? ########## hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestVersionIdAllocator.java: ########## @@ -0,0 +1,194 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om; + +import static org.apache.hadoop.ozone.om.helpers.PinnedFirstVersionIdGenerator.FIRST_VERSION_ID; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Field; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Deque; +import org.apache.hadoop.hdds.client.RatisReplicationConfig; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor; +import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; +import org.apache.hadoop.ozone.om.helpers.PinnedFirstVersionIdGenerator; +import org.apache.hadoop.ozone.om.helpers.UniqueIdVersionIdGenerator; +import org.apache.hadoop.ozone.om.helpers.VersionIdGenerator; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link VersionIdAllocator}: a proposal is a floor, and the id a version + * is applied with always comes after the key's current version, however the + * clock behaved. + */ +public class TestVersionIdAllocator { + + private static final String VOLUME = "vol1"; + private static final String BUCKET = "bucket1"; + private static final String KEY = "key1"; + + /** Hands out the readings it was given, standing in for a wall clock. */ + private static VersionIdGenerator clockReading(long... readings) { + final Deque<Long> queue = new ArrayDeque<>(); + Arrays.stream(readings).forEach(queue::add); + return () -> queue.size() > 1 ? queue.poll() : queue.peek(); + } + + private static VersionIdAllocator allocator() { + return new VersionIdAllocator(new UniqueIdVersionIdGenerator()); + } + + private static OmKeyInfo versionWithId(Long versionId) { + OmKeyInfo.Builder builder = new OmKeyInfo.Builder() + .setVolumeName(VOLUME) + .setBucketName(BUCKET) + .setKeyName(KEY) + .setReplicationConfig( + RatisReplicationConfig.getInstance(ReplicationFactor.THREE)); + if (versionId != null) { + builder.setVersionId(versionId); + } + return builder.build(); + } + + @Test + void theProposalComesFromTheGenerator() { + VersionIdAllocator allocator = + new VersionIdAllocator(clockReading(1234L, 5678L)); + + assertEquals(1234L, allocator.propose()); + assertEquals(5678L, allocator.propose()); + } + + @Test + void theFirstVersionOfAKeyTakesTheProposedId() { + assertEquals(5000L, allocator().allocate(5000L, null)); + } + + @Test + void aLaterVersionTakesTheProposedIdWhenTheClockMovedOn() { + assertEquals(5000L, allocator().allocate(5000L, versionWithId(4000L))); + } + + /** + * Two versions of one key proposed inside the same millisecond can exhaust + * the counter that separates them; the second still has to come after the + * first. + */ + @Test + void aRepeatedProposalStillMovesTheKeyForward() { + assertEquals(4001L, allocator().allocate(4000L, versionWithId(4000L))); + } + + /** + * The case no wall clock handles on its own: a leader change onto a node + * whose clock lags, which would otherwise sort the new version before the one + * it supersedes. + */ + @Test + void aClockThatWentBackwardsDoesNotReorderTheKey() { + assertEquals(9001L, allocator().allocate(3000L, versionWithId(9000L))); + } + + @Test + void everyAllocationComesAfterTheCurrentVersion() { + VersionIdAllocator allocator = allocator(); + long currentId = 1_000_000L; + + for (long proposed : new long[] {2_000_000L, 1L, 1_000_000L, 2_000_001L}) { + long allocated = allocator.allocate(proposed, versionWithId(currentId)); + assertTrue(allocated > currentId, + "allocated " + allocated + " does not come after " + currentId); + currentId = allocated; + } + } + + /** + * A current version written before versioning was enabled carries no id, and + * a key in that state has no other versions to order against. + */ + @Test + void aVersionPredatingVersioningDoesNotConstrainTheId() { + assertEquals(7000L, allocator().allocate(7000L, versionWithId(null))); + } + + /** + * A request that reaches apply without a proposal would otherwise take the + * reserved unset value as its id, and read back as a version that predates + * versioning. + */ + @Test + void aVersionCannotBeAppliedWithoutAProposal() { + assertThrows(IllegalArgumentException.class, + () -> allocator().allocate(VersionIdGenerator.UNSET_VERSION_ID, null)); + } + + /** + * The floor comes from the current version the write path already holds, so + * the allocator reads nothing of its own - versionedKeyTable included. It + * holds no table to read one from. + */ + @Test + void theAllocatorReadsNoTable() { + for (Field field : VersionIdAllocator.class.getDeclaredFields()) { Review Comment: A `LOG` field would fail this. Is the reflection check worth keeping? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
