Skip to content

Commit 565c3f7

Browse files
authored
feat: Implement incremental update protocol with multi-version compatibility (#1611)
* Add incremental protocol comparison tool * Add protocol version on the server * Add protocol version on the client * Add protocol upgrade test code * Remove the commented test method * Centralize protocol version constant * Extract helper methods for coreSize and maxSize * Add cross-version compatibility tests for incremental protocol * Fix extended parameter loss in config refresh due to protocol version misuse * Eliminate hardcoded protocol version and implement field-level version control * Support semantic version-aware incremental md5 comparison * Remove protocol version mechanism
1 parent 9aa3be0 commit 565c3f7

File tree

16 files changed

+1813
-33
lines changed

16 files changed

+1813
-33
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 cn.hippo4j.common.model;
19+
20+
import java.util.Collections;
21+
import java.util.Map;
22+
23+
/**
24+
* Optional provider for field-version metadata.
25+
* <p>
26+
* Thread pool parameter models implementing this interface can explicitly declare the
27+
* relationship between fields and the protocol versions that understand them. This allows
28+
* the incremental content builder to omit unsupported fields for legacy clients and avoid
29+
* unnecessary refresh loops triggered by unknown data.
30+
*/
31+
public interface IncrementalFieldMetadataProvider {
32+
33+
/**
34+
* Return a mapping of field name to the minimum protocol version that can observe it.
35+
*
36+
* @return field -> minimum semantic version; fields not present fall back to defaults
37+
*/
38+
default Map<String, String> getFieldVersionMetadata() {
39+
return Collections.emptyMap();
40+
}
41+
42+
/**
43+
* Optional version string of the metadata definition, useful for caching or diagnostics.
44+
*
45+
* @return metadata version identifier, or {@code null} if not set
46+
*/
47+
default String getFieldMetadataVersion() {
48+
return null;
49+
}
50+
}

infra/common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameterInfo.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import lombok.experimental.Accessors;
2626

2727
import java.io.Serializable;
28+
import java.util.Map;
2829

2930
/**
3031
* Thread pool parameter info.
@@ -34,7 +35,7 @@
3435
@NoArgsConstructor
3536
@AllArgsConstructor
3637
@Accessors(chain = true)
37-
public class ThreadPoolParameterInfo implements ThreadPoolParameter, Serializable {
38+
public class ThreadPoolParameterInfo implements ThreadPoolParameter, Serializable, IncrementalFieldMetadataProvider {
3839

3940
private static final long serialVersionUID = -7123935122108553864L;
4041

@@ -127,6 +128,16 @@ public class ThreadPoolParameterInfo implements ThreadPoolParameter, Serializabl
127128
*/
128129
private Integer allowCoreThreadTimeOut;
129130

131+
/**
132+
* Field-to-minimum-version mapping used by clients to filter unsupported fields.
133+
*/
134+
private Map<String, String> fieldVersionMetadata;
135+
136+
/**
137+
* Optional metadata version identifier for diagnostics or caching.
138+
*/
139+
private String fieldMetadataVersion;
140+
130141
public Integer corePoolSizeAdapt() {
131142
return this.corePoolSize == null ? this.coreSize : this.corePoolSize;
132143
}

infra/common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
package cn.hippo4j.common.toolkit;
1919

2020
import cn.hippo4j.common.constant.Constants;
21+
import cn.hippo4j.common.model.IncrementalFieldMetadataProvider;
2122
import cn.hippo4j.common.model.ThreadPoolParameter;
2223
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
2324

25+
import java.util.LinkedHashMap;
26+
import java.util.Map;
27+
2428
/**
2529
* Content util.
2630
*/
@@ -37,8 +41,12 @@ public static String getPoolContent(ThreadPoolParameter parameter) {
3741
threadPoolParameterInfo.setTenantId(parameter.getTenantId())
3842
.setItemId(parameter.getItemId())
3943
.setTpId(parameter.getTpId())
40-
.setCoreSize(parameter.getCoreSize())
41-
.setMaxSize(parameter.getMaxSize())
44+
.setCoreSize((parameter instanceof ThreadPoolParameterInfo)
45+
? ((ThreadPoolParameterInfo) parameter).corePoolSizeAdapt()
46+
: parameter.getCoreSize())
47+
.setMaxSize((parameter instanceof ThreadPoolParameterInfo)
48+
? ((ThreadPoolParameterInfo) parameter).maximumPoolSizeAdapt()
49+
: parameter.getMaxSize())
4250
.setQueueType(parameter.getQueueType())
4351
.setCapacity(parameter.getCapacity())
4452
.setKeepAliveTime(parameter.getKeepAliveTime())
@@ -48,6 +56,14 @@ public static String getPoolContent(ThreadPoolParameter parameter) {
4856
.setLivenessAlarm(parameter.getLivenessAlarm())
4957
.setAllowCoreThreadTimeOut(parameter.getAllowCoreThreadTimeOut())
5058
.setRejectedType(parameter.getRejectedType());
59+
if (parameter instanceof IncrementalFieldMetadataProvider) {
60+
IncrementalFieldMetadataProvider provider = (IncrementalFieldMetadataProvider) parameter;
61+
Map<String, String> metadata = provider.getFieldVersionMetadata();
62+
if (metadata != null && !metadata.isEmpty()) {
63+
threadPoolParameterInfo.setFieldVersionMetadata(new LinkedHashMap<>(metadata));
64+
}
65+
threadPoolParameterInfo.setFieldMetadataVersion(provider.getFieldMetadataVersion());
66+
}
5167
return JSONUtil.toJSONString(threadPoolParameterInfo);
5268
}
5369

0 commit comments

Comments
 (0)