简体   繁体   中英

COSMOS DB Trigger Azure Function changefeed error

I am using azure function 1.0.13 SDK with 1.2.0 DocumentDB extension. The following is the code that i am trying to run:

  [FunctionName("Function1")]
        public static void Run([CosmosDBTrigger(
            databaseName: "database",
            collectionName: "collection",
            ConnectionStringSetting = "DBConn",
            LeaseCollectionName = "leases")]IReadOnlyList<Document> documents, TraceWriter log)
        {
            if (documents != null && documents.Count > 0)
            {
                log.Verbose("Documents modified " + documents.Count);
                log.Verbose("First document Id " + documents[0].Id);
            }
        }

Now when I try debugging locally in VS, I get the following error:

Function1: The listener for function 'Function1' was unable to start. Microsoft.Azure.WebJobs.Extensions.DocumentDB: Either the source collection 'database' (in database 'database') or the lease collection 'leases' (in database 'database') does not exist. Both collections must exist before the listener starts. To automatically create the lease collection, set 'CreateLeaseCollectionIfNotExists' to 'true'. Microsoft.Azure.Documents.Client: Message: {"Errors":["Resource Not Found"]}

[6/26/2018 3:24:49 PM] ActivityId: 7738cc1a-f8f6-45a2-a3c6-73d342a8d4c3, Request URI: /apps/4c8d65d7-216b-46b4-abb7-52c1a0c7123f/services/b3a1db8d-b82c-403e-8d89-9709b5068482/partitions/0a2bdc5c-471b-4acc-a093-6332c8ce1d5d/replicas/131727365611181690s, RequestStats: [6/26/2018 3:24:49 PM] ResponseTime: 2018-06-26T15:24:48.5014600Z, StoreReadResult: StorePhysicalAddress: rntbd://10.98.106.50:11000/apps/4c8d65d7-216b-46b4-abb7-52c1a0c7123f/services/b3a1db8d-b82c-403e-8d89-9709b5068482/partitions/0a2bdc5c-471b-4acc-a093-6332c8ce1d5d/replicas/131727365611181690s, LSN: 125, GlobalCommittedLsn: 125, PartitionKeyRangeId: , IsValid: True, StatusCode: 0, IsGone: False, IsNotFound: True, IsInvalidPartition: False, RequestCharge: 1, ItemLSN: -1, ResourceType: Collection, OperationType: Read [6/26/2018 3:24:49 PM] ResponseTime: 2018-06-26T15:24:48.5014600Z, StoreReadResult: StorePhysicalAddress: rntbd://10.98.108.179:11000/apps/4c8d65d7-216b-46b4-abb7-52c1a0c7123f/services/b3a1db8d-b82c-403e-8d89- 9709b5068482/partitions/0a2bdc5c-471b-4acc-a093-6332c8ce1d5d/replicas/131727455073557671s, LSN: 125, GlobalCommittedLsn: 125, PartitionKeyRangeId: , IsValid: True, StatusCode: 0, IsGone: False, IsNotFound: True, IsInvalidPartition: False, RequestCharge: 1, ItemLSN: -1, ResourceType: Collection, OperationType: Read [6/26/2018 3:24:49 PM] , SDK: Microsoft.Azure.Documents.Common/2.0.0.0.

I have copied and pasted the entire primary connection string from azure portal. I have checked multiple times to make sure that the database and collection names are found.

"database" and "collection" were modified for security reason.

What did I do wrong?

For the function trigger to work it needs to create a collection called leases. You can do this your self, or update the CosmosDBTrigger trigger to tell the function host it can make it if it doesn't exist

[CosmosDBTrigger( databaseName: "database", collectionName: "collection", ConnectionStringSetting = "DBConn", LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists = true)]

You can see here for more info: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb#trigger---c-example

The reason is explained in the error you are seeing:

Function1: The listener for function 'Function1' was unable to start. Microsoft.Azure.WebJobs.Extensions.DocumentDB: Either the source collection 'database' (in database 'database') or the lease collection 'leases' (in database 'database') does not exist. Both collections must exist before the listener starts. To automatically create the lease collection, set 'CreateLeaseCollectionIfNotExists' to 'true'. Microsoft.Azure.Documents.Client: Message: {"Errors":["Resource Not Found"]}

Both collections need to exist, the collection called database in your case and the leases collection called leases . As the message says, you can use CreateLeaseCollectionIfNotExists to automatically create the leases collection, but the source collection needs to exists.

If you want to understand the reason behind the need of the leases collection, you can read it here . In short, as the Function reads the Change Feed , it needs to checkpoint its progress in order to be resilient to any restart/stop in a separate collection. This collection can be in the same Account or you can customize its account and database.

You should check the Cosmos DB connection string. Try opening the same via Cosmos DB Explorer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM