You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ho...@apache.org on 2017/07/11 19:05:25 UTC

[incubator-openwhisk-package-kafka] branch master updated: Use one canary document per provider instance (#197)

This is an automated email from the ASF dual-hosted git repository.

houshengbo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-package-kafka.git


The following commit(s) were added to refs/heads/master by this push:
     new 028088c  Use one canary document per provider instance (#197)
028088c is described below

commit 028088cda88320acd0665c09cc3bb171f4bc82c7
Author: Justin Berstler <bj...@us.ibm.com>
AuthorDate: Tue Jul 11 15:05:23 2017 -0400

    Use one canary document per provider instance (#197)
    
    * Use one canary document per provider instance
    
    This eliminates canary document deletes, which is good news for the changes feed as it won't have to process all the deletes when the container starts. This also avoids the problem of getting a 404 while trying to delete a canary that was already deleted by another instance.
    
    * respond to review feedback
---
 provider/database.py | 42 ++++++++++++++++++++++++------------------
 provider/service.py  |  5 +----
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/provider/database.py b/provider/database.py
index 0d2e3b6..4d0f78a 100644
--- a/provider/database.py
+++ b/provider/database.py
@@ -41,6 +41,9 @@ class Database:
     filters_design_doc_id = '_design/filters'
     only_triggers_view_id = 'only-triggers'
 
+    instance = os.getenv('INSTANCE', 'messageHubTrigger-0')
+    canaryId = "canary-{}".format(instance)
+
     if dbname in client.all_dbs():
         logging.info('Database exists - connecting to it.')
         database = client[dbname]
@@ -85,31 +88,34 @@ class Database:
 
         while retryCount < maxRetries:
             try:
-                document = dict()
-                document['canary'] = datetime.now().isoformat()
+                if self.canaryId in self.database.keys(remote=True):
+                    # update the timestamp to cause a document change
+                    logging.debug("[database] Canary doc exists, updating it.")
+
+                    myCanaryDocument = self.database[self.canaryId]
+                    myCanaryDocument["canary-timestamp"] = datetime.now().isoformat()
+                    myCanaryDocument.save()
+
+                    return
+                else:
+                    # create the canary doc for this instance
+                    logging.debug("[database] Canary doc does not exist, creating it.")
 
-                result = self.database.create_document(document)
-                logging.info('[canary] Successfully wrote canary to DB')
+                    document = dict()
+                    document['_id'] = self.canaryId
+                    document['canary-timestamp'] = datetime.now().isoformat()
 
-                return result
+                    result = self.database.create_document(document)
+                    logging.debug('[canary] Successfully wrote canary to DB')
+
+                    return
             except Exception as e:
                 retryCount += 1
-                logging.error('[canary] Uncaught exception while recording trigger to database: {}'.format(e))
+                logging.error(
+                    '[canary] Uncaught exception while writing canary document: {}'.format(e))
 
         logging.error('[canary] Retried and failed {} times to create a canary'.format(maxRetries))
 
-    def deleteDoc(self, docId):
-        try:
-            document = self.database[docId]
-
-            if document.exists():
-                document.delete()
-                logging.debug('[database] Successfully deleted document from DB: {}'.format(docId))
-            else:
-                logging.warn('[database] Attempted to delete non-existent document from DB: {}'.format(docId))
-        except Exception as e:
-            logging.error('[database] Uncaught exception while deleting document {} from database: {}'.format(docId, e))
-
     def migrate(self):
         logging.info('Starting DB migration')
 
diff --git a/provider/service.py b/provider/service.py
index e12dbb6..7183b7f 100644
--- a/provider/service.py
+++ b/provider/service.py
@@ -104,13 +104,10 @@ class Service (Thread):
                                 existingConsumer.disable()
                             else:
                                 logging.debug('[changes] Found non-interesting trigger change: \n{}\n{}'.format(existingConsumer.desiredState(), document))
-                    elif 'canary' in change['doc']:
+                    elif 'canary-timestamp' in change['doc']:
                         # found a canary - update lastCanaryTime
                         logging.info('[canary] I found a canary. The last one was {} seconds ago.'.format(secondsSince(self.lastCanaryTime)))
                         self.lastCanaryTime = datetime.now()
-
-                        # delete the canary document
-                        self.database.deleteDoc(change['id'])
                     else:
                         logging.debug('[changes] Found a change for a non-trigger document')
 

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].