Giter VIP home page Giter VIP logo

Comments (2)

eduard-vasinskyi avatar eduard-vasinskyi commented on June 24, 2024 1

Hi, @PanagiotisDrakatos

It is indeed possible to launch a multithreaded RPC server. Here is a demo launcher that does just that.

However, looking at your project, I can see that the issue is that your code is blocking the eventloop thread.

The code in RpcRequestHandler is executed inside the eventloop thread. So any blocking operations will make eventloop unavailable until that operation is completed. This is why it appears that requests are processed sequentially. You should avoid blocking code, such as I/O or Thread.sleep(), inside the eventloop.

For example, letโ€™s rewrite the RpcErasureServer#downloadErasureChunks method. It has blocking code inside the while loop (Thread.sleep()). The same logic could be achieved by using Promises. For example, the Promises#loop method, which can be used to write asynchronous loops. Your code would look like this:

private RpcRequestHandler<ErasureRequest, ErasureResponse> downloadErasureChunks(IChunksService<T> service) {
   return request -> Promises.loop(0,
               rc -> rc < ConsensusConfiguration.CYCLES &&
                    (serializable_length == 0 ||
                     CachedSerializableErasureObject.getInstance().getSerializableErasureObject() == null),
               rc -> Promises.delay(ConsensusConfiguration.HEARTBEAT_INTERVAL, rc + 1)
         )
         .map(rc -> {
            if (rc == ConsensusConfiguration.CYCLES)
               return new ErasureResponse(null);
            T result = service.downloadErasureChunks();
            if (result == null)
               return new ErasureResponse(null);
            return new ErasureResponse(this.valueMapper.encode(result, serializable_length));
         });
}

Look at the Promises#loop call in place of the while cycle. Promises#loop takes an initial seed value 0 (rc=0). It also takes a loop break condition as its second argument. Finally, we pass a function that converts the current rc value to the next rc value (rc + 1), but it also returns the next value after a delay of HEARTBEAT_INTERVAL milliseconds. A delay does not cause a thread to block (like with Thread.sleep()), but rather schedules a task to the eventloop. So, other requests/tasks can be executed while the eventloop is waiting for the delay to pass.

Notice that we call service.downloadErasureChunks() inside the eventloop. I looked at this method and did not find blocking operations, so itโ€™s fine to call it from the eventloop thread. However, if downloadErasure() would access DB, file system, or perform any other I/O operations, you would need to execute it using an external Executor. I noticed there is some blocking code in RPC handlers located in the RpcAdrestusServer class. You might consider rewriting them, and wrapping blocking code using Promise#ofBlocking methods.

You can look at this example for reference. Here we wrap blocking calls to JDBC API using Promise#ofBlocking with an external Executor.

from activej.

PanagiotisDrakatos avatar PanagiotisDrakatos commented on June 24, 2024

@eduard-vasinskyi I have no words to say excellent as always you gave solutions to the point. Thank you very much. I appreciate it. God bless you

from activej.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.