Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,33 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

import com.tencent.polaris.logging.LoggerFactory;
import org.slf4j.Logger;

public class MetadataContextHolder {

private static final ThreadLocal<MetadataContext> THREAD_LOCAL_CONTEXT = new ThreadLocal<>();
private static final Logger LOG = LoggerFactory.getLogger(MetadataContextHolder.class);

private static final ThreadLocal<MetadataContext> THREAD_LOCAL_CONTEXT;

private static Supplier<MetadataContext> initializer;

static {
ThreadLocal<MetadataContext> tempThreadLocalContext;
try {
// the class name need to be excluded in maven shade plugin
Class<?> clazz = Class.forName("com.alibaba.ttl.TransmittableThreadLocal");
tempThreadLocalContext = (ThreadLocal<MetadataContext>) clazz.getDeclaredConstructor(new Class[0]).newInstance();
LOG.info("Use TransmittableThreadLocal for thread local context");
} catch (Exception e) {
LOG.debug("Failed to use TransmittableThreadLocal for thread local context, msg: {}", e.getMessage());
LOG.info("Use standard ThreadLocal for thread local context");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只有TTL才打印info日志,大部分情况都是ThreadLocal

tempThreadLocalContext = new ThreadLocal<>();
}
THREAD_LOCAL_CONTEXT = tempThreadLocalContext;
}


public static MetadataContext getOrCreate() {
MetadataContext metadataContext = THREAD_LOCAL_CONTEXT.get();
if (null != metadataContext) {
Expand Down
3 changes: 3 additions & 0 deletions polaris-distribution/polaris-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@
<pattern>com.alibaba</pattern>
<shadedPattern>shade.polaris.com.alibaba
</shadedPattern>
<excludes>
<exclude>com.alibaba.ttl.TransmittableThreadLocal</exclude>
</excludes>
</relocation>
<relocation>
<pattern>io.perfmark</pattern>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
import java.util.concurrent.atomic.AtomicReference;

import static com.tencent.polaris.api.config.plugin.DefaultPlugins.SERVER_CONNECTOR_CONSUL;
import static com.tencent.polaris.metadata.core.constant.TsfMetadataConstants.TSF_APPLICATION_ID;
import static com.tencent.polaris.metadata.core.constant.TsfMetadataConstants.TSF_GROUP_ID;
import static com.tencent.polaris.metadata.core.constant.TsfMetadataConstants.TSF_NAMESPACE_ID;
import static com.tencent.polaris.plugins.connector.consul.service.common.TagConditionUtil.parseMatchStringType;

/**
Expand Down Expand Up @@ -216,6 +218,9 @@ private List<LaneProto.LaneGroup> parseResponse(LaneInfoResponse laneInfoRespons
if (laneGroup.isEntrance()) {
entranceList.add(laneGroup.getGroupId());
}
if (StringUtils.isNotEmpty(laneGroup.getNamespaceId()) && StringUtils.isNotEmpty(laneGroup.getApplicationId())) {
laneGroupBuilder.putMetadata(laneGroup.getNamespaceId() + "," + laneGroup.getApplicationId(), TSF_NAMESPACE_ID + "," + TSF_APPLICATION_ID);
}
}
}
for (String entrance : entranceList) {
Expand Down
Loading
Loading