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

Fix WebSocket threads CPU usage bug #89

Closed
wants to merge 1 commit into from

Conversation

afondard
Copy link

What does this PR do ?

This PR optimizes the WebSocket reconnection logic in the Kuzzle SDK to address high CPU usage during frequent reconnect attempts.

How should this be manually tested?

  • Step 1: Start the Kuzzle backend.
  • Step 2: Write a test Java application using the updated SDK. You can build the jar and then add the dependency in your configuration file like described below.

Maven: Add the updated SDK jar to your project’s root, then add the following dependency to your pom.xml:

        <!-- Kuzzle JVM SDK -->
        <dependency>
            <groupId>io.kuzzle</groupId>
            <artifactId>sdk-jvm</artifactId>
            <version>1.3.3</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/sdk-jvm-1.3.3.jar</systemPath>
        </dependency>

Gradle: Add the updated SDK jar to your project’s lib/ folder and update your build.gradle as follows:

        // Define repository for local jars
        repositories {
            flatDir {
                dirs 'libs'
            }
        }
        
        dependencies {
            implementation name: 'sdk-jvm-1.3.3'
        }

Here's an example of a test Java main:

package kuzzle.kuzzleio;

import io.kuzzle.sdk.Kuzzle;
import io.kuzzle.sdk.coreClasses.responses.Response;
import io.kuzzle.sdk.protocol.WebSocket;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        System.out.println("Hello world!");
        WebSocket ws = new WebSocket("localhost", 7512, false, true, 1000L);
        var kuzzle = new Kuzzle(ws);
        kuzzle.connect();
        System.out.println("Connected to Kuzzle server successfully!");

        Map<String, Object> query = new HashMap<>();
        query.put("controller", "document");
        query.put("action", "get");
        query.put("index", "tenant-asset_tracking-toto");
        query.put("collection", "devices");
        query.put("_id", "Tool-Tool1");
        Response res = kuzzle.query(query).get();
        System.out.println(res);

        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter your first name:");
        // Arrêter le back ici pour tester la queue
        String firstName = scanner.nextLine();
        System.out.println("Hello " + firstName);

        Response res2 = kuzzle.query(query).get();
        System.out.println(res2);
        // Relancer le back pour se reco et faire la requête
        System.out.println("Please enter your last name:");
        String lastName = scanner.nextLine();
        System.out.println("Last name: " + lastName);
        scanner.close();

        Response res3 = kuzzle.query(query).get();
        System.out.println(res3);
    }
}
  • Step 3 : Run the test Java application and simulate network interruptions while monitoring the CPU usage (top command for Linux).

@afondard afondard self-assigned this May 21, 2024
@afondard afondard closed this May 21, 2024
@afondard afondard deleted the KZLPRD-338-High-CPU-usage-bug branch May 21, 2024 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant