Skip to content

Commit 7af38a9

Browse files
authored
HDDS-11963. Add parent interface of component and layout versions for use in request validator (#7598)
1 parent 0306d97 commit 7af38a9

6 files changed

Lines changed: 174 additions & 4 deletions

File tree

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/ComponentVersion.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
*/
1818
package org.apache.hadoop.hdds;
1919

20+
import org.apache.hadoop.ozone.Versioned;
21+
2022
/**
2123
* Base type for component version enums.
2224
*/
23-
public interface ComponentVersion {
25+
public interface ComponentVersion extends Versioned {
2426

2527
/**
2628
* Returns the description of the version enum value.
@@ -34,4 +36,9 @@ public interface ComponentVersion {
3436
* @return the version associated with the enum value.
3537
*/
3638
int toProtoValue();
39+
40+
@Override
41+
default int version() {
42+
return toProtoValue();
43+
}
3744
}

hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/ClientVersion.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.hadoop.hdds.ComponentVersion;
2121

2222
import java.util.Arrays;
23+
import java.util.Comparator;
2324
import java.util.Map;
2425

2526
import static java.util.function.Function.identity;
@@ -75,8 +76,8 @@ public static ClientVersion fromProtoValue(int value) {
7576
}
7677

7778
private static ClientVersion latest() {
78-
ClientVersion[] versions = ClientVersion.values();
79-
return versions[versions.length - 2];
79+
return Arrays.stream(ClientVersion.values())
80+
.max(Comparator.comparingInt(ComponentVersion::toProtoValue)).orElse(null);
8081
}
8182

8283
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.ozone;
19+
20+
21+
/**
22+
* Base class defining the version in the entire system.
23+
*/
24+
public interface Versioned {
25+
int version();
26+
}

hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/upgrade/LayoutFeature.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
package org.apache.hadoop.ozone.upgrade;
2020

21+
import org.apache.hadoop.ozone.Versioned;
22+
2123
import java.util.Optional;
2224

2325
/**
2426
* Generic Layout feature interface for Ozone.
2527
*/
26-
public interface LayoutFeature {
28+
public interface LayoutFeature extends Versioned {
2729
String name();
2830

2931
int layoutVersion();
@@ -48,6 +50,11 @@ default String name() {
4850
void execute(T arg) throws Exception;
4951
}
5052

53+
@Override
54+
default int version() {
55+
return this.layoutVersion();
56+
}
57+
5158
/**
5259
* Phase of execution for this action.
5360
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* <p>
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
package org.apache.hadoop.ozone.om.request.validation;
18+
19+
import org.apache.hadoop.ozone.ClientVersion;
20+
import org.apache.hadoop.ozone.Versioned;
21+
import org.apache.hadoop.ozone.om.upgrade.OMLayoutFeature;
22+
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
23+
import org.apache.hadoop.ozone.upgrade.LayoutVersionManager;
24+
25+
/**
26+
* Class to extract version out of OM request.
27+
*/
28+
public enum VersionExtractor {
29+
/**
30+
* Extracts current metadata layout version.
31+
*/
32+
LAYOUT_VERSION_EXTRACTOR {
33+
@Override
34+
public Versioned extractVersion(OMRequest req, ValidationContext ctx) {
35+
LayoutVersionManager layoutVersionManager = ctx.versionManager();
36+
return ctx.versionManager().getFeature(layoutVersionManager.getMetadataLayoutVersion());
37+
}
38+
39+
@Override
40+
public Class<OMLayoutFeature> getVersionClass() {
41+
return OMLayoutFeature.class;
42+
}
43+
},
44+
45+
/**
46+
* Extracts client version from the OMRequests.
47+
*/
48+
CLIENT_VERSION_EXTRACTOR {
49+
@Override
50+
public Versioned extractVersion(OMRequest req, ValidationContext ctx) {
51+
return req.getVersion() > ClientVersion.CURRENT_VERSION ?
52+
ClientVersion.FUTURE_VERSION : ClientVersion.fromProtoValue(req.getVersion());
53+
}
54+
55+
@Override
56+
public Class<ClientVersion> getVersionClass() {
57+
return ClientVersion.class;
58+
}
59+
};
60+
61+
public abstract Versioned extractVersion(OMRequest req, ValidationContext ctx);
62+
public abstract Class<? extends Versioned> getVersionClass();
63+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* <p>
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
package org.apache.hadoop.ozone.om.request.validation;
18+
19+
import org.apache.hadoop.ozone.ClientVersion;
20+
import org.apache.hadoop.ozone.Versioned;
21+
import org.apache.hadoop.ozone.om.exceptions.OMException;
22+
import org.apache.hadoop.ozone.om.upgrade.OMLayoutFeature;
23+
import org.apache.hadoop.ozone.om.upgrade.OMLayoutVersionManager;
24+
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
25+
import org.apache.hadoop.ozone.upgrade.LayoutVersionManager;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.EnumSource;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.when;
33+
34+
class TestVersionExtractor {
35+
36+
@ParameterizedTest
37+
@EnumSource(OMLayoutFeature.class)
38+
void testLayoutVersionExtractor(OMLayoutFeature layoutVersionValue) throws OMException {
39+
ValidationContext context = mock(ValidationContext.class);
40+
LayoutVersionManager layoutVersionManager = new OMLayoutVersionManager(layoutVersionValue.version());
41+
when(context.versionManager()).thenReturn(layoutVersionManager);
42+
Versioned version = VersionExtractor.LAYOUT_VERSION_EXTRACTOR.extractVersion(null, context);
43+
assertEquals(layoutVersionValue, version);
44+
assertEquals(OMLayoutFeature.class, VersionExtractor.LAYOUT_VERSION_EXTRACTOR.getVersionClass());
45+
}
46+
47+
@ParameterizedTest
48+
@EnumSource(ClientVersion.class)
49+
void testClientVersionExtractor(ClientVersion expectedClientVersion) {
50+
OMRequest request = mock(OMRequest.class);
51+
when(request.getVersion()).thenReturn(expectedClientVersion.version());
52+
Versioned version = VersionExtractor.CLIENT_VERSION_EXTRACTOR.extractVersion(request, null);
53+
assertEquals(expectedClientVersion, version);
54+
assertEquals(ClientVersion.class, VersionExtractor.CLIENT_VERSION_EXTRACTOR.getVersionClass());
55+
}
56+
57+
@ParameterizedTest
58+
@ValueSource(ints = {1, 2, 5, 10, 1000, 10000})
59+
void testClientVersionExtractorForFutureValues(int futureVersion) {
60+
OMRequest request = mock(OMRequest.class);
61+
when(request.getVersion()).thenReturn(ClientVersion.CURRENT_VERSION + futureVersion);
62+
Versioned version = VersionExtractor.CLIENT_VERSION_EXTRACTOR.extractVersion(request, null);
63+
assertEquals(ClientVersion.FUTURE_VERSION, version);
64+
assertEquals(ClientVersion.class, VersionExtractor.CLIENT_VERSION_EXTRACTOR.getVersionClass());
65+
}
66+
}

0 commit comments

Comments
 (0)