You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Vladimir Sitnikov <si...@gmail.com> on 2019/02/14 20:15:12 UTC

CalciteRemoteDriverTest vs avatica.MetaImpl vs avatica.server.Main thread safety

Hi,

CalciteRemoteDriverTest seems to be very unstable at
https://builds.apache.org/job/Calcite-Master/

I guess the problem there is the test is using a single CalciteMetaImpl
object to handle all the requests.
Apparently CalciteMetaImpl is using no synchronization and it uses simple
HashMaps (Meta is associated with exactly one Connection which is not
thread-safe).

I wonder:
1) Is org.apache.calcite.avatica.Meta supposed to be thread-safe? Javadoc
does not clarify that. It looks
like org.apache.calcite.avatica.jdbc.JdbcMeta is thread-safe.
2) In case Meta is supposed to be thread-safe,
why org.apache.calcite.avatica.MetaImpl has Connection constructor? Does
Avatica suppose that all connection implementations are thread-safe?
3) In case Meta is supposed to be thread-safe, how MetaImpl#connProps is
supposed to work? Apparently different connections might have different
properties (e.g. autocommit settings), so how a single connProps would fit
all?
4) In case Meta is not supposed to be thread-safe,
why org.apache.calcite.avatica.server.Main#start uses just a single Meta
object to handle all the requests?
5) Does anybody use CalciteRemoteServer at all?

I don't have Calcite-remote use-cases yet, however the current state of
builds.apache.org is useless as it fails extremely often.

Vladimir

Re: CalciteRemoteDriverTest vs avatica.MetaImpl vs avatica.server.Main thread safety

Posted by Julian Hyde <jh...@apache.org>.
Simplest thing would be a singleton guarded by some kind of lock. But I haven’t looked at the code.

> On Feb 15, 2019, at 12:59 PM, Vladimir Sitnikov <si...@gmail.com> wrote:
> 
> Julian> allocate a new object per request
> 
> Josh> The Meta implementation inside of the Avatica server is a singleton,
> 
> Julian, I don't think singletones can be allocated per request.
> I don't think final fields of singletones can be changed on a per-request
> basis.
> 
> What's your suggestion then?
> 
> I've created https://issues.apache.org/jira/browse/CALCITE-2853
> It looks like the issue was there for ages, so it does not look like a
> release blocker, however the bug does break CI, and I'm tired of getting
> "build's broken" mails from Apache Jenknis, especially after I commit an
> innocent fix.
> 
> Vladimir


Re: CalciteRemoteDriverTest vs avatica.MetaImpl vs avatica.server.Main thread safety

Posted by Vladimir Sitnikov <si...@gmail.com>.
Julian> allocate a new object per request

Josh> The Meta implementation inside of the Avatica server is a singleton,

Julian, I don't think singletones can be allocated per request.
I don't think final fields of singletones can be changed on a per-request
basis.

What's your suggestion then?

I've created https://issues.apache.org/jira/browse/CALCITE-2853
It looks like the issue was there for ages, so it does not look like a
release blocker, however the bug does break CI, and I'm tired of getting
"build's broken" mails from Apache Jenknis, especially after I commit an
innocent fix.

Vladimir

Re: CalciteRemoteDriverTest vs avatica.MetaImpl vs avatica.server.Main thread safety

Posted by Julian Hyde <jh...@apache.org>.
Common strategies for implementing servers using non-thread-safe objects: use one object and ensure only one thread uses it at a time (the actor model), allocate a new object per request, or (if creating an object is expensive) pool objects and re-use them for later requests.

