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

Cosmos DB: Migration to V3 SDK #717

Merged
merged 1 commit into from
Jul 1, 2021
Merged

Cosmos DB: Migration to V3 SDK #717

merged 1 commit into from
Jul 1, 2021

Conversation

ealsur
Copy link
Member

@ealsur ealsur commented Mar 30, 2021

This PR migrates the current implementation to V3 SDK and bumps the version to 4.0.0.

Closes #610

@pragnagopa
Copy link
Member

Tagging @divyagandhisethi - for reference - for PR: kedacore/external-scaler-azure-cosmos-db#1

Copy link
Member

@brettsam brettsam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial comments...

if (item is string)
{
convertedItem = JObject.Parse(item.ToString());
JObject asJObject = JObject.Parse(item.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use System.Text.Json here? Would that allow you to remove a ref to Newtonsoft?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind... I forgot we require Newtonsoft.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SDK could take System.Text.Json using a custom serializer if that is the approach we want to take.

document = await _context.Service.GetContainer(_context.ResolvedAttribute.DatabaseName, _context.ResolvedAttribute.CollectionName)
.ReadItemAsync<T>(_context.ResolvedAttribute.Id, partitionKey);

_originalItem = JObject.FromObject(document);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we want to continue supporting this "update-in-place" behavior. It's never worked for out-of-proc languages and has resulted in a decent amount of confusion as it seems all of the bindings handle it differently.

@fabiocav -- thoughts?

/// <summary>
/// Gets or sets the lease options for the DocumentDB Trigger.
/// </summary>
public ChangeFeedHostOptions LeaseOptions { get; set; } = new ChangeFeedHostOptions();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these not settable now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were already there in the Trigger properties. Normally these are for a very advanced customization of the trigger, and having it as a general configuration (for all the Triggers in the project) wouldn't have sense.

/// </summary>
[AppSetting]
[ConnectionString]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably know this... but what's the difference between this and [AppSetting]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't know, but the CosmosDBAttribute file had it as ConnectionString so thought it should be consistent?


Type documentType = CosmosDBTriggerAttributeBindingProviderGenerator.GetParameterType(context.Parameter);

if (typeof(JArray).IsAssignableFrom(documentType))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right... I forgot we required a Newtonsoft ref...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to change to System.Text.Json, which would be the equivalent?

// TODO: What's up with string
if (typeof(string).IsAssignableFrom(documentType))
{
documentType = typeof(JObject); // When binding to JArray, use JObject as contract.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I added E2E tests for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            public static void TriggerWithString(
                [CosmosDBTrigger(DatabaseName, CollectionName, CreateLeaseCollectionIfNotExists = true, LeaseCollectionPrefix = "withstring")] string documents,
                ILogger log)
            {
                foreach (var document in JArray.Parse(documents))
                {
                    log.LogInformation("Trigger with string called!");
                }
            }

}

Dictionary<string, object> bindingData = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
bindingData.Add("CosmosDBTrigger", value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this supported before? Not sure if we need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was required, but I removed it and still worked.

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.

Update CosmosDB SDK
3 participants