Skip to content
Permalink
Browse files

Merge fe686d0 into a67c09f

  • Loading branch information
betterjava committed Aug 15, 2019
2 parents a67c09f + fe686d0 commit 9971f928c54f1ca236c42a3c35fb5145a00a49f6
@@ -18,17 +18,23 @@
package org.apache.shardingsphere.opentracing;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.opentracing.spi.ShardingTracerGenerator;
import org.apache.shardingsphere.opentracing.spi.ShardingTracerGeneratorServiceLoader;

import java.util.Properties;

/**
* Sharding tracer object container.
*
* @author gaohongtao
* @author wangkai
* @author liya
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingTracer {
@@ -58,6 +64,28 @@ public static void init(final Tracer tracer) {
GlobalTracer.register(tracer);
}
}

/**
* Initialize sharding tracer by SPI with properties.
*
* @param tracerGeneratorType type of tracerGenerator
* @param properties properties of tracerGenerator
*/
public static void init(final String tracerGeneratorType, final Properties properties) {
Preconditions.checkNotNull(tracerGeneratorType, "Can not find opentracing tracer generator implementation class via SPI");
ShardingTracerGeneratorServiceLoader serviceLoader = new ShardingTracerGeneratorServiceLoader();
ShardingTracerGenerator shardingTracerGenerator = serviceLoader.newService(tracerGeneratorType, properties);
ShardingTracer.init(shardingTracerGenerator.generate());
}

/**
* Initialize sharding tracer by SPI.
*
* @param tracerGeneratorType type of tracerGenerator
*/
public static void init(final String tracerGeneratorType) {
init(tracerGeneratorType, new Properties());
}

/**
* Get tracer.
@@ -0,0 +1,36 @@
/*
* 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.opentracing.spi;

import io.opentracing.Tracer;
import org.apache.shardingsphere.spi.TypeBasedSPI;

/**
* Tracer generator.
*
* @author liya
*/
public interface ShardingTracerGenerator extends TypeBasedSPI {

/**
* Generate Tracer.
*
* @return Tracer
*/
Tracer generate();
}
@@ -0,0 +1,44 @@
/*
* 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.
*/

/**
* Key generator service loader.
*
* @author zhangliang
* @author panjuan
*/

package org.apache.shardingsphere.opentracing.spi;

import org.apache.shardingsphere.core.spi.NewInstanceServiceLoader;
import org.apache.shardingsphere.core.spi.algorithm.TypeBasedSPIServiceLoader;