> On Feb 15, 2019, at 12:47 PM, Vladimir Sitnikov <si...@gmail.com> wrote:
> 
> Julian> Indeed, JDBC itself is not thread-safe
> 
> Just in case: CalciteRemoteDriverTest seems to be pretty sane test (it
> does not use the same java.sql.Connection across multiple threads),
> yet it fails with race-condition-like reasons.
> 
> I won't really want to dig into JDBC thread-safety, however that is a
> slippery slope. I used to think that JDBC is not thread-safe, however
> now I don't think so (after being pgjdbc maintainer for a several
> years).
> For instance, there's Connection#getWarnings, and clients expect that
> to work from a separate thread while statement is still running (and
> producing warnings).
> There's #close which apparently should be thread-safe (e.g. to play
> well with automatic flnalize-like cleanup).
> Threre's Connection#abort, and so on.
> 
> Julian> So, the solution is not necessarily to add locks and/or
> synchronized all over MetaImpl.
> 
> Apparently the solution is to replace "connection" and "connProps"
> fields of MetaImpl with Map<ConnectionId, ...> kind of field.
> It is really hard to imagine a HTTP service that produces correct
> results only in case it is accessed from within a single connection
> only.
> 
> For instance, connectionSync is a separate wire command, so currently
> it just overwrites MetaImpl field, and it is pretty close to plug&pray
> kind of technology.
> I wonder how it managed to survive for such a long go.
> 
> 
> I'm not sure how much sense MetaImpl(AvaticaConnection connection)
> constructor parameter makes.
> I think connection creation should happen in
> org.apache.calcite.avatica.MetaImpl#openConnection
> 
> Of course, MetaImpl might want to have a connection instance for calls
> like catalogs(), then it could just call openConnection on its own and
> that's it.
> 
> Am I missing something?
> 
> Vladimir


Re: CalciteRemoteDriverTest vs avatica.MetaImpl vs avatica.server.Main thread safety

Posted by Vladimir Sitnikov <si...@gmail.com>.
Julian> Indeed, JDBC itself is not thread-safe

Just in case: CalciteRemoteDriverTest seems to be pretty sane test (it
does not use the same java.sql.Connection across multiple threads),
yet it fails with race-condition-like reasons.

I won't really want to dig into JDBC thread-safety, however that is a
slippery slope. I used to think that JDBC is not thread-safe, however
now I don't think so (after being pgjdbc maintainer for a several
years).
For instance, there's Connection#getWarnings, and clients expect that
to work from a separate thread while statement is still running (and
producing warnings).
There's #close which apparently should be thread-safe (e.g. to play
well with automatic flnalize-like cleanup).
Threre's Connection#abort, and so on.

Julian> So, the solution is not necessarily to add locks and/or
synchronized all over MetaImpl.

Apparently the solution is to replace "connection" and "connProps"
fields of MetaImpl with Map<ConnectionId, ...> kind of field.
It is really hard to imagine a HTTP service that produces correct
results only in case it is accessed from within a single connection
only.

For instance, connectionSync is a separate wire command, so currently
it just overwrites MetaImpl field, and it is pretty close to plug&pray
kind of technology.
I wonder how it managed to survive for such a long go.


I'm not sure how much sense MetaImpl(AvaticaConnection connection)
constructor parameter makes.
I think connection creation should happen in
org.apache.calcite.avatica.MetaImpl#openConnection

Of course, MetaImpl might want to have a connection instance for calls
like catalogs(), then it could just call openConnection on its own and
that's it.

Am I missing something?

Vladimir

Re: CalciteRemoteDriverTest vs avatica.MetaImpl vs avatica.server.Main thread safety

Posted by Julian Hyde <jh...@apache.org>.
+1 what Josh said.

Also: non-thread-safe is not necessarily a bad thing.  As long as the caller knows the limitation. Code that is written only to work single-threaded is more efficient and has fewer bugs.

Indeed, JDBC itself is not thread-safe. It is not safe for two threads to access the same Connection, Statement or ResultSet at the same time. (With a special exception for the Statement.cancel() method.)

So, the solution is not necessarily to add locks and/or synchronized all over MetaImpl.

Julian


> On Feb 14, 2019, at 2:46 PM, Josh Elser <el...@apache.org> wrote:
> 
> The Meta implementation inside of the Avatica server is a singleton, so yes the implementation must be threadsafe as the server will dispatch multiple user request threads to it. The Meta implementation is largely (by virtue of the wire API) stateless. I think the expectation should be that all Meta implementations are threadsafe, but this probably isn't something anything has really looked long and hard at.
> 
> On 2/14/19 3:15 PM, Vladimir Sitnikov wrote:
>> Hi,
>> CalciteRemoteDriverTest seems to be very unstable at
>> https://builds.apache.org/job/Calcite-Master/
>> I guess the problem there is the test is using a single CalciteMetaImpl
>> object to handle all the requests.
>> Apparently CalciteMetaImpl is using no synchronization and it uses simple
>> HashMaps (Meta is associated with exactly one Connection which is not
>> thread-safe).
>> I wonder:
>> 1) Is org.apache.calcite.avatica.Meta supposed to be thread-safe? Javadoc
>> does not clarify that. It looks
>> like org.apache.calcite.avatica.jdbc.JdbcMeta is thread-safe.
>> 2) In case Meta is supposed to be thread-safe,
>> why org.apache.calcite.avatica.MetaImpl has Connection constructor? Does
>> Avatica suppose that all connection implementations are thread-safe?
>> 3) In case Meta is supposed to be thread-safe, how MetaImpl#connProps is
>> supposed to work? Apparently different connections might have different
>> properties (e.g. autocommit settings), so how a single connProps would fit
>> all?
>> 4) In case Meta is not supposed to be thread-safe,
>> why org.apache.calcite.avatica.server.Main#start uses just a single Meta
>> object to handle all the requests?
>> 5) Does anybody use CalciteRemoteServer at all?
>> I don't have Calcite-remote use-cases yet, however the current state of
>> builds.apache.org is useless as it fails extremely often.
>> Vladimir


Re: CalciteRemoteDriverTest vs avatica.MetaImpl vs avatica.server.Main thread safety

Posted by Josh Elser <el...@apache.org>.
The Meta implementation inside of the Avatica server is a singleton, so 
yes the implementation must be threadsafe as the server will dispatch 
multiple user request threads to it. The Meta implementation is largely 
(by virtue of the wire API) stateless. I think the expectation should be 
that all Meta implementations are threadsafe, but this probably isn't 
something anything has really looked long and hard at.

On 2/14/19 3:15 PM, Vladimir Sitnikov wrote:
> Hi,
> 
> CalciteRemoteDriverTest seems to be very unstable at
> https://builds.apache.org/job/Calcite-Master/
> 
> I guess the problem there is the test is using a single CalciteMetaImpl
> object to handle all the requests.
> Apparently CalciteMetaImpl is using no synchronization and it uses simple
> HashMaps (Meta is associated with exactly one Connection which is not
> thread-safe).
> 
> I wonder:
> 1) Is org.apache.calcite.avatica.Meta supposed to be thread-safe? Javadoc
> does not clarify that. It looks
> like org.apache.calcite.avatica.jdbc.JdbcMeta is thread-safe.
> 2) In case Meta is supposed to be thread-safe,
> why org.apache.calcite.avatica.MetaImpl has Connection constructor? Does
> Avatica suppose that all connection implementations are thread-safe?
> 3) In case Meta is supposed to be thread-safe, how MetaImpl#connProps is
> supposed to work? Apparently different connections might have different
> properties (e.g. autocommit settings), so how a single connProps would fit
> all?
> 4) In case Meta is not supposed to be thread-safe,
> why org.apache.calcite.avatica.server.Main#start uses just a single Meta
> object to handle all the requests?
> 5) Does anybody use CalciteRemoteServer at all?
> 
> I don't have Calcite-remote use-cases yet, however the current state of
> builds.apache.org is useless as it fails extremely often.
> 
> Vladimir
>