Skip to content

Commit

Permalink
Merge pull request #459 from splitio/fix-headers
Browse files Browse the repository at this point in the history
Fix Split’s handler response headers.
  • Loading branch information
sanzmauro authored Nov 8, 2023
2 parents 09cd178 + 17de44d commit ef46ef3
Show file tree
Hide file tree
Showing 15 changed files with 19 additions and 143 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
4.10.1 (Nov 9, 2023)
- Fixed handler for response http headers.

4.10.0 (Nov 2, 2023)
- Added support for Flag Sets on the SDK, which enables grouping feature flags and interacting with the group rather than individually (more details in our documentation):
- Added new variations of the get treatment methods to support evaluating flags in given flag set/s.
Expand Down
2 changes: 1 addition & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.split.client</groupId>
<artifactId>java-client-parent</artifactId>
<version>4.10.0</version>
<version>4.10.1</version>
</parent>
<artifactId>java-client</artifactId>
<packaging>jar</packaging>
Expand Down
13 changes: 0 additions & 13 deletions client/src/main/java/io/split/client/HttpSegmentChangeFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;
import org.slf4j.Logger;
Expand All @@ -23,8 +22,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -40,9 +37,6 @@ public final class HttpSegmentChangeFetcher implements SegmentChangeFetcher {
private static final String CACHE_CONTROL_HEADER_NAME = "Cache-Control";
private static final String CACHE_CONTROL_HEADER_VALUE = "no-cache";

private static final String HEADER_FASTLY_DEBUG_NAME = "Fastly-Debug";
private static final String HEADER_FASTLY_DEBUG_VALUE = "1";

private final CloseableHttpClient _client;
private final URI _target;
private final TelemetryRuntimeProducer _telemetryRuntimeProducer;
Expand Down Expand Up @@ -81,15 +75,8 @@ public SegmentChange fetch(String segmentName, long since, FetchOptions options)
request.setHeader(CACHE_CONTROL_HEADER_NAME, CACHE_CONTROL_HEADER_VALUE);
}

if (options.fastlyDebugHeaderEnabled()) {
request.addHeader(HEADER_FASTLY_DEBUG_NAME, HEADER_FASTLY_DEBUG_VALUE);
}

response = _client.execute(request);

options.handleResponseHeaders(Arrays.stream(response.getHeaders())
.collect(Collectors.toMap(Header::getName, Header::getValue)));

int statusCode = response.getCode();

