Skip to content

Commit 839e58f

Browse files
committed
MetaExecutor interface definition and demo
1 parent 98fb07c commit 839e58f

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

src/main/java/com/alipay/oceanbase/rpc/ObTableClient.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.alipay.remoting.util.StringUtils;
4949
import org.slf4j.Logger;
5050

51+
import java.lang.reflect.Array;
5152
import java.util.*;
5253
import java.util.concurrent.ConcurrentHashMap;
5354
import java.util.concurrent.TimeUnit;
@@ -1112,6 +1113,17 @@ public ObTable getTable(ObTableApiMove moveResponse) throws Exception {
11121113
// If the node address does not exist, a new table is created
11131114
return addTable(addr);
11141115
}
1116+
1117+
public ObTable getRandomTable() {
1118+
ObTable anyTable;
1119+
if (odpMode) {
1120+
anyTable = tableRoute.getOdpTable();
1121+
} else {
1122+
ConcurrentHashMap<ObServerAddr, ObTable> tableRoster = tableRoute.getTableRoster().getTables();
1123+
anyTable = tableRoster.values().stream().findAny().orElse(null);
1124+
}
1125+
return anyTable;
1126+
}
11151127

11161128
public ObTable addTable(ObServerAddr addr){
11171129

src/main/java/com/alipay/oceanbase/rpc/bolt/protocol/ObTablePacketCode.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package com.alipay.oceanbase.rpc.bolt.protocol;
1919

2020
import com.alipay.oceanbase.rpc.exception.ObTableRoutingWrongException;
21+
import com.alipay.oceanbase.rpc.meta.ObTableMetaRequest;
22+
import com.alipay.oceanbase.rpc.meta.ObTableMetaResponse;
2123
import com.alipay.oceanbase.rpc.protocol.packet.ObRpcPacketHeader;
2224
import com.alipay.oceanbase.rpc.protocol.payload.ObPayload;
2325
import com.alipay.oceanbase.rpc.protocol.payload.Pcodes;
@@ -33,6 +35,8 @@
3335
import com.alipay.oceanbase.rpc.protocol.payload.impl.login.ObTableLoginResult;
3436
import com.alipay.remoting.CommandCode;
3537

38+
import static com.alipay.oceanbase.rpc.protocol.payload.Pcodes.OB_TABLE_API_META_INFO_EXECUTE;
39+
3640
public enum ObTablePacketCode implements CommandCode {
3741

3842
OB_TABLE_API_LOGIN(Pcodes.OB_TABLE_API_LOGIN) {
@@ -133,6 +137,12 @@ public ObPayload newPayload(ObRpcPacketHeader header) {
133137
public ObPayload newPayload(ObRpcPacketHeader header) {
134138
throw new IllegalArgumentException("OB_ERROR_PACKET has no payload implementation");
135139
}
140+
},
141+
OB_TABLE_META_INFO_EXECUTE(Pcodes.OB_TABLE_API_META_INFO_EXECUTE) {
142+
@Override
143+
public ObPayload newPayload(ObRpcPacketHeader header) {
144+
return new ObTableMetaResponse();
145+
}
136146
};
137147

138148
private short value;
@@ -175,6 +185,8 @@ public static ObTablePacketCode valueOf(short value) {
175185
return OB_TABLE_API_MOVE;
176186
case Pcodes.OB_ERROR_PACKET:
177187
return OB_ERROR_PACKET;
188+
case OB_TABLE_API_META_INFO_EXECUTE:
189+
return OB_TABLE_META_INFO_EXECUTE;
178190
}
179191
throw new IllegalArgumentException("Unknown Rpc command code value ," + value);
180192
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*-
2+
* #%L
3+
* com.oceanbase:obkv-table-client
4+
* %%
5+
* Copyright (C) 2021 - 2025 OceanBase
6+
* %%
7+
* OBKV Table Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.rpc.meta;
19+
20+
import com.alipay.oceanbase.rpc.protocol.payload.AbstractPayload;
21+
import com.alipay.oceanbase.rpc.protocol.payload.Credentialable;
22+
import com.alipay.oceanbase.rpc.protocol.payload.Pcodes;
23+
import com.alipay.oceanbase.rpc.util.ObBytesString;
24+
import com.alipay.oceanbase.rpc.util.Serialization;
25+
26+
import static com.alipay.oceanbase.rpc.util.Serialization.encodeObUniVersionHeader;
27+
import static com.alipay.oceanbase.rpc.util.Serialization.getObUniVersionHeaderLength;
28+
29+
public class ObTableMetaRequest extends AbstractPayload implements Credentialable {
30+
private ObBytesString credential;
31+
private ObTableRpcMetaType metaType;
32+
private String data;
33+
34+
@Override
35+
public void setCredential(ObBytesString credential) {
36+
this.credential = credential;
37+
}
38+
39+
@Override
40+
public int getPcode() {
41+
return Pcodes.OB_TABLE_API_META_INFO_EXECUTE;
42+
}
43+
44+
@Override
45+
public byte[] encode() {
46+
byte[] bytes = new byte[(int) getPayloadSize()];
47+
int idx = 0;
48+
49+
// 0. encode header
50+
int headerLen = (int) getObUniVersionHeaderLength(getVersion(), getPayloadContentSize());
51+
System.arraycopy(encodeObUniVersionHeader(getVersion(), getPayloadContentSize()), 0, bytes,
52+
idx, headerLen);
53+
idx += headerLen;
54+
int len = Serialization.getNeedBytes(credential);
55+
System.arraycopy(Serialization.encodeBytesString(credential), 0, bytes, idx, len);
56+
idx += len;
57+
len = Serialization.getNeedBytes(metaType.getType());
58+
System.arraycopy(Serialization.encodeI8((short)metaType.getType()), 0, bytes, idx, len);
59+
idx += len;
60+
len = Serialization.getNeedBytes(data);
61+
System.arraycopy(Serialization.encodeVString(data), 0, bytes, idx, len);
62+
return bytes;
63+
}
64+
65+
@Override
66+
public long getPayloadContentSize() {
67+
return Serialization.getNeedBytes(credential)
68+
+ Serialization.getNeedBytes(metaType.getType()) + Serialization.getNeedBytes(data);
69+
}
70+
71+
public void setMetaType(ObTableRpcMetaType metaType) {
72+
this.metaType = metaType;
73+
}
74+
75+
public ObTableRpcMetaType getMetaType() {
76+
return metaType;
77+
}
78+
79+
public void setData(String data) {
80+
this.data = data;
81+
}
82+
83+
public String getData() {
84+
return data;
85+
}
86+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*-
2+
* #%L
3+
* com.oceanbase:obkv-table-client
4+
* %%
5+
* Copyright (C) 2021 - 2025 OceanBase
6+
* %%
7+
* OBKV Table Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.rpc.meta;
19+
20+
import com.alipay.oceanbase.rpc.protocol.payload.AbstractPayload;
21+
import com.alipay.oceanbase.rpc.protocol.payload.ObPayload;
22+
import com.alipay.oceanbase.rpc.protocol.payload.Pcodes;
23+
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.ObTableResult;
24+
import com.alipay.oceanbase.rpc.util.Serialization;
25+
import io.netty.buffer.ByteBuf;
26+
27+
import static com.alipay.oceanbase.rpc.util.Serialization.encodeObUniVersionHeader;
28+
import static com.alipay.oceanbase.rpc.util.Serialization.getObUniVersionHeaderLength;
29+
30+
public class ObTableMetaResponse extends AbstractPayload {
31+
private ObTableRpcMetaType metaType; // 元信息类型
32+
private final ObTableResult header = new ObTableResult();
33+
private String data; // 服务端拿到的分片的元数据, json字符串
34+
35+
@Override
36+
public int getPcode() {
37+
return Pcodes.OB_TABLE_API_META_INFO_EXECUTE;
38+
}
39+
40+
@Override
41+
public byte[] encode() {
42+
return null;
43+
}
44+
45+
@Override
46+
public Object decode(ByteBuf buf) {
47+
super.decode(buf);
48+
// 1. decode ObTableResult
49+
header.decode(buf);
50+
// 2. decode itself
51+
data = Serialization.decodeVString(buf);
52+
53+
return this;
54+
}
55+
56+
@Override
57+
public long getPayloadContentSize() {
58+
return 0;
59+
}
60+
61+
public String getData() {
62+
return data;
63+
}
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*-
2+
* #%L
3+
* com.oceanbase:obkv-table-client
4+
* %%
5+
* Copyright (C) 2021 - 2025 OceanBase
6+
* %%
7+
* OBKV Table Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.rpc.meta;
19+
20+
// define rpc meta type enum
21+
public enum ObTableRpcMetaType {
22+
INVALID(0), TABLE_PARTITION_INFO(1), // 分区信息, 用于路由刷新
23+
HTABLE_REGION_LOCATOR(2), // 分区上下界
24+
HTABLE_REGION_METRICS(3), // 分区统计信息
25+
HTABLE_CREATE_TABLE(4), // 建表
26+
HTABLE_DELETE_TABLE(5), // 删表
27+
HTABLE_TRUNCATE_TABLE(6), // 清空表
28+
HTABLE_EXISTS(7), // 检查表是否存在
29+
HTABLE_GET_DESC(8), // 获取表元信息
30+
HTABLE_META_MAX(255);
31+
private int type;
32+
33+
ObTableRpcMetaType(int type) {
34+
this.type = type;
35+
}
36+
37+
public int getType() {
38+
return type;
39+
}
40+
}

src/main/java/com/alipay/oceanbase/rpc/protocol/payload/Pcodes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ public interface Pcodes {
3434
int OB_TABLE_API_DIRECT_LOAD = 0x1123;
3535
int OB_TABLE_API_MOVE = 0x1124;
3636
int OB_TABLE_API_LS_EXECUTE = 0x1125;
37+
int OB_TABLE_API_META_INFO_EXECUTE = 0x1128;
3738
}

0 commit comments

Comments
 (0)