You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/09/07 06:15:12 UTC

[GitHub] [shardingsphere] sandynz commented on a diff in pull request #20849: Extract pipeline meta data node change listener SPI for common usage

sandynz commented on code in PR #20849:
URL: https://github.com/apache/shardingsphere/pull/20849#discussion_r964401988


##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContext.java:
##########
@@ -29,6 +33,8 @@ public final class PipelineContext {
     
     private static volatile ContextManager contextManager;
     
+    private static final ExecutorService PIPELINE_EXECUTOR = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Pipeline-Context-%d").build());
+    

Review Comment:
   It's better to set a dedicated name, since it's just used by registry center watcher



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/execute/PipelineJobExecutor.java:
##########
@@ -18,103 +18,72 @@
 package org.apache.shardingsphere.data.pipeline.core.execute;
 
 import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.data.pipeline.api.config.job.MigrationJobConfiguration;
-import org.apache.shardingsphere.data.pipeline.api.config.job.yaml.YamlMigrationJobConfigurationSwapper;
-import org.apache.shardingsphere.data.pipeline.api.executor.AbstractLifecycleExecutor;
+import org.apache.shardingsphere.data.pipeline.api.job.JobType;
 import org.apache.shardingsphere.data.pipeline.core.api.PipelineAPIFactory;
 import org.apache.shardingsphere.data.pipeline.core.constant.DataPipelineConstants;
-import org.apache.shardingsphere.data.pipeline.core.job.PipelineJobCenter;
-import org.apache.shardingsphere.data.pipeline.core.job.progress.PipelineJobProgressDetector;
 import org.apache.shardingsphere.data.pipeline.core.metadata.node.PipelineMetaDataNode;
+import org.apache.shardingsphere.data.pipeline.core.spi.listener.PipelineMetaDataListener;
+import org.apache.shardingsphere.data.pipeline.core.spi.listener.PipelineMetaDataListenerFactory;
 import org.apache.shardingsphere.data.pipeline.core.util.PipelineDistributedBarrier;
-import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJob;
-import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJobAPIFactory;
-import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJobPreparer;
 import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
-import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
 import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent.Type;
 
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Pattern;
 
 /**
  * Pipeline job executor.
  */
 @Slf4j
-public final class PipelineJobExecutor extends AbstractLifecycleExecutor {
+public final class PipelineJobExecutor {
     
-    private final ExecutorService executor = Executors.newFixedThreadPool(20);
+    private static final Map<Pattern, PipelineMetaDataListener> LISTENER_MAP = new ConcurrentHashMap<>();
     
-    @Override
-    protected void doStart() {
+    /**
+     * Register listener.
+     */
+    public static void registerListener() {
+        if (!LISTENER_MAP.isEmpty()) {
+            log.info("listener already register");
+            return;
+        }
+        for (JobType each : JobType.values()) {
+            Optional<PipelineMetaDataListener> instance = PipelineMetaDataListenerFactory.findInstance(each.getTypeName());
+            if (!instance.isPresent()) {
+                continue;
+            }
+            PipelineMetaDataListener pipelineMetaDataListener = instance.get();
+            LISTENER_MAP.put(Pattern.compile(pipelineMetaDataListener.getWatchKey()), pipelineMetaDataListener);

Review Comment:
   1, It could be put it in constructor, use sinleton, then it'll be initialized on demand.
   
   2, SPI type could not be job type. Since there's SPI impl could not be defined by job type.
   



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/spi/listener/PipelineMetaDataListenerFactory.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.spi.listener;
+
+import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
+
+import java.util.Optional;
+
+/**
+ * Pipeline meta data listener factory.
+ */
+public final class PipelineMetaDataListenerFactory {
+    
+    static {
+        ShardingSphereServiceLoader.register(PipelineMetaDataListener.class);
+    }
+    
+    /**
+     * Get pipeline meta data listener instance.
+     *
+     * @param jobTypeName job type name
+     * @return pipeline meta data listener
+     */
+    public static Optional<PipelineMetaDataListener> findInstance(final String jobTypeName) {
+        return TypedSPIRegistry.findRegisteredService(PipelineMetaDataListener.class, jobTypeName);
+    }

Review Comment:
   A no argument method could be better, to get all SPI implementations. It's not necessary to for-each job types and get them (there might be no type)



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/spi/listener/MigrationPipelineMetaDataListener.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.spi.listener;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.data.pipeline.api.config.job.MigrationJobConfiguration;
+import org.apache.shardingsphere.data.pipeline.api.config.job.yaml.YamlMigrationJobConfigurationSwapper;
+import org.apache.shardingsphere.data.pipeline.api.job.JobType;
+import org.apache.shardingsphere.data.pipeline.core.api.PipelineAPIFactory;
+import org.apache.shardingsphere.data.pipeline.core.constant.DataPipelineConstants;
+import org.apache.shardingsphere.data.pipeline.core.context.PipelineContext;
+import org.apache.shardingsphere.data.pipeline.core.job.PipelineJobCenter;
+import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJob;
+import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJobPreparer;
+import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
+import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap;
+import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Migration pipeline meta data listener.
+ */
+@Slf4j
+public final class MigrationPipelineMetaDataListener implements PipelineMetaDataListener {

Review Comment:
   `Pipeline` in class name is duplicated, there's already `Migration`



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/execute/PipelineJobWorker.java:
##########
@@ -41,7 +41,7 @@ public static void initialize() {
                 return;
             }
             log.info("start worker initialization");
-            new PipelineJobExecutor().start();
+            PipelineJobExecutor.registerListener();

Review Comment:
   `PipelineJobExecutor.getInstance();` could be called here, it doesn't matter if it's missed



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/data/pipeline/core/spi/listener/PipelineMetaDataListenerFactoryTest.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.spi.listener;
+
+import org.apache.shardingsphere.data.pipeline.core.fixture.FixturePipelineMetaDataListener;
+import org.junit.Test;
+
+import java.util.Optional;
+
+import static org.hamcrest.Matchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class PipelineMetaDataListenerFactoryTest {
+    
+    @Test
+    public void assertFindInstance() {
+        Optional<PipelineMetaDataListener> actual = PipelineMetaDataListenerFactory.findInstance("FIXTURE");
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), instanceOf(FixturePipelineMetaDataListener.class));
+    }

Review Comment:
   Real SPI impl should be tested, FIXTURE doesn't benefit for real running



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/spi/listener/PipelineMetaDataListener.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.spi.listener;
+
+import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
+import org.apache.shardingsphere.infra.util.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
+import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
+
+/**
+ * Pipeline meta data listener.
+ */
+@SingletonSPI
+public interface PipelineMetaDataListener extends TypedSPI {
+    
+    /**
+     * Get watch key of listener.
+     *
+     * @return watch key
+     */
+    String getWatchKey();
+    

Review Comment:
   `getWatchKey()` doesn't return watch key, but key pattern, method name could be improved



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/data/pipeline/core/fixture/FixturePipelineMetaDataListener.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.fixture;
+
+import org.apache.shardingsphere.data.pipeline.core.spi.listener.PipelineMetaDataListener;
+import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
+import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
+
+public class FixturePipelineMetaDataListener implements PipelineMetaDataListener {
+    

Review Comment:
   FIXTURE impl is not necessary



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/spi/listener/PipelineMetaDataListener.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.spi.listener;
+
+import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
+import org.apache.shardingsphere.infra.util.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
+import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
+
+/**
+ * Pipeline meta data listener.
+ */
+@SingletonSPI
+public interface PipelineMetaDataListener extends TypedSPI {

Review Comment:
   1, Class name could be improved, it's not listener, it's event handler / processor
   
   2, Consider to choose another SPI type, looks TypedSPI is not suitable here
   



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContext.java:
##########
@@ -29,6 +33,8 @@ public final class PipelineContext {
     
     private static volatile ContextManager contextManager;
     
+    private static final ExecutorService PIPELINE_EXECUTOR = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Pipeline-Context-%d").build());
+    

Review Comment:
   And also getPipelineExecutor()



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/execute/PipelineJobExecutor.java:
##########
@@ -18,103 +18,72 @@
 package org.apache.shardingsphere.data.pipeline.core.execute;
 
 import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.data.pipeline.api.config.job.MigrationJobConfiguration;
-import org.apache.shardingsphere.data.pipeline.api.config.job.yaml.YamlMigrationJobConfigurationSwapper;
-import org.apache.shardingsphere.data.pipeline.api.executor.AbstractLifecycleExecutor;
+import org.apache.shardingsphere.data.pipeline.api.job.JobType;
 import org.apache.shardingsphere.data.pipeline.core.api.PipelineAPIFactory;
 import org.apache.shardingsphere.data.pipeline.core.constant.DataPipelineConstants;
-import org.apache.shardingsphere.data.pipeline.core.job.PipelineJobCenter;
-import org.apache.shardingsphere.data.pipeline.core.job.progress.PipelineJobProgressDetector;
 import org.apache.shardingsphere.data.pipeline.core.metadata.node.PipelineMetaDataNode;
+import org.apache.shardingsphere.data.pipeline.core.spi.listener.PipelineMetaDataListener;
+import org.apache.shardingsphere.data.pipeline.core.spi.listener.PipelineMetaDataListenerFactory;
 import org.apache.shardingsphere.data.pipeline.core.util.PipelineDistributedBarrier;
-import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJob;
-import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJobAPIFactory;
-import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJobPreparer;
 import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
-import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
 import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent.Type;
 
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Pattern;
 
 /**
  * Pipeline job executor.
  */
 @Slf4j
-public final class PipelineJobExecutor extends AbstractLifecycleExecutor {
+public final class PipelineJobExecutor {
     
-    private final ExecutorService executor = Executors.newFixedThreadPool(20);
+    private static final Map<Pattern, PipelineMetaDataListener> LISTENER_MAP = new ConcurrentHashMap<>();
     
-    @Override
-    protected void doStart() {
+    /**
+     * Register listener.
+     */
+    public static void registerListener() {
+        if (!LISTENER_MAP.isEmpty()) {
+            log.info("listener already register");
+            return;
+        }
+        for (JobType each : JobType.values()) {
+            Optional<PipelineMetaDataListener> instance = PipelineMetaDataListenerFactory.findInstance(each.getTypeName());
+            if (!instance.isPresent()) {
+                continue;
+            }
+            PipelineMetaDataListener pipelineMetaDataListener = instance.get();
+            LISTENER_MAP.put(Pattern.compile(pipelineMetaDataListener.getWatchKey()), pipelineMetaDataListener);
+        }
         PipelineAPIFactory.getGovernanceRepositoryAPI().watch(DataPipelineConstants.DATA_PIPELINE_ROOT, event -> {
             if (PipelineMetaDataNode.BARRIER_PATTERN.matcher(event.getKey()).matches() && event.getType() == Type.ADDED) {
                 PipelineDistributedBarrier.getInstance().checkChildrenNodeCount(event);

Review Comment:
   `PipelineMetaDataNode.BARRIER_PATTERN.matcher` could not be hard-coded, it could be another SPI impl



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/spi/listener/PipelineMetaDataListener.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.spi.listener;
+
+import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
+import org.apache.shardingsphere.infra.util.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
+import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
+
+/**
+ * Pipeline meta data listener.
+ */
+@SingletonSPI
+public interface PipelineMetaDataListener extends TypedSPI {
+    
+    /**
+     * Get watch key of listener.
+     *
+     * @return watch key
+     */
+    String getWatchKey();
+    
+    /**
+     * Handler of listener.
+     *
+     * @param event changed event
+     * @param jobConfigPOJO job config pojo
+     */
+    void handler(DataChangedEvent event, JobConfigurationPOJO jobConfigPOJO);

Review Comment:
   `handler` method name could be improved:
   1, A verb is needed, e.g. `handle`
   2, It's better to set similar method name as class name, if it's `handle`, then class name could be `XxxHandler`
   
   



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/node/PipelineMetaDataNode.java:
##########
@@ -32,8 +32,6 @@ public final class PipelineMetaDataNode {
     
     private static final String JOB_PATTERN_PREFIX = DataPipelineConstants.DATA_PIPELINE_ROOT + "/jobs/(j\\d{2}[0-9a-f]+)";
     
-    public static final Pattern CONFIG_PATTERN = Pattern.compile(JOB_PATTERN_PREFIX + "/config");
-    

Review Comment:
   It should be kept here, for unified maintanance



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org