Skip to content

Commit

Permalink
#168 Remove synchronization on queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dheid committed Apr 19, 2024
1 parent 67c45e9 commit c4f8b32
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 59 deletions.
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,22 @@ Projects that use Matomo Java Tracker:

## What Is New?

### Version 3.4.x

We fixed a synchronization issue in the Java 8 sender (https://github.com/matomo-org/matomo-java-tracker/issues/168).
To consume the exact amount of space needed for the queries to send to Matomo, we need the collection size of the incoming
requests. So we changed `Iterable` to `Collection` in some `MatomoTracker`. This could affect users, that use parameters
of type `Iterable` in the tracker. Please use `Collection` instead.

### Version 3.3.x

Do you still use Matomo Java Tracker 2.x? We created version 3, that is compatible with Matomo 4 and 5 and contains
fewer
dependencies. Release notes can be found here: https://github.com/matomo-org/matomo-java-tracker/releases

Here are the most important changes:

* Matomo Java Tracker 3.3.0 is compatible with Matomo 4 and 5
* Matomo Java Tracker 3.4.0 is compatible with Matomo 4 and 5
* less dependencies
* new dimension parameter
* special types allow to provide valid parameters now
Expand Down Expand Up @@ -140,7 +149,7 @@ Add a dependency on Matomo Java Tracker using Maven. For Java 8:
<dependency>
<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
</dependency>
```

Expand All @@ -151,36 +160,36 @@ For Java 11:
<dependency>
<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker-java11</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
</dependency>
```

or Gradle (Java 8):

```groovy
dependencies {
implementation("org.piwik.java.tracking:matomo-java-tracker:3.3.0")
implementation("org.piwik.java.tracking:matomo-java-tracker:3.4.0")
}
```

or Gradle (Java 11):

```groovy
dependencies {
implementation("org.piwik.java.tracking:matomo-java-tracker-java11:3.3.0")
implementation("org.piwik.java.tracking:matomo-java-tracker-java11:3.4.0")
}
```

or Gradle with Kotlin DSL (Java 8)

```kotlin
implementation("org.piwik.java.tracking:matomo-java-tracker:3.3.0")
implementation("org.piwik.java.tracking:matomo-java-tracker:3.4.0")
```

or Gradle with Kotlin DSL (Java 11)

```kotlin
implementation("org.piwik.java.tracking:matomo-java-tracker-java11:3.3.0")
implementation("org.piwik.java.tracking:matomo-java-tracker-java11:3.4.0")
```

### Spring Boot Module
Expand All @@ -193,22 +202,22 @@ and allows you to configure the tracker via application properties. Add the foll
<dependency>
<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker-spring-boot-starter</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
</dependency>
```

or Gradle:

```groovy
dependencies {
implementation("org.piwik.java.tracking:matomo-java-tracker-spring-boot-starter:3.3.0")
implementation("org.piwik.java.tracking:matomo-java-tracker-spring-boot-starter:3.4.0")
}
```

or Gradle with Kotlin DSL

```kotlin
implementation("org.piwik.java.tracking:matomo-java-tracker-spring-boot-starter:3.3.0")
implementation("org.piwik.java.tracking:matomo-java-tracker-spring-boot-starter:3.4.0")
```

The following properties are supported:
Expand Down Expand Up @@ -678,7 +687,7 @@ version can be used in your local Maven repository for testing purposes, e.g.
<dependency>
<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker</artifactId>
<version>3.3.1-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker-parent</artifactId>
<version>3.3.2-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
25 changes: 13 additions & 12 deletions core/src/main/java/org/matomo/java/tracking/MatomoTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.URI;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -135,7 +136,7 @@ public MatomoTracker(
*
* <p>Use this method if you want to send a single request. If you want to send multiple requests at once, use
* {@link #sendBulkRequest(Iterable)} instead. If you want to send multiple requests asynchronously, use
* {@link #sendRequestAsync(MatomoRequest)} or {@link #sendBulkRequestAsync(Iterable)} instead.
* {@link #sendRequestAsync(MatomoRequest)} or {@link #sendBulkRequestAsync(Collection)} instead.
*
* @param request request to send. must not be null
*/
Expand All @@ -160,7 +161,7 @@ private void initializeSender() {
* Send a request asynchronously via HTTP GET.
*
* <p>Use this method if you want to send a single request. If you want to send multiple requests at once, use
* {@link #sendBulkRequestAsync(Iterable)} instead. If you want to send multiple requests synchronously, use
* {@link #sendBulkRequestAsync(Collection)} instead. If you want to send multiple requests synchronously, use
* {@link #sendRequest(MatomoRequest)} or {@link #sendBulkRequest(Iterable)} instead.
*
* @param request request to send
Expand All @@ -176,8 +177,8 @@ public CompletableFuture<MatomoRequest> sendRequestAsync(
* Send a request asynchronously via HTTP GET and specify a callback that gets executed when the response arrives.
*
* <p>Use this method if you want to send a single request. If you want to send multiple requests at once, use
* {@link #sendBulkRequestAsync(Iterable, Consumer)} instead. If you want to send multiple requests synchronously, use
* {@link #sendRequest(MatomoRequest)} or {@link #sendBulkRequest(Iterable)} instead.
* {@link #sendBulkRequestAsync(Collection, Consumer)} instead. If you want to send multiple requests synchronously,
* use {@link #sendRequest(MatomoRequest)} or {@link #sendBulkRequest(Iterable)} instead.
*
* @param request request to send
* @param callback callback that gets executed when response arrives, must not be null
Expand Down Expand Up @@ -223,7 +224,7 @@ private void applyGoalIdAndCheckSiteId(
*
* <p>More efficient than sending several individual requests. If you want to send a single request, use
* {@link #sendRequest(MatomoRequest)} instead. If you want to send multiple requests asynchronously, use
* {@link #sendBulkRequestAsync(Iterable)} instead.
* {@link #sendBulkRequestAsync(Collection)} instead.
*
* @param requests the requests to send
*/
Expand All @@ -236,7 +237,7 @@ public void sendBulkRequest(MatomoRequest... requests) {
*
* <p>More efficient than sending several individual requests. If you want to send a single request, use
* {@link #sendRequest(MatomoRequest)} instead. If you want to send multiple requests asynchronously, use
* {@link #sendBulkRequestAsync(Iterable)} instead.
* {@link #sendBulkRequestAsync(Collection)} instead.
*
* @param requests the requests to send
*/
Expand All @@ -250,7 +251,7 @@ public void sendBulkRequest(@NonNull Iterable<? extends MatomoRequest> requests)
*
* <p>Specify the AuthToken if parameters that require an auth token is used. If you want to send a single request,
* use {@link #sendRequest(MatomoRequest)} instead. If you want to send multiple requests asynchronously, use
* {@link #sendBulkRequestAsync(Iterable)} instead.
* {@link #sendBulkRequestAsync(Collection)} instead.
*
* @param requests the requests to send
* @param authToken specify if any of the parameters use require AuthToken, if null the default auth token from the
Expand Down Expand Up @@ -293,7 +294,7 @@ public CompletableFuture<Void> sendBulkRequestAsync(MatomoRequest... requests) {
* @return completable future to let you know when the request is done
*/
public CompletableFuture<Void> sendBulkRequestAsync(
@NonNull Iterable<? extends MatomoRequest> requests
@NonNull Collection<? extends MatomoRequest> requests
) {
return sendBulkRequestAsync(requests, null, null);
}
Expand All @@ -313,7 +314,7 @@ public CompletableFuture<Void> sendBulkRequestAsync(
*/
@Deprecated
public CompletableFuture<Void> sendBulkRequestAsync(
@NonNull Iterable<? extends MatomoRequest> requests,
@NonNull Collection<? extends MatomoRequest> requests,
@Nullable String authToken,
@Nullable Consumer<Void> callback
) {
Expand Down Expand Up @@ -342,7 +343,7 @@ public CompletableFuture<Void> sendBulkRequestAsync(
* @return completable future to let you know when the request is done
*/
public CompletableFuture<Void> sendBulkRequestAsync(
@NonNull Iterable<? extends MatomoRequest> requests,
@NonNull Collection<? extends MatomoRequest> requests,
@Nullable Consumer<Void> callback
) {
return sendBulkRequestAsync(requests, null, callback);
Expand All @@ -357,10 +358,10 @@ public CompletableFuture<Void> sendBulkRequestAsync(
* @param authToken specify if any of the parameters use require AuthToken, null allowed
* @return completable future to let you know when the request is done
* @deprecated Please set the auth token in the tracker configuration or the requests directly and use
* {@link #sendBulkRequestAsync(Iterable)} instead.
* {@link #sendBulkRequestAsync(Collection)} instead.
*/
public CompletableFuture<Void> sendBulkRequestAsync(
@NonNull Iterable<? extends MatomoRequest> requests, @Nullable String authToken
@NonNull Collection<? extends MatomoRequest> requests, @Nullable String authToken
) {
return sendBulkRequestAsync(requests, authToken, null);
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/matomo/java/tracking/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;

interface Sender extends AutoCloseable {
Expand All @@ -20,6 +21,6 @@ void sendBulk(

@NonNull
CompletableFuture<Void> sendBulkAsync(
@NonNull Iterable<? extends MatomoRequest> requests, @Nullable String overrideAuthToken
@NonNull Collection<? extends MatomoRequest> requests, @Nullable String overrideAuthToken
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void sendBulk(
@NonNull
@Override
public CompletableFuture<Void> sendBulkAsync(
@NonNull Iterable<? extends MatomoRequest> requests, @Nullable String overrideAuthToken
@NonNull Collection<? extends MatomoRequest> requests, @Nullable String overrideAuthToken
) {
for (MatomoRequest request : requests) {
createQueryAndAddRequest(request, overrideAuthToken);
Expand Down
4 changes: 2 additions & 2 deletions java11/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker-parent</artifactId>
<version>3.3.2-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>matomo-java-tracker-java11</artifactId>
<version>3.3.2-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Matomo Java Tracker Java 11</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private HttpRequest buildHttpPostRequest(
@NonNull
@Override
public CompletableFuture<Void> sendBulkAsync(
@NonNull @lombok.NonNull Iterable<? extends MatomoRequest> requests,
@NonNull @lombok.NonNull Collection<? extends MatomoRequest> requests,
@Nullable String overrideAuthToken
) {
return sendAsyncAndCheckResponse(buildHttpPostRequest(requests, overrideAuthToken), null);
Expand Down
4 changes: 2 additions & 2 deletions java8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker-parent</artifactId>
<version>3.3.2-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>matomo-java-tracker</artifactId>
<version>3.3.2-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Matomo Java Tracker Java 8</name>
Expand Down
36 changes: 17 additions & 19 deletions java8/src/main/java/org/matomo/java/tracking/Java8Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -60,7 +61,6 @@ class Java8Sender implements Sender {
private final QueryCreator queryCreator;

private final ExecutorService executorService;
private final Collection<String> queries = new ArrayList<>(16);

@Override
@NonNull
Expand Down Expand Up @@ -322,39 +322,37 @@ private static void preparePostConnection(HttpURLConnection connection) {
@Override
@NonNull
public CompletableFuture<Void> sendBulkAsync(
@NonNull Iterable<? extends MatomoRequest> requests, @Nullable String overrideAuthToken
@NonNull Collection<? extends MatomoRequest> requests, @Nullable String overrideAuthToken
) {
String authToken = AuthToken.determineAuthToken(overrideAuthToken, requests, trackerConfiguration);
Map<String, String> headers = new LinkedHashMap<>();
String headerUserAgent = findHeaderUserAgent(requests);
String sessionId = findSessionId(requests);
Map<String, String> cookies = findCookies(requests);
synchronized (queries) {
for (MatomoRequest request : requests) {
RequestValidator.validate(request, authToken);
if (request.getHeaders() != null && !request.getHeaders().isEmpty()) {
headers.putAll(request.getHeaders());
}
String query = queryCreator.createQuery(request, null);
queries.add(query);
List<String> queries = new ArrayList<>(requests.size());
for (MatomoRequest request : requests) {
RequestValidator.validate(request, authToken);
if (request.getHeaders() != null && !request.getHeaders().isEmpty()) {
headers.putAll(request.getHeaders());
}
queries.add(queryCreator.createQuery(request, null));
}
return CompletableFuture.supplyAsync(() ->
sendBulkAsync(authToken, headers, headerUserAgent, sessionId, cookies), executorService);
sendBulkAsync(queries, authToken, headers, headerUserAgent, sessionId, cookies),
executorService);
}

@Nullable
private Void sendBulkAsync(
@Nullable String authToken, Map<String, String> headers, String headerUserAgent, String sessionId,
List<String> queries,
@Nullable String authToken,
Map<String, String> headers,
String headerUserAgent,
String sessionId,
Map<String, String> cookies
) {
synchronized (queries) {
if (!queries.isEmpty()) {
sendBulk(queries, authToken, headers, headerUserAgent, sessionId, cookies);
queries.clear();
}
return null;
}
sendBulk(queries, authToken, headers, headerUserAgent, sessionId, cookies);
return null;
}

@Nullable
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker-parent</artifactId>
<version>3.3.2-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Matomo Java Tracker Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions servlet-jakarta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>org.piwik.java.tracking</groupId>
<artifactId>matomo-java-tracker-parent</artifactId>
<version>3.3.2-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>matomo-java-tracker-servlet-jakarta</artifactId>
<version>3.3.2-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Matomo Java Tracker Servlet Jakarta</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void sendBulk(
@NonNull
@Override
public CompletableFuture<Void> sendBulkAsync(
@NonNull Iterable<? extends MatomoRequest> requests, @Nullable String overrideAuthToken
@NonNull Collection<? extends MatomoRequest> requests, @Nullable String overrideAuthToken
) {
throw new UnsupportedOperationException();
}
Expand Down
Loading

0 comments on commit c4f8b32

Please sign in to comment.