orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Is it possible to use cockroachdb as grain storage?

Open shubhanjalik opened this issue 7 years ago • 6 comments

We are trying to evaluate if we could use Cockroachdb (https://www.cockroachlabs.com/) as persistence storage for grainstate. Followed the link https://dotnet.github.io/orleans/Documentation/grains/grain_persistence/index.html#configuring-igrainstorage-providers to set up the storage provider for Cockroach as postgresql. As per the instruction, when I tried to execute the postgresql statements from https://github.com/dotnet/orleans/blob/master/src/AdoNet/Orleans.Persistence.AdoNet/PostgreSQL-Persistence.sql, I am getting an error while creating Function. It appears that cockroachdb does not support creating custom Functions. Are there any workarounds? Is using cockroachdb as grain persistence feasible? Has anybody tried doing this?

shubhanjalik avatar Jul 08 '19 09:07 shubhanjalik

@shubhanjalik You can take any of the scripts and modify them as needed. It might make sense to modify the PostgreSQL scripts so that they'll be compatible both with PostgreSQL and CockroachDb is possible (haven't used CocrochDb, don't know). Orleans needs the name and type of the parameters (also return values), otherwise the contents of the queries and database structures can be whatever if they just match each other.

veikkoeeva avatar Jul 11 '19 06:07 veikkoeeva

@shubhanjalik Alternatively, you could write a direct CockroachDb provider.

sergeybykov avatar Jul 11 '19 17:07 sergeybykov

@sergeybykov That is one option, though I'm not sure if it'd be easier. :)

The problem here is that CockroachDb isn't fully compatible with PostgreSQL and in this particular case CREATE FUNCTION isn't a supported feature. There are options to that, as noted at https://dba.stackexchange.com/questions/75142/postgresql-equivalent-of-mysql-query-variables and it seems like the PostgreSQL script could use a more standard SQL notation. SQL Server for instance does not use stored procedures (it could, compiled native procedures could be useful in some cases).

For the curious about the the compatibility, https://www.cockroachlabs.com/blog/why-postgres/ is interesting.

veikkoeeva avatar Jul 13 '19 09:07 veikkoeeva

I tried to make it work with CockroachDB, more or less as an exercice. As @veikkoeeva said, the main problem is that it doesn't support any kind of procedural language, so it is really hard to write a single query that insert or update the state while ensuring that the version is the same, etc.

I'm pretty sure that it is theoretically possible by doing a very ugly workaround that requires an unique index using all the grain identities columns, and by using the on conflict clause (also exists in Postgres).

Something as ugly as

insert into storage (
   select that returns the state only if it is new and no records exist with the same identity
   union all
   select that returns the state only if it is not new and a record with the same identity exists and the version matches
)
on conflict do update existing record
returning version

If it works it is very cool! but it is clearly not a solution 🤣

And again, as said by @veikkoeeva it is not an easy task to write a specialized provider from scratch.

So, what do you think about refactoring the existing ADO provider to make it extensible? Maybe something quite simple that provides 3 virtual methods like

protected virtual Task<State> WriteState(State state, DbConnection connection);
protected virtual Task<State> ReadState(StateIdentity stateId, DbConnection connection);
protected virtual Task ClearState(State state, DbConnection connection);

where State already contains all the formatted/serialized values that must be inserted and StateIdentity contains all the values used to fetch the state

By providing these methods, it will be quite easy to customize the queries while keeping all the infrastructure of the existing provider.

srollinet avatar Nov 20 '19 21:11 srollinet

@srollinet Just got back, a quick note takes 1-2 days to get back to life. :)

veikkoeeva avatar Nov 24 '19 16:11 veikkoeeva

@srollinet Maybe writing from scratch isn't that difficult, but maybe can be easier. I think the critical pieces are at https://github.com/dotnet/orleans/blob/master/src/AdoNet/Orleans.Persistence.AdoNet/Storage/Provider/AdoNetGrainStorage.cs#L172. In this case it may be a good choice also to special case CockroachDb on these places either by just adding a conditional or refactoring those pieces out to, say, a lambda or to local function for clarity.

For tests, you need to implement https://github.com/dotnet/orleans/search?q=StorageDataSetPlain&unscoped_q=StorageDataSetPlain like there (I used the test data set only as a search word). You need to add the connection string to https://github.com/dotnet/orleans/blob/dad62cc16d4d6de671563f6b1e54196f617926a5/test/Extensions/TesterAdoNet/StorageTests/Relational/TestEnvironmentInvariant.cs#L17 too.

More context: I provide this context since one point or another I hopefully get to work on this a bit more and see how to improve systematically.

As you can see, the parameter names, types and values are important in AdoNetGrainStorage. If streaming is enabled, also the order. The rest of the code just for deciding which (de)serializer to use on which parameters with the purpose of having an ability to change it while the system is running, do transformation to the data if needed (e.g. have classes renamed and still reading them from storage) and removing version strings etc. from classes. If you want to maintain these abilities, it makes sense to make the changes here. You need to detect the special case for CockroachDb.

Some of this is experimental from 1.n era before there was dependency injection. There are a few other tickets with the idea to refactor this to make use of DI and surface some knobs to the configuration surface (while also refactoring a bit), one being allowing one to pick which (de)serializer to use and which placement (Tag, XML/JSON/blob) to use in storage. So that's why there's more code.

Going further with dependency injection and public configuration surface and refactoring that class, removing dependency to Newtonsoft library now that it is possible, which means getting the (de)serializers injected, more tracing, maybe more logging (injected) etc. A more systemic improvement would be to enable streaming all the way from grains especially now in .NET Core 3.0 era as it would help with larger blobs. That sort of things. :)

The test setup is what you see and likely works for your needs. I would like to refactor it to be more like at https://github.com/dotnet/orleans/blob/master/Samples/OneBoxDeployment/test/OneBoxDeployment.IntegrationTests/IntegrationTestFixture.cs. I.e. use the XUnit standard facilities to set up tests asynchronously, do DI, read settings and then tear them down (in fact would like to do that for all of the tests and work on making all the backend storage to run the same tests, but it'd require more work and going to "test toolkit for plugins" idea that's been rolling here for some years :)). You see there are a separate class for AdoNet providers. That's mainly to tackle easily missing DBs and have clear names for them in tests, there may be a handier way to do that these days.

veikkoeeva avatar Nov 27 '19 06:11 veikkoeeva