2121import io .openmessaging .internal .DefaultKeyValue ;
2222import java .util .ArrayList ;
2323import java .util .Collections ;
24+ import java .util .HashMap ;
2425import java .util .List ;
26+ import java .util .Map ;
2527import java .util .Set ;
2628import java .util .UUID ;
2729import java .util .concurrent .ThreadLocalRandom ;
2830import org .apache .commons .lang3 .StringUtils ;
31+ import org .apache .commons .lang3 .text .StrSubstitutor ;
2932import org .apache .rocketmq .acl .common .AclClientRPCHook ;
3033import org .apache .rocketmq .acl .common .SessionCredentials ;
34+ import org .apache .rocketmq .client .consumer .DefaultMQPullConsumer ;
3135import org .apache .rocketmq .client .exception .MQClientException ;
3236import org .apache .rocketmq .common .TopicConfig ;
37+ import org .apache .rocketmq .common .message .MessageClientExt ;
38+ import org .apache .rocketmq .common .message .MessageExt ;
3339import org .apache .rocketmq .common .protocol .route .BrokerData ;
3440import org .apache .rocketmq .common .protocol .route .TopicRouteData ;
3541import org .apache .rocketmq .remoting .RPCHook ;
3844import org .apache .rocketmq .replicator .config .RmqConnectorConfig ;
3945import org .apache .rocketmq .replicator .config .TaskConfig ;
4046import org .apache .rocketmq .replicator .config .TaskConfigEnum ;
47+ import org .apache .rocketmq .replicator .config .TaskDivideConfig ;
4148import org .apache .rocketmq .tools .admin .DefaultMQAdminExt ;
4249import org .apache .rocketmq .tools .command .CommandUtil ;
4350import org .slf4j .Logger ;
@@ -118,8 +125,9 @@ public static void createTopic(DefaultMQAdminExt defaultMQAdminExt, TopicConfig
118125 }
119126
120127 public static List <KeyValue > groupPartitions (List <String > elements , int numGroups , RmqConnectorConfig tdc ) {
121- if (numGroups <= 0 )
128+ if (numGroups <= 0 ) {
122129 throw new IllegalArgumentException ("Number of groups must be positive." );
130+ }
123131
124132 List <KeyValue > result = new ArrayList <>(numGroups );
125133
@@ -143,6 +151,17 @@ public static List<KeyValue> groupPartitions(List<String> elements, int numGroup
143151 keyValue .put (TaskConfigEnum .TASK_DATA_TYPE .getKey (), DataType .OFFSET .ordinal ());
144152 keyValue .put (TaskConfigEnum .TASK_GROUP_INFO .getKey (), JSONObject .toJSONString (groupList ));
145153 keyValue .put (TaskConfigEnum .TASK_SOURCE_RECORD_CONVERTER .getKey (), tdc .getConverter ());
154+
155+ keyValue .put (TaskConfigEnum .TASK_SOURCE_ACL_ENABLE .getKey (), String .valueOf (tdc .isSrcAclEnable ()));
156+ keyValue .put (TaskConfigEnum .TASK_SOURCE_ACCESS_KEY .getKey (), tdc .getSrcAccessKey ());
157+ keyValue .put (TaskConfigEnum .TASK_SOURCE_SECRET_KEY .getKey (), tdc .getSrcSecretKey ());
158+
159+ keyValue .put (TaskConfigEnum .TASK_TARGET_ROCKETMQ .getKey (), tdc .getTargetNamesrvs ());
160+ keyValue .put (TaskConfigEnum .TASK_TARGET_CLUSTER .getKey (), tdc .getTargetCluster ());
161+ keyValue .put (TaskConfigEnum .TASK_TARGET_ACL_ENABLE .getKey (), String .valueOf (tdc .isTargetAclEnable ()));
162+ keyValue .put (TaskConfigEnum .TASK_TARGET_ACCESS_KEY .getKey (), tdc .getTargetAccessKey ());
163+ keyValue .put (TaskConfigEnum .TASK_TARGET_SECRET_KEY .getKey (), tdc .getTargetSecretKey ());
164+ keyValue .put (TaskConfigEnum .TASK_RENAME_PATTERN .getKey (), tdc .getRenamePattern ());
146165 result .add (keyValue );
147166
148167 log .debug ("allocate group partition: {}" , keyValue );
@@ -216,4 +235,52 @@ public static DefaultMQAdminExt startTargetMQAdminTool(TaskConfig taskConfig) th
216235
217236 return targetMQAdminExt ;
218237 }
238+
239+ public static DefaultMQPullConsumer startSrcMQPullConsumer (TaskConfig taskConfig ) throws MQClientException {
240+ RPCHook rpcHook = null ;
241+ if (taskConfig .isSrcAclEnable ()) {
242+ rpcHook = new AclClientRPCHook (new SessionCredentials (taskConfig .getSrcAccessKey (), taskConfig .getSrcSecretKey ()));
243+ }
244+ DefaultMQPullConsumer srcConsumer = new DefaultMQPullConsumer (rpcHook );
245+ srcConsumer .setNamesrvAddr (taskConfig .getSourceRocketmq ());
246+ srcConsumer .setConsumerGroup (ConstDefine .REPLICATOR_SOURCE_TASK_GROUP );
247+ srcConsumer .setInstanceName (Utils .createUniqIntanceName (taskConfig .getSourceRocketmq ()));
248+
249+ srcConsumer .start ();
250+ log .info ("SOURCE: RocketMQ srcConsumer started." );
251+
252+ return srcConsumer ;
253+ }
254+
255+ public static DefaultMQPullConsumer startTargetMQPullConsumer (TaskConfig taskConfig ) throws MQClientException {
256+ RPCHook rpcHook = null ;
257+ if (taskConfig .isTargetAclEnable ()) {
258+ rpcHook = new AclClientRPCHook (new SessionCredentials (taskConfig .getTargetAccessKey (), taskConfig .getTargetSecretKey ()));
259+ }
260+ DefaultMQPullConsumer targetConsumer = new DefaultMQPullConsumer (rpcHook );
261+ targetConsumer .setNamesrvAddr (taskConfig .getTargetRocketmq ());
262+ targetConsumer .setConsumerGroup (ConstDefine .REPLICATOR_SINK_TASK_GROUP );
263+ targetConsumer .setInstanceName (Utils .createUniqIntanceName (taskConfig .getTargetRocketmq ()));
264+
265+ targetConsumer .start ();
266+ log .info ("TARGET: RocketMQ targetConsumer started." );
267+
268+ return targetConsumer ;
269+ }
270+
271+ public static String getOffsetMsgId (MessageExt msg ) {
272+ if (msg instanceof MessageClientExt ) {
273+ return ((MessageClientExt ) msg ).getOffsetMsgId ();
274+ }
275+ return msg .getMsgId ();
276+ }
277+
278+ public static String getTargetTopic (String topic , String renamePattern ) {
279+ if (StringUtils .isNotEmpty (renamePattern )) {
280+ Map <String , String > params = new HashMap <>();
281+ params .put ("topic" , topic );
282+ return StrSubstitutor .replace (renamePattern , params );
283+ }
284+ return topic ;
285+ }
219286}
0 commit comments