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 @@ -186,22 +186,12 @@ private EventStore makeEventStore(ClientContext clientContext, ClientContextImpl
impl.getPlatformState(),
clientContext.getMobileKey(),
capacity,
shareOfCapacityBytes(clientContext.getConfig()),
capacityBytes,
maxAgeMillis,
eventPersistence != EventPersistence.DISABLED,
clientContext.getBaseLogger());
}

/**
* This environment's share of the byte capacity, which is for every environment the application configured
* together, where the event capacity is for each one.
*/
private long shareOfCapacityBytes(LDConfig config) {
int environments = config == null ? 1 : Math.max(1, config.getMobileKeys().size());
// At least one byte, since the store reads zero as the default.
return Math.max(1, capacityBytes / environments);
}

@Override
public LDValue describeConfiguration(ClientContext clientContext) {
return LDValue.buildObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public abstract class EventProcessorBuilder implements ComponentConfigurer<Event
protected EventPersistence eventPersistence = DEFAULT_EVENT_PERSISTENCE;

/**
* The most bytes of events kept for all environments together where events are persisted
* The most bytes of events kept for each environment where events are persisted
*/
protected long capacityBytes = DEFAULT_CAPACITY_BYTES;

Expand Down Expand Up @@ -191,25 +191,23 @@ public EventProcessorBuilder eventPersistence(EventPersistence eventPersistence)
}

/**
* Sets the maximum number of bytes of events the SDK keeps, on disk and in memory together, whatever
* {@link #eventPersistence(EventPersistence)} is.
* Sets the maximum number of bytes of events the SDK keeps for each environment, on disk and in memory
* together, whatever {@link #eventPersistence(EventPersistence)} is.
* <p>
* It is for every environment the application configures together: with
* {@link com.launchdarkly.sdk.android.LDConfig.Builder#secondaryMobileKeys(java.util.Map)}, each
* environment gets an equal share of it. That is unlike {@link #capacity(int)}, which each environment
* has in full.
* Like {@link #capacity(int)}, each environment in
* {@link com.launchdarkly.sdk.android.LDConfig.Builder#secondaryMobileKeys(java.util.Map)} has it in full.
* <p>
* When an environment reaches its share, the SDK discards new events until it successfully delivers the stored ones, as
* When an environment reaches it, the SDK discards new events until it successfully delivers the stored ones, as
* it does when {@link #capacity(int)} is reached; it never deletes stored events to make room. A fifth
* of each share is kept for the summaries of flag evaluations, so that evaluations are still counted after
* of it is kept for the summaries of flag evaluations, so that evaluations are still counted after
* full events start being discarded.
* <p>
* Ordinary events reach {@link #capacity(int)} long before this. It binds where events are unusually
* large, or where delivery keeps failing and summaries, which {@link #capacity(int)} never refuses,
* accumulate. The default value is {@link #DEFAULT_CAPACITY_BYTES}; a value of zero or less means the
* default.
*
* @param capacityBytes the most bytes of events to keep, for all environments together
* @param capacityBytes the most bytes of events to keep, for each environment
* @return the builder
*/
public EventProcessorBuilder capacityBytes(long capacityBytes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1377,35 +1377,29 @@ public void aFlushFailsWhenAnAcceptedEventIsTooLargeForTheBytesLeft() throws Exc
}

@Test
public void theByteCapacityIsSharedEquallyByTheConfiguredEnvironments() throws Exception {
public void eachEnvironmentHasTheWholeByteCapacityHoweverManyAreConfigured() throws Exception {
assumePersisting();
StringBuilder data = new StringBuilder();
for (int i = 0; i < 700; i++) {
data.append('x');
}
Map<String, String> secondaryMobileKeys = new HashMap<>();
secondaryMobileKeys.put("second", "mob-second");
secondaryMobileKeys.put("third", "mob-third");
secondaryMobileKeys.put("fourth", "mob-fourth");
for (boolean shared : new boolean[]{false, true}) {
try (HttpServer server = startEventsServer()) {
EventProcessor eventProcessor = buildOfflineEventProcessor(server.getUri(),
eventsBuilder(DEFAULT_CAPACITY).capacityBytes(2_400), true, platformState(),
shared ? secondaryMobileKeys : null);
eventProcessor.setOffline(false);
try {
// About 850 bytes: within the full 2,400 bytes, and past a quarter of them.
eventProcessor.recordCustomEvent(CONTEXT, "large", LDValue.of(data.toString()), null);

assertEquals(!shared, awaitFlush(eventProcessor, 10, TimeUnit.SECONDS));
if (shared) {
assertEquals(0, server.getRecorder().count());
} else {
assertEquals(1, collectDelivered(server).size());
}
} finally {
eventProcessor.close();
}
for (int i = 2; i <= 7; i++) {
secondaryMobileKeys.put("env-" + i, "mob-" + i);
}
try (HttpServer server = startEventsServer()) {
EventProcessor eventProcessor = buildOfflineEventProcessor(server.getUri(),
eventsBuilder(DEFAULT_CAPACITY).capacityBytes(2_400), true, platformState(),
secondaryMobileKeys);
eventProcessor.setOffline(false);
try {
// About 850 bytes: within the full 2,400 bytes, and past a seventh of them.
eventProcessor.recordCustomEvent(CONTEXT, "large", LDValue.of(data.toString()), null);

assertTrue(awaitFlush(eventProcessor, 10, TimeUnit.SECONDS));
assertEquals(1, collectDelivered(server).size());
} finally {
eventProcessor.close();
}
}
}
Expand Down
Loading