Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: honor polling interval across process restarts #282

Merged
merged 7 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -32,21 +32,25 @@ final class ClientContextImpl extends ClientContext {
private final FeatureFetcher fetcher;
private final PlatformState platformState;
private final TaskExecutor taskExecutor;
private final PersistentDataStoreWrapper.PerEnvironmentData perEnvironmentData;

ClientContextImpl(
ClientContext base,
DiagnosticStore diagnosticStore,
FeatureFetcher fetcher,
PlatformState platformState,
TaskExecutor taskExecutor
TaskExecutor taskExecutor,
PersistentDataStoreWrapper.PerEnvironmentData perEnvironmentData
) {
super(base);
this.diagnosticStore = diagnosticStore;
this.fetcher = fetcher;
this.platformState = platformState;
this.taskExecutor = taskExecutor;
this.perEnvironmentData = perEnvironmentData;
}

// TODO: consider re-ordering so perEnvironmentData is earlier in list of params
static ClientContextImpl fromConfig(
LDConfig config,
String mobileKey,
Expand All @@ -56,7 +60,8 @@ static ClientContextImpl fromConfig(
LDLogger logger,
PlatformState platformState,
IEnvironmentReporter environmentReporter,
TaskExecutor taskExecutor
TaskExecutor taskExecutor,
PersistentDataStoreWrapper.PerEnvironmentData perEnvironmentData
) {
boolean initiallyInBackground = platformState != null && !platformState.isForeground();
ClientContext minimalContext = new ClientContext(mobileKey, environmentReporter, logger, config,
Expand All @@ -82,14 +87,14 @@ static ClientContextImpl fromConfig(
if (!config.getDiagnosticOptOut()) {
diagnosticStore = new DiagnosticStore(EventUtil.makeDiagnosticParams(baseClientContext));
}
return new ClientContextImpl(baseClientContext, diagnosticStore, fetcher, platformState, taskExecutor);
return new ClientContextImpl(baseClientContext, diagnosticStore, fetcher, platformState, taskExecutor, perEnvironmentData);
}

public static ClientContextImpl get(ClientContext context) {
if (context instanceof ClientContextImpl) {
return (ClientContextImpl)context;
}
return new ClientContextImpl(context, null, null, null, null);
return new ClientContextImpl(context, null, null, null, null, null);
}

public static ClientContextImpl forDataSource(
Expand Down Expand Up @@ -119,7 +124,8 @@ public static ClientContextImpl forDataSource(
baseContextImpl.getDiagnosticStore(),
baseContextImpl.getFetcher(),
baseContextImpl.getPlatformState(),
baseContextImpl.getTaskExecutor()
baseContextImpl.getTaskExecutor(),
baseContextImpl.getPerEnvironmentData()
);
}

Expand All @@ -134,7 +140,8 @@ public ClientContextImpl setEvaluationContext(LDContext context) {
this.diagnosticStore,
this.fetcher,
this.platformState,
this.taskExecutor
this.taskExecutor,
this.perEnvironmentData
);
}

Expand All @@ -154,6 +161,10 @@ public TaskExecutor getTaskExecutor() {
return throwExceptionIfNull(taskExecutor);
}

public PersistentDataStoreWrapper.PerEnvironmentData getPerEnvironmentData() {
return throwExceptionIfNull(perEnvironmentData);
}

private static <T> T throwExceptionIfNull(T o) {
if (o == null) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,30 +244,44 @@ static final class PollingDataSourceBuilderImpl extends PollingDataSourceBuilder
implements DiagnosticDescription, DataSourceRequiresFeatureFetcher {
@Override
public DataSource build(ClientContext clientContext) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For reviewers: Updated from using ClientContext to ClientContextImpl type here to have access to an internal only object, namely the PerEnvironmentData.

clientContext.getDataSourceUpdateSink().setStatus(
clientContext.isInBackground() ? ConnectionInformation.ConnectionMode.BACKGROUND_POLLING :
ClientContextImpl clientContextImpl = ClientContextImpl.get(clientContext);
clientContextImpl.getDataSourceUpdateSink().setStatus(
clientContextImpl.isInBackground() ? ConnectionInformation.ConnectionMode.BACKGROUND_POLLING :
ConnectionInformation.ConnectionMode.POLLING,
null
);
int actualPollIntervalMillis = clientContext.isInBackground() ? backgroundPollIntervalMillis :

int pollInterval = clientContextImpl.isInBackground() ? backgroundPollIntervalMillis :
pollIntervalMillis;
int initialDelayMillis;

long initialDelayMillis;
if (clientContext.isInBackground() && Boolean.FALSE.equals(clientContext.getPreviouslyInBackground())) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For reviewers: This background/foregrounding distinction is no more! The only impact foreground/background has is on the polling interval. Now they are treated the same after that which results in some improvements in behavioral consistency.

// If we're transitioning from foreground to background, then we don't want to do a
// poll right away because we already have recent flag data. Start polling *after*
// the first background poll interval.
initialDelayMillis = backgroundPollIntervalMillis;
} else {
// If we're in the foreground-- or, if we're in the background but we started out
// that way rather than transitioning-- then we should do the first poll right away.
initialDelayMillis = 0;
// get the last updated timestamp for this context
PersistentDataStoreWrapper.PerEnvironmentData perEnvironmentData = clientContextImpl.getPerEnvironmentData();
String hashedContextId = LDUtil.urlSafeBase64HashedContextId(clientContextImpl.getEvaluationContext());
String fingerprint = LDUtil.urlSafeBase64Hash(clientContextImpl.getEvaluationContext());
Long lastUpdated = perEnvironmentData.getLastUpdated(hashedContextId, fingerprint);
if (lastUpdated == null) {
lastUpdated = 0L; // default to beginning of time
}

// To avoid unnecessarily frequent polling requests due to process or application lifecycle, we have added
// this initial delay logic. Calculate how much time has passed since the last update, if that is less than
// the polling interval, delay by the difference, otherwise 0 delay.
long elapsedSinceUpdate = System.currentTimeMillis() - lastUpdated;
initialDelayMillis = Math.max(pollInterval - elapsedSinceUpdate, 0);
}
ClientContextImpl clientContextImpl = ClientContextImpl.get(clientContext);

return new PollingDataSource(
clientContext.getEvaluationContext(),
clientContext.getDataSourceUpdateSink(),
clientContextImpl.getEvaluationContext(),
clientContextImpl.getDataSourceUpdateSink(),
initialDelayMillis,
actualPollIntervalMillis,
pollInterval,
clientContextImpl.getFetcher(),
clientContextImpl.getPlatformState(),
clientContextImpl.getTaskExecutor(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public void onSuccess(String flagsJson) {
@Override
public void onError(Throwable e) {
logger.error("Error when attempting to get flag data: [{}] [{}]: {}",
LDUtil.base64Url(contextToFetch),
LDUtil.urlSafeBase64(contextToFetch),
contextToFetch,
LogValues.exceptionSummary(e));
resultCallback.onError(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
* deferred listener calls are done via the {@link TaskExecutor} abstraction.
*/
final class ContextDataManager {
static final ContextHasher HASHER = new ContextHasher();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For reviewers: ContextHasher was eliminated in favor of a static method.


private final PersistentDataStoreWrapper.PerEnvironmentData environmentStore;
private final int maxCachedContexts;
private final TaskExecutor taskExecutor;
Expand All @@ -57,14 +55,15 @@ final class ContextDataManager {

@NonNull private volatile LDContext currentContext;
@NonNull private volatile EnvironmentData flags = new EnvironmentData();
@NonNull private volatile ContextIndex index = null;
@NonNull private volatile ContextIndex index;

ContextDataManager(
@NonNull ClientContext clientContext,
@NonNull PersistentDataStoreWrapper.PerEnvironmentData environmentStore,
int maxCachedContexts
) {
this.environmentStore = environmentStore;
this.index = environmentStore.getIndex();
this.maxCachedContexts = maxCachedContexts;
this.taskExecutor = ClientContextImpl.get(clientContext).getTaskExecutor();
this.logger = clientContext.getBaseLogger();
Expand Down Expand Up @@ -121,7 +120,8 @@ private void initDataInternal(
boolean writeFlagsToPersistentStore
) {
List<String> removedContextIds = new ArrayList<>();
String contextId = hashedContextId(context);
String contextId = LDUtil.urlSafeBase64HashedContextId(context);
String fingerprint = LDUtil.urlSafeBase64Hash(context);
EnvironmentData oldData;
ContextIndex newIndex;

Expand All @@ -133,9 +133,6 @@ private void initDataInternal(

oldData = flags;
flags = newData;
if (index == null) {
index = environmentStore.getIndex();
}
newIndex = index.updateTimestamp(contextId, System.currentTimeMillis())
.prune(maxCachedContexts, removedContextIds);
index = newIndex;
Expand All @@ -145,7 +142,7 @@ private void initDataInternal(
logger.debug("Removed flag data for context {} from persistent store", removedContextId);
}
if (writeFlagsToPersistentStore && maxCachedContexts != 0) {
environmentStore.setContextData(contextId, newData);
environmentStore.setContextData(contextId, fingerprint, newData);
logger.debug("Updated flag data for context {} in persistent store", contextId);
}
environmentStore.setIndex(newIndex);
Expand Down Expand Up @@ -241,8 +238,11 @@ public boolean upsert(@NonNull LDContext context, @NonNull Flag flag) {
updatedFlags = flags.withFlagUpdatedOrAdded(flag);
flags = updatedFlags;

String contextId = hashedContextId(context);
environmentStore.setContextData(contextId, updatedFlags);
String hashedContextId = LDUtil.urlSafeBase64HashedContextId(context);
String fingerprint = LDUtil.urlSafeBase64Hash(context);
environmentStore.setContextData(hashedContextId, fingerprint, updatedFlags);
index = index.updateTimestamp(hashedContextId, System.currentTimeMillis());
environmentStore.setIndex(index);
Copy link
Contributor Author

@tanderson-ld tanderson-ld Nov 22, 2024

Choose a reason for hiding this comment

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

For reviewers: This code path is hit when a flag patch comes in.

}

Collection<String> updatedFlag = Collections.singletonList(flag.getKey());
Expand Down Expand Up @@ -292,10 +292,6 @@ public Collection<FeatureFlagChangeListener> getListenersByKey(String key) {
return res == null ? new HashSet<>() : res;
}

public static String hashedContextId(final LDContext context) {
return HASHER.hash(context.getFullyQualifiedKey());
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For Reviewers: Moved to LDUtil

/**
* Attempts to retrieve data for the specified context, if any, from the persistent store. This
* does not affect the current context/flags state.
Expand All @@ -305,7 +301,7 @@ public static String hashedContextId(final LDContext context) {
*/
@VisibleForTesting
public @Nullable EnvironmentData getStoredData(LDContext context) {
return environmentStore.getContextData(hashedContextId(context));
return environmentStore.getContextData(LDUtil.urlSafeBase64HashedContextId(context));
}

private void notifyFlagListeners(Collection<String> updatedFlagKeys) {
Expand Down
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For reviewers: This class was a glorified static method, so I got rid of it and moved it to LDUtil.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private Request getDefaultRequest(LDContext ldContext) throws IOException {
// and methods like Uri.withAppendedPath, simply to minimize the amount of code that relies on
// Android-specific APIs so our components are more easily unit-testable.
URI uri = HttpHelpers.concatenateUriPath(pollUri, StandardEndpoints.POLLING_REQUEST_GET_BASE_PATH);
uri = HttpHelpers.concatenateUriPath(uri, LDUtil.base64Url(ldContext));
uri = HttpHelpers.concatenateUriPath(uri, LDUtil.urlSafeBase64(ldContext));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For reviewers: Just a rename

if (evaluationReasons) {
uri = URI.create(uri.toString() + "?withReasons=true");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ protected LDClient(
FeatureFetcher fetcher = null;
if (config.dataSource instanceof ComponentsImpl.DataSourceRequiresFeatureFetcher) {
ClientContextImpl minimalContext = ClientContextImpl.fromConfig(config, mobileKey,
environmentName, null, initialContext, logger, platformState, environmentReporter, taskExecutor
environmentName, null, initialContext, logger, platformState, environmentReporter, taskExecutor, environmentStore
);
fetcher = new HttpFeatureFlagFetcher(minimalContext);
}
Expand All @@ -344,7 +344,8 @@ protected LDClient(
logger,
platformState,
environmentReporter,
taskExecutor
taskExecutor,
environmentStore
);

this.contextDataManager = new ContextDataManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,15 @@ static String urlSafeBase64Hash(String input) {
}
}

static String base64Url(final LDContext context) {
public static String urlSafeBase64HashedContextId(LDContext context) {
return urlSafeBase64Hash(context.getFullyQualifiedKey());
}

static String urlSafeBase64Hash(LDContext context) {
return urlSafeBase64Hash(JsonSerialization.serialize(context));
}

static String urlSafeBase64(LDContext context) {
return Base64.encodeToString(JsonSerialization.serialize(context).getBytes(),
Base64.URL_SAFE + Base64.NO_WRAP);
}
Expand Down
Loading
Loading