View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.core.registrycenter.elasticjob;
19  
20  import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
21  import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
22  import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
23  import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
24  import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
25  import org.apache.shardingsphere.mode.repository.cluster.zookeeper.props.ZookeeperProperties;
26  import org.apache.shardingsphere.mode.repository.cluster.zookeeper.props.ZookeeperPropertyKey;
27  
28  import java.util.Properties;
29  
30  /**
31   * {@linkplain CoordinatorRegistryCenter} initializer.
32   */
33  public final class CoordinatorRegistryCenterInitializer {
34      
35      /**
36       * Create ZooKeeper registry center instance.
37       *
38       * @param modeConfig mode configuration
39       * @param namespaceRelativePath namespace relative path
40       * @return registry center instance
41       */
42      public CoordinatorRegistryCenter createZookeeperRegistryCenter(final ModeConfiguration modeConfig, final String namespaceRelativePath) {
43          ClusterPersistRepositoryConfiguration repositoryConfig = (ClusterPersistRepositoryConfiguration) modeConfig.getRepository();
44          // TODO Add registry center cache. Refer to RegistryCenterFactory.createCoordinatorRegistryCenter
45          CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(getZookeeperConfig(repositoryConfig, namespaceRelativePath));
46          result.init();
47          return result;
48      }
49      
50      private ZookeeperConfiguration getZookeeperConfig(final ClusterPersistRepositoryConfiguration repositoryConfig, final String namespaceRelativePath) {
51          // TODO Merge registry center code in ElasticJob and ShardingSphere mode; Use SPI to load impl
52          Properties props = repositoryConfig.getProps();
53          ZookeeperProperties zookeeperProps = new ZookeeperProperties(props);
54          String namespace = repositoryConfig.getNamespace() + (null != namespaceRelativePath ? namespaceRelativePath : "");
55          ZookeeperConfiguration result = new ZookeeperConfiguration(repositoryConfig.getServerLists(), namespace);
56          int retryIntervalMilliseconds = zookeeperProps.getValue(ZookeeperPropertyKey.RETRY_INTERVAL_MILLISECONDS);
57          result.setBaseSleepTimeMilliseconds(retryIntervalMilliseconds);
58          int maxRetries = zookeeperProps.getValue(ZookeeperPropertyKey.MAX_RETRIES);
59          result.setMaxRetries(maxRetries);
60          result.setMaxSleepTimeMilliseconds(retryIntervalMilliseconds * maxRetries);
61          int timeToLiveSeconds = zookeeperProps.getValue(ZookeeperPropertyKey.TIME_TO_LIVE_SECONDS);
62          if (0 != timeToLiveSeconds) {
63              result.setSessionTimeoutMilliseconds(timeToLiveSeconds * 1000);
64          }
65          int operationTimeoutMilliseconds = zookeeperProps.getValue(ZookeeperPropertyKey.OPERATION_TIMEOUT_MILLISECONDS);
66          if (0 != operationTimeoutMilliseconds) {
67              result.setConnectionTimeoutMilliseconds(operationTimeoutMilliseconds);
68          }
69          result.setDigest(zookeeperProps.getValue(ZookeeperPropertyKey.DIGEST));
70          return result;
71      }
72  }