|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.uniffle.common.rpc; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +public class ServiceVersion { |
| 25 | + public static final ServiceVersion NEWEST_VERSION = |
| 26 | + new ServiceVersion(Feature.getNewest().getVersion()); |
| 27 | + static final Map<Integer, Feature> VALUE_MAP = |
| 28 | + Arrays.stream(Feature.values()).collect(Collectors.toMap(Feature::getVersion, s -> s)); |
| 29 | + private final int version; |
| 30 | + |
| 31 | + public ServiceVersion(int version) { |
| 32 | + this.version = version; |
| 33 | + } |
| 34 | + |
| 35 | + public Feature getCurrentFeature() { |
| 36 | + return VALUE_MAP.get(version); |
| 37 | + } |
| 38 | + |
| 39 | + public boolean supportFeature(Feature registerBlockIdLayout) { |
| 40 | + return version >= registerBlockIdLayout.getVersion(); |
| 41 | + } |
| 42 | + |
| 43 | + public int getVersion() { |
| 44 | + return version; |
| 45 | + } |
| 46 | + |
| 47 | + public enum Feature { |
| 48 | + // Treat the old version as init version |
| 49 | + INIT_VERSION(0), |
| 50 | + // Register block id layout to server to avoid sending block id layout for each getShuffleResult |
| 51 | + // request |
| 52 | + REGISTER_BLOCK_ID_LAYOUT(1), |
| 53 | + ; |
| 54 | + |
| 55 | + private final int version; |
| 56 | + |
| 57 | + Feature(int version) { |
| 58 | + this.version = version; |
| 59 | + } |
| 60 | + |
| 61 | + public int getVersion() { |
| 62 | + return version; |
| 63 | + } |
| 64 | + |
| 65 | + public static Feature getNewest() { |
| 66 | + Feature[] enumConstants = Feature.class.getEnumConstants(); |
| 67 | + return enumConstants[enumConstants.length - 1]; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments