You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by cs...@apache.org on 2018/02/20 03:35:20 UTC

[incubator-openwhisk] branch master updated: Convert inline code to attachment in database (#2938)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 7bc1869  Convert inline code to attachment in database (#2938)
7bc1869 is described below

commit 7bc18698f4389563f96f082d725f9a6bae2c4727
Author: James Dubee <jw...@us.ibm.com>
AuthorDate: Mon Feb 19 22:35:18 2018 -0500

    Convert inline code to attachment in database (#2938)
---
 tools/db/README.md               |   9 +++
 tools/db/moveCodeToAttachment.py | 117 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+)

diff --git a/tools/db/README.md b/tools/db/README.md
index 10ad2cf..a702361 100644
--- a/tools/db/README.md
+++ b/tools/db/README.md
@@ -137,3 +137,12 @@ Using that command will result in a replication for every database that matches
 ### Replaying a snapshot
 
 To replay a snapshot, swap `--sourceDbUrl` and `--targetDbUrl` and call the script with the `replay` command. That command takes only 1 parameter: `--dbPrefix` to determine which backup to play back. Matching databases will be replicated back to the target database with the `backup_${TIMESTAMP_IN_SECONDS}_` removed, so they'd look just like the original database.
+
+## Database migration to new schema
+
+To reduce the memory consumption in the OpenWhisk controller, all code inlined in action documents has been moved to attachments. This change allows only metadata for actions to be fetched instead of the entire action. Though the OpenWhisk controller supports both mentioned schemas, it is ideal to update existing databases to use the new schema for memory consumption relief.
+
+Run `moveCodeToAttachment.py` to update actions in an existing database to the new action schema. Two parameters are required:
+
+* `--dbUrl`: Server URL of the database. E.g. 'https://xxx:yyy@domain.couch.com:443'.
+* `--dbName`: Name of the Database to update.
diff --git a/tools/db/moveCodeToAttachment.py b/tools/db/moveCodeToAttachment.py
new file mode 100755
index 0000000..1f31cb6
--- /dev/null
+++ b/tools/db/moveCodeToAttachment.py
@@ -0,0 +1,117 @@
+#!/usr/bin/env python
+'''Python script update actions.
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the 'License'); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an 'AS IS' BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+'''
+
+import argparse
+import couchdb.client
+import time
+import base64
+from couchdb import ResourceNotFound
+
+def updateJavaAction(db, doc, id):
+    updated = False
+    attachment = db.get_attachment(doc, 'jarfile')
+
+    if attachment != None:
+        encodedAttachment = base64.b64encode(attachment.getvalue())
+        db.put_attachment(doc, encodedAttachment, 'codefile', 'text/plain')
+        doc = db.get(id)
+        doc['exec']['code'] = {
+            'attachmentName': 'codefile',
+            'attachmentType': 'text/plain'
+        }
+        if 'jar' in doc['exec']:
+            del doc['exec']['jar']
+        db.save(doc)
+        db.delete_attachment(doc, 'jarfile')
+        updated = True
+
+    return updated
+
+def updateNonJavaAction(db, doc, id):
+    updated = False
+    code = doc['exec']['code']
+
+    if not isinstance(code, dict):
+        db.put_attachment(doc, code, 'codefile', 'text/plain')
+        doc = db.get(id)
+        doc['exec']['code'] = {
+            'attachmentName': 'codefile',
+            'attachmentType': 'text/plain'
+        }
+        db.save(doc)
+        updated = True
+
+    return updated
+
+def createNonMigratedDoc(db):
+    try:
+        db['_design/nonMigrated']
+    except ResourceNotFound:
+        db.save({
+            '_id': '_design/nonMigrated',
+            'language': 'javascript',
+            'views': {
+                'actions': {
+                    'map': 'function (doc) {   var isAction = function (doc) {     return (doc.exec !== undefined)   };   var isMigrated = function (doc) {     return (doc._attachments !== undefined && doc._attachments.codefile !== undefined && typeof doc.code != \'string\')   };   if (isAction(doc) && !isMigrated(doc)) try {     emit([doc.name]);   } catch (e) {} }'
+                }
+            }
+        })
+
+def deleteNonMigratedDoc(db):
+    del db['_design/nonMigrated']
+
+def main(args):
+    db = couchdb.client.Server(args.dbUrl)[args.dbName]
+    createNonMigratedDoc(db)
+    docs = db.view('_design/nonMigrated/_view/actions')
+    docCount = len(docs)
+    docIndex = 1
+
+    print('Number of actions to update: {}'.format(docCount))
+
+    for row in docs:
+        id = row.id
+        doc = db.get(id)
+
+        print('Updating action {0}/{1}: "{2}"'.format(docIndex, docCount, id))
+
+        if 'exec' in doc and 'code' in doc['exec']:
+            if doc['exec']['kind'] != 'java':
+                updated = updateNonJavaAction(db, doc, id)
+            else:
+                updated = updateJavaAction(db, doc, id)
+
+            if updated:
+                print('Updated action: "{0}"'.format(id))
+                time.sleep(.500)
+            else:
+                print('Action already updated: "{0}"'.format(id))
+
+        docIndex = docIndex + 1
+
+    deleteNonMigratedDoc(db)
+
+parser = argparse.ArgumentParser(description='Utility to update database action schema.')
+parser.add_argument('--dbUrl', required=True, help='Server URL of the database. E.g. \"https://xxx:yyy@domain.couch.com:443\"')
+parser.add_argument('--dbName', required=True, help='Name of the Database to update.')
+args = parser.parse_args()
+
+main(args)

-- 
To stop receiving notification emails like this one, please contact
csantanapr@apache.org.