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

[Java] fix concurrent modification exception for EQ cache #174

Closed
wants to merge 4 commits into from
Closed
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
55 changes: 35 additions & 20 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/EventQueue.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
package ly.count.sdk.java.internal;

import ly.count.sdk.java.Countly;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nonnull;

public class EventQueue {

static final String DELIMITER = ":::";
Log L;
List<EventImpl> eventQueueMemoryCache;

protected final Object lockEQ = new Object();

protected EventQueue() {
}

protected EventQueue(@Nonnull Log logger, int eventThreshold) {
protected EventQueue(@Nonnull Log logger) {
L = logger;
eventQueueMemoryCache = new ArrayList<>(eventThreshold);
eventQueueMemoryCache = new ArrayList<>();
}

/**
* Returns the number of events currently stored in the queue.
*/
protected int eqSize() {
return eventQueueMemoryCache.size();
synchronized (lockEQ) {
return eventQueueMemoryCache.size();
}
}

protected List<EventImpl> getEQ() {
synchronized (lockEQ) {
return new ArrayList<>(eventQueueMemoryCache);
}
}

void addEvent(@Nonnull final EventImpl event) {
Expand All @@ -33,8 +42,10 @@ void addEvent(@Nonnull final EventImpl event) {
return;
}
L.d("[EventQueue] Adding event: " + event.key);
eventQueueMemoryCache.add(event);
writeEventQueueToStorage();
synchronized (lockEQ) {
eventQueueMemoryCache.add(event);
writeEventQueueToStorage();
}
}

/**
Expand All @@ -56,20 +67,22 @@ void writeEventQueueToStorage() {
* Restores events from disk
*/
void restoreFromDisk() {
L.d("[EventQueue] Restoring events from disk");
eventQueueMemoryCache.clear();

final String[] array = getEvents();
for (String s : array) {

final EventImpl event = EventImpl.fromJSON(s, (ev) -> {
}, L);
if (event != null) {
eventQueueMemoryCache.add(event);
synchronized (lockEQ) {
L.d("[EventQueue] Restoring events from disk");
eventQueueMemoryCache.clear();

final String[] array = getEvents();
for (String s : array) {

final EventImpl event = EventImpl.fromJSON(s, (ev) -> {
}, L);
if (event != null) {
eventQueueMemoryCache.add(event);
}
}
// order the events from least to most recent
eventQueueMemoryCache.sort((e1, e2) -> (int) (e1.timestamp - e2.timestamp));
}
// order the events from least to most recent
eventQueueMemoryCache.sort((e1, e2) -> (int) (e1.timestamp - e2.timestamp));
}

@Nonnull String joinEvents(@Nonnull final Collection<EventImpl> collection) {
Expand All @@ -91,6 +104,8 @@ void restoreFromDisk() {

public void clear() {
SDKCore.instance.sdkStorage.storeEventQueue("");
eventQueueMemoryCache.clear();
synchronized (lockEQ) {
eventQueueMemoryCache.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ModuleEvents extends ModuleBase {
public void init(InternalConfig config) {
super.init(config);
L.d("[ModuleEvents] init: config = " + config);
eventQueue = new EventQueue(L, config.getEventsBufferSize());
eventQueue = new EventQueue(L);
eventQueue.restoreFromDisk();
eventsInterface = new Events();
}
Expand Down Expand Up @@ -69,7 +69,7 @@ public void stop(InternalConfig config, final boolean clear) {
private synchronized void addEventsToRequestQ(String deviceId) {
L.d("[ModuleEvents] addEventsToRequestQ");

if (eventQueue.eventQueueMemoryCache.isEmpty()) {
if (eventQueue.getEQ().isEmpty()) {
L.d("[ModuleEvents] addEventsToRequestQ, eventQueueMemoryCache is empty, skipping");
return;
}
Expand All @@ -78,7 +78,7 @@ private synchronized void addEventsToRequestQ(String deviceId) {
if (deviceId != null) {
request.params.add("device_id", deviceId);
}
request.params.arr("events").put(eventQueue.eventQueueMemoryCache).add();
request.params.arr("events").put(eventQueue.getEQ()).add();
request.own(ModuleEvents.class);

eventQueue.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class EventQueueTests {

private void init(Config cc) {
Countly.instance().init(cc);
eventQueue = new EventQueue(L, cc.getEventsBufferSize());
eventQueue = new EventQueue(L);
}

@After
Expand Down
Loading