if (_log.isDebugEnabled()) {
Expand Down
12 changes: 0 additions & 12 deletions client/src/main/java/io/split/client/HttpSplitChangeFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;
import org.slf4j.Logger;
Expand All @@ -23,8 +22,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -41,9 +38,6 @@ public final class HttpSplitChangeFetcher implements SplitChangeFetcher {
private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control";
private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache";

private static final String HEADER_FASTLY_DEBUG_NAME = "Fastly-Debug";
private static final String HEADER_FASTLY_DEBUG_VALUE = "1";

private final CloseableHttpClient _client;
private final URI _target;
private final TelemetryRuntimeProducer _telemetryRuntimeProducer;
Expand Down Expand Up @@ -87,13 +81,7 @@ public SplitChange fetch(long since, FetchOptions options) {
request.setHeader(HEADER_CACHE_CONTROL_NAME, HEADER_CACHE_CONTROL_VALUE);
}

if (options.fastlyDebugHeaderEnabled()) {
request.addHeader(HEADER_FASTLY_DEBUG_NAME, HEADER_FASTLY_DEBUG_VALUE);
}

response = _client.execute(request);
options.handleResponseHeaders(Arrays.stream(response.getHeaders())
.collect(Collectors.toMap(Header::getName, Header::getValue)));

int statusCode = response.getCode();

Expand Down
7 changes: 0 additions & 7 deletions client/src/main/java/io/split/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public class SplitClientConfig {
private final int _uniqueKeysRefreshRateInMemory;
private final int _uniqueKeysRefreshRateRedis;
private static int _filterUniqueKeysRefreshRate;
private final boolean _cdnDebugLogging;
private final OperationMode _operationMode;
private long _validateAfterInactivityInMillis;
private final long _startingSyncCallBackoffBaseMs;
Expand Down Expand Up @@ -135,7 +134,6 @@ private SplitClientConfig(String endpoint,
int onDemandFetchRetryDelayMs,
int onDemandFetchMaxRetries,
int failedAttemptsBeforeLogging,
boolean cdnDebugLogging,
OperationMode operationMode,
long validateAfterInactivityInMillis,
long startingSyncCallBackoffBaseMs,
Expand Down Expand Up @@ -190,7 +188,6 @@ private SplitClientConfig(String endpoint,
_onDemandFetchRetryDelayMs = onDemandFetchRetryDelayMs;
_onDemandFetchMaxRetries = onDemandFetchMaxRetries;
_failedAttemptsBeforeLogging = failedAttemptsBeforeLogging;
_cdnDebugLogging = cdnDebugLogging;
_operationMode = operationMode;
_storageMode = storageMode;
_validateAfterInactivityInMillis = validateAfterInactivityInMillis;
Expand Down Expand Up @@ -367,8 +364,6 @@ public int get_telemetryRefreshRate() {

public int failedAttemptsBeforeLogging() {return _failedAttemptsBeforeLogging;}

public boolean cdnDebugLogging() { return _cdnDebugLogging; }

public OperationMode operationMode() { return _operationMode;}

public long validateAfterInactivityInMillis() {
Expand Down Expand Up @@ -445,7 +440,6 @@ public static final class Builder {
private int _onDemandFetchRetryDelayMs = 50;
private final int _onDemandFetchMaxRetries = 10;
private final int _failedAttemptsBeforeLogging = 10;
private final boolean _cdnDebugLogging = true;
private OperationMode _operationMode = OperationMode.STANDALONE;
private long _validateAfterInactivityInMillis = 1000;
private static final long STARTING_SYNC_CALL_BACKOFF_BASE_MS = 1000; //backoff base starting at 1 seconds
Expand Down Expand Up @@ -1086,7 +1080,6 @@ public SplitClientConfig build() {
_onDemandFetchRetryDelayMs,
_onDemandFetchMaxRetries,
_failedAttemptsBeforeLogging,
_cdnDebugLogging,
_operationMode,
_validateAfterInactivityInMillis,
STARTING_SYNC_CALL_BACKOFF_BASE_MS,
Expand Down
39 changes: 2 additions & 37 deletions client/src/main/java/io/split/engine/common/FetchOptions.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.split.engine.common;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

public class FetchOptions {

Expand All @@ -15,8 +13,6 @@ public Builder() {}
public Builder(FetchOptions opts) {
_targetCN = opts._targetCN;
_cacheControlHeaders = opts._cacheControlHeaders;
_fastlyDebugHeader = opts._fastlyDebugHeader;
_responseHeadersCallback = opts._responseHeadersCallback;
_flagSetsFilter = opts._flagSetsFilter;
}

Expand All @@ -25,16 +21,6 @@ public Builder cacheControlHeaders(boolean on) {
return this;
}

public Builder fastlyDebugHeader(boolean on) {
_fastlyDebugHeader = on;
return this;
}

public Builder responseHeadersCallback(Function<Map<String, String>, Void> callback) {
_responseHeadersCallback = callback;
return this;
}

public Builder targetChangeNumber(long targetCN) {
_targetCN = targetCN;
return this;
Expand All @@ -46,24 +32,18 @@ public Builder flagSetsFilter(String flagSetsFilter) {
}

public FetchOptions build() {
return new FetchOptions(_cacheControlHeaders, _targetCN, _responseHeadersCallback, _fastlyDebugHeader, _flagSetsFilter);
return new FetchOptions(_cacheControlHeaders, _targetCN, _flagSetsFilter);
}

private long _targetCN = DEFAULT_TARGET_CHANGENUMBER;
private boolean _cacheControlHeaders = false;
private boolean _fastlyDebugHeader = false;
private Function<Map<String, String>, Void> _responseHeadersCallback = null;
private String _flagSetsFilter = "";
}

public boolean cacheControlHeadersEnabled() {
return _cacheControlHeaders;
}

public boolean fastlyDebugHeaderEnabled() {
return _fastlyDebugHeader;
}

public long targetCN() { return _targetCN; }

public boolean hasCustomCN() { return _targetCN != DEFAULT_TARGET_CHANGENUMBER; }
Expand All @@ -72,22 +52,11 @@ public String flagSetsFilter() {
return _flagSetsFilter;
}

public void handleResponseHeaders(Map<String, String> headers) {
if (Objects.isNull(_responseHeadersCallback) || Objects.isNull(headers)) {
return;
}
_responseHeadersCallback.apply(headers);
}

private FetchOptions(boolean cacheControlHeaders,
long targetCN,
Function<Map<String, String>, Void> responseHeadersCallback,
boolean fastlyDebugHeader,
String flagSetsFilter) {
_cacheControlHeaders = cacheControlHeaders;
_targetCN = targetCN;
_responseHeadersCallback = responseHeadersCallback;
_fastlyDebugHeader = fastlyDebugHeader;
_flagSetsFilter = flagSetsFilter;
}

Expand All @@ -100,21 +69,17 @@ public boolean equals(Object obj) {
FetchOptions other = (FetchOptions) obj;

return Objects.equals(_cacheControlHeaders, other._cacheControlHeaders)
&& Objects.equals(_fastlyDebugHeader, other._fastlyDebugHeader)
&& Objects.equals(_responseHeadersCallback, other._responseHeadersCallback)
&& Objects.equals(_targetCN, other._targetCN)
&& Objects.equals(_flagSetsFilter, other._flagSetsFilter);
}

@Override
public int hashCode() {
return com.google.common.base.Objects.hashCode(_cacheControlHeaders, _fastlyDebugHeader, _responseHeadersCallback,
return com.google.common.base.Objects.hashCode(_cacheControlHeaders,
_targetCN, _flagSetsFilter);
}

private final boolean _cacheControlHeaders;
private final boolean _fastlyDebugHeader;
private final long _targetCN;
private final Function<Map<String, String>, Void> _responseHeadersCallback;
private final String _flagSetsFilter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public static SyncManagerImp build(SplitTasks splitTasks,
config.streamingRetryDelay(),
config.streamingFetchMaxRetries(),
config.failedAttemptsBeforeLogging(),
config.cdnDebugLogging(),
config.getSetsFilter());

PushManager pushManager = PushManagerImp.build(synchronizer,
Expand Down
34 changes: 2 additions & 32 deletions client/src/main/java/io/split/engine/common/SynchronizerImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.split.client.events.EventsTask;
import io.split.client.impressions.ImpressionsManager;
import io.split.client.impressions.UniqueKeysTracker;
import io.split.client.utils.Json;
import io.split.engine.experiments.FetchResult;
import io.split.engine.experiments.SplitFetcher;
import io.split.engine.experiments.SplitSynchronizationTask;
Expand All @@ -17,8 +16,6 @@
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -45,7 +42,6 @@ public class SynchronizerImp implements Synchronizer {
private final int _onDemandFetchRetryDelayMs;
private final int _onDemandFetchMaxRetries;
private final int _failedAttemptsBeforeLogging;
private final boolean _cdnResponseHeadersLogging;
private final String _sets;

public SynchronizerImp(SplitTasks splitTasks,
Expand All @@ -55,15 +51,13 @@ public SynchronizerImp(SplitTasks splitTasks,
int onDemandFetchRetryDelayMs,
int onDemandFetchMaxRetries,
int failedAttemptsBeforeLogging,
boolean cdnResponseHeadersLogging,
HashSet<String> sets) {
_splitSynchronizationTask = checkNotNull(splitTasks.getSplitSynchronizationTask());
_splitFetcher = checkNotNull(splitFetcher);
_segmentSynchronizationTaskImp = checkNotNull(splitTasks.getSegmentSynchronizationTask());
_splitCacheProducer = checkNotNull(splitCacheProducer);
this.segmentCacheProducer = checkNotNull(segmentCacheProducer);
_onDemandFetchRetryDelayMs = checkNotNull(onDemandFetchRetryDelayMs);
_cdnResponseHeadersLogging = cdnResponseHeadersLogging;
_onDemandFetchMaxRetries = onDemandFetchMaxRetries;
_failedAttemptsBeforeLogging = failedAttemptsBeforeLogging;
_impressionManager = splitTasks.getImpressionManager();
Expand Down Expand Up @@ -135,12 +129,6 @@ private SyncResult attemptSplitsSync(long targetChangeNumber,
}
}

private void logCdnHeaders(String prefix, int maxRetries, int remainingAttempts, List<Map<String, String>> headers) {
if (maxRetries - remainingAttempts > _failedAttemptsBeforeLogging) {
_log.info(String.format("%s: CDN Debug headers: %s", prefix, Json.toJson(headers)));
}
}

@Override
public void refreshSplits(Long targetChangeNumber) {

Expand All @@ -151,8 +139,6 @@ public void refreshSplits(Long targetChangeNumber) {
FastlyHeadersCaptor captor = new FastlyHeadersCaptor();
FetchOptions opts = new FetchOptions.Builder()
.cacheControlHeaders(true)
.fastlyDebugHeader(_cdnResponseHeadersLogging)
.responseHeadersCallback(_cdnResponseHeadersLogging ? captor::handle : null)
.flagSetsFilter(_sets)
.build();

Expand All @@ -162,9 +148,7 @@ public void refreshSplits(Long targetChangeNumber) {
int attempts = _onDemandFetchMaxRetries - regularResult.remainingAttempts();
if (regularResult.success()) {
_log.debug(String.format("Refresh completed in %s attempts.", attempts));
if (_cdnResponseHeadersLogging) {
logCdnHeaders("[splits]", _onDemandFetchMaxRetries , regularResult.remainingAttempts(), captor.get());
}

regularResult._fetchResult.getSegments().stream()
.forEach(segmentName -> forceRefreshSegment(segmentName));
return;
Expand All @@ -184,11 +168,6 @@ public void refreshSplits(Long targetChangeNumber) {
} else {
_log.debug(String.format("No changes fetched after %s attempts with CDN bypassed.", withoutCDNAttempts));
}

if (_cdnResponseHeadersLogging) {
logCdnHeaders("[splits]", _onDemandFetchMaxRetries + ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES,
withCDNBypassed.remainingAttempts(), captor.get());
}
}

@Override
Expand Down Expand Up @@ -238,8 +217,6 @@ public void refreshSegment(String segmentName, Long targetChangeNumber) {
FastlyHeadersCaptor captor = new FastlyHeadersCaptor();
FetchOptions opts = new FetchOptions.Builder()
.cacheControlHeaders(true)
.fastlyDebugHeader(_cdnResponseHeadersLogging)
.responseHeadersCallback(_cdnResponseHeadersLogging ? captor::handle : null)
.build();

SyncResult regularResult = attemptSegmentSync(segmentName, targetChangeNumber, opts,
Expand All @@ -248,9 +225,7 @@ public void refreshSegment(String segmentName, Long targetChangeNumber) {
int attempts = _onDemandFetchMaxRetries - regularResult.remainingAttempts();
if (regularResult.success()) {
_log.debug(String.format("Segment %s refresh completed in %s attempts.", segmentName, attempts));
if (_cdnResponseHeadersLogging) {
logCdnHeaders(String.format("[segment/%s]", segmentName), _onDemandFetchMaxRetries , regularResult.remainingAttempts(), captor.get());
}

return;
}

Expand All @@ -266,11 +241,6 @@ public void refreshSegment(String segmentName, Long targetChangeNumber) {
} else {
_log.debug(String.format("No changes fetched for segment %s after %s attempts with CDN bypassed.", segmentName, withoutCDNAttempts));
}

if (_cdnResponseHeadersLogging) {
logCdnHeaders(String.format("[segment/%s]", segmentName), _onDemandFetchMaxRetries + ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES,
withCDNBypassed.remainingAttempts(), captor.get());
}
}

@Override
Expand Down
Loading

0 comments on commit ef46ef3

Please sign in to comment.