Skip to content
This repository was archived by the owner on May 10, 2022. It is now read-only.

Commit ed7d4c7

Browse files
feat: using "interceptor" to enhance the api(compress) (#126)
1 parent 5d60bd1 commit ed7d4c7

21 files changed

+291
-76
lines changed

src/main/java/com/xiaomi/infra/pegasus/client/PegasusClient.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ public long hash(byte[] key) {
4343
}
4444

4545
private PegasusTable getTable(String tableName) throws PException {
46-
return getTable(tableName, 0);
46+
return getTable(tableName, new InternalTableOptions(new PegasusHasher(), new TableOptions()));
4747
}
4848

49-
private PegasusTable getTable(String tableName, int backupRequestDelayMs) throws PException {
49+
private PegasusTable getTable(String tableName, InternalTableOptions internalTableOptions)
50+
throws PException {
5051
PegasusTable table = tableMap.get(tableName);
5152
if (table == null) {
5253
synchronized (tableMapLock) {
5354
table = tableMap.get(tableName);
5455
if (table == null) {
5556
try {
56-
TableOptions options = new TableOptions(new PegasusHasher(), backupRequestDelayMs);
57-
Table internalTable = cluster.openTable(tableName, options);
57+
Table internalTable = cluster.openTable(tableName, internalTableOptions);
5858
table = new PegasusTable(this, internalTable);
5959
} catch (Throwable e) {
6060
throw new PException(e);
@@ -191,7 +191,17 @@ public PegasusTableInterface openTable(String tableName) throws PException {
191191
@Override
192192
public PegasusTableInterface openTable(String tableName, int backupRequestDelayMs)
193193
throws PException {
194-
return getTable(tableName, backupRequestDelayMs);
194+
return getTable(
195+
tableName,
196+
new InternalTableOptions(
197+
new PegasusHasher(),
198+
new TableOptions().withBackupRequestDelayMs(backupRequestDelayMs)));
199+
}
200+
201+
@Override
202+
public PegasusTableInterface openTable(String tableName, TableOptions tableOptions)
203+
throws PException {
204+
return getTable(tableName, new InternalTableOptions(new PegasusHasher(), tableOptions));
195205
}
196206

197207
@Override

src/main/java/com/xiaomi/infra/pegasus/client/PegasusClientInterface.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ public interface PegasusClientInterface {
4141
*/
4242
public PegasusTableInterface openTable(String tableName) throws PException;
4343

44+
/**
45+
* Open a table, and prepare the sessions and route-table to the replica-servers.
46+
*
47+
* @deprecated Retained only for backward compatibility, will be removed later. Don't use it any
48+
* more. The latest interface please see {@link PegasusClientInterface#openTable(String,
49+
* TableOptions)}
50+
*/
51+
@Deprecated
52+
public PegasusTableInterface openTable(String tableName, int backupRequestDelayMs)
53+
throws PException;
54+
4455
/**
4556
* Open a table, and prepare the sessions and route-table to the replica-servers.
4657
*
@@ -55,13 +66,13 @@ public interface PegasusClientInterface {
5566
* 4. You can't specify a per-operation timeout. So we recommend you to use the table-interface.
5667
*
5768
* @param tableName the table should be exist on the server, which is created before by the system
58-
* administrator
59-
* @param backupRequestDelayMs the delay time to send backup request. If backupRequestDelayMs <=
60-
* 0, The backup request is disabled.
61-
* @return the table handler
62-
* @throws PException throws exception if any error occurs.
69+
* * administrator
70+
* @param tableOptions control the table feature, such as open backup-request, compress and etc,
71+
* see {@link TableOptions}
72+
* @return
73+
* @throws PException
6374
*/
64-
public PegasusTableInterface openTable(String tableName, int backupRequestDelayMs)
75+
public PegasusTableInterface openTable(String tableName, TableOptions tableOptions)
6576
throws PException;
6677

6778
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
2+
// This source code is licensed under the Apache License Version 2.0, which
3+
// can be found in the LICENSE file in the root directory of this source tree.
4+
package com.xiaomi.infra.pegasus.client;
5+
6+
/** TableOptions is the internal options for opening a Pegasus table. */
7+
public class TableOptions {
8+
private int backupRequestDelayMs;
9+
private boolean enableCompression;
10+
11+
public TableOptions() {
12+
this.backupRequestDelayMs = 0;
13+
this.enableCompression = false;
14+
}
15+
16+
public TableOptions withBackupRequestDelayMs(int backupRequestDelayMs) {
17+
this.backupRequestDelayMs = backupRequestDelayMs;
18+
return this;
19+
}
20+
21+
public TableOptions withCompression(boolean enableCompression) {
22+
this.enableCompression = enableCompression;
23+
return this;
24+
}
25+
26+
public int backupRequestDelayMs() {
27+
return this.backupRequestDelayMs;
28+
}
29+
30+
public boolean enableBackupRequest() {
31+
return backupRequestDelayMs > 0;
32+
}
33+
34+
public boolean enableCompression() {
35+
return enableCompression;
36+
}
37+
}

src/main/java/com/xiaomi/infra/pegasus/operator/rrdb_check_and_mutate_operator.java

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public check_and_mutate_response get_response() {
4747
return resp;
4848
}
4949

50+
public check_and_mutate_request get_request() {
51+
return request;
52+
}
53+
5054
private check_and_mutate_request request;
5155
private check_and_mutate_response resp;
5256
}

src/main/java/com/xiaomi/infra/pegasus/operator/rrdb_check_and_set_operator.java

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public check_and_set_response get_response() {
4747
return resp;
4848
}
4949

50+
public check_and_set_request get_request() {
51+
return request;
52+
}
53+
5054
private check_and_set_request request;
5155
private check_and_set_response resp;
5256
}

src/main/java/com/xiaomi/infra/pegasus/operator/rrdb_multi_put_operator.java

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public update_response get_response() {
4747
return resp;
4848
}
4949

50+
public multi_put_request get_request() {
51+
return request;
52+
}
53+
5054
private multi_put_request request;
5155
private update_response resp;
5256
}

src/main/java/com/xiaomi/infra/pegasus/operator/rrdb_put_operator.java

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public update_response get_response() {
4646
return resp;
4747
}
4848

49+
public update_request get_request() {
50+
return request;
51+
}
52+
4953
private update_request request;
5054
private update_response resp;
5155
}

src/main/java/com/xiaomi/infra/pegasus/rpc/Cluster.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static Cluster createCluster(ClientOptions clientOptions)
1717

1818
public abstract String[] getMetaList();
1919

20-
public abstract Table openTable(String name, TableOptions options)
20+
public abstract Table openTable(String name, InternalTableOptions options)
2121
throws ReplicationException, TException;
2222

2323
public abstract void close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.xiaomi.infra.pegasus.rpc;
2+
3+
import com.xiaomi.infra.pegasus.client.TableOptions;
4+
5+
public class InternalTableOptions {
6+
private final KeyHasher keyHasher;
7+
private final TableOptions tableOptions;
8+
9+
public InternalTableOptions(KeyHasher keyHasher, TableOptions tableOptions) {
10+
this.keyHasher = keyHasher;
11+
this.tableOptions = tableOptions;
12+
}
13+
14+
public KeyHasher keyHasher() {
15+
return keyHasher;
16+
}
17+
18+
public TableOptions tableOptions() {
19+
return tableOptions;
20+
}
21+
22+
public static InternalTableOptions forTest() {
23+
return new InternalTableOptions(KeyHasher.DEFAULT, new TableOptions());
24+
}
25+
}

src/main/java/com/xiaomi/infra/pegasus/rpc/TableOptions.java

-31
This file was deleted.

src/main/java/com/xiaomi/infra/pegasus/rpc/async/ClusterManager.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import com.xiaomi.infra.pegasus.client.ClientOptions;
1010
import com.xiaomi.infra.pegasus.metrics.MetricsManager;
1111
import com.xiaomi.infra.pegasus.rpc.Cluster;
12+
import com.xiaomi.infra.pegasus.rpc.InternalTableOptions;
1213
import com.xiaomi.infra.pegasus.rpc.ReplicationException;
13-
import com.xiaomi.infra.pegasus.rpc.TableOptions;
1414
import io.netty.channel.EventLoopGroup;
1515
import io.netty.channel.nio.NioEventLoopGroup;
1616
import io.netty.channel.socket.nio.NioSocketChannel;
@@ -126,8 +126,9 @@ public String[] getMetaList() {
126126
}
127127

128128
@Override
129-
public TableHandler openTable(String name, TableOptions options) throws ReplicationException {
130-
return new TableHandler(this, name, options);
129+
public TableHandler openTable(String name, InternalTableOptions internalTableOptions)
130+
throws ReplicationException {
131+
return new TableHandler(this, name, internalTableOptions);
131132
}
132133

133134
@Override

src/main/java/com/xiaomi/infra/pegasus/rpc/async/TableHandler.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
import com.xiaomi.infra.pegasus.replication.partition_configuration;
1414
import com.xiaomi.infra.pegasus.replication.query_cfg_request;
1515
import com.xiaomi.infra.pegasus.replication.query_cfg_response;
16+
import com.xiaomi.infra.pegasus.rpc.InternalTableOptions;
1617
import com.xiaomi.infra.pegasus.rpc.ReplicationException;
1718
import com.xiaomi.infra.pegasus.rpc.Table;
18-
import com.xiaomi.infra.pegasus.rpc.TableOptions;
1919
import com.xiaomi.infra.pegasus.rpc.interceptor.InterceptorManger;
2020
import io.netty.channel.ChannelFuture;
2121
import io.netty.util.concurrent.EventExecutor;
@@ -53,7 +53,7 @@ static final class TableConfiguration {
5353
int backupRequestDelayMs;
5454
private InterceptorManger interceptorManger;
5555

56-
public TableHandler(ClusterManager mgr, String name, TableOptions options)
56+
public TableHandler(ClusterManager mgr, String name, InternalTableOptions internalTableOptions)
5757
throws ReplicationException {
5858
int i = 0;
5959
for (; i < name.length(); i++) {
@@ -93,12 +93,12 @@ public TableHandler(ClusterManager mgr, String name, TableOptions options)
9393
// superclass members
9494
tableName_ = name;
9595
appID_ = resp.app_id;
96-
hasher_ = options.keyHasher();
96+
hasher_ = internalTableOptions.keyHasher();
9797

9898
// members of this
9999
manager_ = mgr;
100100
executor_ = manager_.getExecutor();
101-
this.backupRequestDelayMs = options.backupRequestDelayMs();
101+
this.backupRequestDelayMs = internalTableOptions.tableOptions().backupRequestDelayMs();
102102
if (backupRequestDelayMs > 0) {
103103
logger.info("the delay time of backup request is \"{}\"", backupRequestDelayMs);
104104
}
@@ -109,7 +109,7 @@ public TableHandler(ClusterManager mgr, String name, TableOptions options)
109109
inQuerying_ = new AtomicBoolean(false);
110110
lastQueryTime_ = 0;
111111

112-
this.interceptorManger = new InterceptorManger(options);
112+
this.interceptorManger = new InterceptorManger(internalTableOptions.tableOptions());
113113
}
114114

115115
public ReplicaConfiguration getReplicaConfig(int index) {

src/main/java/com/xiaomi/infra/pegasus/rpc/interceptor/BackupRequestInterceptor.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
import java.util.concurrent.TimeUnit;
1111

1212
public class BackupRequestInterceptor implements TableInterceptor {
13+
private final long backupRequestDelayMs;
1314

14-
private boolean isOpen;
15-
16-
public BackupRequestInterceptor(boolean isOpen) {
17-
this.isOpen = isOpen;
15+
public BackupRequestInterceptor(long backupRequestDelayMs) {
16+
this.backupRequestDelayMs = backupRequestDelayMs;
1817
}
1918

2019
@Override
@@ -33,7 +32,7 @@ public void after(
3332
}
3433

3534
private void backupCall(ClientRequestRound clientRequestRound, TableHandler tableHandler) {
36-
if (!isOpen || !clientRequestRound.getOperator().supportBackupRequest()) {
35+
if (!clientRequestRound.getOperator().supportBackupRequest()) {
3736
return;
3837
}
3938

@@ -59,7 +58,7 @@ private void backupCall(ClientRequestRound clientRequestRound, TableHandler tabl
5958
clientRequestRound.timeoutMs(),
6059
true);
6160
},
62-
tableHandler.backupRequestDelayMs(),
61+
backupRequestDelayMs,
6362
TimeUnit.MILLISECONDS));
6463
}
6564
}

0 commit comments

Comments
 (0)