Skip to content

Commit 7ec185c

Browse files
committed
add refresh flag in getPartition to get latest table and partition information
1 parent d40573e commit 7ec185c

File tree

4 files changed

+72
-126
lines changed

4 files changed

+72
-126
lines changed

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,10 +2150,10 @@ public List<ObPair<Long, ObTableParam>> getTables(String tableName, ObTableQuery
21502150

21512151
public List<ObPair<Long, ObTableParam>> getOdpTables(String tableName, ObTableQuery query,
21522152
Object[] start, boolean startInclusive,
2153-
Object[] end, boolean endInclusive)
2153+
Object[] end, boolean endInclusive, boolean needRenew)
21542154
throws Exception {
21552155
List<ObPair<Long, ObTableParam>> obTableParams = new ArrayList<ObPair<Long, ObTableParam>>();
2156-
TableEntry odpTableEntry = getOrFetchODPPartitionMeta(tableName, false);
2156+
TableEntry odpTableEntry = getOrFetchODPPartitionMeta(tableName, needRenew);
21572157

21582158
List<String> scanRangeColumns = query.getScanRangeColumns();
21592159
if (scanRangeColumns == null || scanRangeColumns.isEmpty()) {
@@ -3009,8 +3009,8 @@ public BatchOperation batchOperation(String tableName) {
30093009
* @return Partition information
30103010
* @throws Exception Exception
30113011
*/
3012-
public Partition getPartition(String tableName, Row rowKey) throws Exception {
3013-
return getSinglePartitionInternal(tableName, rowKey);
3012+
public Partition getPartition(String tableName, Row rowKey, boolean refresh) throws Exception {
3013+
return getSinglePartitionInternal(tableName, rowKey, refresh);
30143014
}
30153015

30163016
/**
@@ -3020,12 +3020,16 @@ public Partition getPartition(String tableName, Row rowKey) throws Exception {
30203020
* @return Partition information
30213021
* @throws Exception Exception
30223022
*/
3023-
private Partition getSinglePartitionInternal(String tableName, Row rowKey) throws Exception {
3023+
private Partition getSinglePartitionInternal(String tableName, Row rowKey, boolean refresh) throws Exception {
30243024
ObPair<Long, ObTableParam> obPair = null;
30253025
if (odpMode) {
3026-
obPair = getODPTableWithRowKey(tableName, rowKey, false);
3026+
obPair = getODPTableWithRowKey(tableName, rowKey, refresh);
30273027
} else {
3028-
obPair = getTable(tableName, rowKey, true, true, true, getRoute(false));
3028+
if (refresh) {
3029+
obPair = getTable(tableName, rowKey, true, true, true, getRoute(false));
3030+
} else {
3031+
obPair = getTable(tableName, rowKey, false, false, false, getRoute(false));
3032+
}
30293033
}
30303034
ObTableParam tableParam = obPair.getRight();
30313035
return new Partition(tableParam.getPartitionId(), obPair.getLeft(),
@@ -3039,8 +3043,8 @@ private Partition getSinglePartitionInternal(String tableName, Row rowKey) throw
30393043
* @return partitions
30403044
* @throws Exception Exception
30413045
*/
3042-
public List<Partition> getPartition(String tableName) throws Exception {
3043-
return getAllPartitionInternal(tableName);
3046+
public List<Partition> getPartition(String tableName, boolean refresh) throws Exception {
3047+
return getAllPartitionInternal(tableName, refresh);
30443048
}
30453049

30463050
/**
@@ -3049,21 +3053,28 @@ public List<Partition> getPartition(String tableName) throws Exception {
30493053
* @return List<Partition> partitions
30503054
* @throws Exception Exception
30513055
*/
3052-
private List<Partition> getAllPartitionInternal(String tableName) throws Exception {
3056+
private List<Partition> getAllPartitionInternal(String tableName, boolean refresh) throws Exception {
30533057
List<Partition> partitions = new ArrayList<>();
30543058
if (odpMode) {
30553059
List<ObPair<Long, ObTableParam>> allTables = getOdpTables(tableName, new ObTableQuery(), new Object[]{ ObObj.getMin() }, true,
3056-
new Object[]{ ObObj.getMax() }, true);
3060+
new Object[]{ ObObj.getMax() }, true, refresh);
30573061
for (ObPair<Long, ObTableParam> table : allTables) {
30583062
ObTableParam tableParam = table.getRight();
30593063
Partition partition = new Partition(tableParam.getPartitionId(), table.getLeft(), tableParam.getTableId(),
30603064
tableParam.getObTable().getIp(), tableParam.getObTable().getPort(), tableParam.getLsId());
30613065
partitions.add(partition);
30623066
}
30633067
} else {
3064-
// List<ObPair<logic partId, obTableParam>>
3065-
List<ObPair<Long, ObTableParam>> allTables = getTables(tableName, new ObTableQuery(), new Object[]{ ObObj.getMin() }, true,
3066-
new Object[]{ ObObj.getMax() }, true, true, true, true, getRoute(false));
3068+
List<ObPair<Long, ObTableParam>> allTables;
3069+
if (refresh) {
3070+
// List<ObPair<logic partId, obTableParam>>
3071+
allTables = getTables(tableName, new ObTableQuery(), new Object[]{ ObObj.getMin() }, true,
3072+
new Object[]{ ObObj.getMax() }, true, true, true, true, getRoute(false));
3073+
} else {
3074+
// List<ObPair<logic partId, obTableParam>>
3075+
allTables = getTables(tableName, new ObTableQuery(), new Object[]{ ObObj.getMin() }, true,
3076+
new Object[]{ ObObj.getMax() }, true, false, false, false, getRoute(false));
3077+
}
30673078
for (ObPair<Long, ObTableParam> table : allTables) {
30683079
ObTableParam tableParam = table.getRight();
30693080
Partition partition = new Partition(tableParam.getPartitionId(), table.getLeft(), tableParam.getTableId(),

src/main/java/com/alipay/oceanbase/rpc/table/ObTableClientQueryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public void initPartitions() throws Exception {
293293
false, false);
294294
} else {
295295
pairs = this.obTableClient.getOdpTables(tableName, tableQuery, start,
296-
borderFlag.isInclusiveStart(), end, borderFlag.isInclusiveEnd());
296+
borderFlag.isInclusiveStart(), end, borderFlag.isInclusiveEnd(), false);
297297
}
298298
if (this.tableQuery.getScanOrder() == ObScanOrder.Reverse) {
299299
for (int i = pairs.size() - 1; i >= 0; i--) {

src/test/java/com/alipay/oceanbase/rpc/ODPGetPartitionMetaTest.java

Lines changed: 25 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ public void testNonPartition() throws Exception {
9595
}
9696
BatchOperationResult batchOperationResult = batchOperation.execute();
9797
// test get all partitions
98-
List<Partition> partitions = client.getPartition(table_name);
98+
List<Partition> partitions = client.getPartition(table_name, false);
9999
Assert.assertEquals(1, partitions.size());
100100
for (Partition partition : partitions) {
101101
System.out.println(partition.toString());
102102
}
103103
Long lastPartId = -1L;
104104
for (int i = 0; i < rowCnt; i++) {
105105
Object[] curRow = values[i];
106-
Partition partition = client.getPartition(table_name, row(colVal("c1", curRow[0])));
106+
Partition partition = client.getPartition(table_name, row(colVal("c1", curRow[0])), false);
107107
if (lastPartId == -1L) {
108108
lastPartId = partition.getPartitionId();
109109
} else {
@@ -133,7 +133,7 @@ PRIMARY KEY(`K`, `Q`, `T`)
133133
public void testOneLevelKeyPartition() throws Exception {
134134
String table_name = "testKey";
135135
BatchOperation batchOperation = client.batchOperation(table_name);
136-
client.setRunningMode(ObTableClient.RunningMode.HBASE);
136+
// client.setRunningMode(ObTableClient.RunningMode.HBASE);
137137
Object values[][] = { { "K_val1", "Q_val1", 1L, "V_val1" },
138138
{ "K_val2", "Q_val2", 101L, "V_val2" }, { "K_val3", "Q_val3", 501L, "V_val3" },
139139
{ "K_val4", "Q_val4", 1001L, "V_val4" }, { "K_val5", "Q_val5", 5001L, "V_val5" },
@@ -152,17 +152,17 @@ public void testOneLevelKeyPartition() throws Exception {
152152
}
153153
BatchOperationResult batchOperationResult = batchOperation.execute();
154154
// test get all partitions
155-
List<Partition> partitions = client.getPartition(table_name);
155+
List<Partition> partitions = client.getPartition(table_name, false);
156156
Assert.assertEquals(15, partitions.size());
157157
for (Partition partition : partitions) {
158158
System.out.println(partition.toString());
159159
}
160160

161161
// test get the partition with only partition key with only partition key
162162
Partition first_partition = client.getPartition(table_name,
163-
row(colVal("K", "K_val1"), colVal("Q", "Q_val1"), colVal("T", 1L)));
163+
row(colVal("K", "K_val1"), colVal("Q", "Q_val1"), colVal("T", 1L)), false);
164164
Partition part_key_partition = client.getPartition(table_name,
165-
row(colVal("K", "K_val1")));
165+
row(colVal("K", "K_val1")), false);
166166
Assert.assertEquals(first_partition.getPartitionId(),
167167
part_key_partition.getPartitionId());
168168
} catch (Exception e) {
@@ -180,22 +180,23 @@ public void testOneLevelKeyPartition() throws Exception {
180180
}
181181

182182
/*
183-
* CREATE TABLE IF NOT EXISTS `testHash` (
184-
`K` int,
183+
* CREATE TABLE IF NOT EXISTS `testHash`(
184+
`K` bigint,
185185
`Q` varbinary(256),
186186
`T` bigint,
187187
`V` varbinary(1024),
188+
INDEX i1(`K`, `V`) local,
188189
PRIMARY KEY(`K`, `Q`, `T`)
189-
) partition by hash(K) partitions 15;
190+
) partition by hash(`K`) partitions 16;
190191
* */
191192
@Test
192193
public void testOneLevelHashPartition() throws Exception {
193194
String table_name = "testHash";
194195
BatchOperation batchOperation = client.batchOperation(table_name);
195-
client.setRunningMode(ObTableClient.RunningMode.HBASE);
196-
Object values[][] = { { 1, "Q_val1", 1L, "V_val1" }, { 10, "Q_val2", 101L, "V_val2" },
197-
{ 501, "Q_val3", 501L, "V_val3" }, { 1001, "Q_val4", 1001L, "V_val4" },
198-
{ 5001, "Q_val5", 5001L, "V_val5" }, { 10001, "Q_val6", 10001L, "V_val6" }, };
196+
// client.setRunningMode(ObTableClient.RunningMode.HBASE);
197+
Object values[][] = { { 1L, "Q_val1", 1L, "V_val1" }, { 10L, "Q_val2", 101L, "V_val2" },
198+
{ 501L, "Q_val3", 501L, "V_val3" }, { 1001L, "Q_val4", 1001L, "V_val4" },
199+
{ 5001L, "Q_val5", 5001L, "V_val5" }, { 10001L, "Q_val6", 10001L, "V_val6" }, };
199200
int rowCnt = values.length;
200201

201202
try {
@@ -210,16 +211,16 @@ public void testOneLevelHashPartition() throws Exception {
210211
}
211212
BatchOperationResult batchOperationResult = batchOperation.execute();
212213
// test get all partitions
213-
List<Partition> partitions = client.getPartition(table_name);
214-
Assert.assertEquals(15, partitions.size());
214+
List<Partition> partitions = client.getPartition(table_name, false);
215+
Assert.assertEquals(16, partitions.size());
215216
for (Partition partition : partitions) {
216217
System.out.println(partition.toString());
217218
}
218219

219220
// test get the partition with only partition key with only partition key
220221
Partition first_partition = client.getPartition(table_name,
221-
row(colVal("K", 1), colVal("Q", "Q_val1"), colVal("T", 1L)));
222-
Partition part_key_partition = client.getPartition(table_name, row(colVal("K", 1)));
222+
row(colVal("K", 1), colVal("Q", "Q_val1"), colVal("T", 1L)), false);
223+
Partition part_key_partition = client.getPartition(table_name, row(colVal("K", 1)), false);
223224
Assert.assertEquals(first_partition.getPartitionId(),
224225
part_key_partition.getPartitionId());
225226
} catch (Exception e) {
@@ -236,72 +237,6 @@ public void testOneLevelHashPartition() throws Exception {
236237
}
237238
}
238239

239-
/*
240-
* CREATE TABLE IF NOT EXISTS `testSubStrKey` (
241-
`K` varbinary(1024),
242-
`Q` varbinary(256),
243-
`T` bigint,
244-
`V` varbinary(1024),
245-
K_PREFIX varbinary(1024) generated always as (substring(`K`, 1, 4)),
246-
PRIMARY KEY(`K`, `Q`, `T`)
247-
) partition by key(K_PREFIX) partitions 15;
248-
* */
249-
@Test
250-
public void testOneLevelSubStrKeyPartition() throws Exception {
251-
String table_name = "testSubStrKey";
252-
// BatchOperation batchOperation = client.batchOperation(TABLE_NAME);
253-
client.addRowKeyElement(table_name, new String[] { "K", "Q", "T" });
254-
Object values[][] = { { "K_val1", "Q_val1", 1L, "V_val1" },
255-
{ "K_val2", "Q_val2", 101L, "V_val2" }, { "K_val3", "Q_val3", 501L, "V_val3" },
256-
{ "K_val4", "Q_val4", 1001L, "V_val4" }, { "K_val5", "Q_val5", 5001L, "V_val5" },
257-
{ "K_val6", "Q_val6", 10001L, "V_val6" }, };
258-
int rowCnt = values.length;
259-
260-
try {
261-
// batch insert
262-
// for (int i = 0; i < rowCnt; i++) {
263-
// Object[] curRow = values[i];
264-
// InsertOrUpdate insertOrUpdate = new InsertOrUpdate();
265-
// insertOrUpdate.setRowKey(row(colVal("c1", curRow[0]), colVal("c2", curRow[1])));
266-
// insertOrUpdate.addMutateRow(row(colVal("c3", curRow[2]), colVal("c4", curRow[3])));
267-
// batchOperation.addOperation(insertOrUpdate);
268-
// }
269-
// BatchOperationResult batchOperationResult = batchOperation.execute();
270-
// MutationResult res = client.insert(table_name)
271-
// .setRowKey(row(colVal("K", "K_val1"), colVal("Q", "Q_val1".getBytes()), colVal("T", 1L)))
272-
// .addMutateRow(row(colVal("V", "V_val1".getBytes()))).execute();
273-
// Assert.assertEquals(1, res.getAffectedRows());
274-
// test get all partitions
275-
List<Partition> partitions = client.getPartition(table_name);
276-
Assert.assertEquals(15, partitions.size());
277-
278-
// Map<Long, Partition> partIdMap = new HashMap<>();
279-
// for (Partition partition : partitions) {
280-
// partIdMap.put(partition.getPartitionId(), partition);
281-
// }
282-
//
283-
// // test get the first partition
284-
// Partition first_partition = client.getPartition(TABLE_NAME, row(colVal("c1", 1L), colVal("c2", "c2_val")));
285-
// Assert.assertEquals(partitions.get(0).getPartitionId(), first_partition.getPartitionId());
286-
// // test get the second partition
287-
// Partition sec_partition = client.getPartition(TABLE_NAME, row(colVal("c1", 401L), colVal("c2", "c2_val")));
288-
// Assert.assertEquals(partitions.get(1).getPartitionId(), sec_partition.getPartitionId());
289-
// // test get the partition with only partition key
290-
// Partition partition1 = client.getPartition(TABLE_NAME, row(colVal("c1", 1L)));
291-
// Assert.assertEquals(first_partition.getPartitionId(), partition1.getPartitionId());
292-
} catch (Exception e) {
293-
e.printStackTrace();
294-
Assert.assertTrue(false);
295-
} finally {
296-
// for (int j = 0; j < rowCnt; j++) {
297-
// Delete delete = client.delete(TABLE_NAME);
298-
// delete.setRowKey(row(colVal("K", values[j][0]), colVal("Q", values[j][1]), colVal("T", values[j][2])));
299-
// MutationResult res = delete.execute();
300-
// Assert.assertEquals(1, res.getAffectedRows());
301-
// }
302-
}
303-
}
304-
305240
/*
306241
* CREATE TABLE IF NOT EXISTS `testRange` (
307242
`c1` int NOT NULL,
@@ -333,24 +268,24 @@ public void testOneLevelRangePartition() throws Exception {
333268
}
334269
BatchOperationResult batchOperationResult = batchOperation.execute();
335270
// test get all partitions
336-
List<Partition> partitions = client.getPartition(table_name);
271+
List<Partition> partitions = client.getPartition(table_name, false);
337272
Assert.assertEquals(3, partitions.size());
338273
for (Partition partition : partitions) {
339274
System.out.println(partition.toString());
340275
}
341276

342277
// test get the first partition
343278
Partition first_partition = client.getPartition(table_name,
344-
row(colVal("c1", 1L), colVal("c2", "c2_val")));
279+
row(colVal("c1", 1L), colVal("c2", "c2_val")), false);
345280
Assert.assertEquals(partitions.get(0).getPartitionId(),
346281
first_partition.getPartitionId());
347282
// test get the second partition
348283
Partition sec_partition = client.getPartition(table_name,
349-
row(colVal("c1", 401L), colVal("c2", "c2_val")));
284+
row(colVal("c1", 401L), colVal("c2", "c2_val")), false);
350285
Assert.assertEquals(partitions.get(1).getPartitionId(), sec_partition.getPartitionId());
351286
// test get the same partition with the first partition key
352287
Partition partition1 = client.getPartition(table_name,
353-
row(colVal("c1", 101L), colVal("c2", "c2_val")));
288+
row(colVal("c1", 101L), colVal("c2", "c2_val")), false);
354289
Assert.assertEquals(first_partition.getPartitionId(), partition1.getPartitionId());
355290
} catch (Exception e) {
356291
e.printStackTrace();
@@ -401,7 +336,7 @@ public void testTwoLevelKeyPartition() throws Exception {
401336
Partition partition = client.getPartition(
402337
testTable,
403338
row(colVal("c0", c0), colVal("c1", c1), colVal("c2", c2), colVal("c3", c3),
404-
colVal("c4", c4), colVal("c5", c5)));
339+
colVal("c4", c4), colVal("c5", c5)), false);
405340
Assert.assertNotNull(partition);
406341
System.out.println(partition.toString());
407342
// test scan range with partition
@@ -467,7 +402,7 @@ public void testTwoLevelRangePartition() throws Exception {
467402
statement.execute("insert into " + testTable + "(c1, c2, c3, c4, c5) values (" + c1
468403
+ "," + c2 + ",'" + c3 + "','" + c4 + "'," + "'value')");
469404
Partition partition = client.getPartition(testTable,
470-
row(colVal("c1", c1), colVal("c2", c2), colVal("c3", c3), colVal("c4", c4)));
405+
row(colVal("c1", c1), colVal("c2", c2), colVal("c3", c3), colVal("c4", c4)), false);
471406
Assert.assertNotNull(partition);
472407
System.out.println(partition.toString());
473408
// test scan range with partition
@@ -494,7 +429,7 @@ public void testConcurrentGetPartition() throws Exception {
494429
executorService.submit(() -> {
495430
try {
496431
String table_name = table_names[random.nextInt(table_names.length)];
497-
List<Partition> partitions = client.getPartition(table_name);
432+
List<Partition> partitions = client.getPartition(table_name, false);
498433
if (table_name.equalsIgnoreCase("testHash")) {
499434
Assert.assertEquals(15, partitions.size());
500435
for (Partition partition : partitions) {

0 commit comments

Comments
 (0)