Skip to content

Commit

Permalink
Cache thread local value to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jan 27, 2025
1 parent 979c55f commit 56c1539
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void addCargo(Cargo cargo) {
String name = cargo.getKey();
if (name != null && !name.isEmpty()) {
if (cargos == null) {
cargos = new HashMap<>(1);
cargos = new HashMap<>(8);
}
Cargo old = cargos.putIfAbsent(cargo.getKey(), cargo);
if (old != null && old != cargo) {
Expand All @@ -55,7 +55,7 @@ public void addCargo(Cargo cargo) {
public void addCargo(String key, String value) {
if (key != null && !key.isEmpty()) {
if (cargos == null) {
cargos = new HashMap<>(1);
cargos = new HashMap<>(8);
}
cargos.computeIfAbsent(key, Cargo::new).add(value);
}
Expand All @@ -65,7 +65,7 @@ public void addCargo(String key, String value) {
public void setCargo(String key, String value) {
if (key != null && !key.isEmpty()) {
if (cargos == null) {
cargos = new HashMap<>(1);
cargos = new HashMap<>(8);
}
cargos.put(key, new Cargo(key, value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.governance.annotation.ConditionalOnLiveEnabled;
import com.jd.live.agent.governance.context.RequestContext;
import com.jd.live.agent.governance.context.bag.Carrier;
import com.jd.live.agent.governance.invoke.CellAction;
import com.jd.live.agent.governance.invoke.CellAction.CellActionType;
Expand Down Expand Up @@ -52,7 +51,7 @@ public <T extends InboundRequest> CompletionStage<Object> filter(InboundInvocati
CellAction cellAction = cellAction(invocation);
invocation.setCellAction(cellAction);
if (cellAction.getType() == CellActionType.FAILOVER) {
Carrier carrier = RequestContext.getOrCreate();
Carrier carrier = invocation.getRequest().getOrCreateCarrier();
carrier.setAttribute(Carrier.ATTRIBUTE_FAILOVER_CELL, cellAction);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.core.instance.GatewayRole;
import com.jd.live.agent.governance.annotation.ConditionalOnLiveEnabled;
import com.jd.live.agent.governance.context.RequestContext;
import com.jd.live.agent.governance.context.bag.Carrier;
import com.jd.live.agent.governance.invoke.InboundInvocation;
import com.jd.live.agent.governance.invoke.UnitAction;
Expand Down Expand Up @@ -52,7 +51,7 @@ public <T extends InboundRequest> CompletionStage<Object> filter(InboundInvocati
switch (unitAction.getType()) {
case FAILOVER_CENTER:
case FAILOVER:
Carrier carrier = RequestContext.getOrCreate();
Carrier carrier = invocation.getRequest().getOrCreateCarrier();
carrier.setAttribute(Carrier.ATTRIBUTE_FAILOVER_UNIT, unitAction);
break;
case REJECT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import com.jd.live.agent.core.Constants;
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.governance.context.RequestContext;
import com.jd.live.agent.governance.context.bag.Cargo;
import com.jd.live.agent.governance.context.bag.Carrier;
import com.jd.live.agent.governance.request.ServiceRequest;

import java.util.List;
Expand All @@ -31,7 +31,8 @@ public class ConsumerTagProvider implements SystemTagProvider {

@Override
public List<String> getValues(ServiceRequest request) {
Cargo cargo = RequestContext.getCargo(Constants.LABEL_SERVICE_CONSUMER);
Carrier carrier = request.getCarrier();
Cargo cargo = carrier == null ? null : carrier.getCargo(Constants.LABEL_SERVICE_CONSUMER);
return cargo == null ? null : cargo.getValues();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.jd.live.agent.core.instance.Location;
import com.jd.live.agent.core.util.matcher.Matcher;
import com.jd.live.agent.governance.config.LaneConfig;
import com.jd.live.agent.governance.context.RequestContext;
import com.jd.live.agent.governance.context.bag.Cargo;
import com.jd.live.agent.governance.context.bag.Carrier;
import com.jd.live.agent.governance.invoke.metadata.LaneMetadata;
Expand Down Expand Up @@ -118,7 +117,7 @@ public LaneMetadata parse() {
* @return The lane space id, or null if not found.
*/
protected String parseLaneSpace() {
Cargo cargo = RequestContext.getCargo(Constants.LABEL_LANE_SPACE_ID);
Cargo cargo = request.getCargo(Constants.LABEL_LANE_SPACE_ID);
String laneSpaceId = cargo == null ? null : cargo.getFirstValue();
if ((laneSpaceId == null || laneSpaceId.isEmpty()) && laneConfig.isFallbackLocationIfNoSpace()) {
laneSpaceId = application.getLocation().getLaneSpaceId();
Expand All @@ -134,7 +133,7 @@ protected String parseLaneSpace() {
* @return The lane code as a String, or null if not found.
*/
protected String parseLane(String laneSpaceId, LaneSpace laneSpace) {
Cargo cargo = RequestContext.getCargo(Constants.LABEL_LANE);
Cargo cargo = request.getCargo(Constants.LABEL_LANE);
String lane = cargo == null ? null : cargo.getFirstValue();
Location location = application.getLocation();
if ((lane == null || lane.isEmpty())
Expand All @@ -152,7 +151,7 @@ protected String parseLane(String laneSpaceId, LaneSpace laneSpace) {
* @param metadata the lane metadata to inject into the request context
*/
protected void inject(LaneMetadata metadata) {
Carrier carrier = RequestContext.getOrCreate();
Carrier carrier = request.getOrCreateCarrier();
if (metadata.getTargetSpaceId() != null
&& !metadata.getTargetSpaceId().isEmpty()) {
addCargo(carrier, metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ protected LiveSpace parseLiveSpace(String spaceId) {
*/
protected String parseLiveSpaceId() {
String result = null;
Cargo cargo = RequestContext.getCargo(Constants.LABEL_LIVE_SPACE_ID);
Cargo cargo = request.getCargo(Constants.LABEL_LIVE_SPACE_ID);
if (cargo != null) {
result = cargo.getFirstValue();
} else if (isFallbackLocationIfNoSpace()) {
result = application.getLocation().getLiveSpaceId();
if (result != null && !result.isEmpty()) {
RequestContext.getOrCreate().addCargo(Constants.LABEL_LIVE_SPACE_ID, result);
request.getOrCreateCarrier().addCargo(Constants.LABEL_LIVE_SPACE_ID, result);
}
}
return result;
Expand All @@ -140,15 +140,15 @@ protected String parseLiveSpaceId() {
*/
protected String parseRuleId(String spaceId) {
String result = null;
Cargo cargo = RequestContext.getCargo(Constants.LABEL_RULE_ID);
Cargo cargo = request.getCargo(Constants.LABEL_RULE_ID);
if (cargo != null) {
result = cargo.getFirstValue();
} else if (spaceId != null && !spaceId.isEmpty() && isFallbackLocationIfNoSpace()) {
Location location = application.getLocation();
if (spaceId.equals(location.getLiveSpaceId())) {
result = location.getUnitRuleId();
if (result != null && !result.isEmpty()) {
RequestContext.getOrCreate().addCargo(Constants.LABEL_RULE_ID, result);
request.getOrCreateCarrier().addCargo(Constants.LABEL_RULE_ID, result);
}
}
}
Expand Down Expand Up @@ -291,7 +291,7 @@ public LiveMetadata parse() {
sourceName = path.isCustomVariableSource() ? path.getVariableSource() : sourceName;
}
UnitRule unitRule = unitRuleId == null ? null : liveSpace.getUnitRule(unitRuleId);
Carrier carrier = RequestContext.getOrCreate();
Carrier carrier = request.getOrCreateCarrier();
// The gateway may have decrypted the user and placed it in the context attribute
String variable = carrier.getAttribute(Constants.LABEL_VARIABLE);
if (variable == null) {
Expand Down Expand Up @@ -341,7 +341,7 @@ protected void inject(LiveDomainMetadata metadata) {
// Overwrite contextual tags
LiveSpace liveSpace = metadata.getTargetSpace();
UnitRule unitRule = metadata.getRule();
Carrier carrier = RequestContext.getOrCreate();
Carrier carrier = request.getOrCreateCarrier();
if (unitRule != null) {
carrier.setCargo(Constants.LABEL_LIVE_SPACE_ID, liveSpace.getId());
carrier.setCargo(Constants.LABEL_RULE_ID, unitRule.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.jd.live.agent.core.util.URI;
import com.jd.live.agent.core.util.http.HttpMethod;
import com.jd.live.agent.governance.config.ServiceConfig;
import com.jd.live.agent.governance.context.RequestContext;
import com.jd.live.agent.governance.context.bag.Cargo;
import com.jd.live.agent.governance.invoke.metadata.ServiceMetadata;
import com.jd.live.agent.governance.invoke.metadata.parser.MetadataParser.ServiceParser;
Expand Down Expand Up @@ -181,7 +180,7 @@ public OutboundServiceMetadataParser(ServiceRequest request, ServiceConfig servi

@Override
protected String parseConsumer() {
RequestContext.getOrCreate().setCargo(Constants.LABEL_SERVICE_CONSUMER, application.getName());
request.getOrCreateCarrier().setCargo(Constants.LABEL_SERVICE_CONSUMER, application.getName());
return application.getName();
}

Expand Down Expand Up @@ -215,7 +214,7 @@ public InboundServiceMetadataParser(ServiceRequest request, ServiceConfig servic

@Override
protected String parseConsumer() {
Cargo cargo = RequestContext.getCargo(Constants.LABEL_SERVICE_CONSUMER);
Cargo cargo = request.getCargo(Constants.LABEL_SERVICE_CONSUMER);
return cargo == null ? null : cargo.getFirstValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.jd.live.agent.governance.request;

import com.jd.live.agent.bootstrap.util.AbstractAttributes;
import com.jd.live.agent.governance.context.RequestContext;
import com.jd.live.agent.governance.context.bag.Carrier;
import lombok.Getter;

import java.util.HashSet;
Expand All @@ -25,25 +27,29 @@
* Provides an abstract base class for service requests.*
* @param <T> The type of the original request object this class wraps.
*/
@Getter
public abstract class AbstractServiceRequest<T> extends AbstractAttributes implements ServiceRequest {

/**
* The original request object associated with this service request.
*/
@Getter
protected final T request;

/**
* A set of identifiers representing attempts made in the context of this service request.
* This is useful for tracking retries or duplicate handling.
*/
@Getter
protected Set<String> attempts;

/**
* The timestamp when the service request was created.
*/
@Getter
protected long startTime;

protected Carrier carrier;

/**
* Constructs an instance of {@code AbstractServiceRequest} with the original request object.
*
Expand Down Expand Up @@ -74,5 +80,24 @@ public void addAttempt(String attempt) {
attempts.add(attempt);
}
}

@Override
public Carrier getCarrier() {
// cache thread local value to improve performance
if (carrier == null) {
carrier = RequestContext.get();
}
return carrier;
}

@Override
public Carrier getOrCreateCarrier() {
// cache thread local value to improve performance
if (carrier == null) {
carrier = RequestContext.getOrCreate();
}
return carrier;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.jd.live.agent.core.util.tag.Label;
import com.jd.live.agent.core.util.type.ValuePath;
import com.jd.live.agent.governance.context.RequestContext;
import com.jd.live.agent.governance.context.bag.Cargo;

import java.util.ArrayList;
Expand Down Expand Up @@ -95,13 +94,13 @@ default String getHeader(String key) {

@Override
default List<String> getCookies(String key) {
Cargo cargo = RequestContext.getCargo(key);
Cargo cargo = getCargo(key);
return cargo == null ? null : cargo.getValues();
}

@Override
default String getCookie(String key) {
Cargo cargo = RequestContext.getCargo(key);
Cargo cargo = getCargo(key);
return cargo == null ? null : cargo.getFirstValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.jd.live.agent.governance.request;

import com.jd.live.agent.bootstrap.exception.LiveException;
import com.jd.live.agent.governance.context.bag.Cargo;
import com.jd.live.agent.governance.context.bag.Carrier;
import com.jd.live.agent.governance.exception.ErrorName;
import com.jd.live.agent.governance.exception.ErrorPolicy;
import com.jd.live.agent.governance.policy.live.FaultType;
Expand All @@ -37,6 +39,31 @@
*/
public interface ServiceRequest extends Request {

/**
* Returns the carrier associated with this instance.
*
* @return the carrier
*/
Carrier getCarrier();

/**
* Returns the carrier associated with this instance, creating a new one if it does not exist.
*
* @return the carrier
*/
Carrier getOrCreateCarrier();

/**
* Returns the cargo with the specified key from the carrier associated with this instance.
*
* @param key the key of the cargo
* @return the cargo with the specified key, or null if the carrier is null or the cargo is not found
*/
default Cargo getCargo(String key) {
Carrier carrier = getCarrier();
return carrier == null ? null : carrier.getCargo(key);
}

/**
* Retrieves the name of the service.
*
Expand Down

0 comments on commit 56c1539

Please sign in to comment.