/**
* Tracer generator service loader.
*
* @author liya
*/
public class ShardingTracerGeneratorServiceLoader extends TypeBasedSPIServiceLoader<ShardingTracerGenerator> {

static {
NewInstanceServiceLoader.register(ShardingTracerGenerator.class);
}

public ShardingTracerGeneratorServiceLoader() {
super(ShardingTracerGenerator.class);
}
}
@@ -0,0 +1,38 @@
/*
* 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.opentracing.yaml.config;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.core.yaml.config.YamlConfiguration;

import java.util.Properties;

/**
* OpenTracing configuration for yaml.
*
* @author liya
*/
@Getter
@Setter
public class YamlOpenTracingConfiguration implements YamlConfiguration {

private String type;

private Properties props = new Properties();
}
@@ -28,6 +28,7 @@
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
@@ -62,16 +63,32 @@ public void assertDuplicatedLoading() {
@Test
public void assertTracer() {
ShardingTracer.init();
assertTracerValid();
}

private void assertTracerValid() {
assertThat((GlobalTracer) ShardingTracer.get(), isA(GlobalTracer.class));
assertTrue(GlobalTracer.isRegistered());
assertThat(ShardingTracer.get(), is(ShardingTracer.get()));
}

@Test(expected = ShardingException.class)
public void assertTracerClassError() {
System.setProperty("org.apache.shardingsphere.opentracing.tracer.class", "com.foo.FooTracer");
ShardingTracer.init();
}

@Test
public void assertInitWithType() {
ShardingTracer.init("foo");
assertTracerValid();
}

@Test
public void assertInitWithTypeAndProperties() {
ShardingTracer.init("foo", new Properties());
assertTracerValid();
}

@SneakyThrows
private static void clearGlobalTracer() {
@@ -0,0 +1,36 @@
/*
* 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.opentracing.spi;

import org.apache.shardingsphere.opentracing.spi.fixture.FooTracerGenerator;
import org.junit.Test;

import java.util.Properties;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;

public class ShardingTracerGeneratorServiceLoaderTest {

private ShardingTracerGeneratorServiceLoader serviceLoader = new ShardingTracerGeneratorServiceLoader();

@Test
public void assertNewFooTracerGenerator() {
assertThat(serviceLoader.newService("foo", new Properties()), instanceOf(FooTracerGenerator.class));
}
}
@@ -0,0 +1,41 @@
/*
* 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.opentracing.spi.fixture;

import io.opentracing.Tracer;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.opentracing.fixture.FooTracer;
import org.apache.shardingsphere.opentracing.spi.ShardingTracerGenerator;

import java.util.Properties;

public final class FooTracerGenerator implements ShardingTracerGenerator {

@Getter
@Setter
private Properties properties = new Properties();

@Getter
private String type = "foo";

@Override
public Tracer generate() {
return new FooTracer();
}
}
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.shardingsphere.opentracing.spi.fixture.FooTracerGenerator
@@ -32,6 +32,7 @@
import org.apache.shardingsphere.core.yaml.swapper.impl.ShardingRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.opentracing.ShardingTracer;
import org.apache.shardingsphere.orchestration.internal.registry.ShardingOrchestrationFacade;
import org.apache.shardingsphere.opentracing.yaml.config.YamlOpenTracingConfiguration;
import org.apache.shardingsphere.orchestration.yaml.swapper.OrchestrationConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchemas;
import org.apache.shardingsphere.shardingproxy.config.ShardingConfiguration;
@@ -58,6 +59,7 @@
* @author wangkai
* @author panjuan
* @author sunbufu
* @author liya
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Bootstrap {
@@ -75,7 +77,7 @@ public static void main(final String[] args) throws IOException {
logRuleConfigurationMap(getRuleConfiguration(shardingConfig.getRuleConfigurationMap()).values());
int port = getPort(args);
if (null == shardingConfig.getServerConfiguration().getOrchestration()) {
startWithoutRegistryCenter(shardingConfig.getRuleConfigurationMap(), shardingConfig.getServerConfiguration().getAuthentication(), shardingConfig.getServerConfiguration().getProps(), port);
startWithoutRegistryCenter(shardingConfig.getServerConfiguration(), shardingConfig.getRuleConfigurationMap(), port);
} else {
startWithRegistryCenter(shardingConfig.getServerConfiguration(), shardingConfig.getRuleConfigurationMap().keySet(), shardingConfig.getRuleConfigurationMap(), port);
}
@@ -92,14 +94,16 @@ private static int getPort(final String[] args) {
}
}

private static void startWithoutRegistryCenter(final Map<String, YamlProxyRuleConfiguration> ruleConfigs,
final YamlAuthenticationConfiguration authentication, final Properties prop, final int port) {
Authentication authenticationConfiguration = getAuthentication(authentication);
private static void startWithoutRegistryCenter(final YamlProxyServerConfiguration serverConfig,
final Map<String, YamlProxyRuleConfiguration> ruleConfigs,
final int port) {
Authentication authenticationConfiguration = getAuthentication(serverConfig.getAuthentication());
Properties prop = serverConfig.getProps();
ConfigurationLogger.log(authenticationConfiguration);
ConfigurationLogger.log(prop);
ShardingProxyContext.getInstance().init(authenticationConfiguration, prop);
LogicSchemas.getInstance().init(getDataSourceParameterMap(ruleConfigs), getRuleConfiguration(ruleConfigs));
initOpenTracing();
initOpenTracing(serverConfig.getOpentracing());
ShardingProxy.getInstance().start(port);
}

@@ -114,7 +118,7 @@ private static void startWithRegistryCenter(final YamlProxyServerConfiguration s
initShardingOrchestrationFacade(serverConfig, ruleConfigs, shardingOrchestrationFacade);
ShardingProxyContext.getInstance().init(authentication, properties);
LogicSchemas.getInstance().init(shardingSchemaNames, getSchemaDataSourceParameterMap(shardingOrchestrationFacade), getSchemaRules(shardingOrchestrationFacade), true);
initOpenTracing();
initOpenTracing(serverConfig.getOpentracing());
ShardingProxy.getInstance().start(port);
}
}
@@ -151,12 +155,16 @@ private static void initShardingOrchestrationFacade(
}
}

private static void initOpenTracing() {
private static void initOpenTracing(final YamlOpenTracingConfiguration opentracingConfiguration) {
if (ShardingProxyContext.getInstance().getShardingProperties().<Boolean>getValue(ShardingPropertiesConstant.PROXY_OPENTRACING_ENABLED)) {
ShardingTracer.init();
if (null == opentracingConfiguration) {
ShardingTracer.init();
} else {
ShardingTracer.init(opentracingConfiguration.getType(), opentracingConfiguration.getProps());
}
}
}

private static Map<String, Map<String, DataSourceConfiguration>> getDataSourceConfigurationMap(final Map<String, YamlProxyRuleConfiguration> ruleConfigs) {
Map<String, Map<String, DataSourceConfiguration>> result = new LinkedHashMap<>();
for (Entry<String, YamlProxyRuleConfiguration> entry : ruleConfigs.entrySet()) {

0 comments on commit 9971f92

Please sign in to comment.
You can’t perform that action at this time.