3

There is a client that needs to serialize many lambda functions defined in the client side and send them to the server, which has to be able to correctly deserialize them.

The server also have implementations of all those lambda functions in the client, but the details could be slightly different from the client-side implementations (like minor versioning differences).

Given that Java Lambdas depends on compiler specific artifacts, simply serializing a lambda function to SerializedLambda and send it to the server side wouldn't work since the minor difference in the server side implementation could make itself incompatible to the serialized lambda expression.

My question here is, with a serialized lambda from the client, is there any way to invoke the corresponding lambda function on the server ? I have full controls over the serialization/deserialization in this case, but I do not have controls over the implementations of the lambda functions in both client/server. There can be thousands or tends of thousands of these lambda functions, which makes it difficult for me to define rules for each lambda function manually.

So, given (enclosing) class name, method types (arg types + return type), lambda function name, is it possible to invoke the corresponding implementation of lambda on the server side?


What can I do if lambdas are not from the fixed set? Is it impossible to have a single ser/deser rule to resolve this issue ?

newbie
  • 65
  • 5
  • 4
    If your lambdas don't hold state and are from a fixed set, then maybe you can implement them as an enum and send the enum identifier instead. Properly serializing *real* lambdas between different JVMs is probably not going to fly. – Joachim Sauer Apr 27 '20 at 15:49
  • Unfortunately there are two many lambda functions for me to define enum for them. If we have class name, method types (arg types + return type), lambda function name, isn't it possible to invoke it on the server side ? – newbie Apr 27 '20 at 15:53
  • 1
    If you have “class name, method types (arg types + return type), lambda function name” you have even more than an enum constant requires, as the enum constant only consists of “class name and constant name”. So why do you think you could make the former maintainable but not the latter? – Holger Apr 27 '20 at 15:56
  • ah so I didn't mean to individually define rules for each of those combination. What I meant to do was, to have custom deserialization rule so that it extracts "class name, method types (arg types + return type), lambda function name" from the serialized lambda, and use that to find a corresponding impl. – newbie Apr 27 '20 at 16:03
  • maybe I misunderstood something here. Is it possible to do this enum thing without manually defining them? as the # of lambda functions is huge (and it could be keep growing), I need to avoid manually defining them as much as possible. – newbie Apr 27 '20 at 16:05
  • 1
    Lambda expressions are also manually defined things. It’s still not clear, how they are supposed to match between two different implementations when you are not giving them formal identifiers. When you insert a new lambda expression into the code, you are not only facing potentially renumbered methods for the other lambda expressions, you are also facing the absence of this newly inserted lambda expression on the other side. – Holger Apr 27 '20 at 16:46
  • right, so resolving this "renumbered" thing is a part of challenge that I was hoping to solve (or somehow get around it). For "Lambda expressions are also manually defined thing", yes but they are already defined (by many developers). – newbie Apr 27 '20 at 16:49
  • there wouldn't be case of "the absence of this newly inserted lambda expression" for me because the server side version >= client side version at any point (they share the same code base). – newbie Apr 27 '20 at 16:50
  • 1
    Why don't you create a serializable *representation* of the lambda funcion (i.e. a `LambdaFunction` serializable class) and use this representation to create a lambda expression whenever you need it? – fps Apr 27 '20 at 18:39
  • @FedericoPeraltaSchaffner sure, but how would you correctly specify the lambda function name on the server side? because it contains random number which can be different for client & server. – newbie Apr 27 '20 at 20:58
  • 1
    Lambda's are from an external perspective, unnamed. The compiler may assign names, but these probably oughtn't to be relied upon. Have you considered building tables of lambdas, assigning an ID and a version to each table, then using the triple of `(tableId, tableVersion, lambdaOffset)` as what is transported? That supplies the otherwise missing unique ID for the lambdas. The client and servers must work from the same lambda table source, with room for different table versions. And reflection would probably be needed to apply a transported lambda. – Thomas Bitonti Apr 27 '20 at 21:09
  • 1
    Even when the server is newer than the client, this doesn’t imply that it will contain all the lambda expressions of the older version in its code. That would be even less maintainable, a piece of code containing unused lambda expressions, only retained for backward compatibility. For anonymous functions… – Holger Apr 28 '20 at 10:24
  • yes, that's a great point. I will probably go with enum identifiers as suggested. Thank you. – newbie Apr 29 '20 at 03:11

0 Answers0