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

[GitHub] csantanapr closed pull request #2938: Convert inline code to attachment in database

csantanapr closed pull request #2938: Convert inline code to attachment in database
URL: https://github.com/apache/incubator-openwhisk/pull/2938
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/tools/db/README.md b/tools/db/README.md
index 9de0ac4c64..95f3350cf1 100644
--- a/tools/db/README.md
+++ b/tools/db/README.md
@@ -138,3 +138,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 0000000000..1f31cb632b
--- /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)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services