forked from apache/ozone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HDDS-11963. Unify client version and layout version under one interfa…
…ce to accomodate in the request validator framework Change-Id: I4c1844a1dfb6127a73b46a92933db33b09c6b06e
- Loading branch information
1 parent
f5ff2f0
commit 49efb20
Showing
6 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/Version.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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; | ||
|
||
|
||
/** | ||
* Base class defining the version in the entire system. | ||
*/ | ||
public interface Version { | ||
int version(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...manager/src/main/java/org/apache/hadoop/ozone/om/request/validation/VersionExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.request.validation; | ||
|
||
import org.apache.hadoop.ozone.ClientVersion; | ||
import org.apache.hadoop.ozone.Version; | ||
import org.apache.hadoop.ozone.om.upgrade.OMLayoutFeature; | ||
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; | ||
import org.apache.hadoop.ozone.upgrade.LayoutVersionManager; | ||
|
||
/** | ||
* Class to extract version out of OM request. | ||
*/ | ||
public enum VersionExtractor { | ||
/** | ||
* Extracts current metadata layout version. | ||
*/ | ||
LAYOUT_VERSION_EXTRACTOR { | ||
@Override | ||
public Version extractVersion(OMRequest req, ValidationContext ctx) { | ||
LayoutVersionManager layoutVersionManager = ctx.versionManager(); | ||
return ctx.versionManager().getFeature(layoutVersionManager.getMetadataLayoutVersion()); | ||
} | ||
|
||
@Override | ||
public Class<OMLayoutFeature> getVersionClass() { | ||
return OMLayoutFeature.class; | ||
} | ||
}, | ||
|
||
/** | ||
* Extracts client version from the OMRequests. | ||
*/ | ||
CLIENT_VERSION_EXTRACTOR { | ||
@Override | ||
public Version extractVersion(OMRequest req, ValidationContext ctx) { | ||
return req.getVersion() > ClientVersion.CURRENT_VERSION ? | ||
ClientVersion.FUTURE_VERSION : ClientVersion.fromProtoValue(req.getVersion()); | ||
} | ||
|
||
@Override | ||
public Class<ClientVersion> getVersionClass() { | ||
return ClientVersion.class; | ||
} | ||
}; | ||
|
||
public abstract Version extractVersion(OMRequest req, ValidationContext ctx); | ||
public abstract Class<? extends Version> getVersionClass(); | ||
} |
58 changes: 58 additions & 0 deletions
58
...ger/src/test/java/org/apache/hadoop/ozone/om/request/validation/TestVersionExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.apache.hadoop.ozone.om.request.validation; | ||
|
||
import org.apache.hadoop.ozone.ClientVersion; | ||
import org.apache.hadoop.ozone.Version; | ||
import org.apache.hadoop.ozone.om.exceptions.OMException; | ||
import org.apache.hadoop.ozone.om.upgrade.OMLayoutFeature; | ||
import org.apache.hadoop.ozone.om.upgrade.OMLayoutVersionManager; | ||
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; | ||
import org.apache.hadoop.ozone.upgrade.LayoutVersionManager; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.Arrays; | ||
import java.util.function.Function; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class TestVersionExtractor { | ||
|
||
private static Stream<Arguments> layoutValues() { | ||
return Arrays.stream(OMLayoutFeature.values()).map(Arguments::of); | ||
} | ||
|
||
private static Stream<Arguments> clientVersionValues() { | ||
return Stream.of( | ||
Arrays.stream(ClientVersion.values()) | ||
.map(clientVersion -> Arguments.of(clientVersion, clientVersion.version())), | ||
IntStream.range(1, 10) | ||
.mapToObj(delta -> Arguments.of(ClientVersion.FUTURE_VERSION, ClientVersion.CURRENT_VERSION + delta)) | ||
).flatMap(Function.identity()); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("layoutValues") | ||
void testLayoutVersionExtractor(OMLayoutFeature layoutVersionValue) throws OMException { | ||
ValidationContext context = mock(ValidationContext.class); | ||
LayoutVersionManager layoutVersionManager = new OMLayoutVersionManager(layoutVersionValue.version()); | ||
when(context.versionManager()).thenReturn(layoutVersionManager); | ||
Version version = VersionExtractor.LAYOUT_VERSION_EXTRACTOR.extractVersion(null, context); | ||
assertEquals(layoutVersionValue, version); | ||
assertEquals(OMLayoutFeature.class, VersionExtractor.LAYOUT_VERSION_EXTRACTOR.getVersionClass()); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("clientVersionValues") | ||
void testClientVersionExtractor(ClientVersion expectedClientVersion, int clientVersion) { | ||
OMRequest request = mock(OMRequest.class); | ||
when(request.getVersion()).thenReturn(clientVersion); | ||
Version version = VersionExtractor.CLIENT_VERSION_EXTRACTOR.extractVersion(request, null); | ||
assertEquals(expectedClientVersion, version); | ||
assertEquals(ClientVersion.class, VersionExtractor.CLIENT_VERSION_EXTRACTOR.getVersionClass()); | ||
} | ||
} |